blob: a9f03b02150bcece9c01c317556a121213d328c3 [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"
Vladimir Marko86c87522020-05-11 16:55:55 +010021#include "arch/arm64/jni_frame_arm64.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000022#include "art_method-inl.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070023#include "base/bit_utils.h"
24#include "base/bit_utils_iterator.h"
Vladimir Marko98873af2020-12-16 12:10:03 +000025#include "class_root-inl.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010026#include "class_table.h"
Zheng Xuc6667102015-05-15 16:08:45 +080027#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000028#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010029#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080030#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010031#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010032#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070033#include "heap_poisoning.h"
Nicolas Geoffray4313ccb2020-08-26 17:01:15 +010034#include "interpreter/mterp/nterp.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080035#include "intrinsics.h"
36#include "intrinsics_arm64.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010037#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070038#include "lock_word.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010039#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070040#include "mirror/class-inl.h"
Vladimir Marko2d98dc22020-10-01 11:21:37 +000041#include "mirror/var_handle.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000042#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010043#include "thread.h"
44#include "utils/arm64/assembler_arm64.h"
45#include "utils/assembler.h"
46#include "utils/stack_checks.h"
47
Scott Wakeling97c72b72016-06-24 16:19:36 +010048using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000049using vixl::ExactAssemblyScope;
50using vixl::CodeBufferCheckScope;
51using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010052
53#ifdef __
54#error "ARM64 Codegen VIXL macro-assembler macro already defined."
55#endif
56
Vladimir Marko0a516052019-10-14 13:00:44 +000057namespace art {
Alexandre Rames5319def2014-10-23 10:03:10 +010058
Roland Levillain22ccc3a2015-11-24 13:10:05 +000059template<class MirrorType>
60class GcRoot;
61
Alexandre Rames5319def2014-10-23 10:03:10 +010062namespace arm64 {
63
Alexandre Ramesbe919d92016-08-23 18:33:36 +010064using helpers::ARM64EncodableConstantOrRegister;
65using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080066using helpers::CPURegisterFrom;
67using helpers::DRegisterFrom;
68using helpers::FPRegisterFrom;
69using helpers::HeapOperand;
70using helpers::HeapOperandFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010071using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080072using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080073using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010074using helpers::InputRegisterAt;
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +010075using helpers::Int64FromLocation;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010076using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080077using helpers::LocationFrom;
78using helpers::OperandFromMemOperand;
79using helpers::OutputCPURegister;
80using helpers::OutputFPRegister;
81using helpers::OutputRegister;
82using helpers::RegisterFrom;
83using helpers::StackOperandFrom;
84using helpers::VIXLRegCodeFromART;
85using helpers::WRegisterFrom;
86using helpers::XRegisterFrom;
87
Vladimir Markof3e0ee22015-12-17 15:23:13 +000088// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080089// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
90// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000091static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010092
Vladimir Markof4f2daa2017-03-20 18:26:59 +000093// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
94// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
Vladimir Marko008e09f32018-08-06 15:42:43 +010095// For the Baker read barrier implementation using link-time generated thunks we need to split
Vladimir Markof4f2daa2017-03-20 18:26:59 +000096// the offset explicitly.
97constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
98
Alexandre Rames5319def2014-10-23 10:03:10 +010099inline Condition ARM64Condition(IfCondition cond) {
100 switch (cond) {
101 case kCondEQ: return eq;
102 case kCondNE: return ne;
103 case kCondLT: return lt;
104 case kCondLE: return le;
105 case kCondGT: return gt;
106 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700107 case kCondB: return lo;
108 case kCondBE: return ls;
109 case kCondA: return hi;
110 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100111 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000112 LOG(FATAL) << "Unreachable";
113 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100114}
115
Vladimir Markod6e069b2016-01-18 11:11:01 +0000116inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
117 // The ARM64 condition codes can express all the necessary branches, see the
118 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
119 // There is no dex instruction or HIR that would need the missing conditions
120 // "equal or unordered" or "not equal".
121 switch (cond) {
122 case kCondEQ: return eq;
123 case kCondNE: return ne /* unordered */;
124 case kCondLT: return gt_bias ? cc : lt /* unordered */;
125 case kCondLE: return gt_bias ? ls : le /* unordered */;
126 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
127 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
128 default:
129 LOG(FATAL) << "UNREACHABLE";
130 UNREACHABLE();
131 }
132}
133
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100134Location ARM64ReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000135 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
136 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
137 // but we use the exact registers for clarity.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100138 if (return_type == DataType::Type::kFloat32) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000139 return LocationFrom(s0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100140 } else if (return_type == DataType::Type::kFloat64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000141 return LocationFrom(d0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100142 } else if (return_type == DataType::Type::kInt64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000143 return LocationFrom(x0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100144 } else if (return_type == DataType::Type::kVoid) {
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100145 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000146 } else {
147 return LocationFrom(w0);
148 }
149}
150
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100151Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000152 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100153}
154
Vladimir Marko3232dbb2018-07-25 15:42:46 +0100155static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
156 InvokeRuntimeCallingConvention calling_convention;
157 RegisterSet caller_saves = RegisterSet::Empty();
158 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
159 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
160 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
161 DataType::Type::kReference).GetCode());
162 return caller_saves;
163}
164
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100165// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
166#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700167#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100168
Zheng Xuda403092015-04-24 17:35:39 +0800169// Calculate memory accessing operand for save/restore live registers.
170static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100171 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800172 int64_t spill_offset,
173 bool is_save) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800174 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
175 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100176 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800177 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100178 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800179 codegen->GetNumberOfFloatingPointRegisters()));
180
Vladimir Marko804b03f2016-09-14 16:26:36 +0100181 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serovc8150b52019-07-31 18:28:00 +0100182 const unsigned v_reg_size_in_bits = codegen->GetSlowPathFPWidth() * 8;
Artem Serov1a719e42019-07-18 14:24:55 +0100183 DCHECK_LE(codegen->GetSIMDRegisterWidth(), kQRegSizeInBytes);
Artem Serovc8150b52019-07-31 18:28:00 +0100184 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size_in_bits, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800185
186 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
187 UseScratchRegisterScope temps(masm);
188
189 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100190 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
191 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800192 int64_t reg_size = kXRegSizeInBytes;
193 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
194 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100195 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800196 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
197 // If the offset does not fit in the instruction's immediate field, use an alternate register
198 // to compute the base address(float point registers spill base address).
199 Register new_base = temps.AcquireSameSizeAs(base);
200 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
201 base = new_base;
202 spill_offset = -core_spill_size;
203 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
204 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
205 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
206 }
207
208 if (is_save) {
209 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
210 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
211 } else {
212 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
213 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
214 }
215}
216
217void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800218 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Andreas Gampe3db70682018-12-26 15:12:03 -0800219 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100220 for (uint32_t i : LowToHighBits(core_spills)) {
221 // If the register holds an object, update the stack mask.
222 if (locations->RegisterContainsObject(i)) {
223 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800224 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100225 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
226 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
227 saved_core_stack_offsets_[i] = stack_offset;
228 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800229 }
230
Artem Serovc8150b52019-07-31 18:28:00 +0100231 const size_t fp_reg_size = codegen->GetSlowPathFPWidth();
Andreas Gampe3db70682018-12-26 15:12:03 -0800232 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100233 for (uint32_t i : LowToHighBits(fp_spills)) {
234 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
235 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
236 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serov9df37b92019-07-23 16:41:54 +0100237 stack_offset += fp_reg_size;
Zheng Xuda403092015-04-24 17:35:39 +0800238 }
239
Vladimir Marko804b03f2016-09-14 16:26:36 +0100240 SaveRestoreLiveRegistersHelper(codegen,
241 locations,
Andreas Gampe3db70682018-12-26 15:12:03 -0800242 codegen->GetFirstRegisterSlotInSlowPath(), /* is_save= */ true);
Zheng Xuda403092015-04-24 17:35:39 +0800243}
244
245void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100246 SaveRestoreLiveRegistersHelper(codegen,
247 locations,
Andreas Gampe3db70682018-12-26 15:12:03 -0800248 codegen->GetFirstRegisterSlotInSlowPath(), /* is_save= */ false);
Zheng Xuda403092015-04-24 17:35:39 +0800249}
250
Alexandre Rames5319def2014-10-23 10:03:10 +0100251class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
252 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000253 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100254
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100255 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100256 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000257 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100258
Alexandre Rames5319def2014-10-23 10:03:10 +0100259 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000260 if (instruction_->CanThrowIntoCatchBlock()) {
261 // Live registers will be restored in the catch block if caught.
262 SaveLiveRegisters(codegen, instruction_->GetLocations());
263 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000264 // We're moving two locations to locations that could overlap, so we need a parallel
265 // move resolver.
266 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100267 codegen->EmitParallelMoves(locations->InAt(0),
268 LocationFrom(calling_convention.GetRegisterAt(0)),
269 DataType::Type::kInt32,
270 locations->InAt(1),
271 LocationFrom(calling_convention.GetRegisterAt(1)),
272 DataType::Type::kInt32);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000273 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
274 ? kQuickThrowStringBounds
275 : kQuickThrowArrayBounds;
276 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100277 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800278 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100279 }
280
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100281 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100282
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100283 const char* GetDescription() const override { return "BoundsCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100284
Alexandre Rames5319def2014-10-23 10:03:10 +0100285 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100286 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
287};
288
Alexandre Rames67555f72014-11-18 10:55:16 +0000289class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
290 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000291 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000292
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100293 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000294 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
295 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000296 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800297 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000298 }
299
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100300 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100301
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100302 const char* GetDescription() const override { return "DivZeroCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100303
Alexandre Rames67555f72014-11-18 10:55:16 +0000304 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000305 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
306};
307
308class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
309 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100310 LoadClassSlowPathARM64(HLoadClass* cls, HInstruction* at)
311 : SlowPathCodeARM64(at), cls_(cls) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000312 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100313 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Alexandre Rames67555f72014-11-18 10:55:16 +0000314 }
315
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100316 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000317 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000318 Location out = locations->Out();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100319 const uint32_t dex_pc = instruction_->GetDexPc();
320 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
321 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
Alexandre Rames67555f72014-11-18 10:55:16 +0000322
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100323 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames67555f72014-11-18 10:55:16 +0000324 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000325 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000326
Vladimir Markof3c52b42017-11-17 17:32:12 +0000327 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100328 if (must_resolve_type) {
329 DCHECK(IsSameDexFile(cls_->GetDexFile(), arm64_codegen->GetGraph()->GetDexFile()));
330 dex::TypeIndex type_index = cls_->GetTypeIndex();
331 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Vladimir Marko8f63f102020-09-28 12:10:28 +0100332 if (cls_->NeedsAccessCheck()) {
333 CheckEntrypointTypes<kQuickResolveTypeAndVerifyAccess, void*, uint32_t>();
334 arm64_codegen->InvokeRuntime(kQuickResolveTypeAndVerifyAccess, instruction_, dex_pc, this);
335 } else {
336 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
337 arm64_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
338 }
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100339 // If we also must_do_clinit, the resolved type is now in the correct register.
340 } else {
341 DCHECK(must_do_clinit);
342 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
343 arm64_codegen->MoveLocation(LocationFrom(calling_convention.GetRegisterAt(0)),
344 source,
345 cls_->GetType());
346 }
347 if (must_do_clinit) {
348 arm64_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
349 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800350 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000351
352 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000353 if (out.IsValid()) {
354 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100355 DataType::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000356 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000357 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000358 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000359 __ B(GetExitLabel());
360 }
361
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100362 const char* GetDescription() const override { return "LoadClassSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100363
Alexandre Rames67555f72014-11-18 10:55:16 +0000364 private:
365 // The class this slow path will load.
366 HLoadClass* const cls_;
367
Alexandre Rames67555f72014-11-18 10:55:16 +0000368 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
369};
370
Vladimir Markoaad75c62016-10-03 08:46:48 +0000371class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
372 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000373 explicit LoadStringSlowPathARM64(HLoadString* instruction)
374 : SlowPathCodeARM64(instruction) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000375
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100376 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000377 LocationSummary* locations = instruction_->GetLocations();
378 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
379 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
380
381 __ Bind(GetEntryLabel());
382 SaveLiveRegisters(codegen, locations);
383
Vladimir Markof3c52b42017-11-17 17:32:12 +0000384 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000385 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
386 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000387 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
388 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100389 DataType::Type type = instruction_->GetType();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000390 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
391
392 RestoreLiveRegisters(codegen, locations);
393
Vladimir Markoaad75c62016-10-03 08:46:48 +0000394 __ B(GetExitLabel());
395 }
396
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100397 const char* GetDescription() const override { return "LoadStringSlowPathARM64"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000398
399 private:
400 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
401};
402
Alexandre Rames5319def2014-10-23 10:03:10 +0100403class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
404 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000405 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100406
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100407 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000408 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100409 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000410 if (instruction_->CanThrowIntoCatchBlock()) {
411 // Live registers will be restored in the catch block if caught.
412 SaveLiveRegisters(codegen, instruction_->GetLocations());
413 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000414 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
415 instruction_,
416 instruction_->GetDexPc(),
417 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800418 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100419 }
420
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100421 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100422
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100423 const char* GetDescription() const override { return "NullCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100424
Alexandre Rames5319def2014-10-23 10:03:10 +0100425 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100426 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
427};
428
429class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
430 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100431 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000432 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100433
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100434 void EmitNativeCode(CodeGenerator* codegen) override {
Artem Serov7957d952017-04-04 15:44:09 +0100435 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000436 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100437 __ Bind(GetEntryLabel());
Artem Serov1a719e42019-07-18 14:24:55 +0100438 SaveLiveRegisters(codegen, locations); // Only saves live vector regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000439 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800440 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov1a719e42019-07-18 14:24:55 +0100441 RestoreLiveRegisters(codegen, locations); // Only restores live vector regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000442 if (successor_ == nullptr) {
443 __ B(GetReturnLabel());
444 } else {
445 __ B(arm64_codegen->GetLabelOf(successor_));
446 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100447 }
448
Scott Wakeling97c72b72016-06-24 16:19:36 +0100449 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100450 DCHECK(successor_ == nullptr);
451 return &return_label_;
452 }
453
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100454 HBasicBlock* GetSuccessor() const {
455 return successor_;
456 }
457
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100458 const char* GetDescription() const override { return "SuspendCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100459
Alexandre Rames5319def2014-10-23 10:03:10 +0100460 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100461 // If not null, the block to branch to after the suspend check.
462 HBasicBlock* const successor_;
463
464 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100465 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100466
467 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
468};
469
Alexandre Rames67555f72014-11-18 10:55:16 +0000470class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
471 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000472 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000473 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000474
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100475 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000476 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800477
Alexandre Rames3e69f162014-12-10 10:36:50 +0000478 DCHECK(instruction_->IsCheckCast()
479 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
480 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100481 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000482
Alexandre Rames67555f72014-11-18 10:55:16 +0000483 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000484
Vladimir Marko87584542017-12-12 17:47:52 +0000485 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000486 SaveLiveRegisters(codegen, locations);
487 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000488
489 // We're moving two locations to locations that could overlap, so we need a parallel
490 // move resolver.
491 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800492 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800493 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100494 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800495 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800496 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100497 DataType::Type::kReference);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000498 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000499 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800500 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100501 DataType::Type ret_type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000502 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
503 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
504 } else {
505 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800506 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
507 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000508 }
509
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000510 if (!is_fatal_) {
511 RestoreLiveRegisters(codegen, locations);
512 __ B(GetExitLabel());
513 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000514 }
515
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100516 const char* GetDescription() const override { return "TypeCheckSlowPathARM64"; }
517 bool IsFatal() const override { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100518
Alexandre Rames67555f72014-11-18 10:55:16 +0000519 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000520 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000521
Alexandre Rames67555f72014-11-18 10:55:16 +0000522 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
523};
524
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700525class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
526 public:
Aart Bik42249c32016-01-07 15:33:50 -0800527 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000528 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700529
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100530 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bik42249c32016-01-07 15:33:50 -0800531 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700532 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100533 LocationSummary* locations = instruction_->GetLocations();
534 SaveLiveRegisters(codegen, locations);
535 InvokeRuntimeCallingConvention calling_convention;
536 __ Mov(calling_convention.GetRegisterAt(0),
537 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000538 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100539 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700540 }
541
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100542 const char* GetDescription() const override { return "DeoptimizationSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100543
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700544 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700545 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
546};
547
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100548class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
549 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000550 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100551
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100552 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100553 LocationSummary* locations = instruction_->GetLocations();
554 __ Bind(GetEntryLabel());
555 SaveLiveRegisters(codegen, locations);
556
557 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100558 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100559 parallel_move.AddMove(
560 locations->InAt(0),
561 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100562 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100563 nullptr);
564 parallel_move.AddMove(
565 locations->InAt(1),
566 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100567 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100568 nullptr);
569 parallel_move.AddMove(
570 locations->InAt(2),
571 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100572 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100573 nullptr);
574 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
575
576 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000577 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100578 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
579 RestoreLiveRegisters(codegen, locations);
580 __ B(GetExitLabel());
581 }
582
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100583 const char* GetDescription() const override { return "ArraySetSlowPathARM64"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100584
585 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100586 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
587};
588
Zheng Xu3927c8b2015-11-18 17:46:25 +0800589void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
590 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000591 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800592
593 // We are about to use the assembler to place literals directly. Make sure we have enough
594 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000595 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
596 num_entries * sizeof(int32_t),
597 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800598
599 __ Bind(&table_start_);
600 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
601 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100602 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800603 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100604 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800605 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
606 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
607 Literal<int32_t> literal(jump_offset);
608 __ place(&literal);
609 }
610}
611
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000612// Slow path generating a read barrier for a heap reference.
613class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
614 public:
615 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
616 Location out,
617 Location ref,
618 Location obj,
619 uint32_t offset,
620 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000621 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000622 out_(out),
623 ref_(ref),
624 obj_(obj),
625 offset_(offset),
626 index_(index) {
627 DCHECK(kEmitCompilerReadBarrier);
628 // If `obj` is equal to `out` or `ref`, it means the initial object
629 // has been overwritten by (or after) the heap object reference load
630 // to be instrumented, e.g.:
631 //
632 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000633 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000634 //
635 // In that case, we have lost the information about the original
636 // object, and the emitted read barrier cannot work properly.
637 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
638 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
639 }
640
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100641 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000642 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
643 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100644 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000645 DCHECK(locations->CanCall());
646 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100647 DCHECK(instruction_->IsInstanceFieldGet() ||
648 instruction_->IsStaticFieldGet() ||
649 instruction_->IsArrayGet() ||
650 instruction_->IsInstanceOf() ||
651 instruction_->IsCheckCast() ||
Vladimir Markoa41ea272020-09-07 15:24:36 +0000652 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000653 << "Unexpected instruction in read barrier for heap reference slow path: "
654 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000655 // The read barrier instrumentation of object ArrayGet
656 // instructions does not support the HIntermediateAddress
657 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000658 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100659 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000660
661 __ Bind(GetEntryLabel());
662
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000663 SaveLiveRegisters(codegen, locations);
664
665 // We may have to change the index's value, but as `index_` is a
666 // constant member (like other "inputs" of this slow path),
667 // introduce a copy of it, `index`.
668 Location index = index_;
669 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100670 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000671 if (instruction_->IsArrayGet()) {
672 // Compute the actual memory offset and store it in `index`.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100673 Register index_reg = RegisterFrom(index_, DataType::Type::kInt32);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000674 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
675 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
676 // We are about to change the value of `index_reg` (see the
677 // calls to vixl::MacroAssembler::Lsl and
678 // vixl::MacroAssembler::Mov below), but it has
679 // not been saved by the previous call to
680 // art::SlowPathCode::SaveLiveRegisters, as it is a
681 // callee-save register --
682 // art::SlowPathCode::SaveLiveRegisters does not consider
683 // callee-save registers, as it has been designed with the
684 // assumption that callee-save registers are supposed to be
685 // handled by the called function. So, as a callee-save
686 // register, `index_reg` _would_ eventually be saved onto
687 // the stack, but it would be too late: we would have
688 // changed its value earlier. Therefore, we manually save
689 // it here into another freely available register,
690 // `free_reg`, chosen of course among the caller-save
691 // registers (as a callee-save `free_reg` register would
692 // exhibit the same problem).
693 //
694 // Note we could have requested a temporary register from
695 // the register allocator instead; but we prefer not to, as
696 // this is a slow path, and we know we can find a
697 // caller-save register that is available.
698 Register free_reg = FindAvailableCallerSaveRegister(codegen);
699 __ Mov(free_reg.W(), index_reg);
700 index_reg = free_reg;
701 index = LocationFrom(index_reg);
702 } else {
703 // The initial register stored in `index_` has already been
704 // saved in the call to art::SlowPathCode::SaveLiveRegisters
705 // (as it is not a callee-save register), so we can freely
706 // use it.
707 }
708 // Shifting the index value contained in `index_reg` by the scale
709 // factor (2) cannot overflow in practice, as the runtime is
710 // unable to allocate object arrays with a size larger than
711 // 2^26 - 1 (that is, 2^28 - 4 bytes).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100712 __ Lsl(index_reg, index_reg, DataType::SizeShift(type));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000713 static_assert(
714 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
715 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
716 __ Add(index_reg, index_reg, Operand(offset_));
717 } else {
Vladimir Markoa41ea272020-09-07 15:24:36 +0000718 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile/VarHandleGet
Roland Levillain3d312422016-06-23 13:53:42 +0100719 // intrinsics, `index_` is not shifted by a scale factor of 2
720 // (as in the case of ArrayGet), as it is actually an offset
721 // to an object field within an object.
722 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000723 DCHECK(instruction_->GetLocations()->Intrinsified());
Vladimir Marko2d98dc22020-10-01 11:21:37 +0000724 Intrinsics intrinsic = instruction_->AsInvoke()->GetIntrinsic();
725 DCHECK(intrinsic == Intrinsics::kUnsafeGetObject ||
726 intrinsic == Intrinsics::kUnsafeGetObjectVolatile ||
Vladimir Markoe17530a2020-11-11 17:02:26 +0000727 intrinsic == Intrinsics::kUnsafeCASObject ||
Vladimir Marko2d98dc22020-10-01 11:21:37 +0000728 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
Vladimir Marko1bff99f2020-11-02 15:07:33 +0000729 mirror::VarHandle::AccessModeTemplate::kGet ||
730 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
731 mirror::VarHandle::AccessModeTemplate::kCompareAndSet ||
732 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
Vladimir Marko32c2eb82020-11-10 16:58:47 +0000733 mirror::VarHandle::AccessModeTemplate::kCompareAndExchange ||
734 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
735 mirror::VarHandle::AccessModeTemplate::kGetAndUpdate)
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000736 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100737 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100738 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000739 }
740 }
741
742 // We're moving two or three locations to locations that could
743 // overlap, so we need a parallel move resolver.
744 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100745 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000746 parallel_move.AddMove(ref_,
747 LocationFrom(calling_convention.GetRegisterAt(0)),
748 type,
749 nullptr);
750 parallel_move.AddMove(obj_,
751 LocationFrom(calling_convention.GetRegisterAt(1)),
752 type,
753 nullptr);
754 if (index.IsValid()) {
755 parallel_move.AddMove(index,
756 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100757 DataType::Type::kInt32,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000758 nullptr);
759 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
760 } else {
761 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
762 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
763 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000764 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000765 instruction_,
766 instruction_->GetDexPc(),
767 this);
768 CheckEntrypointTypes<
769 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
770 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
771
772 RestoreLiveRegisters(codegen, locations);
773
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000774 __ B(GetExitLabel());
775 }
776
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100777 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000778
779 private:
780 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100781 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
782 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000783 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
784 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
785 return Register(VIXLRegCodeFromART(i), kXRegSize);
786 }
787 }
788 // We shall never fail to find a free caller-save register, as
789 // there are more than two core caller-save registers on ARM64
790 // (meaning it is possible to find one which is different from
791 // `ref` and `obj`).
792 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
793 LOG(FATAL) << "Could not find a free register";
794 UNREACHABLE();
795 }
796
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000797 const Location out_;
798 const Location ref_;
799 const Location obj_;
800 const uint32_t offset_;
801 // An additional location containing an index to an array.
802 // Only used for HArrayGet and the UnsafeGetObject &
803 // UnsafeGetObjectVolatile intrinsics.
804 const Location index_;
805
806 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
807};
808
809// Slow path generating a read barrier for a GC root.
810class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
811 public:
812 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000813 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000814 DCHECK(kEmitCompilerReadBarrier);
815 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000816
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100817 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000818 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100819 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000820 DCHECK(locations->CanCall());
821 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Vladimir Markoa41ea272020-09-07 15:24:36 +0000822 DCHECK(instruction_->IsLoadClass() ||
823 instruction_->IsLoadString() ||
824 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000825 << "Unexpected instruction in read barrier for GC root slow path: "
826 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000827
828 __ Bind(GetEntryLabel());
829 SaveLiveRegisters(codegen, locations);
830
831 InvokeRuntimeCallingConvention calling_convention;
832 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
833 // The argument of the ReadBarrierForRootSlow is not a managed
834 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
835 // thus we need a 64-bit move here, and we cannot use
836 //
837 // arm64_codegen->MoveLocation(
838 // LocationFrom(calling_convention.GetRegisterAt(0)),
839 // root_,
840 // type);
841 //
842 // which would emit a 32-bit move, as `type` is a (32-bit wide)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100843 // reference type (`DataType::Type::kReference`).
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000844 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000845 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000846 instruction_,
847 instruction_->GetDexPc(),
848 this);
849 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
850 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
851
852 RestoreLiveRegisters(codegen, locations);
853 __ B(GetExitLabel());
854 }
855
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100856 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000857
858 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000859 const Location out_;
860 const Location root_;
861
862 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
863};
864
Alexandre Rames5319def2014-10-23 10:03:10 +0100865#undef __
866
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100867Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100868 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100869 if (type == DataType::Type::kVoid) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100870 LOG(FATAL) << "Unreachable type " << type;
871 }
872
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100873 if (DataType::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100874 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
875 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100876 } else if (!DataType::IsFloatingPointType(type) &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000877 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000878 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
879 } else {
880 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100881 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
882 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100883 }
884
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000885 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100886 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100887 return next_location;
888}
889
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100890Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100891 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100892}
893
Vladimir Marko86c87522020-05-11 16:55:55 +0100894Location CriticalNativeCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
895 DCHECK_NE(type, DataType::Type::kReference);
896
897 Location location = Location::NoLocation();
898 if (DataType::IsFloatingPointType(type)) {
899 if (fpr_index_ < kParameterFPRegistersLength) {
900 location = LocationFrom(kParameterFPRegisters[fpr_index_]);
901 ++fpr_index_;
902 }
903 } else {
904 // Native ABI uses the same registers as managed, except that the method register x0
905 // is a normal argument.
906 if (gpr_index_ < 1u + kParameterCoreRegistersLength) {
907 location = LocationFrom(gpr_index_ == 0u ? x0 : kParameterCoreRegisters[gpr_index_ - 1u]);
908 ++gpr_index_;
909 }
910 }
911 if (location.IsInvalid()) {
912 if (DataType::Is64BitType(type)) {
913 location = Location::DoubleStackSlot(stack_offset_);
914 } else {
915 location = Location::StackSlot(stack_offset_);
916 }
917 stack_offset_ += kFramePointerSize;
918
919 if (for_register_allocation_) {
920 location = Location::Any();
921 }
922 }
923 return location;
924}
925
926Location CriticalNativeCallingConventionVisitorARM64::GetReturnLocation(DataType::Type type) const {
927 // We perform conversion to the managed ABI return register after the call if needed.
928 InvokeDexCallingConventionVisitorARM64 dex_calling_convention;
929 return dex_calling_convention.GetReturnLocation(type);
930}
931
932Location CriticalNativeCallingConventionVisitorARM64::GetMethodLocation() const {
933 // Pass the method in the hidden argument x15.
934 return Location::RegisterLocation(x15.GetCode());
935}
936
Serban Constantinescu579885a2015-02-22 20:51:33 +0000937CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100938 const CompilerOptions& compiler_options,
939 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100940 : CodeGenerator(graph,
941 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000942 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000943 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100944 callee_saved_core_registers.GetList(),
945 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100946 compiler_options,
947 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100948 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
949 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov1a719e42019-07-18 14:24:55 +0100950 location_builder_neon_(graph, this),
951 instruction_visitor_neon_(graph, this),
952 location_builder_sve_(graph, this),
953 instruction_visitor_sve_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100954 move_resolver_(graph->GetAllocator(), this),
Artem Serovaa6f4832018-11-21 18:57:54 +0000955 assembler_(graph->GetAllocator(),
956 compiler_options.GetInstructionSetFeatures()->AsArm64InstructionSetFeatures()),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000957 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100958 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000959 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100960 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko8f63f102020-09-28 12:10:28 +0100961 public_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
962 package_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000963 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100964 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeb9eb002020-10-02 13:54:19 +0100965 boot_image_jni_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +0100966 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100967 call_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100968 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100969 uint32_literals_(std::less<uint32_t>(),
970 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
971 uint64_literals_(std::less<uint64_t>(),
972 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000973 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100974 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000975 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +0000976 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
977 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
978 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000979 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000980 AddAllocatedRegister(LocationFrom(lr));
Artem Serov1a719e42019-07-18 14:24:55 +0100981
982 bool use_sve = ShouldUseSVE();
983 if (use_sve) {
984 location_builder_ = &location_builder_sve_;
985 instruction_visitor_ = &instruction_visitor_sve_;
986 } else {
987 location_builder_ = &location_builder_neon_;
988 instruction_visitor_ = &instruction_visitor_neon_;
989 }
990}
991
992bool CodeGeneratorARM64::ShouldUseSVE() const {
993 return kArm64AllowSVE && GetInstructionSetFeatures().HasSVE();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000994}
Alexandre Rames5319def2014-10-23 10:03:10 +0100995
Alexandre Rames67555f72014-11-18 10:55:16 +0000996#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100997
Zheng Xu3927c8b2015-11-18 17:46:25 +0800998void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100999 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001000 jump_table->EmitTable(this);
1001 }
1002}
1003
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001004void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001005 EmitJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +00001006
1007 // Emit JIT baker read barrier slow paths.
Vladimir Marko695348f2020-05-19 14:42:02 +01001008 DCHECK(GetCompilerOptions().IsJitCompiler() || jit_baker_read_barrier_slow_paths_.empty());
Vladimir Marko966b46f2018-08-03 10:20:19 +00001009 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
1010 uint32_t encoded_data = entry.first;
1011 vixl::aarch64::Label* slow_path_entry = &entry.second.label;
1012 __ Bind(slow_path_entry);
Andreas Gampe3db70682018-12-26 15:12:03 -08001013 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name= */ nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00001014 }
1015
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001016 // Ensure we emit the literal pool.
1017 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001018
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001019 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001020
1021 // Verify Baker read barrier linker patches.
1022 if (kIsDebugBuild) {
1023 ArrayRef<const uint8_t> code = allocator->GetMemory();
1024 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
1025 DCHECK(info.label.IsBound());
1026 uint32_t literal_offset = info.label.GetLocation();
1027 DCHECK_ALIGNED(literal_offset, 4u);
1028
1029 auto GetInsn = [&code](uint32_t offset) {
1030 DCHECK_ALIGNED(offset, 4u);
1031 return
1032 (static_cast<uint32_t>(code[offset + 0]) << 0) +
1033 (static_cast<uint32_t>(code[offset + 1]) << 8) +
1034 (static_cast<uint32_t>(code[offset + 2]) << 16)+
1035 (static_cast<uint32_t>(code[offset + 3]) << 24);
1036 };
1037
1038 const uint32_t encoded_data = info.custom_data;
1039 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
1040 // Check that the next instruction matches the expected LDR.
1041 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01001042 case BakerReadBarrierKind::kField:
1043 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00001044 DCHECK_GE(code.size() - literal_offset, 8u);
1045 uint32_t next_insn = GetInsn(literal_offset + 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001046 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1047 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko0ecac682018-08-07 10:40:38 +01001048 if (kind == BakerReadBarrierKind::kField) {
1049 // LDR (immediate) with correct base_reg.
1050 CHECK_EQ(next_insn & 0xffc003e0u, 0xb9400000u | (base_reg << 5));
1051 } else {
1052 DCHECK(kind == BakerReadBarrierKind::kAcquire);
1053 // LDAR with correct base_reg.
1054 CHECK_EQ(next_insn & 0xffffffe0u, 0x88dffc00u | (base_reg << 5));
1055 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001056 break;
1057 }
1058 case BakerReadBarrierKind::kArray: {
1059 DCHECK_GE(code.size() - literal_offset, 8u);
1060 uint32_t next_insn = GetInsn(literal_offset + 4u);
1061 // LDR (register) with the correct base_reg, size=10 (32-bit), option=011 (extend = LSL),
1062 // and S=1 (shift amount = 2 for 32-bit version), i.e. LDR Wt, [Xn, Xm, LSL #2].
1063 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1064 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1065 CHECK_EQ(next_insn & 0xffe0ffe0u, 0xb8607800u | (base_reg << 5));
1066 CheckValidReg((next_insn >> 16) & 0x1f); // Check index register
1067 break;
1068 }
1069 case BakerReadBarrierKind::kGcRoot: {
1070 DCHECK_GE(literal_offset, 4u);
1071 uint32_t prev_insn = GetInsn(literal_offset - 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001072 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko94796f82018-08-08 15:15:33 +01001073 // Usually LDR (immediate) with correct root_reg but
Vladimir Markoc8178f52020-11-24 10:38:16 +00001074 // we may have a "MOV marked, old_value" for intrinsic CAS.
Vladimir Marko94796f82018-08-08 15:15:33 +01001075 if ((prev_insn & 0xffe0ffff) != (0x2a0003e0 | root_reg)) { // MOV?
1076 CHECK_EQ(prev_insn & 0xffc0001fu, 0xb9400000u | root_reg); // LDR?
1077 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001078 break;
1079 }
1080 default:
1081 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
1082 UNREACHABLE();
1083 }
1084 }
1085 }
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001086}
1087
Zheng Xuad4450e2015-04-17 18:48:56 +08001088void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1089 // Note: There are 6 kinds of moves:
1090 // 1. constant -> GPR/FPR (non-cycle)
1091 // 2. constant -> stack (non-cycle)
1092 // 3. GPR/FPR -> GPR/FPR
1093 // 4. GPR/FPR -> stack
1094 // 5. stack -> GPR/FPR
1095 // 6. stack -> stack (non-cycle)
1096 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1097 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1098 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1099 // dependency.
1100 vixl_temps_.Open(GetVIXLAssembler());
1101}
1102
1103void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1104 vixl_temps_.Close();
1105}
1106
1107Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001108 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1109 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1110 || kind == Location::kSIMDStackSlot);
1111 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1112 ? Location::kFpuRegister
1113 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001114 Location scratch = GetScratchLocation(kind);
1115 if (!scratch.Equals(Location::NoLocation())) {
1116 return scratch;
1117 }
1118 // Allocate from VIXL temp registers.
1119 if (kind == Location::kRegister) {
1120 scratch = LocationFrom(vixl_temps_.AcquireX());
1121 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001122 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serov1a719e42019-07-18 14:24:55 +01001123 scratch = codegen_->GetGraph()->HasSIMD()
1124 ? codegen_->GetInstructionCodeGeneratorArm64()->AllocateSIMDScratchLocation(&vixl_temps_)
1125 : LocationFrom(vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001126 }
1127 AddScratchLocation(scratch);
1128 return scratch;
1129}
1130
1131void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1132 if (loc.IsRegister()) {
1133 vixl_temps_.Release(XRegisterFrom(loc));
1134 } else {
1135 DCHECK(loc.IsFpuRegister());
Artem Serov1a719e42019-07-18 14:24:55 +01001136 if (codegen_->GetGraph()->HasSIMD()) {
1137 codegen_->GetInstructionCodeGeneratorArm64()->FreeSIMDScratchLocation(loc, &vixl_temps_);
1138 } else {
1139 vixl_temps_.Release(DRegisterFrom(loc));
1140 }
Zheng Xuad4450e2015-04-17 18:48:56 +08001141 }
1142 RemoveScratchLocation(loc);
1143}
1144
Alexandre Rames3e69f162014-12-10 10:36:50 +00001145void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001146 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001147 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001148}
1149
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001150void CodeGeneratorARM64::MaybeIncrementHotness(bool is_frame_entry) {
1151 MacroAssembler* masm = GetVIXLAssembler();
1152 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1153 UseScratchRegisterScope temps(masm);
1154 Register counter = temps.AcquireX();
1155 Register method = is_frame_entry ? kArtMethodRegister : temps.AcquireX();
1156 if (!is_frame_entry) {
1157 __ Ldr(method, MemOperand(sp, 0));
1158 }
1159 __ Ldrh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
1160 __ Add(counter, counter, 1);
1161 // Subtract one if the counter would overflow.
1162 __ Sub(counter, counter, Operand(counter, LSR, 16));
1163 __ Strh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
1164 }
1165
1166 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffray095dc462020-08-17 16:40:28 +01001167 ScopedProfilingInfoUse spiu(
1168 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
1169 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001170 if (info != nullptr) {
Nicolas Geoffrayc1cd1332020-01-25 13:08:24 +00001171 uint64_t address = reinterpret_cast64<uint64_t>(info);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001172 vixl::aarch64::Label done;
1173 UseScratchRegisterScope temps(masm);
1174 Register temp = temps.AcquireX();
1175 Register counter = temps.AcquireW();
1176 __ Mov(temp, address);
1177 __ Ldrh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
1178 __ Add(counter, counter, 1);
Nicolas Geoffray4313ccb2020-08-26 17:01:15 +01001179 __ And(counter, counter, interpreter::kTieredHotnessMask);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001180 __ Strh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
Nicolas Geoffray4313ccb2020-08-26 17:01:15 +01001181 __ Cbnz(counter, &done);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001182 if (is_frame_entry) {
1183 if (HasEmptyFrame()) {
Vladimir Markodec78172020-06-19 15:31:23 +01001184 // The entrypoint expects the method at the bottom of the stack. We
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001185 // claim stack space necessary for alignment.
Vladimir Markodec78172020-06-19 15:31:23 +01001186 IncreaseFrame(kStackAlignment);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001187 __ Stp(kArtMethodRegister, lr, MemOperand(sp, 0));
1188 } else if (!RequiresCurrentMethod()) {
1189 __ Str(kArtMethodRegister, MemOperand(sp, 0));
1190 }
1191 } else {
1192 CHECK(RequiresCurrentMethod());
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001193 }
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001194 uint32_t entrypoint_offset =
1195 GetThreadOffset<kArm64PointerSize>(kQuickCompileOptimized).Int32Value();
1196 __ Ldr(lr, MemOperand(tr, entrypoint_offset));
1197 // Note: we don't record the call here (and therefore don't generate a stack
1198 // map), as the entrypoint should never be suspended.
1199 __ Blr(lr);
1200 if (HasEmptyFrame()) {
1201 CHECK(is_frame_entry);
1202 __ Ldr(lr, MemOperand(sp, 8));
Vladimir Markodec78172020-06-19 15:31:23 +01001203 DecreaseFrame(kStackAlignment);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001204 }
1205 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001206 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001207 }
1208}
1209
Alexandre Rames5319def2014-10-23 10:03:10 +01001210void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001211 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001212 __ Bind(&frame_entry_label_);
1213
Vladimir Marko33bff252017-11-01 14:35:42 +00001214 bool do_overflow_check =
1215 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001216 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001217 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001218 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001219 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001220 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001221 {
1222 // Ensure that between load and RecordPcInfo there are no pools emitted.
1223 ExactAssemblyScope eas(GetVIXLAssembler(),
1224 kInstructionSize,
1225 CodeBufferCheckScope::kExactSize);
1226 __ ldr(wzr, MemOperand(temp, 0));
1227 RecordPcInfo(nullptr, 0);
1228 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001229 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001230
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001231 if (!HasEmptyFrame()) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001232 // Stack layout:
1233 // sp[frame_size - 8] : lr.
1234 // ... : other preserved core registers.
1235 // ... : other preserved fp registers.
1236 // ... : reserved frame space.
1237 // sp[0] : current method.
Vladimir Marko1a225a72019-07-05 13:37:42 +01001238 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1239 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1240 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1241 DCHECK(!preserved_core_registers.IsEmpty());
1242 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1243 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001244
Vladimir Marko1a225a72019-07-05 13:37:42 +01001245 // Save the current method if we need it, or if using STP reduces code
1246 // size. Note that we do not do this in HCurrentMethod, as the
1247 // instruction might have been removed in the SSA graph.
1248 CPURegister lowest_spill;
1249 if (core_spills_offset == kXRegSizeInBytes) {
1250 // If there is no gap between the method and the lowest core spill, use
1251 // aligned STP pre-index to store both. Max difference is 512. We do
1252 // that to reduce code size even if we do not have to save the method.
1253 DCHECK_LE(frame_size, 512); // 32 core registers are only 256 bytes.
1254 lowest_spill = preserved_core_registers.PopLowestIndex();
1255 __ Stp(kArtMethodRegister, lowest_spill, MemOperand(sp, -frame_size, PreIndex));
1256 } else if (RequiresCurrentMethod()) {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001257 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001258 } else {
1259 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001260 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001261 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Vladimir Marko1a225a72019-07-05 13:37:42 +01001262 if (lowest_spill.IsValid()) {
1263 GetAssembler()->cfi().RelOffset(DWARFReg(lowest_spill), core_spills_offset);
1264 core_spills_offset += kXRegSizeInBytes;
1265 }
1266 GetAssembler()->SpillRegisters(preserved_core_registers, core_spills_offset);
1267 GetAssembler()->SpillRegisters(preserved_fp_registers, fp_spills_offset);
Mingyao Yang063fc772016-08-02 11:02:54 -07001268
1269 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1270 // Initialize should_deoptimize flag to 0.
1271 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1272 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1273 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001274 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001275 MaybeIncrementHotness(/* is_frame_entry= */ true);
Andreas Gampe3db70682018-12-26 15:12:03 -08001276 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001277}
1278
1279void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001280 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001281 if (!HasEmptyFrame()) {
Vladimir Marko1a225a72019-07-05 13:37:42 +01001282 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1283 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1284 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1285 DCHECK(!preserved_core_registers.IsEmpty());
1286 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1287 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
1288
1289 CPURegister lowest_spill;
1290 if (core_spills_offset == kXRegSizeInBytes) {
1291 // If there is no gap between the method and the lowest core spill, use
1292 // aligned LDP pre-index to pop both. Max difference is 504. We do
1293 // that to reduce code size even though the loaded method is unused.
1294 DCHECK_LE(frame_size, 504); // 32 core registers are only 256 bytes.
1295 lowest_spill = preserved_core_registers.PopLowestIndex();
1296 core_spills_offset += kXRegSizeInBytes;
1297 }
1298 GetAssembler()->UnspillRegisters(preserved_fp_registers, fp_spills_offset);
1299 GetAssembler()->UnspillRegisters(preserved_core_registers, core_spills_offset);
1300 if (lowest_spill.IsValid()) {
1301 __ Ldp(xzr, lowest_spill, MemOperand(sp, frame_size, PostIndex));
1302 GetAssembler()->cfi().Restore(DWARFReg(lowest_spill));
1303 } else {
1304 __ Drop(frame_size);
1305 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001306 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001307 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001308 __ Ret();
1309 GetAssembler()->cfi().RestoreState();
1310 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001311}
1312
Scott Wakeling97c72b72016-06-24 16:19:36 +01001313CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001314 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001315 return CPURegList(CPURegister::kRegister, kXRegSize,
1316 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001317}
1318
Scott Wakeling97c72b72016-06-24 16:19:36 +01001319CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001320 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1321 GetNumberOfFloatingPointRegisters()));
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001322 return CPURegList(CPURegister::kVRegister, kDRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001323 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001324}
1325
Alexandre Rames5319def2014-10-23 10:03:10 +01001326void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1327 __ Bind(GetLabelOf(block));
1328}
1329
Calin Juravle175dc732015-08-25 15:42:32 +01001330void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1331 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001332 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001333}
1334
Calin Juravlee460d1d2015-09-29 04:52:17 +01001335void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1336 if (location.IsRegister()) {
1337 locations->AddTemp(location);
1338 } else {
1339 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1340 }
1341}
1342
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001343void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001344 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001345 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001346 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001347 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001348 if (value_can_be_null) {
1349 __ Cbz(value, &done);
1350 }
Roland Levillainc73f0522018-08-14 15:16:50 +01001351 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07001352 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01001353 // Calculate the offset (in the card table) of the card corresponding to
1354 // `object`.
Alexandre Rames5319def2014-10-23 10:03:10 +01001355 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Roland Levillainc73f0522018-08-14 15:16:50 +01001356 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
1357 // `object`'s card.
1358 //
1359 // Register `card` contains the address of the card table. Note that the card
1360 // table's base is biased during its creation so that it always starts at an
1361 // address whose least-significant byte is equal to `kCardDirty` (see
1362 // art::gc::accounting::CardTable::Create). Therefore the STRB instruction
1363 // below writes the `kCardDirty` (byte) value into the `object`'s card
1364 // (located at `card + object >> kCardShift`).
1365 //
1366 // This dual use of the value in register `card` (1. to calculate the location
1367 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
1368 // (no need to explicitly load `kCardDirty` as an immediate value).
Serban Constantinescu02164b32014-11-13 14:05:07 +00001369 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001370 if (value_can_be_null) {
1371 __ Bind(&done);
1372 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001373}
1374
David Brazdil58282f42016-01-14 12:45:10 +00001375void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001376 // Blocked core registers:
1377 // lr : Runtime reserved.
1378 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001379 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001380 // ip1 : VIXL core temp.
1381 // ip0 : VIXL core temp.
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001382 // x18 : Platform register.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001383 //
1384 // Blocked fp registers:
1385 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001386 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1387 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001388 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001389 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001390 }
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001391 blocked_core_registers_[X18] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001392
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001393 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001394 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001395 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001396 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001397
David Brazdil58282f42016-01-14 12:45:10 +00001398 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001399 // Stubs do not save callee-save floating point registers. If the graph
1400 // is debuggable, we need to deal with these registers differently. For
1401 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001402 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1403 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001404 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001405 }
1406 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001407}
1408
Alexandre Rames3e69f162014-12-10 10:36:50 +00001409size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1410 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1411 __ Str(reg, MemOperand(sp, stack_index));
1412 return kArm64WordSize;
1413}
1414
1415size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1416 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1417 __ Ldr(reg, MemOperand(sp, stack_index));
1418 return kArm64WordSize;
1419}
1420
Artem Serov9df37b92019-07-23 16:41:54 +01001421size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1422 uint32_t reg_id ATTRIBUTE_UNUSED) {
1423 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1424 << "use SaveRestoreLiveRegistersHelper";
1425 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001426}
1427
Artem Serov9df37b92019-07-23 16:41:54 +01001428size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1429 uint32_t reg_id ATTRIBUTE_UNUSED) {
1430 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1431 << "use SaveRestoreLiveRegistersHelper";
1432 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001433}
1434
Alexandre Rames5319def2014-10-23 10:03:10 +01001435void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001436 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001437}
1438
1439void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001440 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001441}
1442
Vladimir Markoa0431112018-06-25 09:32:54 +01001443const Arm64InstructionSetFeatures& CodeGeneratorARM64::GetInstructionSetFeatures() const {
1444 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArm64InstructionSetFeatures();
1445}
1446
Alexandre Rames67555f72014-11-18 10:55:16 +00001447void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001448 if (constant->IsIntConstant()) {
1449 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1450 } else if (constant->IsLongConstant()) {
1451 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1452 } else if (constant->IsNullConstant()) {
1453 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001454 } else if (constant->IsFloatConstant()) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001455 __ Fmov(VRegister(destination), constant->AsFloatConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001456 } else {
1457 DCHECK(constant->IsDoubleConstant());
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001458 __ Fmov(VRegister(destination), constant->AsDoubleConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001459 }
1460}
1461
Alexandre Rames3e69f162014-12-10 10:36:50 +00001462
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001463static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001464 DCHECK(constant.IsConstant());
1465 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001466 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001467 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001468 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1469 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1470 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1471 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001472}
1473
Roland Levillain952b2352017-05-03 19:49:14 +01001474// Allocate a scratch register from the VIXL pool, querying first
1475// the floating-point register pool, and then the core register
1476// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001477// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1478// using a different allocation strategy.
1479static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1480 vixl::aarch64::UseScratchRegisterScope* temps,
1481 int size_in_bits) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001482 return masm->GetScratchVRegisterList()->IsEmpty()
Roland Levillain558dea12017-01-27 19:40:44 +00001483 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1484 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1485}
1486
Calin Juravlee460d1d2015-09-29 04:52:17 +01001487void CodeGeneratorARM64::MoveLocation(Location destination,
1488 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001489 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001490 if (source.Equals(destination)) {
1491 return;
1492 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001493
1494 // A valid move can always be inferred from the destination and source
1495 // locations. When moving from and to a register, the argument type can be
1496 // used to generate 32bit instead of 64bit moves. In debug mode we also
1497 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001498 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001499
1500 if (destination.IsRegister() || destination.IsFpuRegister()) {
1501 if (unspecified_type) {
1502 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1503 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001504 (src_cst != nullptr && (src_cst->IsIntConstant()
1505 || src_cst->IsFloatConstant()
1506 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001507 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001508 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001509 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001510 // If the source is a double stack slot or a 64bit constant, a 64bit
1511 // type is appropriate. Else the source is a register, and since the
1512 // type has not been specified, we chose a 64bit type to force a 64bit
1513 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001514 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001515 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001516 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001517 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1518 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001519 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001520 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1521 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1522 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001523 } else if (source.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001524 GetInstructionCodeGeneratorArm64()->LoadSIMDRegFromStack(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001525 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001526 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001527 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001528 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001529 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001530 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001531 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001532 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001533 DataType::Type source_type = DataType::Is64BitType(dst_type)
1534 ? DataType::Type::kInt64
1535 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001536 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1537 }
1538 } else {
1539 DCHECK(source.IsFpuRegister());
1540 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001541 DataType::Type source_type = DataType::Is64BitType(dst_type)
1542 ? DataType::Type::kFloat64
1543 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001544 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1545 } else {
1546 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001547 if (GetGraph()->HasSIMD()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001548 GetInstructionCodeGeneratorArm64()->MoveSIMDRegToSIMDReg(destination, source);
Artem Serovd4bccf12017-04-03 18:47:32 +01001549 } else {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001550 __ Fmov(VRegister(dst), FPRegisterFrom(source, dst_type));
Artem Serovd4bccf12017-04-03 18:47:32 +01001551 }
1552 }
1553 }
1554 } else if (destination.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001555 GetInstructionCodeGeneratorArm64()->MoveToSIMDStackSlot(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001556 } else { // The destination is not a register. It must be a stack slot.
1557 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1558 if (source.IsRegister() || source.IsFpuRegister()) {
1559 if (unspecified_type) {
1560 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001561 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001562 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001563 dst_type =
1564 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001565 }
1566 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001567 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1568 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001569 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001570 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001571 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1572 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001573 UseScratchRegisterScope temps(GetVIXLAssembler());
1574 HConstant* src_cst = source.GetConstant();
1575 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001576 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001577 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1578 ? Register(xzr)
1579 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001580 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001581 if (src_cst->IsIntConstant()) {
1582 temp = temps.AcquireW();
1583 } else if (src_cst->IsLongConstant()) {
1584 temp = temps.AcquireX();
1585 } else if (src_cst->IsFloatConstant()) {
1586 temp = temps.AcquireS();
1587 } else {
1588 DCHECK(src_cst->IsDoubleConstant());
1589 temp = temps.AcquireD();
1590 }
1591 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001592 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001593 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001594 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001595 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001596 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001597 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001598 // Use any scratch register (a core or a floating-point one)
1599 // from VIXL scratch register pools as a temporary.
1600 //
1601 // We used to only use the FP scratch register pool, but in some
1602 // rare cases the only register from this pool (D31) would
1603 // already be used (e.g. within a ParallelMove instruction, when
1604 // a move is blocked by a another move requiring a scratch FP
1605 // register, which would reserve D31). To prevent this issue, we
1606 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001607 //
1608 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001609 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001610 // use AcquireFPOrCoreCPURegisterOfSize instead of
1611 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1612 // allocates core scratch registers first.
1613 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1614 GetVIXLAssembler(),
1615 &temps,
1616 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001617 __ Ldr(temp, StackOperandFrom(source));
1618 __ Str(temp, StackOperandFrom(destination));
1619 }
1620 }
1621}
1622
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001623void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001624 CPURegister dst,
1625 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001626 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001627 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001628 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001629 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001630 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001631 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001632 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001633 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001634 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001635 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001636 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001637 case DataType::Type::kInt16:
1638 __ Ldrsh(Register(dst), src);
1639 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001640 case DataType::Type::kInt32:
1641 case DataType::Type::kReference:
1642 case DataType::Type::kInt64:
1643 case DataType::Type::kFloat32:
1644 case DataType::Type::kFloat64:
1645 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001646 __ Ldr(dst, src);
1647 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001648 case DataType::Type::kUint32:
1649 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001650 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001651 LOG(FATAL) << "Unreachable type " << type;
1652 }
1653}
1654
Calin Juravle77520bc2015-01-12 18:45:46 +00001655void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Vladimir Marko98873af2020-12-16 12:10:03 +00001656 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001657 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001658 const MemOperand& src,
1659 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001660 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001661 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001662 Register temp_base = temps.AcquireX();
1663
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001664 DCHECK(!src.IsPreIndex());
1665 DCHECK(!src.IsPostIndex());
1666
1667 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001668 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001669 {
1670 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1671 MemOperand base = MemOperand(temp_base);
1672 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001673 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001674 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001675 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001676 {
1677 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1678 __ ldarb(Register(dst), base);
1679 if (needs_null_check) {
1680 MaybeRecordImplicitNullCheck(instruction);
1681 }
1682 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001683 if (type == DataType::Type::kInt8) {
1684 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001685 }
1686 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001687 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001688 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001689 {
1690 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1691 __ ldarh(Register(dst), base);
1692 if (needs_null_check) {
1693 MaybeRecordImplicitNullCheck(instruction);
1694 }
1695 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001696 if (type == DataType::Type::kInt16) {
1697 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
1698 }
Artem Serov914d7a82017-02-07 14:33:49 +00001699 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001700 case DataType::Type::kInt32:
1701 case DataType::Type::kReference:
1702 case DataType::Type::kInt64:
1703 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001704 {
1705 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1706 __ ldar(Register(dst), base);
1707 if (needs_null_check) {
1708 MaybeRecordImplicitNullCheck(instruction);
1709 }
1710 }
1711 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001712 case DataType::Type::kFloat32:
1713 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00001714 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001715 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001716
Artem Serov914d7a82017-02-07 14:33:49 +00001717 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1718 {
1719 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1720 __ ldar(temp, base);
1721 if (needs_null_check) {
1722 MaybeRecordImplicitNullCheck(instruction);
1723 }
1724 }
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001725 __ Fmov(VRegister(dst), temp);
Artem Serov914d7a82017-02-07 14:33:49 +00001726 break;
Roland Levillain44015862016-01-22 11:47:17 +00001727 }
Aart Bik66c158e2018-01-31 12:55:04 -08001728 case DataType::Type::kUint32:
1729 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001730 case DataType::Type::kVoid:
Artem Serov914d7a82017-02-07 14:33:49 +00001731 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001732 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001733 }
1734}
1735
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001736void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001737 CPURegister src,
1738 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001739 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001740 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001741 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001742 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001743 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001744 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001745 case DataType::Type::kUint16:
1746 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001747 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001748 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001749 case DataType::Type::kInt32:
1750 case DataType::Type::kReference:
1751 case DataType::Type::kInt64:
1752 case DataType::Type::kFloat32:
1753 case DataType::Type::kFloat64:
1754 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001755 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001756 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001757 case DataType::Type::kUint32:
1758 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001759 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001760 LOG(FATAL) << "Unreachable type " << type;
1761 }
1762}
1763
Artem Serov914d7a82017-02-07 14:33:49 +00001764void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001765 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001766 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00001767 const MemOperand& dst,
1768 bool needs_null_check) {
1769 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001770 UseScratchRegisterScope temps(GetVIXLAssembler());
1771 Register temp_base = temps.AcquireX();
1772
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001773 DCHECK(!dst.IsPreIndex());
1774 DCHECK(!dst.IsPostIndex());
1775
1776 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001777 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001778 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001779 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00001780 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001781 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001782 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001783 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001784 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001785 {
1786 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1787 __ stlrb(Register(src), base);
1788 if (needs_null_check) {
1789 MaybeRecordImplicitNullCheck(instruction);
1790 }
1791 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001792 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001793 case DataType::Type::kUint16:
1794 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001795 {
1796 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1797 __ stlrh(Register(src), base);
1798 if (needs_null_check) {
1799 MaybeRecordImplicitNullCheck(instruction);
1800 }
1801 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001802 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001803 case DataType::Type::kInt32:
1804 case DataType::Type::kReference:
1805 case DataType::Type::kInt64:
1806 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001807 {
1808 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1809 __ stlr(Register(src), base);
1810 if (needs_null_check) {
1811 MaybeRecordImplicitNullCheck(instruction);
1812 }
1813 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001814 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001815 case DataType::Type::kFloat32:
1816 case DataType::Type::kFloat64: {
1817 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001818 Register temp_src;
1819 if (src.IsZero()) {
1820 // The zero register is used to avoid synthesizing zero constants.
1821 temp_src = Register(src);
1822 } else {
1823 DCHECK(src.IsFPRegister());
1824 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001825 __ Fmov(temp_src, VRegister(src));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001826 }
Artem Serov914d7a82017-02-07 14:33:49 +00001827 {
1828 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1829 __ stlr(temp_src, base);
1830 if (needs_null_check) {
1831 MaybeRecordImplicitNullCheck(instruction);
1832 }
1833 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001834 break;
1835 }
Aart Bik66c158e2018-01-31 12:55:04 -08001836 case DataType::Type::kUint32:
1837 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001838 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001839 LOG(FATAL) << "Unreachable type " << type;
1840 }
1841}
1842
Calin Juravle175dc732015-08-25 15:42:32 +01001843void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1844 HInstruction* instruction,
1845 uint32_t dex_pc,
1846 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001847 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00001848
Vladimir Markof6675082019-05-17 12:05:28 +01001849 ThreadOffset64 entrypoint_offset = GetThreadOffset<kArm64PointerSize>(entrypoint);
1850 // Reduce code size for AOT by using shared trampolines for slow path runtime calls across the
1851 // entire oat file. This adds an extra branch and we do not want to slow down the main path.
1852 // For JIT, thunk sharing is per-method, so the gains would be smaller or even negative.
Vladimir Marko695348f2020-05-19 14:42:02 +01001853 if (slow_path == nullptr || GetCompilerOptions().IsJitCompiler()) {
Vladimir Markof6675082019-05-17 12:05:28 +01001854 __ Ldr(lr, MemOperand(tr, entrypoint_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00001855 // Ensure the pc position is recorded immediately after the `blr` instruction.
1856 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1857 __ blr(lr);
1858 if (EntrypointRequiresStackMap(entrypoint)) {
1859 RecordPcInfo(instruction, dex_pc, slow_path);
1860 }
Vladimir Markof6675082019-05-17 12:05:28 +01001861 } else {
1862 // Ensure the pc position is recorded immediately after the `bl` instruction.
1863 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1864 EmitEntrypointThunkCall(entrypoint_offset);
1865 if (EntrypointRequiresStackMap(entrypoint)) {
1866 RecordPcInfo(instruction, dex_pc, slow_path);
1867 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001868 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001869}
1870
Roland Levillaindec8f632016-07-22 17:10:06 +01001871void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1872 HInstruction* instruction,
1873 SlowPathCode* slow_path) {
1874 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01001875 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1876 __ Blr(lr);
1877}
1878
Alexandre Rames67555f72014-11-18 10:55:16 +00001879void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001880 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001881 UseScratchRegisterScope temps(GetVIXLAssembler());
1882 Register temp = temps.AcquireW();
Vladimir Markodc682aa2018-01-04 18:42:57 +00001883 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001884 const size_t status_byte_offset =
1885 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1886 constexpr uint32_t shifted_visibly_initialized_value =
1887 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001888
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001889 // CMP (immediate) is limited to imm12 or imm12<<12, so we would need to materialize
1890 // the constant 0xf0000000 for comparison with the full 32-bit field. To reduce the code
1891 // size, load only the high byte of the field and compare with 0xf0.
1892 // Note: The same code size could be achieved with LDR+MNV(asr #24)+CBNZ but benchmarks
1893 // show that this pattern is slower (tested on little cores).
1894 __ Ldrb(temp, HeapOperand(class_reg, status_byte_offset));
1895 __ Cmp(temp, shifted_visibly_initialized_value);
1896 __ B(lo, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001897 __ Bind(slow_path->GetExitLabel());
1898}
Alexandre Rames5319def2014-10-23 10:03:10 +01001899
Vladimir Marko175e7862018-03-27 09:03:13 +00001900void InstructionCodeGeneratorARM64::GenerateBitstringTypeCheckCompare(
1901 HTypeCheckInstruction* check, vixl::aarch64::Register temp) {
1902 uint32_t path_to_root = check->GetBitstringPathToRoot();
1903 uint32_t mask = check->GetBitstringMask();
1904 DCHECK(IsPowerOfTwo(mask + 1));
1905 size_t mask_bits = WhichPowerOf2(mask + 1);
1906
1907 if (mask_bits == 16u) {
1908 // Load only the bitstring part of the status word.
1909 __ Ldrh(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1910 } else {
1911 // /* uint32_t */ temp = temp->status_
1912 __ Ldr(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1913 // Extract the bitstring bits.
1914 __ Ubfx(temp, temp, 0, mask_bits);
1915 }
1916 // Compare the bitstring bits to `path_to_root`.
1917 __ Cmp(temp, path_to_root);
1918}
1919
Roland Levillain44015862016-01-22 11:47:17 +00001920void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001921 BarrierType type = BarrierAll;
1922
1923 switch (kind) {
1924 case MemBarrierKind::kAnyAny:
1925 case MemBarrierKind::kAnyStore: {
1926 type = BarrierAll;
1927 break;
1928 }
1929 case MemBarrierKind::kLoadAny: {
1930 type = BarrierReads;
1931 break;
1932 }
1933 case MemBarrierKind::kStoreStore: {
1934 type = BarrierWrites;
1935 break;
1936 }
1937 default:
1938 LOG(FATAL) << "Unexpected memory barrier " << kind;
1939 }
1940 __ Dmb(InnerShareable, type);
1941}
1942
Serban Constantinescu02164b32014-11-13 14:05:07 +00001943void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1944 HBasicBlock* successor) {
1945 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001946 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1947 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001948 slow_path =
1949 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001950 instruction->SetSlowPath(slow_path);
1951 codegen_->AddSlowPath(slow_path);
1952 if (successor != nullptr) {
1953 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001954 }
1955 } else {
1956 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1957 }
1958
Serban Constantinescu02164b32014-11-13 14:05:07 +00001959 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1960 Register temp = temps.AcquireW();
1961
Andreas Gampe542451c2016-07-26 09:02:02 -07001962 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001963 if (successor == nullptr) {
1964 __ Cbnz(temp, slow_path->GetEntryLabel());
1965 __ Bind(slow_path->GetReturnLabel());
1966 } else {
1967 __ Cbz(temp, codegen_->GetLabelOf(successor));
1968 __ B(slow_path->GetEntryLabel());
1969 // slow_path will return to GetLabelOf(successor).
1970 }
1971}
1972
Alexandre Rames5319def2014-10-23 10:03:10 +01001973InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1974 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001975 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001976 assembler_(codegen->GetAssembler()),
1977 codegen_(codegen) {}
1978
Alexandre Rames67555f72014-11-18 10:55:16 +00001979void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001980 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001981 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001982 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001983 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001984 case DataType::Type::kInt32:
1985 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01001986 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001987 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001988 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001989 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001990
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001991 case DataType::Type::kFloat32:
1992 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001993 locations->SetInAt(0, Location::RequiresFpuRegister());
1994 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001995 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001996 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001997
Alexandre Rames5319def2014-10-23 10:03:10 +01001998 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001999 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002000 }
2001}
2002
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002003void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2004 const FieldInfo& field_info) {
Nicolas Geoffray791df7a2021-01-23 13:28:56 +00002005 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002006
2007 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002008 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01002009 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002010 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2011 object_field_get_with_read_barrier
2012 ? LocationSummary::kCallOnSlowPath
2013 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002014 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002015 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko0ecac682018-08-07 10:40:38 +01002016 // We need a temporary register for the read barrier load in
2017 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2018 // only if the field is volatile or the offset is too big.
2019 if (field_info.IsVolatile() ||
2020 field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2021 locations->AddTemp(FixedTempLocation());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002022 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002023 }
Nicolas Geoffray791df7a2021-01-23 13:28:56 +00002024 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002025 if (DataType::IsFloatingPointType(instruction->GetType())) {
Nicolas Geoffray791df7a2021-01-23 13:28:56 +00002026 locations->SetOut(Location::RequiresFpuRegister());
Alexandre Rames09a99962015-04-15 11:47:56 +01002027 } else {
Nicolas Geoffray791df7a2021-01-23 13:28:56 +00002028 // The output overlaps for an object field get when read barriers
2029 // are enabled: we do not want the load to overwrite the object's
2030 // location, as we need it to emit the read barrier.
2031 locations->SetOut(
2032 Location::RequiresRegister(),
2033 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002034 }
2035}
2036
2037void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2038 const FieldInfo& field_info) {
Nicolas Geoffray791df7a2021-01-23 13:28:56 +00002039 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002040 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray791df7a2021-01-23 13:28:56 +00002041 Location base_loc = locations->InAt(0);
Roland Levillain44015862016-01-22 11:47:17 +00002042 Location out = locations->Out();
2043 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01002044 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
2045 DataType::Type load_type = instruction->GetType();
Nicolas Geoffray791df7a2021-01-23 13:28:56 +00002046 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002047
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002048 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01002049 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002050 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002051 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002052 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002053 Location maybe_temp =
2054 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002055 // Note that potential implicit null checks are handled in this
2056 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2057 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2058 instruction,
2059 out,
2060 base,
2061 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002062 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002063 /* needs_null_check= */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002064 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002065 } else {
2066 // General case.
2067 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002068 // Note that a potential implicit null check is handled in this
2069 // CodeGeneratorARM64::LoadAcquire call.
2070 // NB: LoadAcquire will record the pc info if needed.
Vladimir Marko98873af2020-12-16 12:10:03 +00002071 codegen_->LoadAcquire(instruction,
2072 load_type,
2073 OutputCPURegister(instruction),
2074 field,
2075 /* needs_null_check= */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002076 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002077 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2078 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01002079 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002080 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002081 }
Vladimir Marko61b92282017-10-11 13:23:17 +01002082 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002083 // If read barriers are enabled, emit read barriers other than
2084 // Baker's using a slow path (and also unpoison the loaded
2085 // reference, if heap poisoning is enabled).
2086 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2087 }
Roland Levillain4d027112015-07-01 15:41:14 +01002088 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002089}
2090
2091void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2092 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002093 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01002094 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002095 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2096 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002097 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002098 locations->SetInAt(1, Location::RequiresFpuRegister());
2099 } else {
2100 locations->SetInAt(1, Location::RequiresRegister());
2101 }
2102}
2103
2104void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002105 const FieldInfo& field_info,
2106 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002107 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2108
2109 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002110 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002111 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002112 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002113 DataType::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002114
Roland Levillain4d027112015-07-01 15:41:14 +01002115 {
2116 // We use a block to end the scratch scope before the write barrier, thus
2117 // freeing the temporary registers so they can be used in `MarkGCCard`.
2118 UseScratchRegisterScope temps(GetVIXLAssembler());
2119
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002120 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002121 DCHECK(value.IsW());
2122 Register temp = temps.AcquireW();
2123 __ Mov(temp, value.W());
2124 GetAssembler()->PoisonHeapReference(temp.W());
2125 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002126 }
Roland Levillain4d027112015-07-01 15:41:14 +01002127
2128 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002129 codegen_->StoreRelease(
Andreas Gampe3db70682018-12-26 15:12:03 -08002130 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check= */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002131 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002132 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2133 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002134 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2135 codegen_->MaybeRecordImplicitNullCheck(instruction);
2136 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002137 }
2138
2139 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002140 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002141 }
2142}
2143
Alexandre Rames67555f72014-11-18 10:55:16 +00002144void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002145 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002146
2147 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002148 case DataType::Type::kInt32:
2149 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002150 Register dst = OutputRegister(instr);
2151 Register lhs = InputRegisterAt(instr, 0);
2152 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002153 if (instr->IsAdd()) {
2154 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002155 } else if (instr->IsAnd()) {
2156 __ And(dst, lhs, rhs);
2157 } else if (instr->IsOr()) {
2158 __ Orr(dst, lhs, rhs);
2159 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002160 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002161 } else if (instr->IsRor()) {
2162 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002163 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002164 __ Ror(dst, lhs, shift);
2165 } else {
2166 // Ensure shift distance is in the same size register as the result. If
2167 // we are rotating a long and the shift comes in a w register originally,
2168 // we don't need to sxtw for use as an x since the shift distances are
2169 // all & reg_bits - 1.
2170 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2171 }
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002172 } else if (instr->IsMin() || instr->IsMax()) {
2173 __ Cmp(lhs, rhs);
2174 __ Csel(dst, lhs, rhs, instr->IsMin() ? lt : gt);
Alexandre Rames67555f72014-11-18 10:55:16 +00002175 } else {
2176 DCHECK(instr->IsXor());
2177 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002178 }
2179 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002180 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002181 case DataType::Type::kFloat32:
2182 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002183 VRegister dst = OutputFPRegister(instr);
2184 VRegister lhs = InputFPRegisterAt(instr, 0);
2185 VRegister rhs = InputFPRegisterAt(instr, 1);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002186 if (instr->IsAdd()) {
2187 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002188 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002189 __ Fsub(dst, lhs, rhs);
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002190 } else if (instr->IsMin()) {
2191 __ Fmin(dst, lhs, rhs);
2192 } else if (instr->IsMax()) {
2193 __ Fmax(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002194 } else {
2195 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002196 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002197 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002198 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002199 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002200 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002201 }
2202}
2203
Serban Constantinescu02164b32014-11-13 14:05:07 +00002204void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2205 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2206
Vladimir Markoca6fff82017-10-03 14:49:14 +01002207 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002208 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002209 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002210 case DataType::Type::kInt32:
2211 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002212 locations->SetInAt(0, Location::RequiresRegister());
2213 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002214 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002215 break;
2216 }
2217 default:
2218 LOG(FATAL) << "Unexpected shift type " << type;
2219 }
2220}
2221
2222void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2223 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2224
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002225 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002226 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002227 case DataType::Type::kInt32:
2228 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002229 Register dst = OutputRegister(instr);
2230 Register lhs = InputRegisterAt(instr, 0);
2231 Operand rhs = InputOperandAt(instr, 1);
2232 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002233 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002234 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002235 if (instr->IsShl()) {
2236 __ Lsl(dst, lhs, shift_value);
2237 } else if (instr->IsShr()) {
2238 __ Asr(dst, lhs, shift_value);
2239 } else {
2240 __ Lsr(dst, lhs, shift_value);
2241 }
2242 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002243 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002244
2245 if (instr->IsShl()) {
2246 __ Lsl(dst, lhs, rhs_reg);
2247 } else if (instr->IsShr()) {
2248 __ Asr(dst, lhs, rhs_reg);
2249 } else {
2250 __ Lsr(dst, lhs, rhs_reg);
2251 }
2252 }
2253 break;
2254 }
2255 default:
2256 LOG(FATAL) << "Unexpected shift operation type " << type;
2257 }
2258}
2259
Alexandre Rames5319def2014-10-23 10:03:10 +01002260void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002261 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002262}
2263
2264void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002265 HandleBinaryOp(instruction);
2266}
2267
2268void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2269 HandleBinaryOp(instruction);
2270}
2271
2272void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2273 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002274}
2275
Artem Serov7fc63502016-02-09 17:15:29 +00002276void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002277 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002278 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002279 locations->SetInAt(0, Location::RequiresRegister());
2280 // There is no immediate variant of negated bitwise instructions in AArch64.
2281 locations->SetInAt(1, Location::RequiresRegister());
2282 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2283}
2284
Artem Serov7fc63502016-02-09 17:15:29 +00002285void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002286 Register dst = OutputRegister(instr);
2287 Register lhs = InputRegisterAt(instr, 0);
2288 Register rhs = InputRegisterAt(instr, 1);
2289
2290 switch (instr->GetOpKind()) {
2291 case HInstruction::kAnd:
2292 __ Bic(dst, lhs, rhs);
2293 break;
2294 case HInstruction::kOr:
2295 __ Orn(dst, lhs, rhs);
2296 break;
2297 case HInstruction::kXor:
2298 __ Eon(dst, lhs, rhs);
2299 break;
2300 default:
2301 LOG(FATAL) << "Unreachable";
2302 }
2303}
2304
Anton Kirilov74234da2017-01-13 14:42:47 +00002305void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2306 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002307 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2308 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002309 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002310 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002311 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2312 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2313 } else {
2314 locations->SetInAt(0, Location::RequiresRegister());
2315 }
2316 locations->SetInAt(1, Location::RequiresRegister());
2317 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2318}
2319
Anton Kirilov74234da2017-01-13 14:42:47 +00002320void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2321 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002322 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002323 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002324 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002325 Register out = OutputRegister(instruction);
2326 Register left;
2327 if (kind != HInstruction::kNeg) {
2328 left = InputRegisterAt(instruction, 0);
2329 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002330 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002331 // shifter operand operation, the IR generating `right_reg` (input to the type
2332 // conversion) can have a different type from the current instruction's type,
2333 // so we manually indicate the type.
2334 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002335 Operand right_operand(0);
2336
Anton Kirilov74234da2017-01-13 14:42:47 +00002337 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2338 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002339 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2340 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002341 right_operand = Operand(right_reg,
2342 helpers::ShiftFromOpKind(op_kind),
2343 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002344 }
2345
2346 // Logical binary operations do not support extension operations in the
2347 // operand. Note that VIXL would still manage if it was passed by generating
2348 // the extension as a separate instruction.
2349 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2350 DCHECK(!right_operand.IsExtendedRegister() ||
2351 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2352 kind != HInstruction::kNeg));
2353 switch (kind) {
2354 case HInstruction::kAdd:
2355 __ Add(out, left, right_operand);
2356 break;
2357 case HInstruction::kAnd:
2358 __ And(out, left, right_operand);
2359 break;
2360 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002361 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002362 __ Neg(out, right_operand);
2363 break;
2364 case HInstruction::kOr:
2365 __ Orr(out, left, right_operand);
2366 break;
2367 case HInstruction::kSub:
2368 __ Sub(out, left, right_operand);
2369 break;
2370 case HInstruction::kXor:
2371 __ Eor(out, left, right_operand);
2372 break;
2373 default:
2374 LOG(FATAL) << "Unexpected operation kind: " << kind;
2375 UNREACHABLE();
2376 }
2377}
2378
Artem Serov328429f2016-07-06 16:23:04 +01002379void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002380 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002381 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002382 locations->SetInAt(0, Location::RequiresRegister());
2383 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002384 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002385}
2386
Roland Levillain19c54192016-11-04 13:44:09 +00002387void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002388 __ Add(OutputRegister(instruction),
2389 InputRegisterAt(instruction, 0),
2390 Operand(InputOperandAt(instruction, 1)));
2391}
2392
Artem Serove1811ed2017-04-27 16:50:47 +01002393void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2394 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002395 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002396
2397 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2398
2399 locations->SetInAt(0, Location::RequiresRegister());
2400 // For byte case we don't need to shift the index variable so we can encode the data offset into
2401 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2402 // data offset constant generation out of the loop and reduce the critical path length in the
2403 // loop.
2404 locations->SetInAt(1, shift->GetValue() == 0
2405 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2406 : Location::RequiresRegister());
2407 locations->SetInAt(2, Location::ConstantLocation(shift));
2408 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2409}
2410
2411void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2412 HIntermediateAddressIndex* instruction) {
2413 Register index_reg = InputRegisterAt(instruction, 0);
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002414 uint32_t shift = Int64FromLocation(instruction->GetLocations()->InAt(2));
Artem Serove1811ed2017-04-27 16:50:47 +01002415 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2416
2417 if (shift == 0) {
2418 __ Add(OutputRegister(instruction), index_reg, offset);
2419 } else {
2420 Register offset_reg = InputRegisterAt(instruction, 1);
2421 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2422 }
2423}
2424
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002425void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002426 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002427 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002428 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2429 if (instr->GetOpKind() == HInstruction::kSub &&
2430 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002431 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002432 // Don't allocate register for Mneg instruction.
2433 } else {
2434 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2435 Location::RequiresRegister());
2436 }
2437 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2438 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002439 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2440}
2441
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002442void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002443 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002444 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2445 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002446
2447 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2448 // This fixup should be carried out for all multiply-accumulate instructions:
2449 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002450 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002451 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2452 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002453 vixl::aarch64::Instruction* prev =
2454 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002455 if (prev->IsLoadOrStore()) {
2456 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002457 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002458 __ nop();
2459 }
2460 }
2461
2462 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002463 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002464 __ Madd(res, mul_left, mul_right, accumulator);
2465 } else {
2466 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002467 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002468 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002469 __ Mneg(res, mul_left, mul_right);
2470 } else {
2471 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2472 __ Msub(res, mul_left, mul_right, accumulator);
2473 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002474 }
2475}
2476
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002477void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002478 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002479 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002480 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002481 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2482 object_array_get_with_read_barrier
2483 ? LocationSummary::kCallOnSlowPath
2484 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002485 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002486 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002487 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002488 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002489 // We need a temporary register for the read barrier load in
2490 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2491 // only if the offset is too big.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002492 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2493 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002494 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002495 if (offset >= kReferenceLoadMinFarOffset) {
2496 locations->AddTemp(FixedTempLocation());
2497 }
Artem Serov0806f582018-10-11 20:14:20 +01002498 } else if (!instruction->GetArray()->IsIntermediateAddress()) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01002499 // We need a non-scratch temporary for the array data pointer in
Artem Serov0806f582018-10-11 20:14:20 +01002500 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier() for the case with no
2501 // intermediate address.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002502 locations->AddTemp(Location::RequiresRegister());
2503 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002504 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002505 locations->SetInAt(0, Location::RequiresRegister());
2506 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002507 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002508 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2509 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002510 // The output overlaps in the case of an object array get with
2511 // read barriers enabled: we do not want the move to overwrite the
2512 // array's location, as we need it to emit the read barrier.
2513 locations->SetOut(
2514 Location::RequiresRegister(),
2515 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002516 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002517}
2518
2519void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002520 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002521 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002522 LocationSummary* locations = instruction->GetLocations();
2523 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002524 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002525 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002526 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2527 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002528 MacroAssembler* masm = GetVIXLAssembler();
2529 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002530
Artem Serov0806f582018-10-11 20:14:20 +01002531 // The non-Baker read barrier instrumentation of object ArrayGet instructions
Roland Levillain19c54192016-11-04 13:44:09 +00002532 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002533 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002534 instruction->GetArray()->IsIntermediateAddress() &&
Artem Serov0806f582018-10-11 20:14:20 +01002535 kEmitCompilerReadBarrier &&
2536 !kUseBakerReadBarrier));
Roland Levillain19c54192016-11-04 13:44:09 +00002537
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002538 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002539 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002540 // Note that a potential implicit null check is handled in the
2541 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002542 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002543 if (index.IsConstant()) {
Artem Serov0806f582018-10-11 20:14:20 +01002544 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002545 // Array load with a constant index can be treated as a field load.
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002546 offset += Int64FromLocation(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002547 Location maybe_temp =
2548 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2549 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2550 out,
2551 obj.W(),
2552 offset,
2553 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002554 /* needs_null_check= */ false,
2555 /* use_load_acquire= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002556 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002557 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08002558 instruction, out, obj.W(), offset, index, /* needs_null_check= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002559 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002560 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002561 // General case.
2562 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002563 Register length;
2564 if (maybe_compressed_char_at) {
2565 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2566 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002567 {
2568 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2569 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2570
2571 if (instruction->GetArray()->IsIntermediateAddress()) {
2572 DCHECK_LT(count_offset, offset);
2573 int64_t adjusted_offset =
2574 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2575 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2576 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2577 } else {
2578 __ Ldr(length, HeapOperand(obj, count_offset));
2579 }
2580 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002581 }
jessicahandojo05765752016-09-09 19:01:32 -07002582 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002583 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002584 if (maybe_compressed_char_at) {
2585 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002586 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2587 "Expecting 0=compressed, 1=uncompressed");
2588 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002589 __ Ldrb(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002590 HeapOperand(obj, offset + Int64FromLocation(index)));
jessicahandojo05765752016-09-09 19:01:32 -07002591 __ B(&done);
2592 __ Bind(&uncompressed_load);
2593 __ Ldrh(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002594 HeapOperand(obj, offset + (Int64FromLocation(index) << 1)));
jessicahandojo05765752016-09-09 19:01:32 -07002595 __ Bind(&done);
2596 } else {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002597 offset += Int64FromLocation(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002598 source = HeapOperand(obj, offset);
2599 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002600 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002601 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002602 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002603 // We do not need to compute the intermediate address from the array: the
2604 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002605 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002606 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002607 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2608 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
Roland Levillain44015862016-01-22 11:47:17 +00002609 }
2610 temp = obj;
2611 } else {
2612 __ Add(temp, obj, offset);
2613 }
jessicahandojo05765752016-09-09 19:01:32 -07002614 if (maybe_compressed_char_at) {
2615 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002616 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2617 "Expecting 0=compressed, 1=uncompressed");
2618 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002619 __ Ldrb(Register(OutputCPURegister(instruction)),
2620 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2621 __ B(&done);
2622 __ Bind(&uncompressed_load);
2623 __ Ldrh(Register(OutputCPURegister(instruction)),
2624 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2625 __ Bind(&done);
2626 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002627 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002628 }
Roland Levillain44015862016-01-22 11:47:17 +00002629 }
jessicahandojo05765752016-09-09 19:01:32 -07002630 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002631 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2632 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002633 codegen_->Load(type, OutputCPURegister(instruction), source);
2634 codegen_->MaybeRecordImplicitNullCheck(instruction);
2635 }
Roland Levillain44015862016-01-22 11:47:17 +00002636
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002637 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002638 static_assert(
2639 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2640 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2641 Location obj_loc = locations->InAt(0);
2642 if (index.IsConstant()) {
2643 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2644 } else {
2645 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2646 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002647 }
Roland Levillain4d027112015-07-01 15:41:14 +01002648 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002649}
2650
Alexandre Rames5319def2014-10-23 10:03:10 +01002651void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002652 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002653 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002654 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002655}
2656
2657void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002658 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002659 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002660 {
2661 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2662 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2663 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2664 codegen_->MaybeRecordImplicitNullCheck(instruction);
2665 }
jessicahandojo05765752016-09-09 19:01:32 -07002666 // Mask out compression flag from String's array length.
2667 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002668 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002669 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002670}
2671
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002672void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002673 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002674
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002675 bool needs_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002676 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002677 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002678 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002679 locations->SetInAt(0, Location::RequiresRegister());
2680 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002681 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2682 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002683 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002684 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002685 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002686 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002687 }
2688}
2689
2690void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002691 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002692 LocationSummary* locations = instruction->GetLocations();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002693 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002694 bool needs_write_barrier =
2695 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002696
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002697 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002698 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002699 CPURegister source = value;
2700 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002701 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002702 MemOperand destination = HeapOperand(array);
2703 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002704
2705 if (!needs_write_barrier) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002706 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002707 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002708 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002709 destination = HeapOperand(array, offset);
2710 } else {
2711 UseScratchRegisterScope temps(masm);
2712 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002713 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002714 // We do not need to compute the intermediate address from the array: the
2715 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002716 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002717 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002718 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2719 DCHECK(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002720 }
2721 temp = array;
2722 } else {
2723 __ Add(temp, array, offset);
2724 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002725 destination = HeapOperand(temp,
2726 XRegisterFrom(index),
2727 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002728 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002729 }
Artem Serov914d7a82017-02-07 14:33:49 +00002730 {
2731 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2732 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2733 codegen_->Store(value_type, value, destination);
2734 codegen_->MaybeRecordImplicitNullCheck(instruction);
2735 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002736 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002737 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002738
2739 bool can_value_be_null = instruction->GetValueCanBeNull();
2740 vixl::aarch64::Label do_store;
2741 if (can_value_be_null) {
2742 __ Cbz(Register(value), &do_store);
2743 }
2744
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002745 SlowPathCodeARM64* slow_path = nullptr;
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002746 if (needs_type_check) {
2747 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
2748 codegen_->AddSlowPath(slow_path);
2749
2750 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2751 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2752 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2753
Alexandre Rames97833a02015-04-16 15:07:12 +01002754 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002755 Register temp = temps.AcquireSameSizeAs(array);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002756 Register temp2 = temps.AcquireSameSizeAs(array);
2757
2758 // Note that when Baker read barriers are enabled, the type
2759 // checks are performed without read barriers. This is fine,
2760 // even in the case where a class object is in the from-space
2761 // after the flip, as a comparison involving such a type would
2762 // not produce a false positive; it may of course produce a
2763 // false negative, in which case we would take the ArraySet
2764 // slow path.
2765
2766 // /* HeapReference<Class> */ temp = array->klass_
2767 {
2768 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2769 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2770 __ Ldr(temp, HeapOperand(array, class_offset));
2771 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames97833a02015-04-16 15:07:12 +01002772 }
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002773 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Alexandre Rames97833a02015-04-16 15:07:12 +01002774
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002775 // /* HeapReference<Class> */ temp = temp->component_type_
2776 __ Ldr(temp, HeapOperand(temp, component_offset));
2777 // /* HeapReference<Class> */ temp2 = value->klass_
2778 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2779 // If heap poisoning is enabled, no need to unpoison `temp`
2780 // nor `temp2`, as we are comparing two poisoned references.
2781 __ Cmp(temp, temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002782
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002783 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2784 vixl::aarch64::Label do_put;
2785 __ B(eq, &do_put);
2786 // If heap poisoning is enabled, the `temp` reference has
2787 // not been unpoisoned yet; unpoison it now.
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002788 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002789
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002790 // /* HeapReference<Class> */ temp = temp->super_class_
2791 __ Ldr(temp, HeapOperand(temp, super_offset));
2792 // If heap poisoning is enabled, no need to unpoison
2793 // `temp`, as we are comparing against null below.
2794 __ Cbnz(temp, slow_path->GetEntryLabel());
2795 __ Bind(&do_put);
Vladimir Markod1ef8732017-04-18 13:55:13 +01002796 } else {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002797 __ B(ne, slow_path->GetEntryLabel());
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002798 }
2799 }
2800
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002801 codegen_->MarkGCCard(array, value.W(), /* value_can_be_null= */ false);
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002802
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002803 if (can_value_be_null) {
2804 DCHECK(do_store.IsLinked());
2805 __ Bind(&do_store);
2806 }
2807
2808 UseScratchRegisterScope temps(masm);
2809 if (kPoisonHeapReferences) {
2810 Register temp_source = temps.AcquireSameSizeAs(array);
2811 DCHECK(value.IsW());
2812 __ Mov(temp_source, value.W());
2813 GetAssembler()->PoisonHeapReference(temp_source);
2814 source = temp_source;
2815 }
2816
2817 if (index.IsConstant()) {
2818 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
2819 destination = HeapOperand(array, offset);
2820 } else {
2821 Register temp_base = temps.AcquireSameSizeAs(array);
2822 __ Add(temp_base, array, offset);
2823 destination = HeapOperand(temp_base,
2824 XRegisterFrom(index),
2825 LSL,
2826 DataType::SizeShift(value_type));
2827 }
2828
2829 {
2830 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2831 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2832 __ Str(source, destination);
2833
2834 if (can_value_be_null || !needs_type_check) {
2835 codegen_->MaybeRecordImplicitNullCheck(instruction);
2836 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002837 }
2838
2839 if (slow_path != nullptr) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002840 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002841 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002842 }
2843}
2844
Alexandre Rames67555f72014-11-18 10:55:16 +00002845void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002846 RegisterSet caller_saves = RegisterSet::Empty();
2847 InvokeRuntimeCallingConvention calling_convention;
2848 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2849 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2850 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002851
2852 // If both index and length are constant, we can check the bounds statically and
2853 // generate code accordingly. We want to make sure we generate constant locations
2854 // in that case, regardless of whether they are encodable in the comparison or not.
2855 HInstruction* index = instruction->InputAt(0);
2856 HInstruction* length = instruction->InputAt(1);
2857 bool both_const = index->IsConstant() && length->IsConstant();
2858 locations->SetInAt(0, both_const
2859 ? Location::ConstantLocation(index->AsConstant())
2860 : ARM64EncodableConstantOrRegister(index, instruction));
2861 locations->SetInAt(1, both_const
2862 ? Location::ConstantLocation(length->AsConstant())
2863 : ARM64EncodableConstantOrRegister(length, instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002864}
2865
2866void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Georgia Kouvelibe530852019-01-17 10:46:41 +00002867 LocationSummary* locations = instruction->GetLocations();
2868 Location index_loc = locations->InAt(0);
2869 Location length_loc = locations->InAt(1);
2870
2871 int cmp_first_input = 0;
2872 int cmp_second_input = 1;
2873 Condition cond = hs;
2874
2875 if (index_loc.IsConstant()) {
2876 int64_t index = Int64FromLocation(index_loc);
2877 if (length_loc.IsConstant()) {
2878 int64_t length = Int64FromLocation(length_loc);
2879 if (index < 0 || index >= length) {
2880 BoundsCheckSlowPathARM64* slow_path =
2881 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
2882 codegen_->AddSlowPath(slow_path);
2883 __ B(slow_path->GetEntryLabel());
2884 } else {
2885 // BCE will remove the bounds check if we are guaranteed to pass.
2886 // However, some optimization after BCE may have generated this, and we should not
2887 // generate a bounds check if it is a valid range.
2888 }
2889 return;
2890 }
2891 // Only the index is constant: change the order of the operands and commute the condition
2892 // so we can use an immediate constant for the index (only the second input to a cmp
2893 // instruction can be an immediate).
2894 cmp_first_input = 1;
2895 cmp_second_input = 0;
2896 cond = ls;
2897 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002898 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002899 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002900 __ Cmp(InputRegisterAt(instruction, cmp_first_input),
2901 InputOperandAt(instruction, cmp_second_input));
Alexandre Rames67555f72014-11-18 10:55:16 +00002902 codegen_->AddSlowPath(slow_path);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002903 __ B(slow_path->GetEntryLabel(), cond);
Alexandre Rames67555f72014-11-18 10:55:16 +00002904}
2905
Alexandre Rames67555f72014-11-18 10:55:16 +00002906void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2907 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002908 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00002909 locations->SetInAt(0, Location::RequiresRegister());
2910 if (check->HasUses()) {
2911 locations->SetOut(Location::SameAsFirstInput());
2912 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01002913 // Rely on the type initialization to save everything we need.
2914 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Alexandre Rames67555f72014-11-18 10:55:16 +00002915}
2916
2917void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2918 // We assume the class is not null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01002919 SlowPathCodeARM64* slow_path =
2920 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(check->GetLoadClass(), check);
Alexandre Rames67555f72014-11-18 10:55:16 +00002921 codegen_->AddSlowPath(slow_path);
2922 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2923}
2924
Roland Levillain1a653882016-03-18 18:05:57 +00002925static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2926 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2927 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2928}
2929
2930void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002931 VRegister lhs_reg = InputFPRegisterAt(instruction, 0);
Roland Levillain1a653882016-03-18 18:05:57 +00002932 Location rhs_loc = instruction->GetLocations()->InAt(1);
2933 if (rhs_loc.IsConstant()) {
2934 // 0.0 is the only immediate that can be encoded directly in
2935 // an FCMP instruction.
2936 //
2937 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2938 // specify that in a floating-point comparison, positive zero
2939 // and negative zero are considered equal, so we can use the
2940 // literal 0.0 for both cases here.
2941 //
2942 // Note however that some methods (Float.equal, Float.compare,
2943 // Float.compareTo, Double.equal, Double.compare,
2944 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2945 // StrictMath.min) consider 0.0 to be (strictly) greater than
2946 // -0.0. So if we ever translate calls to these methods into a
2947 // HCompare instruction, we must handle the -0.0 case with
2948 // care here.
2949 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2950 __ Fcmp(lhs_reg, 0.0);
2951 } else {
2952 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2953 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002954}
2955
Serban Constantinescu02164b32014-11-13 14:05:07 +00002956void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002957 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002958 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002959 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002960 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002961 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002962 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002963 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002964 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002965 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002966 case DataType::Type::kInt32:
2967 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002968 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002969 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002970 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2971 break;
2972 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002973 case DataType::Type::kFloat32:
2974 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002975 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002976 locations->SetInAt(1,
2977 IsFloatingPointZeroConstant(compare->InputAt(1))
2978 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2979 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002980 locations->SetOut(Location::RequiresRegister());
2981 break;
2982 }
2983 default:
2984 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2985 }
2986}
2987
2988void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002989 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002990
2991 // 0 if: left == right
2992 // 1 if: left > right
2993 // -1 if: left < right
2994 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002995 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002996 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002997 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002998 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002999 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003000 case DataType::Type::kInt32:
3001 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003002 Register result = OutputRegister(compare);
3003 Register left = InputRegisterAt(compare, 0);
3004 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003005 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003006 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3007 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003008 break;
3009 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003010 case DataType::Type::kFloat32:
3011 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003012 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003013 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003014 __ Cset(result, ne);
3015 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003016 break;
3017 }
3018 default:
3019 LOG(FATAL) << "Unimplemented compare type " << in_type;
3020 }
3021}
3022
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003023void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003024 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003025
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003026 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003027 locations->SetInAt(0, Location::RequiresFpuRegister());
3028 locations->SetInAt(1,
3029 IsFloatingPointZeroConstant(instruction->InputAt(1))
3030 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3031 : Location::RequiresFpuRegister());
3032 } else {
3033 // Integer cases.
3034 locations->SetInAt(0, Location::RequiresRegister());
3035 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3036 }
3037
David Brazdilb3e773e2016-01-26 11:28:37 +00003038 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003039 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003040 }
3041}
3042
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003043void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003044 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003045 return;
3046 }
3047
3048 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003049 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003050 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003051
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003052 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003053 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003054 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003055 } else {
3056 // Integer cases.
3057 Register lhs = InputRegisterAt(instruction, 0);
3058 Operand rhs = InputOperandAt(instruction, 1);
3059 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003060 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003061 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003062}
3063
3064#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3065 M(Equal) \
3066 M(NotEqual) \
3067 M(LessThan) \
3068 M(LessThanOrEqual) \
3069 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003070 M(GreaterThanOrEqual) \
3071 M(Below) \
3072 M(BelowOrEqual) \
3073 M(Above) \
3074 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003075#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003076void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3077void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003078FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003079#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003080#undef FOR_EACH_CONDITION_INSTRUCTION
3081
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003082void InstructionCodeGeneratorARM64::GenerateIntDivForPower2Denom(HDiv* instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003083 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003084 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003085 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
3086
3087 Register out = OutputRegister(instruction);
3088 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003089
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003090 Register final_dividend;
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003091 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003092 // No need to adjust the result for non-negative dividends or the INT32_MIN/INT64_MIN dividends.
3093 // NOTE: The generated code for HDiv correctly works for the INT32_MIN/INT64_MIN dividends:
3094 // imm == 2
3095 // add out, dividend(0x80000000), dividend(0x80000000), lsr #31 => out = 0x80000001
3096 // asr out, out(0x80000001), #1 => out = 0xc0000000
3097 // This is the same as 'asr out, 0x80000000, #1'
3098 //
3099 // imm > 2
3100 // add temp, dividend(0x80000000), imm - 1 => temp = 0b10..01..1, where the number
3101 // of the rightmost 1s is ctz_imm.
3102 // cmp dividend(0x80000000), 0 => N = 1, V = 0 (lt is true)
3103 // csel out, temp(0b10..01..1), dividend(0x80000000), lt => out = 0b10..01..1
3104 // asr out, out(0b10..01..1), #ctz_imm => out = 0b1..10..0, where the number of the
3105 // leftmost 1s is ctz_imm + 1.
3106 // This is the same as 'asr out, dividend(0x80000000), #ctz_imm'.
3107 //
3108 // imm == INT32_MIN
3109 // add tmp, dividend(0x80000000), #0x7fffffff => tmp = -1
3110 // cmp dividend(0x80000000), 0 => N = 1, V = 0 (lt is true)
3111 // csel out, temp(-1), dividend(0x80000000), lt => out = -1
3112 // neg out, out(-1), asr #31 => out = 1
3113 // This is the same as 'neg out, dividend(0x80000000), asr #31'.
3114 final_dividend = dividend;
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003115 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003116 if (abs_imm == 2) {
3117 int bits = DataType::Size(instruction->GetResultType()) * kBitsPerByte;
3118 __ Add(out, dividend, Operand(dividend, LSR, bits - 1));
3119 } else {
3120 UseScratchRegisterScope temps(GetVIXLAssembler());
3121 Register temp = temps.AcquireSameSizeAs(out);
3122 __ Add(temp, dividend, abs_imm - 1);
3123 __ Cmp(dividend, 0);
3124 __ Csel(out, temp, dividend, lt);
3125 }
3126 final_dividend = out;
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003127 }
3128
Zheng Xuc6667102015-05-15 16:08:45 +08003129 int ctz_imm = CTZ(abs_imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003130 if (imm > 0) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003131 __ Asr(out, final_dividend, ctz_imm);
Zheng Xuc6667102015-05-15 16:08:45 +08003132 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003133 __ Neg(out, Operand(final_dividend, ASR, ctz_imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003134 }
3135}
3136
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003137// Return true if the magic number was modified by subtracting 2^32(Int32 div) or 2^64(Int64 div).
3138// So dividend needs to be added.
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003139static inline bool NeedToAddDividend(int64_t magic_number, int64_t divisor) {
3140 return divisor > 0 && magic_number < 0;
3141}
3142
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003143// Return true if the magic number was modified by adding 2^32(Int32 div) or 2^64(Int64 div).
3144// So dividend needs to be subtracted.
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003145static inline bool NeedToSubDividend(int64_t magic_number, int64_t divisor) {
3146 return divisor < 0 && magic_number > 0;
3147}
3148
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003149// Generate code which increments the value in register 'in' by 1 if the value is negative.
3150// It is done with 'add out, in, in, lsr #31 or #63'.
3151// If the value is a result of an operation setting the N flag, CINC MI can be used
3152// instead of ADD. 'use_cond_inc' controls this.
3153void InstructionCodeGeneratorARM64::GenerateIncrementNegativeByOne(
3154 Register out,
3155 Register in,
3156 bool use_cond_inc) {
3157 if (use_cond_inc) {
3158 __ Cinc(out, in, mi);
3159 } else {
3160 __ Add(out, in, Operand(in, LSR, in.GetSizeInBits() - 1));
3161 }
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003162}
3163
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003164// Helper to generate code producing the result of HRem with a constant divisor.
3165void InstructionCodeGeneratorARM64::GenerateResultRemWithAnyConstant(
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003166 Register out,
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003167 Register dividend,
3168 Register quotient,
3169 int64_t divisor,
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003170 UseScratchRegisterScope* temps_scope) {
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003171 Register temp_imm = temps_scope->AcquireSameSizeAs(out);
3172 __ Mov(temp_imm, divisor);
3173 __ Msub(out, quotient, temp_imm, dividend);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003174}
3175
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003176// Helper to generate code for HDiv/HRem instructions when a dividend is non-negative and
3177// a divisor is a positive constant, not power of 2.
3178void InstructionCodeGeneratorARM64::GenerateInt64UnsignedDivRemWithAnyPositiveConstant(
3179 HBinaryOperation* instruction) {
3180 DCHECK(instruction->IsDiv() || instruction->IsRem());
3181 DCHECK(instruction->GetResultType() == DataType::Type::kInt64);
3182
3183 LocationSummary* locations = instruction->GetLocations();
3184 Location second = locations->InAt(1);
3185 DCHECK(second.IsConstant());
3186
3187 Register out = OutputRegister(instruction);
3188 Register dividend = InputRegisterAt(instruction, 0);
3189 int64_t imm = Int64FromConstant(second.GetConstant());
3190 DCHECK_GT(imm, 0);
3191
3192 int64_t magic;
3193 int shift;
3194 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ true, &magic, &shift);
3195
3196 UseScratchRegisterScope temps(GetVIXLAssembler());
3197 Register temp = temps.AcquireSameSizeAs(out);
3198
3199 auto generate_unsigned_div_code = [this, magic, shift](Register out,
3200 Register dividend,
3201 Register temp) {
3202 // temp = get_high(dividend * magic)
3203 __ Mov(temp, magic);
3204 if (magic > 0 && shift == 0) {
3205 __ Smulh(out, dividend, temp);
3206 } else {
3207 __ Smulh(temp, dividend, temp);
3208 if (magic < 0) {
3209 // The negative magic means that the multiplier m is greater than INT64_MAX.
3210 // In such a case shift is never 0. See the proof in
3211 // InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant.
3212 __ Add(temp, temp, dividend);
3213 }
3214 DCHECK_NE(shift, 0);
3215 __ Lsr(out, temp, shift);
3216 }
3217 };
3218
3219 if (instruction->IsDiv()) {
3220 generate_unsigned_div_code(out, dividend, temp);
3221 } else {
3222 generate_unsigned_div_code(temp, dividend, temp);
3223 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3224 }
3225}
3226
3227// Helper to generate code for HDiv/HRem instructions for any dividend and a constant divisor
3228// (not power of 2).
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003229void InstructionCodeGeneratorARM64::GenerateInt64DivRemWithAnyConstant(
3230 HBinaryOperation* instruction) {
Zheng Xuc6667102015-05-15 16:08:45 +08003231 DCHECK(instruction->IsDiv() || instruction->IsRem());
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003232 DCHECK(instruction->GetResultType() == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003233
3234 LocationSummary* locations = instruction->GetLocations();
3235 Location second = locations->InAt(1);
3236 DCHECK(second.IsConstant());
3237
3238 Register out = OutputRegister(instruction);
3239 Register dividend = InputRegisterAt(instruction, 0);
3240 int64_t imm = Int64FromConstant(second.GetConstant());
3241
Zheng Xuc6667102015-05-15 16:08:45 +08003242 int64_t magic;
3243 int shift;
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003244 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ true, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08003245
3246 UseScratchRegisterScope temps(GetVIXLAssembler());
3247 Register temp = temps.AcquireSameSizeAs(out);
3248
3249 // temp = get_high(dividend * magic)
3250 __ Mov(temp, magic);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003251 __ Smulh(temp, dividend, temp);
3252
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003253 // The multiplication result might need some corrections to be finalized.
3254 // The last correction is to increment by 1, if the result is negative.
3255 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3256 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3257 // However if one of the corrections is ADD or SUB, the sign can be detected
3258 // with ADDS/SUBS. They set the N flag if the result is negative.
3259 // This allows to use CINC MI which has latency 1.
3260 bool use_cond_inc = false;
3261
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003262 // Some combinations of magic_number and the divisor require to correct the result.
3263 // Check whether the correction is needed.
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003264 if (NeedToAddDividend(magic, imm)) {
3265 __ Adds(temp, temp, dividend);
3266 use_cond_inc = true;
3267 } else if (NeedToSubDividend(magic, imm)) {
3268 __ Subs(temp, temp, dividend);
3269 use_cond_inc = true;
3270 }
3271
3272 if (shift != 0) {
3273 __ Asr(temp, temp, shift);
3274 }
3275
3276 if (instruction->IsRem()) {
3277 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3278 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3279 } else {
3280 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3281 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003282}
3283
3284void InstructionCodeGeneratorARM64::GenerateInt32DivRemWithAnyConstant(
3285 HBinaryOperation* instruction) {
3286 DCHECK(instruction->IsDiv() || instruction->IsRem());
3287 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
3288
3289 LocationSummary* locations = instruction->GetLocations();
3290 Location second = locations->InAt(1);
3291 DCHECK(second.IsConstant());
3292
3293 Register out = OutputRegister(instruction);
3294 Register dividend = InputRegisterAt(instruction, 0);
3295 int64_t imm = Int64FromConstant(second.GetConstant());
3296
3297 int64_t magic;
3298 int shift;
3299 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
3300 UseScratchRegisterScope temps(GetVIXLAssembler());
3301 Register temp = temps.AcquireSameSizeAs(out);
3302
3303 // temp = get_high(dividend * magic)
3304 __ Mov(temp, magic);
3305 __ Smull(temp.X(), dividend, temp);
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003306
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003307 // The multiplication result might need some corrections to be finalized.
3308 // The last correction is to increment by 1, if the result is negative.
3309 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3310 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3311 // However if one of the corrections is ADD or SUB, the sign can be detected
3312 // with ADDS/SUBS. They set the N flag if the result is negative.
3313 // This allows to use CINC MI which has latency 1.
3314 bool use_cond_inc = false;
3315
3316 // ADD/SUB correction is performed in the high 32 bits
3317 // as high 32 bits are ignored because type are kInt32.
3318 if (NeedToAddDividend(magic, imm)) {
3319 __ Adds(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3320 use_cond_inc = true;
3321 } else if (NeedToSubDividend(magic, imm)) {
3322 __ Subs(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3323 use_cond_inc = true;
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003324 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003325
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003326 // Extract the result from the high 32 bits and apply the final right shift.
3327 DCHECK_LT(shift, 32);
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003328 if (imm > 0 && HasNonNegativeInputAt(instruction, 0)) {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01003329 // No need to adjust the result for a non-negative dividend and a positive divisor.
3330 if (instruction->IsDiv()) {
3331 __ Lsr(out.X(), temp.X(), 32 + shift);
3332 } else {
3333 __ Lsr(temp.X(), temp.X(), 32 + shift);
3334 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3335 }
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003336 } else {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01003337 __ Asr(temp.X(), temp.X(), 32 + shift);
3338
3339 if (instruction->IsRem()) {
3340 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3341 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3342 } else {
3343 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3344 }
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003345 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003346}
3347
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003348void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction,
3349 int64_t divisor) {
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003350 DCHECK(instruction->IsDiv() || instruction->IsRem());
3351 if (instruction->GetResultType() == DataType::Type::kInt64) {
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003352 if (divisor > 0 && HasNonNegativeInputAt(instruction, 0)) {
3353 GenerateInt64UnsignedDivRemWithAnyPositiveConstant(instruction);
3354 } else {
3355 GenerateInt64DivRemWithAnyConstant(instruction);
3356 }
Zheng Xuc6667102015-05-15 16:08:45 +08003357 } else {
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003358 GenerateInt32DivRemWithAnyConstant(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003359 }
3360}
3361
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003362void InstructionCodeGeneratorARM64::GenerateIntDivForConstDenom(HDiv *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003363 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Zheng Xuc6667102015-05-15 16:08:45 +08003364
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003365 if (imm == 0) {
3366 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3367 return;
3368 }
Zheng Xuc6667102015-05-15 16:08:45 +08003369
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003370 if (IsPowerOfTwo(AbsOrMin(imm))) {
3371 GenerateIntDivForPower2Denom(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003372 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003373 // Cases imm == -1 or imm == 1 are handled by InstructionSimplifier.
3374 DCHECK(imm < -2 || imm > 2) << imm;
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003375 GenerateDivRemWithAnyConstant(instruction, imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003376 }
3377}
3378
3379void InstructionCodeGeneratorARM64::GenerateIntDiv(HDiv *instruction) {
3380 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
3381 << instruction->GetResultType();
3382
3383 if (instruction->GetLocations()->InAt(1).IsConstant()) {
3384 GenerateIntDivForConstDenom(instruction);
3385 } else {
3386 Register out = OutputRegister(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003387 Register dividend = InputRegisterAt(instruction, 0);
3388 Register divisor = InputRegisterAt(instruction, 1);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003389 __ Sdiv(out, dividend, divisor);
Zheng Xuc6667102015-05-15 16:08:45 +08003390 }
3391}
3392
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003393void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3394 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003395 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003396 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003397 case DataType::Type::kInt32:
3398 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003399 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003400 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003401 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3402 break;
3403
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003404 case DataType::Type::kFloat32:
3405 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003406 locations->SetInAt(0, Location::RequiresFpuRegister());
3407 locations->SetInAt(1, Location::RequiresFpuRegister());
3408 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3409 break;
3410
3411 default:
3412 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3413 }
3414}
3415
3416void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003417 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003418 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003419 case DataType::Type::kInt32:
3420 case DataType::Type::kInt64:
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003421 GenerateIntDiv(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003422 break;
3423
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003424 case DataType::Type::kFloat32:
3425 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003426 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3427 break;
3428
3429 default:
3430 LOG(FATAL) << "Unexpected div type " << type;
3431 }
3432}
3433
Alexandre Rames67555f72014-11-18 10:55:16 +00003434void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003435 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003436 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003437}
3438
3439void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3440 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003441 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003442 codegen_->AddSlowPath(slow_path);
3443 Location value = instruction->GetLocations()->InAt(0);
3444
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003445 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003446
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003447 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003448 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Elliott Hughesc1896c92018-11-29 11:33:18 -08003449 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003450 }
3451
Alexandre Rames67555f72014-11-18 10:55:16 +00003452 if (value.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003453 int64_t divisor = Int64FromLocation(value);
Alexandre Rames67555f72014-11-18 10:55:16 +00003454 if (divisor == 0) {
3455 __ B(slow_path->GetEntryLabel());
3456 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003457 // A division by a non-null constant is valid. We don't need to perform
3458 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003459 }
3460 } else {
3461 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3462 }
3463}
3464
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003465void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3466 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003467 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003468 locations->SetOut(Location::ConstantLocation(constant));
3469}
3470
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003471void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3472 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003473 // Will be generated at use site.
3474}
3475
Alexandre Rames5319def2014-10-23 10:03:10 +01003476void LocationsBuilderARM64::VisitExit(HExit* exit) {
3477 exit->SetLocations(nullptr);
3478}
3479
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003480void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003481}
3482
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003483void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3484 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003485 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003486 locations->SetOut(Location::ConstantLocation(constant));
3487}
3488
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003489void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003490 // Will be generated at use site.
3491}
3492
David Brazdilfc6a86a2015-06-26 10:33:45 +00003493void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003494 if (successor->IsExitBlock()) {
3495 DCHECK(got->GetPrevious()->AlwaysThrows());
3496 return; // no code needed
3497 }
3498
Serban Constantinescu02164b32014-11-13 14:05:07 +00003499 HBasicBlock* block = got->GetBlock();
3500 HInstruction* previous = got->GetPrevious();
3501 HLoopInformation* info = block->GetLoopInformation();
3502
David Brazdil46e2a392015-03-16 17:31:52 +00003503 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00003504 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003505 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3506 return;
3507 }
3508 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3509 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08003510 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003511 }
3512 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003513 __ B(codegen_->GetLabelOf(successor));
3514 }
3515}
3516
David Brazdilfc6a86a2015-06-26 10:33:45 +00003517void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3518 got->SetLocations(nullptr);
3519}
3520
3521void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3522 HandleGoto(got, got->GetSuccessor());
3523}
3524
3525void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3526 try_boundary->SetLocations(nullptr);
3527}
3528
3529void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3530 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3531 if (!successor->IsExitBlock()) {
3532 HandleGoto(try_boundary, successor);
3533 }
3534}
3535
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003536void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003537 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003538 vixl::aarch64::Label* true_target,
3539 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003540 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003541
David Brazdil0debae72015-11-12 18:37:00 +00003542 if (true_target == nullptr && false_target == nullptr) {
3543 // Nothing to do. The code always falls through.
3544 return;
3545 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003546 // Constant condition, statically compared against "true" (integer value 1).
3547 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003548 if (true_target != nullptr) {
3549 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003550 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003551 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003552 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003553 if (false_target != nullptr) {
3554 __ B(false_target);
3555 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003556 }
David Brazdil0debae72015-11-12 18:37:00 +00003557 return;
3558 }
3559
3560 // The following code generates these patterns:
3561 // (1) true_target == nullptr && false_target != nullptr
3562 // - opposite condition true => branch to false_target
3563 // (2) true_target != nullptr && false_target == nullptr
3564 // - condition true => branch to true_target
3565 // (3) true_target != nullptr && false_target != nullptr
3566 // - condition true => branch to true_target
3567 // - branch to false_target
3568 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003569 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003570 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003571 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003572 if (true_target == nullptr) {
3573 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3574 } else {
3575 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3576 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003577 } else {
3578 // The condition instruction has not been materialized, use its inputs as
3579 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003580 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003581
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003582 DataType::Type type = condition->InputAt(0)->GetType();
3583 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003584 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003585 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003586 IfCondition opposite_condition = condition->GetOppositeCondition();
3587 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003588 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003589 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003590 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003591 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003592 // Integer cases.
3593 Register lhs = InputRegisterAt(condition, 0);
3594 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003595
3596 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003597 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003598 if (true_target == nullptr) {
3599 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3600 non_fallthrough_target = false_target;
3601 } else {
3602 arm64_cond = ARM64Condition(condition->GetCondition());
3603 non_fallthrough_target = true_target;
3604 }
3605
Aart Bik086d27e2016-01-20 17:02:00 -08003606 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003607 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003608 switch (arm64_cond) {
3609 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003610 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003611 break;
3612 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003613 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003614 break;
3615 case lt:
3616 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003617 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003618 break;
3619 case ge:
3620 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003621 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003622 break;
3623 default:
3624 // Without the `static_cast` the compiler throws an error for
3625 // `-Werror=sign-promo`.
3626 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3627 }
3628 } else {
3629 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003630 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003631 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003632 }
3633 }
David Brazdil0debae72015-11-12 18:37:00 +00003634
3635 // If neither branch falls through (case 3), the conditional branch to `true_target`
3636 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3637 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003638 __ B(false_target);
3639 }
3640}
3641
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003642void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003643 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003644 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003645 locations->SetInAt(0, Location::RequiresRegister());
3646 }
3647}
3648
3649void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003650 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3651 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003652 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3653 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3654 true_target = nullptr;
3655 }
3656 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3657 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3658 false_target = nullptr;
3659 }
Andreas Gampe3db70682018-12-26 15:12:03 -08003660 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003661}
3662
3663void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003664 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003665 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003666 InvokeRuntimeCallingConvention calling_convention;
3667 RegisterSet caller_saves = RegisterSet::Empty();
3668 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3669 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003670 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003671 locations->SetInAt(0, Location::RequiresRegister());
3672 }
3673}
3674
3675void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003676 SlowPathCodeARM64* slow_path =
3677 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003678 GenerateTestAndBranch(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08003679 /* condition_input_index= */ 0,
David Brazdil0debae72015-11-12 18:37:00 +00003680 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08003681 /* false_target= */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003682}
3683
Mingyao Yang063fc772016-08-02 11:02:54 -07003684void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003685 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003686 LocationSummary(flag, LocationSummary::kNoCall);
3687 locations->SetOut(Location::RequiresRegister());
3688}
3689
3690void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3691 __ Ldr(OutputRegister(flag),
3692 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3693}
3694
David Brazdilc0b601b2016-02-08 14:20:45 +00003695static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3696 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003697 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003698}
3699
Alexandre Rames880f1192016-06-13 16:04:50 +01003700static inline Condition GetConditionForSelect(HCondition* condition) {
3701 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003702 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3703 : ARM64Condition(cond);
3704}
3705
David Brazdil74eb1b22015-12-14 11:44:01 +00003706void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003707 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003708 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003709 locations->SetInAt(0, Location::RequiresFpuRegister());
3710 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003711 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003712 } else {
3713 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3714 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3715 bool is_true_value_constant = cst_true_value != nullptr;
3716 bool is_false_value_constant = cst_false_value != nullptr;
3717 // Ask VIXL whether we should synthesize constants in registers.
3718 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3719 Operand true_op = is_true_value_constant ?
3720 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3721 Operand false_op = is_false_value_constant ?
3722 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3723 bool true_value_in_register = false;
3724 bool false_value_in_register = false;
3725 MacroAssembler::GetCselSynthesisInformation(
3726 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3727 true_value_in_register |= !is_true_value_constant;
3728 false_value_in_register |= !is_false_value_constant;
3729
3730 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3731 : Location::ConstantLocation(cst_true_value));
3732 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3733 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003734 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003735 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003736
David Brazdil74eb1b22015-12-14 11:44:01 +00003737 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3738 locations->SetInAt(2, Location::RequiresRegister());
3739 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003740}
3741
3742void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003743 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003744 Condition csel_cond;
3745
3746 if (IsBooleanValueOrMaterializedCondition(cond)) {
3747 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003748 // Use the condition flags set by the previous instruction.
3749 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003750 } else {
3751 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003752 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003753 }
3754 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003755 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003756 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003757 } else {
3758 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003759 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003760 }
3761
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003762 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003763 __ Fcsel(OutputFPRegister(select),
3764 InputFPRegisterAt(select, 1),
3765 InputFPRegisterAt(select, 0),
3766 csel_cond);
3767 } else {
3768 __ Csel(OutputRegister(select),
3769 InputOperandAt(select, 1),
3770 InputOperandAt(select, 0),
3771 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003772 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003773}
3774
David Srbecky0cf44932015-12-09 14:09:59 +00003775void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003776 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00003777}
3778
David Srbeckyd28f4a02016-03-14 17:14:24 +00003779void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3780 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003781}
3782
Vladimir Markodec78172020-06-19 15:31:23 +01003783void CodeGeneratorARM64::IncreaseFrame(size_t adjustment) {
3784 __ Claim(adjustment);
3785 GetAssembler()->cfi().AdjustCFAOffset(adjustment);
3786}
3787
3788void CodeGeneratorARM64::DecreaseFrame(size_t adjustment) {
3789 __ Drop(adjustment);
3790 GetAssembler()->cfi().AdjustCFAOffset(-adjustment);
3791}
3792
David Srbeckyc7098ff2016-02-09 14:30:11 +00003793void CodeGeneratorARM64::GenerateNop() {
3794 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003795}
3796
Alexandre Rames5319def2014-10-23 10:03:10 +01003797void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003798 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003799}
3800
3801void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003802 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003803}
3804
3805void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003806 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003807}
3808
3809void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003810 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003811}
3812
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003813// Temp is used for read barrier.
3814static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3815 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003816 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003817 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3818 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3819 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3820 return 1;
3821 }
3822 return 0;
3823}
3824
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003825// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003826// interface pointer, one for loading the current interface.
3827// The other checks have one temp for loading the object's class.
3828static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3829 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3830 return 3;
3831 }
3832 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003833}
3834
Alexandre Rames67555f72014-11-18 10:55:16 +00003835void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003836 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003837 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003838 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003839 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003840 case TypeCheckKind::kExactCheck:
3841 case TypeCheckKind::kAbstractClassCheck:
3842 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00003843 case TypeCheckKind::kArrayObjectCheck: {
3844 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
3845 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
3846 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003847 break;
Vladimir Marko87584542017-12-12 17:47:52 +00003848 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003849 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003850 case TypeCheckKind::kUnresolvedCheck:
3851 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003852 call_kind = LocationSummary::kCallOnSlowPath;
3853 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00003854 case TypeCheckKind::kBitstringCheck:
3855 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003856 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003857
Vladimir Markoca6fff82017-10-03 14:49:14 +01003858 LocationSummary* locations =
3859 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003860 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003861 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003862 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003863 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003864 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3865 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3866 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3867 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3868 } else {
3869 locations->SetInAt(1, Location::RequiresRegister());
3870 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003871 // The "out" register is used as a temporary, so it overlaps with the inputs.
3872 // Note that TypeCheckSlowPathARM64 uses this register too.
3873 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003874 // Add temps if necessary for read barriers.
3875 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003876}
3877
3878void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003879 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003880 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003881 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003882 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003883 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3884 ? Register()
3885 : InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003886 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003887 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003888 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3889 DCHECK_LE(num_temps, 1u);
3890 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003891 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3892 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3893 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3894 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003895
Scott Wakeling97c72b72016-06-24 16:19:36 +01003896 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003897 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003898
3899 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003900 // Avoid null check if we know `obj` is not null.
3901 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003902 __ Cbz(obj, &zero);
3903 }
3904
Roland Levillain44015862016-01-22 11:47:17 +00003905 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003906 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003907 ReadBarrierOption read_barrier_option =
3908 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003909 // /* HeapReference<Class> */ out = obj->klass_
3910 GenerateReferenceLoadTwoRegisters(instruction,
3911 out_loc,
3912 obj_loc,
3913 class_offset,
3914 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003915 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003916 __ Cmp(out, cls);
3917 __ Cset(out, eq);
3918 if (zero.IsLinked()) {
3919 __ B(&done);
3920 }
3921 break;
3922 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003923
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003924 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003925 ReadBarrierOption read_barrier_option =
3926 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003927 // /* HeapReference<Class> */ out = obj->klass_
3928 GenerateReferenceLoadTwoRegisters(instruction,
3929 out_loc,
3930 obj_loc,
3931 class_offset,
3932 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003933 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003934 // If the class is abstract, we eagerly fetch the super class of the
3935 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003936 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003937 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003938 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003939 GenerateReferenceLoadOneRegister(instruction,
3940 out_loc,
3941 super_offset,
3942 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003943 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003944 // If `out` is null, we use it for the result, and jump to `done`.
3945 __ Cbz(out, &done);
3946 __ Cmp(out, cls);
3947 __ B(ne, &loop);
3948 __ Mov(out, 1);
3949 if (zero.IsLinked()) {
3950 __ B(&done);
3951 }
3952 break;
3953 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003954
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003955 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003956 ReadBarrierOption read_barrier_option =
3957 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003958 // /* HeapReference<Class> */ out = obj->klass_
3959 GenerateReferenceLoadTwoRegisters(instruction,
3960 out_loc,
3961 obj_loc,
3962 class_offset,
3963 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003964 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003965 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003966 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003967 __ Bind(&loop);
3968 __ Cmp(out, cls);
3969 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003970 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003971 GenerateReferenceLoadOneRegister(instruction,
3972 out_loc,
3973 super_offset,
3974 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003975 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003976 __ Cbnz(out, &loop);
3977 // If `out` is null, we use it for the result, and jump to `done`.
3978 __ B(&done);
3979 __ Bind(&success);
3980 __ Mov(out, 1);
3981 if (zero.IsLinked()) {
3982 __ B(&done);
3983 }
3984 break;
3985 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003986
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003987 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003988 ReadBarrierOption read_barrier_option =
3989 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003990 // /* HeapReference<Class> */ out = obj->klass_
3991 GenerateReferenceLoadTwoRegisters(instruction,
3992 out_loc,
3993 obj_loc,
3994 class_offset,
3995 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003996 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003997 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003998 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003999 __ Cmp(out, cls);
4000 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004001 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004002 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004003 GenerateReferenceLoadOneRegister(instruction,
4004 out_loc,
4005 component_offset,
4006 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004007 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004008 // If `out` is null, we use it for the result, and jump to `done`.
4009 __ Cbz(out, &done);
4010 __ Ldrh(out, HeapOperand(out, primitive_offset));
4011 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4012 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004013 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004014 __ Mov(out, 1);
4015 __ B(&done);
4016 break;
4017 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004018
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004019 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004020 // No read barrier since the slow path will retry upon failure.
4021 // /* HeapReference<Class> */ out = obj->klass_
4022 GenerateReferenceLoadTwoRegisters(instruction,
4023 out_loc,
4024 obj_loc,
4025 class_offset,
4026 maybe_temp_loc,
4027 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004028 __ Cmp(out, cls);
4029 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004030 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08004031 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004032 codegen_->AddSlowPath(slow_path);
4033 __ B(ne, slow_path->GetEntryLabel());
4034 __ Mov(out, 1);
4035 if (zero.IsLinked()) {
4036 __ B(&done);
4037 }
4038 break;
4039 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004040
Calin Juravle98893e12015-10-02 21:05:03 +01004041 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004042 case TypeCheckKind::kInterfaceCheck: {
4043 // Note that we indeed only call on slow path, but we always go
4044 // into the slow path for the unresolved and interface check
4045 // cases.
4046 //
4047 // We cannot directly call the InstanceofNonTrivial runtime
4048 // entry point without resorting to a type checking slow path
4049 // here (i.e. by calling InvokeRuntime directly), as it would
4050 // require to assign fixed registers for the inputs of this
4051 // HInstanceOf instruction (following the runtime calling
4052 // convention), which might be cluttered by the potential first
4053 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004054 //
4055 // TODO: Introduce a new runtime entry point taking the object
4056 // to test (instead of its class) as argument, and let it deal
4057 // with the read barrier issues. This will let us refactor this
4058 // case of the `switch` code as it was previously (with a direct
4059 // call to the runtime not using a type checking slow path).
4060 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004061 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004062 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08004063 instruction, /* is_fatal= */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004064 codegen_->AddSlowPath(slow_path);
4065 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004066 if (zero.IsLinked()) {
4067 __ B(&done);
4068 }
4069 break;
4070 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004071
4072 case TypeCheckKind::kBitstringCheck: {
4073 // /* HeapReference<Class> */ temp = obj->klass_
4074 GenerateReferenceLoadTwoRegisters(instruction,
4075 out_loc,
4076 obj_loc,
4077 class_offset,
4078 maybe_temp_loc,
4079 kWithoutReadBarrier);
4080
4081 GenerateBitstringTypeCheckCompare(instruction, out);
4082 __ Cset(out, eq);
4083 if (zero.IsLinked()) {
4084 __ B(&done);
4085 }
4086 break;
4087 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004088 }
4089
4090 if (zero.IsLinked()) {
4091 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004092 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004093 }
4094
4095 if (done.IsLinked()) {
4096 __ Bind(&done);
4097 }
4098
4099 if (slow_path != nullptr) {
4100 __ Bind(slow_path->GetExitLabel());
4101 }
4102}
4103
4104void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004105 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00004106 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004107 LocationSummary* locations =
4108 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004109 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00004110 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
4111 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
4112 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
4113 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
4114 } else {
4115 locations->SetInAt(1, Location::RequiresRegister());
4116 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004117 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4118 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004119}
4120
4121void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004122 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004123 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004124 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004125 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00004126 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
4127 ? Register()
4128 : InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004129 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4130 DCHECK_GE(num_temps, 1u);
4131 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004132 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004133 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4134 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004135 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004136 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4137 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4138 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4139 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4140 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4141 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4142 const uint32_t object_array_data_offset =
4143 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004144
Vladimir Marko87584542017-12-12 17:47:52 +00004145 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004146 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004147 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4148 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004149 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004150
Scott Wakeling97c72b72016-06-24 16:19:36 +01004151 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004152 // Avoid null check if we know obj is not null.
4153 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004154 __ Cbz(obj, &done);
4155 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004156
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004157 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004158 case TypeCheckKind::kExactCheck:
4159 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004160 // /* HeapReference<Class> */ temp = obj->klass_
4161 GenerateReferenceLoadTwoRegisters(instruction,
4162 temp_loc,
4163 obj_loc,
4164 class_offset,
4165 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004166 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004167
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004168 __ Cmp(temp, cls);
4169 // Jump to slow path for throwing the exception or doing a
4170 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004171 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004172 break;
4173 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004174
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004175 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004176 // /* HeapReference<Class> */ temp = obj->klass_
4177 GenerateReferenceLoadTwoRegisters(instruction,
4178 temp_loc,
4179 obj_loc,
4180 class_offset,
4181 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004182 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004183
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004184 // If the class is abstract, we eagerly fetch the super class of the
4185 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004186 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004187 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004188 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004189 GenerateReferenceLoadOneRegister(instruction,
4190 temp_loc,
4191 super_offset,
4192 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004193 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004194
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004195 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4196 // exception.
4197 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4198 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004199 __ Cmp(temp, cls);
4200 __ B(ne, &loop);
4201 break;
4202 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004203
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004204 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004205 // /* HeapReference<Class> */ temp = obj->klass_
4206 GenerateReferenceLoadTwoRegisters(instruction,
4207 temp_loc,
4208 obj_loc,
4209 class_offset,
4210 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004211 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004212
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004213 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004214 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004215 __ Bind(&loop);
4216 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004217 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004218
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004219 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004220 GenerateReferenceLoadOneRegister(instruction,
4221 temp_loc,
4222 super_offset,
4223 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004224 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004225
4226 // If the class reference currently in `temp` is not null, jump
4227 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004228 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004229 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004230 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004231 break;
4232 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004233
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004234 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004235 // /* HeapReference<Class> */ temp = obj->klass_
4236 GenerateReferenceLoadTwoRegisters(instruction,
4237 temp_loc,
4238 obj_loc,
4239 class_offset,
4240 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004241 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004242
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004243 // Do an exact check.
4244 __ Cmp(temp, cls);
4245 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004246
4247 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004248 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004249 GenerateReferenceLoadOneRegister(instruction,
4250 temp_loc,
4251 component_offset,
4252 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004253 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004254
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004255 // If the component type is null, jump to the slow path to throw the exception.
4256 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4257 // Otherwise, the object is indeed an array. Further check that this component type is not a
4258 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004259 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4260 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004261 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004262 break;
4263 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004264
Calin Juravle98893e12015-10-02 21:05:03 +01004265 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004266 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004267 //
4268 // We cannot directly call the CheckCast runtime entry point
4269 // without resorting to a type checking slow path here (i.e. by
4270 // calling InvokeRuntime directly), as it would require to
4271 // assign fixed registers for the inputs of this HInstanceOf
4272 // instruction (following the runtime calling convention), which
4273 // might be cluttered by the potential first read barrier
4274 // emission at the beginning of this method.
4275 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004276 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004277 case TypeCheckKind::kInterfaceCheck: {
4278 // /* HeapReference<Class> */ temp = obj->klass_
4279 GenerateReferenceLoadTwoRegisters(instruction,
4280 temp_loc,
4281 obj_loc,
4282 class_offset,
4283 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004284 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004285
4286 // /* HeapReference<Class> */ temp = temp->iftable_
4287 GenerateReferenceLoadTwoRegisters(instruction,
4288 temp_loc,
4289 temp_loc,
4290 iftable_offset,
4291 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004292 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004293 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004294 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004295 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004296 vixl::aarch64::Label start_loop;
4297 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004298 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004299 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4300 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004301 // Go to next interface.
4302 __ Add(temp, temp, 2 * kHeapReferenceSize);
4303 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004304 // Compare the classes and continue the loop if they do not match.
4305 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4306 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004307 break;
4308 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004309
4310 case TypeCheckKind::kBitstringCheck: {
4311 // /* HeapReference<Class> */ temp = obj->klass_
4312 GenerateReferenceLoadTwoRegisters(instruction,
4313 temp_loc,
4314 obj_loc,
4315 class_offset,
4316 maybe_temp2_loc,
4317 kWithoutReadBarrier);
4318
4319 GenerateBitstringTypeCheckCompare(instruction, temp);
4320 __ B(ne, type_check_slow_path->GetEntryLabel());
4321 break;
4322 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004323 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004324 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004325
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004326 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004327}
4328
Alexandre Rames5319def2014-10-23 10:03:10 +01004329void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004330 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004331 locations->SetOut(Location::ConstantLocation(constant));
4332}
4333
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004334void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004335 // Will be generated at use site.
4336}
4337
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004338void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004339 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004340 locations->SetOut(Location::ConstantLocation(constant));
4341}
4342
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004343void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004344 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004345}
4346
Calin Juravle175dc732015-08-25 15:42:32 +01004347void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4348 // The trampoline uses the same calling convention as dex calling conventions,
4349 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4350 // the method_idx.
4351 HandleInvoke(invoke);
4352}
4353
4354void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4355 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004356 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004357}
4358
Alexandre Rames5319def2014-10-23 10:03:10 +01004359void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004360 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004361 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004362}
4363
Alexandre Rames67555f72014-11-18 10:55:16 +00004364void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4365 HandleInvoke(invoke);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004366 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
4367 // We cannot request ip1 as it's blocked by the register allocator.
4368 invoke->GetLocations()->SetInAt(invoke->GetNumberOfArguments() - 1, Location::Any());
4369 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004370}
4371
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004372void CodeGeneratorARM64::MaybeGenerateInlineCacheCheck(HInstruction* instruction,
4373 Register klass) {
4374 DCHECK_EQ(klass.GetCode(), 0u);
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004375 // We know the destination of an intrinsic, so no need to record inline
4376 // caches.
4377 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00004378 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004379 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004380 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
Nicolas Geoffray095dc462020-08-17 16:40:28 +01004381 ScopedProfilingInfoUse spiu(
4382 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
4383 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00004384 if (info != nullptr) {
4385 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
4386 uint64_t address = reinterpret_cast64<uint64_t>(cache);
4387 vixl::aarch64::Label done;
4388 __ Mov(x8, address);
4389 __ Ldr(x9, MemOperand(x8, InlineCache::ClassesOffset().Int32Value()));
4390 // Fast path for a monomorphic cache.
4391 __ Cmp(klass, x9);
4392 __ B(eq, &done);
4393 InvokeRuntime(kQuickUpdateInlineCache, instruction, instruction->GetDexPc());
4394 __ Bind(&done);
4395 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004396 }
4397}
4398
Alexandre Rames67555f72014-11-18 10:55:16 +00004399void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4400 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004401 LocationSummary* locations = invoke->GetLocations();
4402 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004403 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004404 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004405 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004406
Artem Serov914d7a82017-02-07 14:33:49 +00004407 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004408 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004409 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004410 {
4411 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4412 // /* HeapReference<Class> */ temp = temp->klass_
4413 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4414 codegen_->MaybeRecordImplicitNullCheck(invoke);
4415 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004416 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004417 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004418 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004419 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004420 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004421 }
Artem Serov914d7a82017-02-07 14:33:49 +00004422
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004423 // Instead of simply (possibly) unpoisoning `temp` here, we should
4424 // emit a read barrier for the previous class reference load.
4425 // However this is not required in practice, as this is an
4426 // intermediate/temporary reference and because the current
4427 // concurrent copying collector keeps the from-space memory
4428 // intact/accessible until the end of the marking phase (the
4429 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004430 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004431
4432 // If we're compiling baseline, update the inline cache.
4433 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
4434
4435 // The register ip1 is required to be used for the hidden argument in
4436 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
4437 MacroAssembler* masm = GetVIXLAssembler();
4438 UseScratchRegisterScope scratch_scope(masm);
4439 scratch_scope.Exclude(ip1);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004440 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
4441 Location interface_method = locations->InAt(invoke->GetNumberOfArguments() - 1);
4442 if (interface_method.IsStackSlot()) {
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00004443 __ Ldr(ip1, StackOperandFrom(interface_method));
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004444 } else {
4445 __ Mov(ip1, XRegisterFrom(interface_method));
4446 }
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00004447 // If the load kind is through a runtime call, we will pass the method we
4448 // fetch the IMT, which will either be a no-op if we don't hit the conflict
4449 // stub, or will make us always go through the trampoline when there is a
4450 // conflict.
4451 } else if (invoke->GetHiddenArgumentLoadKind() != MethodLoadKind::kRuntimeCall) {
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004452 codegen_->LoadMethod(
4453 invoke->GetHiddenArgumentLoadKind(), Location::RegisterLocation(ip1.GetCode()), invoke);
4454 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004455
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004456 __ Ldr(temp,
4457 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4458 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004459 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004460 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004461 __ Ldr(temp, MemOperand(temp, method_offset));
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00004462 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRuntimeCall) {
4463 // We pass the method from the IMT in case of a conflict. This will ensure
4464 // we go into the runtime to resolve the actual method.
4465 __ Mov(ip1, temp);
4466 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004467 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004468 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004469
4470 {
4471 // Ensure the pc position is recorded immediately after the `blr` instruction.
4472 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4473
4474 // lr();
4475 __ blr(lr);
4476 DCHECK(!codegen_->IsLeafMethod());
4477 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4478 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004479
Andreas Gampe3db70682018-12-26 15:12:03 -08004480 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004481}
4482
4483void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004484 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004485 if (intrinsic.TryDispatch(invoke)) {
4486 return;
4487 }
4488
Alexandre Rames67555f72014-11-18 10:55:16 +00004489 HandleInvoke(invoke);
4490}
4491
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004492void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004493 // Explicit clinit checks triggered by static invokes must have been pruned by
4494 // art::PrepareForRegisterAllocation.
4495 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004496
Vladimir Markoca6fff82017-10-03 14:49:14 +01004497 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004498 if (intrinsic.TryDispatch(invoke)) {
4499 return;
4500 }
4501
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004502 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
Vladimir Marko86c87522020-05-11 16:55:55 +01004503 CriticalNativeCallingConventionVisitorARM64 calling_convention_visitor(
4504 /*for_register_allocation=*/ true);
4505 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4506 } else {
4507 HandleInvoke(invoke);
4508 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004509}
4510
Andreas Gampe878d58c2015-01-15 23:24:00 -08004511static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4512 if (invoke->GetLocations()->Intrinsified()) {
4513 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4514 intrinsic.Dispatch(invoke);
4515 return true;
4516 }
4517 return false;
4518}
4519
Vladimir Markodc151b22015-10-15 18:02:30 +01004520HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4521 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004522 ArtMethod* method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004523 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004524 return desired_dispatch_info;
4525}
4526
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004527void CodeGeneratorARM64::LoadMethod(MethodLoadKind load_kind, Location temp, HInvoke* invoke) {
4528 switch (load_kind) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004529 case MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01004530 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01004531 // Add ADRP with its PC-relative method patch.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004532 vixl::aarch64::Label* adrp_label =
4533 NewBootImageMethodPatch(invoke->GetResolvedMethodReference());
Vladimir Marko65979462017-05-19 17:25:12 +01004534 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4535 // Add ADD with its PC-relative method patch.
4536 vixl::aarch64::Label* add_label =
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004537 NewBootImageMethodPatch(invoke->GetResolvedMethodReference(), adrp_label);
Vladimir Marko65979462017-05-19 17:25:12 +01004538 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4539 break;
4540 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004541 case MethodLoadKind::kBootImageRelRo: {
Vladimir Markob066d432018-01-03 13:14:37 +00004542 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004543 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00004544 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_offset);
4545 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4546 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
4547 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_offset, adrp_label);
4548 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
4549 EmitLdrOffsetPlaceholder(ldr_label, WRegisterFrom(temp), XRegisterFrom(temp));
4550 break;
4551 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004552 case MethodLoadKind::kBssEntry: {
Vladimir Markob066d432018-01-03 13:14:37 +00004553 // Add ADRP with its PC-relative .bss entry patch.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004554 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(invoke->GetMethodReference());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004555 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Markob066d432018-01-03 13:14:37 +00004556 // Add LDR with its PC-relative .bss entry patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004557 vixl::aarch64::Label* ldr_label =
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004558 NewMethodBssEntryPatch(invoke->GetMethodReference(), adrp_label);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01004559 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004560 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004561 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004562 }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004563 case MethodLoadKind::kJitDirectAddress: {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004564 // Load method address from literal pool.
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004565 __ Ldr(XRegisterFrom(temp),
4566 DeduplicateUint64Literal(reinterpret_cast<uint64_t>(invoke->GetResolvedMethod())));
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004567 break;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004568 }
4569 case MethodLoadKind::kRuntimeCall: {
4570 // Test situation, don't do anything.
4571 break;
4572 }
4573 default: {
4574 LOG(FATAL) << "Load kind should have already been handled " << load_kind;
4575 UNREACHABLE();
4576 }
4577 }
4578}
4579
4580void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4581 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
4582 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
4583 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4584 switch (invoke->GetMethodLoadKind()) {
4585 case MethodLoadKind::kStringInit: {
4586 uint32_t offset =
4587 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4588 // temp = thread->string_init_entrypoint
4589 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
4590 break;
4591 }
4592 case MethodLoadKind::kRecursive: {
4593 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
4594 break;
4595 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004596 case MethodLoadKind::kRuntimeCall: {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004597 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4598 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004599 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004600 case MethodLoadKind::kBootImageLinkTimePcRelative:
4601 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
4602 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
4603 // Do not materialize the method pointer, load directly the entrypoint.
4604 // Add ADRP with its PC-relative JNI entrypoint patch.
4605 vixl::aarch64::Label* adrp_label =
4606 NewBootImageJniEntrypointPatch(invoke->GetResolvedMethodReference());
4607 EmitAdrpPlaceholder(adrp_label, lr);
4608 // Add the LDR with its PC-relative method patch.
4609 vixl::aarch64::Label* add_label =
4610 NewBootImageJniEntrypointPatch(invoke->GetResolvedMethodReference(), adrp_label);
4611 EmitLdrOffsetPlaceholder(add_label, lr, lr);
4612 break;
4613 }
4614 FALLTHROUGH_INTENDED;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004615 default: {
4616 LoadMethod(invoke->GetMethodLoadKind(), temp, invoke);
4617 break;
4618 }
Vladimir Marko58155012015-08-19 12:49:41 +00004619 }
4620
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004621 auto call_lr = [&]() {
4622 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4623 ExactAssemblyScope eas(GetVIXLAssembler(),
4624 kInstructionSize,
4625 CodeBufferCheckScope::kExactSize);
4626 // lr()
4627 __ blr(lr);
4628 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko86c87522020-05-11 16:55:55 +01004629 };
Vladimir Marko58155012015-08-19 12:49:41 +00004630 switch (invoke->GetCodePtrLocation()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004631 case CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004632 {
4633 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4634 ExactAssemblyScope eas(GetVIXLAssembler(),
4635 kInstructionSize,
4636 CodeBufferCheckScope::kExactSize);
4637 __ bl(&frame_entry_label_);
4638 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4639 }
Vladimir Marko58155012015-08-19 12:49:41 +00004640 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004641 case CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01004642 size_t out_frame_size =
4643 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorARM64,
4644 kAapcs64StackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01004645 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004646 if (invoke->GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative) {
4647 call_lr();
4648 } else {
4649 // LR = callee_method->ptr_sized_fields_.data_; // EntryPointFromJni
4650 MemberOffset offset = ArtMethod::EntryPointFromJniOffset(kArm64PointerSize);
4651 __ Ldr(lr, MemOperand(XRegisterFrom(callee_method), offset.Int32Value()));
4652 // lr()
4653 call_lr();
4654 }
Vladimir Marko86c87522020-05-11 16:55:55 +01004655 // Zero-/sign-extend the result when needed due to native and managed ABI mismatch.
4656 switch (invoke->GetType()) {
4657 case DataType::Type::kBool:
4658 __ Ubfx(w0, w0, 0, 8);
4659 break;
4660 case DataType::Type::kInt8:
4661 __ Sbfx(w0, w0, 0, 8);
4662 break;
4663 case DataType::Type::kUint16:
4664 __ Ubfx(w0, w0, 0, 16);
4665 break;
4666 case DataType::Type::kInt16:
4667 __ Sbfx(w0, w0, 0, 16);
4668 break;
4669 case DataType::Type::kInt32:
4670 case DataType::Type::kInt64:
4671 case DataType::Type::kFloat32:
4672 case DataType::Type::kFloat64:
4673 case DataType::Type::kVoid:
4674 break;
4675 default:
4676 DCHECK(false) << invoke->GetType();
4677 break;
4678 }
4679 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01004680 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01004681 }
4682 break;
4683 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004684 case CodePtrLocation::kCallArtMethod: {
4685 // LR = callee_method->ptr_sized_fields_.entry_point_from_quick_compiled_code_;
4686 MemberOffset offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
4687 __ Ldr(lr, MemOperand(XRegisterFrom(callee_method), offset.Int32Value()));
4688 // lr()
4689 call_lr();
Vladimir Marko58155012015-08-19 12:49:41 +00004690 break;
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004691 }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004692 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004693
Andreas Gampe878d58c2015-01-15 23:24:00 -08004694 DCHECK(!IsLeafMethod());
4695}
4696
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004697void CodeGeneratorARM64::GenerateVirtualCall(
4698 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004699 // Use the calling convention instead of the location of the receiver, as
4700 // intrinsics may have put the receiver in a different register. In the intrinsics
4701 // slow path, the arguments have been moved to the right place, so here we are
4702 // guaranteed that the receiver is the first register of the calling convention.
4703 InvokeDexCallingConvention calling_convention;
4704 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004705 Register temp = XRegisterFrom(temp_in);
4706 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4707 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4708 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004709 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004710
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004711 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004712
4713 {
4714 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4715 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4716 // /* HeapReference<Class> */ temp = receiver->klass_
4717 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4718 MaybeRecordImplicitNullCheck(invoke);
4719 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004720 // Instead of simply (possibly) unpoisoning `temp` here, we should
4721 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004722 // intermediate/temporary reference and because the current
4723 // concurrent copying collector keeps the from-space memory
4724 // intact/accessible until the end of the marking phase (the
4725 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004726 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004727
4728 // If we're compiling baseline, update the inline cache.
4729 MaybeGenerateInlineCacheCheck(invoke, temp);
4730
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004731 // temp = temp->GetMethodAt(method_offset);
4732 __ Ldr(temp, MemOperand(temp, method_offset));
4733 // lr = temp->GetEntryPoint();
4734 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004735 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004736 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004737 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4738 // lr();
4739 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004740 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004741 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004742}
4743
Vladimir Marko9922f002020-06-08 15:05:15 +01004744void CodeGeneratorARM64::MoveFromReturnRegister(Location trg, DataType::Type type) {
4745 if (!trg.IsValid()) {
4746 DCHECK(type == DataType::Type::kVoid);
4747 return;
4748 }
4749
4750 DCHECK_NE(type, DataType::Type::kVoid);
4751
4752 if (DataType::IsIntegralType(type) || type == DataType::Type::kReference) {
4753 Register trg_reg = RegisterFrom(trg, type);
4754 Register res_reg = RegisterFrom(ARM64ReturnLocation(type), type);
4755 __ Mov(trg_reg, res_reg, kDiscardForSameWReg);
4756 } else {
4757 VRegister trg_reg = FPRegisterFrom(trg, type);
4758 VRegister res_reg = FPRegisterFrom(ARM64ReturnLocation(type), type);
4759 __ Fmov(trg_reg, res_reg);
4760 }
4761}
4762
Orion Hodsonac141392017-01-13 11:53:47 +00004763void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00004764 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
4765 if (intrinsic.TryDispatch(invoke)) {
4766 return;
4767 }
Orion Hodsonac141392017-01-13 11:53:47 +00004768 HandleInvoke(invoke);
4769}
4770
4771void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00004772 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4773 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
4774 return;
4775 }
Orion Hodsonac141392017-01-13 11:53:47 +00004776 codegen_->GenerateInvokePolymorphicCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004777 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004778}
4779
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004780void LocationsBuilderARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4781 HandleInvoke(invoke);
4782}
4783
4784void InstructionCodeGeneratorARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4785 codegen_->GenerateInvokeCustomCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004786 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004787}
4788
Vladimir Marko6fd16062018-06-26 11:02:04 +01004789vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageIntrinsicPatch(
4790 uint32_t intrinsic_data,
4791 vixl::aarch64::Label* adrp_label) {
4792 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004793 /* dex_file= */ nullptr, intrinsic_data, adrp_label, &boot_image_other_patches_);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004794}
4795
Vladimir Markob066d432018-01-03 13:14:37 +00004796vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageRelRoPatch(
4797 uint32_t boot_image_offset,
4798 vixl::aarch64::Label* adrp_label) {
4799 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004800 /* dex_file= */ nullptr, boot_image_offset, adrp_label, &boot_image_other_patches_);
Vladimir Markob066d432018-01-03 13:14:37 +00004801}
4802
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004803vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004804 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004805 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004806 return NewPcRelativePatch(
4807 target_method.dex_file, target_method.index, adrp_label, &boot_image_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004808}
4809
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004810vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4811 MethodReference target_method,
4812 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004813 return NewPcRelativePatch(
4814 target_method.dex_file, target_method.index, adrp_label, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004815}
4816
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004817vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageTypePatch(
Scott Wakeling97c72b72016-06-24 16:19:36 +01004818 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004819 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004820 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004821 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &boot_image_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004822}
4823
Vladimir Marko1998cd02017-01-13 13:02:58 +00004824vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
Vladimir Marko8f63f102020-09-28 12:10:28 +01004825 HLoadClass* load_class,
Vladimir Marko1998cd02017-01-13 13:02:58 +00004826 vixl::aarch64::Label* adrp_label) {
Vladimir Marko8f63f102020-09-28 12:10:28 +01004827 const DexFile& dex_file = load_class->GetDexFile();
4828 dex::TypeIndex type_index = load_class->GetTypeIndex();
4829 ArenaDeque<PcRelativePatchInfo>* patches = nullptr;
4830 switch (load_class->GetLoadKind()) {
4831 case HLoadClass::LoadKind::kBssEntry:
4832 patches = &type_bss_entry_patches_;
4833 break;
4834 case HLoadClass::LoadKind::kBssEntryPublic:
4835 patches = &public_type_bss_entry_patches_;
4836 break;
4837 case HLoadClass::LoadKind::kBssEntryPackage:
4838 patches = &package_type_bss_entry_patches_;
4839 break;
4840 default:
4841 LOG(FATAL) << "Unexpected load kind: " << load_class->GetLoadKind();
4842 UNREACHABLE();
4843 }
4844 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004845}
4846
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004847vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004848 const DexFile& dex_file,
4849 dex::StringIndex string_index,
4850 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004851 return NewPcRelativePatch(
4852 &dex_file, string_index.index_, adrp_label, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01004853}
4854
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004855vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4856 const DexFile& dex_file,
4857 dex::StringIndex string_index,
4858 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004859 return NewPcRelativePatch(&dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004860}
4861
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004862vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageJniEntrypointPatch(
4863 MethodReference target_method,
4864 vixl::aarch64::Label* adrp_label) {
4865 return NewPcRelativePatch(
4866 target_method.dex_file, target_method.index, adrp_label, &boot_image_jni_entrypoint_patches_);
4867}
4868
Vladimir Markof6675082019-05-17 12:05:28 +01004869void CodeGeneratorARM64::EmitEntrypointThunkCall(ThreadOffset64 entrypoint_offset) {
4870 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01004871 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markof6675082019-05-17 12:05:28 +01004872 call_entrypoint_patches_.emplace_back(/*dex_file*/ nullptr, entrypoint_offset.Uint32Value());
4873 vixl::aarch64::Label* bl_label = &call_entrypoint_patches_.back().label;
4874 __ bind(bl_label);
4875 __ bl(static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4876}
4877
Vladimir Marko966b46f2018-08-03 10:20:19 +00004878void CodeGeneratorARM64::EmitBakerReadBarrierCbnz(uint32_t custom_data) {
Vladimir Marko94796f82018-08-08 15:15:33 +01004879 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01004880 if (GetCompilerOptions().IsJitCompiler()) {
Vladimir Marko966b46f2018-08-03 10:20:19 +00004881 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
4882 vixl::aarch64::Label* slow_path_entry = &it->second.label;
4883 __ cbnz(mr, slow_path_entry);
4884 } else {
4885 baker_read_barrier_patches_.emplace_back(custom_data);
4886 vixl::aarch64::Label* cbnz_label = &baker_read_barrier_patches_.back().label;
4887 __ bind(cbnz_label);
4888 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4889 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004890}
4891
Scott Wakeling97c72b72016-06-24 16:19:36 +01004892vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004893 const DexFile* dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004894 uint32_t offset_or_index,
4895 vixl::aarch64::Label* adrp_label,
4896 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004897 // Add a patch entry and return the label.
4898 patches->emplace_back(dex_file, offset_or_index);
4899 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004900 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004901 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4902 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4903 return label;
4904}
4905
Scott Wakeling97c72b72016-06-24 16:19:36 +01004906vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4907 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004908 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004909}
4910
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004911vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004912 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004913 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004914 return jit_string_patches_.GetOrCreate(
4915 StringReference(&dex_file, string_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004916 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004917}
4918
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004919vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004920 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004921 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004922 return jit_class_patches_.GetOrCreate(
4923 TypeReference(&dex_file, type_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004924 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004925}
4926
Vladimir Markoaad75c62016-10-03 08:46:48 +00004927void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4928 vixl::aarch64::Register reg) {
4929 DCHECK(reg.IsX());
4930 SingleEmissionCheckScope guard(GetVIXLAssembler());
4931 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004932 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004933}
4934
4935void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4936 vixl::aarch64::Register out,
4937 vixl::aarch64::Register base) {
4938 DCHECK(out.IsX());
4939 DCHECK(base.IsX());
4940 SingleEmissionCheckScope guard(GetVIXLAssembler());
4941 __ Bind(fixup_label);
4942 __ add(out, base, Operand(/* offset placeholder */ 0));
4943}
4944
4945void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4946 vixl::aarch64::Register out,
4947 vixl::aarch64::Register base) {
4948 DCHECK(base.IsX());
4949 SingleEmissionCheckScope guard(GetVIXLAssembler());
4950 __ Bind(fixup_label);
4951 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4952}
4953
Vladimir Markoeebb8212018-06-05 14:57:24 +01004954void CodeGeneratorARM64::LoadBootImageAddress(vixl::aarch64::Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01004955 uint32_t boot_image_reference) {
4956 if (GetCompilerOptions().IsBootImage()) {
4957 // Add ADRP with its PC-relative type patch.
4958 vixl::aarch64::Label* adrp_label = NewBootImageIntrinsicPatch(boot_image_reference);
4959 EmitAdrpPlaceholder(adrp_label, reg.X());
4960 // Add ADD with its PC-relative type patch.
4961 vixl::aarch64::Label* add_label = NewBootImageIntrinsicPatch(boot_image_reference, adrp_label);
4962 EmitAddPlaceholder(add_label, reg.X(), reg.X());
Vladimir Markoa2da9b92018-10-10 14:21:55 +01004963 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01004964 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004965 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004966 EmitAdrpPlaceholder(adrp_label, reg.X());
4967 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004968 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_reference, adrp_label);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004969 EmitLdrOffsetPlaceholder(ldr_label, reg.W(), reg.X());
4970 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01004971 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01004972 gc::Heap* heap = Runtime::Current()->GetHeap();
4973 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004974 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01004975 __ Ldr(reg.W(), DeduplicateBootImageAddressLiteral(reinterpret_cast<uintptr_t>(address)));
4976 }
4977}
4978
Vladimir Marko98873af2020-12-16 12:10:03 +00004979void CodeGeneratorARM64::LoadTypeForBootImageIntrinsic(vixl::aarch64::Register reg,
4980 TypeReference target_type) {
4981 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
4982 DCHECK(GetCompilerOptions().IsBootImage());
4983 // Add ADRP with its PC-relative type patch.
4984 vixl::aarch64::Label* adrp_label =
4985 NewBootImageTypePatch(*target_type.dex_file, target_type.TypeIndex());
4986 EmitAdrpPlaceholder(adrp_label, reg.X());
4987 // Add ADD with its PC-relative type patch.
4988 vixl::aarch64::Label* add_label =
4989 NewBootImageTypePatch(*target_type.dex_file, target_type.TypeIndex(), adrp_label);
4990 EmitAddPlaceholder(add_label, reg.X(), reg.X());
4991}
4992
Vladimir Markode91ca92020-10-27 13:41:40 +00004993void CodeGeneratorARM64::LoadIntrinsicDeclaringClass(vixl::aarch64::Register reg, HInvoke* invoke) {
4994 DCHECK_NE(invoke->GetIntrinsic(), Intrinsics::kNone);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004995 if (GetCompilerOptions().IsBootImage()) {
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004996 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01004997 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
Vladimir Marko98873af2020-12-16 12:10:03 +00004998 LoadTypeForBootImageIntrinsic(reg, TypeReference(target_method.dex_file, type_idx));
Vladimir Marko6fd16062018-06-26 11:02:04 +01004999 } else {
Vladimir Markode91ca92020-10-27 13:41:40 +00005000 uint32_t boot_image_offset = GetBootImageOffsetOfIntrinsicDeclaringClass(invoke);
5001 LoadBootImageAddress(reg, boot_image_offset);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005002 }
Vladimir Marko6fd16062018-06-26 11:02:04 +01005003}
5004
Vladimir Marko98873af2020-12-16 12:10:03 +00005005void CodeGeneratorARM64::LoadClassRootForIntrinsic(vixl::aarch64::Register reg,
5006 ClassRoot class_root) {
5007 if (GetCompilerOptions().IsBootImage()) {
5008 ScopedObjectAccess soa(Thread::Current());
5009 ObjPtr<mirror::Class> klass = GetClassRoot(class_root);
5010 TypeReference target_type(&klass->GetDexFile(), klass->GetDexTypeIndex());
5011 LoadTypeForBootImageIntrinsic(reg, target_type);
5012 } else {
5013 uint32_t boot_image_offset = GetBootImageOffset(class_root);
5014 LoadBootImageAddress(reg, boot_image_offset);
5015 }
5016}
5017
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005018template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005019inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
5020 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005021 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005022 for (const PcRelativePatchInfo& info : infos) {
5023 linker_patches->push_back(Factory(info.label.GetLocation(),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005024 info.target_dex_file,
Vladimir Markoaad75c62016-10-03 08:46:48 +00005025 info.pc_insn_label->GetLocation(),
5026 info.offset_or_index));
5027 }
5028}
5029
Vladimir Marko6fd16062018-06-26 11:02:04 +01005030template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5031linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5032 const DexFile* target_dex_file,
5033 uint32_t pc_insn_offset,
5034 uint32_t boot_image_offset) {
5035 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5036 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005037}
5038
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005039void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005040 DCHECK(linker_patches->empty());
5041 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005042 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005043 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005044 boot_image_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005045 type_bss_entry_patches_.size() +
Vladimir Marko8f63f102020-09-28 12:10:28 +01005046 public_type_bss_entry_patches_.size() +
5047 package_type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005048 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005049 string_bss_entry_patches_.size() +
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005050 boot_image_jni_entrypoint_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005051 boot_image_other_patches_.size() +
Vladimir Markof6675082019-05-17 12:05:28 +01005052 call_entrypoint_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005053 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00005054 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005055 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005056 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005057 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005058 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005059 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005060 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005061 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01005062 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005063 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005064 DCHECK(boot_image_type_patches_.empty());
5065 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005066 }
5067 if (GetCompilerOptions().IsBootImage()) {
5068 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5069 boot_image_other_patches_, linker_patches);
5070 } else {
5071 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5072 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005073 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005074 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5075 method_bss_entry_patches_, linker_patches);
5076 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5077 type_bss_entry_patches_, linker_patches);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005078 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PublicTypeBssEntryPatch>(
5079 public_type_bss_entry_patches_, linker_patches);
5080 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PackageTypeBssEntryPatch>(
5081 package_type_bss_entry_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005082 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5083 string_bss_entry_patches_, linker_patches);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005084 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeJniEntrypointPatch>(
5085 boot_image_jni_entrypoint_patches_, linker_patches);
Vladimir Markof6675082019-05-17 12:05:28 +01005086 for (const PatchInfo<vixl::aarch64::Label>& info : call_entrypoint_patches_) {
5087 DCHECK(info.target_dex_file == nullptr);
5088 linker_patches->push_back(linker::LinkerPatch::CallEntrypointPatch(
5089 info.label.GetLocation(), info.offset_or_index));
5090 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005091 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005092 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
5093 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005094 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00005095 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005096}
5097
Vladimir Markoca1e0382018-04-11 09:58:41 +00005098bool CodeGeneratorARM64::NeedsThunkCode(const linker::LinkerPatch& patch) const {
Vladimir Markof6675082019-05-17 12:05:28 +01005099 return patch.GetType() == linker::LinkerPatch::Type::kCallEntrypoint ||
5100 patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
Vladimir Markoca1e0382018-04-11 09:58:41 +00005101 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
5102}
5103
5104void CodeGeneratorARM64::EmitThunkCode(const linker::LinkerPatch& patch,
5105 /*out*/ ArenaVector<uint8_t>* code,
5106 /*out*/ std::string* debug_name) {
5107 Arm64Assembler assembler(GetGraph()->GetAllocator());
5108 switch (patch.GetType()) {
5109 case linker::LinkerPatch::Type::kCallRelative: {
5110 // The thunk just uses the entry point in the ArtMethod. This works even for calls
5111 // to the generic JNI and interpreter trampolines.
5112 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
5113 kArm64PointerSize).Int32Value());
5114 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
5115 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
5116 *debug_name = "MethodCallThunk";
5117 }
5118 break;
5119 }
Vladimir Markof6675082019-05-17 12:05:28 +01005120 case linker::LinkerPatch::Type::kCallEntrypoint: {
5121 Offset offset(patch.EntrypointOffset());
5122 assembler.JumpTo(ManagedRegister(arm64::TR), offset, ManagedRegister(arm64::IP0));
5123 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
5124 *debug_name = "EntrypointCallThunk_" + std::to_string(offset.Uint32Value());
5125 }
5126 break;
5127 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00005128 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
5129 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
5130 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
5131 break;
5132 }
5133 default:
5134 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
5135 UNREACHABLE();
5136 }
5137
5138 // Ensure we emit the literal pool if any.
5139 assembler.FinalizeCode();
5140 code->resize(assembler.CodeSize());
5141 MemoryRegion code_region(code->data(), code->size());
5142 assembler.FinalizeInstructions(code_region);
5143}
5144
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005145vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
5146 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005147 value,
5148 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
5149}
5150
Scott Wakeling97c72b72016-06-24 16:19:36 +01005151vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005152 return uint64_literals_.GetOrCreate(
5153 value,
5154 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00005155}
5156
Andreas Gampe878d58c2015-01-15 23:24:00 -08005157void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005158 // Explicit clinit checks triggered by static invokes must have been pruned by
5159 // art::PrepareForRegisterAllocation.
5160 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01005161
Andreas Gampe878d58c2015-01-15 23:24:00 -08005162 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08005163 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08005164 return;
5165 }
5166
Vladimir Marko86c87522020-05-11 16:55:55 +01005167 LocationSummary* locations = invoke->GetLocations();
5168 codegen_->GenerateStaticOrDirectCall(
5169 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005170
Andreas Gampe3db70682018-12-26 15:12:03 -08005171 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005172}
5173
5174void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08005175 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08005176 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08005177 return;
5178 }
5179
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005180 {
5181 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
5182 // are no pools emitted.
5183 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
5184 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
5185 DCHECK(!codegen_->IsLeafMethod());
5186 }
5187
Andreas Gampe3db70682018-12-26 15:12:03 -08005188 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005189}
5190
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005191HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
5192 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005193 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005194 case HLoadClass::LoadKind::kInvalid:
5195 LOG(FATAL) << "UNREACHABLE";
5196 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005197 case HLoadClass::LoadKind::kReferrersClass:
5198 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005199 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005200 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005201 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8f63f102020-09-28 12:10:28 +01005202 case HLoadClass::LoadKind::kBssEntryPublic:
5203 case HLoadClass::LoadKind::kBssEntryPackage:
Vladimir Marko695348f2020-05-19 14:42:02 +01005204 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005205 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005206 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005207 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01005208 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005209 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005210 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005211 break;
5212 }
5213 return desired_class_load_kind;
5214}
5215
Alexandre Rames67555f72014-11-18 10:55:16 +00005216void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005217 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005218 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005219 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005220 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005221 cls,
5222 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005223 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00005224 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005225 return;
5226 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005227 DCHECK_EQ(cls->NeedsAccessCheck(),
5228 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
5229 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005230
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005231 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5232 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005233 ? LocationSummary::kCallOnSlowPath
5234 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005235 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005236 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005237 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005238 }
5239
Vladimir Marko41559982017-01-06 14:04:23 +00005240 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005241 locations->SetInAt(0, Location::RequiresRegister());
5242 }
5243 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005244 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
5245 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5246 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005247 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005248 } else {
5249 // For non-Baker read barrier we have a temp-clobbering call.
5250 }
5251 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005252}
5253
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005254// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5255// move.
5256void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005257 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005258 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005259 codegen_->GenerateLoadClassRuntimeCall(cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08005260 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01005261 return;
5262 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005263 DCHECK_EQ(cls->NeedsAccessCheck(),
5264 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
5265 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Calin Juravle580b6092015-10-06 17:35:58 +01005266
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005267 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01005268 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00005269
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005270 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5271 ? kWithoutReadBarrier
5272 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005273 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005274 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005275 case HLoadClass::LoadKind::kReferrersClass: {
5276 DCHECK(!cls->CanCallRuntime());
5277 DCHECK(!cls->MustGenerateClinitCheck());
5278 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5279 Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00005280 codegen_->GenerateGcRootFieldLoad(cls,
5281 out_loc,
5282 current_method,
5283 ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005284 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005285 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005286 break;
5287 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005288 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005289 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5290 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005291 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005292 // Add ADRP with its PC-relative type patch.
5293 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08005294 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005295 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005296 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005297 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005298 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005299 codegen_->NewBootImageTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005300 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005301 break;
5302 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005303 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005304 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markode91ca92020-10-27 13:41:40 +00005305 uint32_t boot_image_offset = CodeGenerator::GetBootImageOffset(cls);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005306 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5307 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005308 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005309 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005310 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005311 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005312 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005313 break;
5314 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005315 case HLoadClass::LoadKind::kBssEntry:
5316 case HLoadClass::LoadKind::kBssEntryPublic:
5317 case HLoadClass::LoadKind::kBssEntryPackage: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005318 // Add ADRP with its PC-relative Class .bss entry patch.
Vladimir Markof3c52b42017-11-17 17:32:12 +00005319 vixl::aarch64::Register temp = XRegisterFrom(out_loc);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005320 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(cls);
Vladimir Markof3c52b42017-11-17 17:32:12 +00005321 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005322 // Add LDR with its PC-relative Class .bss entry patch.
Vladimir Marko8f63f102020-09-28 12:10:28 +01005323 vixl::aarch64::Label* ldr_label = codegen_->NewBssEntryTypePatch(cls, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005324 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005325 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005326 codegen_->GenerateGcRootFieldLoad(cls,
5327 out_loc,
5328 temp,
5329 /* offset placeholder */ 0u,
5330 ldr_label,
5331 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005332 generate_null_check = true;
5333 break;
5334 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005335 case HLoadClass::LoadKind::kJitBootImageAddress: {
5336 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5337 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
5338 DCHECK_NE(address, 0u);
5339 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5340 break;
5341 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005342 case HLoadClass::LoadKind::kJitTableAddress: {
5343 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5344 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005345 cls->GetClass()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005346 codegen_->GenerateGcRootFieldLoad(cls,
5347 out_loc,
5348 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005349 /* offset= */ 0,
5350 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005351 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005352 break;
5353 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005354 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005355 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005356 LOG(FATAL) << "UNREACHABLE";
5357 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005358 }
5359
Vladimir Markoea4c1262017-02-06 19:59:33 +00005360 bool do_clinit = cls->MustGenerateClinitCheck();
5361 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005362 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01005363 SlowPathCodeARM64* slow_path =
5364 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005365 codegen_->AddSlowPath(slow_path);
5366 if (generate_null_check) {
5367 __ Cbz(out, slow_path->GetEntryLabel());
5368 }
5369 if (cls->MustGenerateClinitCheck()) {
5370 GenerateClassInitializationCheck(slow_path, out);
5371 } else {
5372 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005373 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005374 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005375 }
5376}
5377
Orion Hodsondbaa5c72018-05-10 08:22:46 +01005378void LocationsBuilderARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5379 InvokeRuntimeCallingConvention calling_convention;
5380 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5381 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
5382}
5383
5384void InstructionCodeGeneratorARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5385 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
5386}
5387
Orion Hodson18259d72018-04-12 11:18:23 +01005388void LocationsBuilderARM64::VisitLoadMethodType(HLoadMethodType* load) {
5389 InvokeRuntimeCallingConvention calling_convention;
5390 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5391 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
5392}
5393
5394void InstructionCodeGeneratorARM64::VisitLoadMethodType(HLoadMethodType* load) {
5395 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
5396}
5397
David Brazdilcb1c0552015-08-04 16:22:25 +01005398static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005399 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005400}
5401
Alexandre Rames67555f72014-11-18 10:55:16 +00005402void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5403 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005404 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00005405 locations->SetOut(Location::RequiresRegister());
5406}
5407
5408void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005409 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5410}
5411
5412void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005413 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005414}
5415
5416void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5417 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005418}
5419
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005420HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5421 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005422 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005423 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005424 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005425 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01005426 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005427 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005428 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005429 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01005430 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005431 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005432 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005433 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005434 }
5435 return desired_string_load_kind;
5436}
5437
Alexandre Rames67555f72014-11-18 10:55:16 +00005438void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005439 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005440 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005441 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005442 InvokeRuntimeCallingConvention calling_convention;
5443 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5444 } else {
5445 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005446 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5447 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005448 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005449 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005450 } else {
5451 // For non-Baker read barrier we have a temp-clobbering call.
5452 }
5453 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005454 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005455}
5456
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005457// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5458// move.
5459void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005460 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005461 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005462
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005463 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005464 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005465 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5466 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005467 // Add ADRP with its PC-relative String patch.
5468 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005469 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005470 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005471 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005472 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005473 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005474 codegen_->NewBootImageStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005475 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005476 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005477 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005478 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005479 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005480 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markode91ca92020-10-27 13:41:40 +00005481 uint32_t boot_image_offset = CodeGenerator::GetBootImageOffset(load);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005482 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005483 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005484 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005485 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005486 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005487 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
5488 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005489 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005490 case HLoadString::LoadKind::kBssEntry: {
5491 // Add ADRP with its PC-relative String .bss entry patch.
5492 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005493 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00005494 Register temp = XRegisterFrom(out_loc);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005495 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005496 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005497 // Add LDR with its PC-relative String .bss entry patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005498 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005499 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005500 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005501 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005502 codegen_->GenerateGcRootFieldLoad(load,
5503 out_loc,
5504 temp,
5505 /* offset placeholder */ 0u,
5506 ldr_label,
5507 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005508 SlowPathCodeARM64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005509 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005510 codegen_->AddSlowPath(slow_path);
5511 __ Cbz(out.X(), slow_path->GetEntryLabel());
5512 __ Bind(slow_path->GetExitLabel());
Andreas Gampe3db70682018-12-26 15:12:03 -08005513 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005514 return;
5515 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005516 case HLoadString::LoadKind::kJitBootImageAddress: {
5517 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
5518 DCHECK_NE(address, 0u);
5519 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5520 return;
5521 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005522 case HLoadString::LoadKind::kJitTableAddress: {
5523 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005524 load->GetStringIndex(),
5525 load->GetString()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005526 codegen_->GenerateGcRootFieldLoad(load,
5527 out_loc,
5528 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005529 /* offset= */ 0,
5530 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005531 kCompilerReadBarrierOption);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005532 return;
5533 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005534 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005535 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005536 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005537
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005538 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005539 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005540 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005541 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005542 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5543 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005544 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005545}
5546
Alexandre Rames5319def2014-10-23 10:03:10 +01005547void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005548 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01005549 locations->SetOut(Location::ConstantLocation(constant));
5550}
5551
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005552void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005553 // Will be generated at use site.
5554}
5555
Alexandre Rames67555f72014-11-18 10:55:16 +00005556void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005557 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5558 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005559 InvokeRuntimeCallingConvention calling_convention;
5560 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5561}
5562
5563void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005564 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005565 instruction,
5566 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005567 if (instruction->IsEnter()) {
5568 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5569 } else {
5570 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5571 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005572 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005573}
5574
Alexandre Rames42d641b2014-10-27 14:00:51 +00005575void LocationsBuilderARM64::VisitMul(HMul* mul) {
5576 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005577 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005578 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005579 case DataType::Type::kInt32:
5580 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005581 locations->SetInAt(0, Location::RequiresRegister());
5582 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005583 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005584 break;
5585
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005586 case DataType::Type::kFloat32:
5587 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005588 locations->SetInAt(0, Location::RequiresFpuRegister());
5589 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005590 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005591 break;
5592
5593 default:
5594 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5595 }
5596}
5597
5598void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5599 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005600 case DataType::Type::kInt32:
5601 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005602 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5603 break;
5604
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005605 case DataType::Type::kFloat32:
5606 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005607 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005608 break;
5609
5610 default:
5611 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5612 }
5613}
5614
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005615void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5616 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005617 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005618 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005619 case DataType::Type::kInt32:
5620 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005621 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005622 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005623 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005624
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005625 case DataType::Type::kFloat32:
5626 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005627 locations->SetInAt(0, Location::RequiresFpuRegister());
5628 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005629 break;
5630
5631 default:
5632 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5633 }
5634}
5635
5636void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5637 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005638 case DataType::Type::kInt32:
5639 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005640 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5641 break;
5642
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005643 case DataType::Type::kFloat32:
5644 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005645 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005646 break;
5647
5648 default:
5649 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5650 }
5651}
5652
5653void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005654 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5655 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005656 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005657 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005658 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5659 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005660}
5661
5662void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005663 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5664 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005665 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005666 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005667 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005668}
5669
Alexandre Rames5319def2014-10-23 10:03:10 +01005670void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005671 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5672 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005673 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07005674 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005675 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005676}
5677
5678void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005679 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5680 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005681 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005682}
5683
5684void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005685 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005686 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005687 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005688}
5689
5690void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005691 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005692 case DataType::Type::kInt32:
5693 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005694 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005695 break;
5696
5697 default:
5698 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5699 }
5700}
5701
David Brazdil66d126e2015-04-03 16:02:44 +01005702void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005703 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005704 locations->SetInAt(0, Location::RequiresRegister());
5705 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5706}
5707
5708void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005709 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005710}
5711
Alexandre Rames5319def2014-10-23 10:03:10 +01005712void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005713 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5714 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005715}
5716
Calin Juravle2ae48182016-03-16 14:05:09 +00005717void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5718 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005719 return;
5720 }
Artem Serov914d7a82017-02-07 14:33:49 +00005721 {
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01005722 // Ensure that between load and RecordPcInfo there are no pools emitted.
Artem Serov914d7a82017-02-07 14:33:49 +00005723 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5724 Location obj = instruction->GetLocations()->InAt(0);
5725 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5726 RecordPcInfo(instruction, instruction->GetDexPc());
5727 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005728}
5729
Calin Juravle2ae48182016-03-16 14:05:09 +00005730void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005731 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005732 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005733
5734 LocationSummary* locations = instruction->GetLocations();
5735 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005736
5737 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005738}
5739
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005740void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005741 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005742}
5743
Alexandre Rames67555f72014-11-18 10:55:16 +00005744void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5745 HandleBinaryOp(instruction);
5746}
5747
5748void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5749 HandleBinaryOp(instruction);
5750}
5751
Alexandre Rames3e69f162014-12-10 10:36:50 +00005752void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5753 LOG(FATAL) << "Unreachable";
5754}
5755
5756void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005757 if (instruction->GetNext()->IsSuspendCheck() &&
5758 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5759 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5760 // The back edge will generate the suspend check.
5761 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5762 }
5763
Alexandre Rames3e69f162014-12-10 10:36:50 +00005764 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5765}
5766
Alexandre Rames5319def2014-10-23 10:03:10 +01005767void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005768 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005769 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5770 if (location.IsStackSlot()) {
5771 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5772 } else if (location.IsDoubleStackSlot()) {
5773 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5774 }
5775 locations->SetOut(location);
5776}
5777
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005778void InstructionCodeGeneratorARM64::VisitParameterValue(
5779 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005780 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005781}
5782
5783void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5784 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005785 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005786 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005787}
5788
5789void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5790 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5791 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005792}
5793
5794void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005795 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005796 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005797 locations->SetInAt(i, Location::Any());
5798 }
5799 locations->SetOut(Location::Any());
5800}
5801
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005802void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005803 LOG(FATAL) << "Unreachable";
5804}
5805
Serban Constantinescu02164b32014-11-13 14:05:07 +00005806void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005807 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005808 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005809 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005810 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005811 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005812
5813 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005814 case DataType::Type::kInt32:
5815 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005816 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005817 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005818 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5819 break;
5820
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005821 case DataType::Type::kFloat32:
5822 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005823 InvokeRuntimeCallingConvention calling_convention;
5824 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5825 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5826 locations->SetOut(calling_convention.GetReturnLocation(type));
5827
5828 break;
5829 }
5830
Serban Constantinescu02164b32014-11-13 14:05:07 +00005831 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005832 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005833 }
5834}
5835
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005836void InstructionCodeGeneratorARM64::GenerateIntRemForPower2Denom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005837 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005838 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
5839 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
5840
5841 Register out = OutputRegister(instruction);
5842 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005843
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01005844 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01005845 // No need to adjust the result for non-negative dividends or the INT32_MIN/INT64_MIN dividends.
5846 // NOTE: The generated code for HRem correctly works for the INT32_MIN/INT64_MIN dividends.
5847 // INT*_MIN % imm must be 0 for any imm of power 2. 'and' works only with bits
5848 // 0..30 (Int32 case)/0..62 (Int64 case) of a dividend. For INT32_MIN/INT64_MIN they are zeros.
5849 // So 'and' always produces zero.
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005850 __ And(out, dividend, abs_imm - 1);
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01005851 } else {
5852 if (abs_imm == 2) {
5853 __ Cmp(dividend, 0);
5854 __ And(out, dividend, 1);
5855 __ Csneg(out, out, out, ge);
5856 } else {
5857 UseScratchRegisterScope temps(GetVIXLAssembler());
5858 Register temp = temps.AcquireSameSizeAs(out);
5859
5860 __ Negs(temp, dividend);
5861 __ And(out, dividend, abs_imm - 1);
5862 __ And(temp, temp, abs_imm - 1);
5863 __ Csneg(out, out, temp, mi);
5864 }
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005865 }
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005866}
5867
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005868void InstructionCodeGeneratorARM64::GenerateIntRemForConstDenom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005869 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005870
5871 if (imm == 0) {
5872 // Do not generate anything.
5873 // DivZeroCheck would prevent any code to be executed.
5874 return;
5875 }
5876
Evgeny Astigeevichf58dc652018-06-25 17:54:07 +01005877 if (IsPowerOfTwo(AbsOrMin(imm))) {
5878 // Cases imm == -1 or imm == 1 are handled in constant folding by
5879 // InstructionWithAbsorbingInputSimplifier.
5880 // If the cases have survided till code generation they are handled in
5881 // GenerateIntRemForPower2Denom becauses -1 and 1 are the power of 2 (2^0).
5882 // The correct code is generated for them, just more instructions.
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005883 GenerateIntRemForPower2Denom(instruction);
5884 } else {
5885 DCHECK(imm < -2 || imm > 2) << imm;
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01005886 GenerateDivRemWithAnyConstant(instruction, imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005887 }
5888}
5889
5890void InstructionCodeGeneratorARM64::GenerateIntRem(HRem* instruction) {
5891 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
5892 << instruction->GetResultType();
5893
5894 if (instruction->GetLocations()->InAt(1).IsConstant()) {
5895 GenerateIntRemForConstDenom(instruction);
5896 } else {
5897 Register out = OutputRegister(instruction);
5898 Register dividend = InputRegisterAt(instruction, 0);
5899 Register divisor = InputRegisterAt(instruction, 1);
5900 UseScratchRegisterScope temps(GetVIXLAssembler());
5901 Register temp = temps.AcquireSameSizeAs(out);
5902 __ Sdiv(temp, dividend, divisor);
5903 __ Msub(out, temp, divisor, dividend);
5904 }
5905}
5906
Serban Constantinescu02164b32014-11-13 14:05:07 +00005907void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005908 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005909
Serban Constantinescu02164b32014-11-13 14:05:07 +00005910 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005911 case DataType::Type::kInt32:
5912 case DataType::Type::kInt64: {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005913 GenerateIntRem(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005914 break;
5915 }
5916
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005917 case DataType::Type::kFloat32:
5918 case DataType::Type::kFloat64: {
5919 QuickEntrypointEnum entrypoint =
5920 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005921 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005922 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00005923 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5924 } else {
5925 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5926 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005927 break;
5928 }
5929
Serban Constantinescu02164b32014-11-13 14:05:07 +00005930 default:
5931 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005932 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005933 }
5934}
5935
Aart Bik1f8d51b2018-02-15 10:42:37 -08005936void LocationsBuilderARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005937 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005938}
5939
Aart Bik1f8d51b2018-02-15 10:42:37 -08005940void InstructionCodeGeneratorARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005941 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005942}
5943
5944void LocationsBuilderARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005945 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005946}
5947
5948void InstructionCodeGeneratorARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005949 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005950}
5951
Aart Bik3dad3412018-02-28 12:01:46 -08005952void LocationsBuilderARM64::VisitAbs(HAbs* abs) {
5953 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
5954 switch (abs->GetResultType()) {
5955 case DataType::Type::kInt32:
5956 case DataType::Type::kInt64:
5957 locations->SetInAt(0, Location::RequiresRegister());
5958 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5959 break;
5960 case DataType::Type::kFloat32:
5961 case DataType::Type::kFloat64:
5962 locations->SetInAt(0, Location::RequiresFpuRegister());
5963 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5964 break;
5965 default:
5966 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5967 }
5968}
5969
5970void InstructionCodeGeneratorARM64::VisitAbs(HAbs* abs) {
5971 switch (abs->GetResultType()) {
5972 case DataType::Type::kInt32:
5973 case DataType::Type::kInt64: {
5974 Register in_reg = InputRegisterAt(abs, 0);
5975 Register out_reg = OutputRegister(abs);
5976 __ Cmp(in_reg, Operand(0));
5977 __ Cneg(out_reg, in_reg, lt);
5978 break;
5979 }
5980 case DataType::Type::kFloat32:
5981 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01005982 VRegister in_reg = InputFPRegisterAt(abs, 0);
5983 VRegister out_reg = OutputFPRegister(abs);
Aart Bik3dad3412018-02-28 12:01:46 -08005984 __ Fabs(out_reg, in_reg);
5985 break;
5986 }
5987 default:
5988 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5989 }
5990}
5991
Igor Murashkind01745e2017-04-05 16:40:31 -07005992void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5993 constructor_fence->SetLocations(nullptr);
5994}
5995
5996void InstructionCodeGeneratorARM64::VisitConstructorFence(
5997 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5998 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5999}
6000
Calin Juravle27df7582015-04-17 19:12:31 +01006001void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6002 memory_barrier->SetLocations(nullptr);
6003}
6004
6005void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006006 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01006007}
6008
Alexandre Rames5319def2014-10-23 10:03:10 +01006009void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006010 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006011 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00006012 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01006013}
6014
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00006015void InstructionCodeGeneratorARM64::VisitReturn(HReturn* ret) {
6016 if (GetGraph()->IsCompilingOsr()) {
6017 // To simplify callers of an OSR method, we put the return value in both
6018 // floating point and core register.
6019 switch (ret->InputAt(0)->GetType()) {
6020 case DataType::Type::kFloat32:
6021 __ Fmov(w0, s0);
6022 break;
6023 case DataType::Type::kFloat64:
6024 __ Fmov(x0, d0);
6025 break;
6026 default:
6027 break;
6028 }
6029 }
Alexandre Rames5319def2014-10-23 10:03:10 +01006030 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01006031}
6032
6033void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
6034 instruction->SetLocations(nullptr);
6035}
6036
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006037void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01006038 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01006039}
6040
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006041void LocationsBuilderARM64::VisitRor(HRor* ror) {
6042 HandleBinaryOp(ror);
6043}
6044
6045void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
6046 HandleBinaryOp(ror);
6047}
6048
Serban Constantinescu02164b32014-11-13 14:05:07 +00006049void LocationsBuilderARM64::VisitShl(HShl* shl) {
6050 HandleShift(shl);
6051}
6052
6053void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
6054 HandleShift(shl);
6055}
6056
6057void LocationsBuilderARM64::VisitShr(HShr* shr) {
6058 HandleShift(shr);
6059}
6060
6061void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
6062 HandleShift(shr);
6063}
6064
Alexandre Rames5319def2014-10-23 10:03:10 +01006065void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006066 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006067}
6068
6069void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006070 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006071}
6072
Alexandre Rames67555f72014-11-18 10:55:16 +00006073void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006074 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00006075}
6076
6077void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01006078 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00006079}
6080
6081void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01006082 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006083}
6084
Alexandre Rames67555f72014-11-18 10:55:16 +00006085void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006086 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01006087}
6088
Vladimir Marko552a1342017-10-31 10:56:47 +00006089void LocationsBuilderARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6090 codegen_->CreateStringBuilderAppendLocations(instruction, LocationFrom(x0));
6091}
6092
6093void InstructionCodeGeneratorARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6094 __ Mov(w0, instruction->GetFormat()->GetValue());
6095 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
6096}
6097
Calin Juravlee460d1d2015-09-29 04:52:17 +01006098void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
6099 HUnresolvedInstanceFieldGet* instruction) {
6100 FieldAccessCallingConventionARM64 calling_convention;
6101 codegen_->CreateUnresolvedFieldLocationSummary(
6102 instruction, instruction->GetFieldType(), calling_convention);
6103}
6104
6105void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
6106 HUnresolvedInstanceFieldGet* instruction) {
6107 FieldAccessCallingConventionARM64 calling_convention;
6108 codegen_->GenerateUnresolvedFieldAccess(instruction,
6109 instruction->GetFieldType(),
6110 instruction->GetFieldIndex(),
6111 instruction->GetDexPc(),
6112 calling_convention);
6113}
6114
6115void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
6116 HUnresolvedInstanceFieldSet* instruction) {
6117 FieldAccessCallingConventionARM64 calling_convention;
6118 codegen_->CreateUnresolvedFieldLocationSummary(
6119 instruction, instruction->GetFieldType(), calling_convention);
6120}
6121
6122void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
6123 HUnresolvedInstanceFieldSet* instruction) {
6124 FieldAccessCallingConventionARM64 calling_convention;
6125 codegen_->GenerateUnresolvedFieldAccess(instruction,
6126 instruction->GetFieldType(),
6127 instruction->GetFieldIndex(),
6128 instruction->GetDexPc(),
6129 calling_convention);
6130}
6131
6132void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
6133 HUnresolvedStaticFieldGet* instruction) {
6134 FieldAccessCallingConventionARM64 calling_convention;
6135 codegen_->CreateUnresolvedFieldLocationSummary(
6136 instruction, instruction->GetFieldType(), calling_convention);
6137}
6138
6139void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
6140 HUnresolvedStaticFieldGet* instruction) {
6141 FieldAccessCallingConventionARM64 calling_convention;
6142 codegen_->GenerateUnresolvedFieldAccess(instruction,
6143 instruction->GetFieldType(),
6144 instruction->GetFieldIndex(),
6145 instruction->GetDexPc(),
6146 calling_convention);
6147}
6148
6149void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
6150 HUnresolvedStaticFieldSet* instruction) {
6151 FieldAccessCallingConventionARM64 calling_convention;
6152 codegen_->CreateUnresolvedFieldLocationSummary(
6153 instruction, instruction->GetFieldType(), calling_convention);
6154}
6155
6156void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
6157 HUnresolvedStaticFieldSet* instruction) {
6158 FieldAccessCallingConventionARM64 calling_convention;
6159 codegen_->GenerateUnresolvedFieldAccess(instruction,
6160 instruction->GetFieldType(),
6161 instruction->GetFieldIndex(),
6162 instruction->GetDexPc(),
6163 calling_convention);
6164}
6165
Alexandre Rames5319def2014-10-23 10:03:10 +01006166void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006167 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6168 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01006169 // In suspend check slow path, usually there are no caller-save registers at all.
6170 // If SIMD instructions are present, however, we force spilling all live SIMD
6171 // registers in full width (since the runtime only saves/restores lower part).
6172 locations->SetCustomSlowPathCallerSaves(
6173 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01006174}
6175
6176void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006177 HBasicBlock* block = instruction->GetBlock();
6178 if (block->GetLoopInformation() != nullptr) {
6179 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6180 // The back edge will generate the suspend check.
6181 return;
6182 }
6183 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6184 // The goto will generate the suspend check.
6185 return;
6186 }
6187 GenerateSuspendCheck(instruction, nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08006188 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01006189}
6190
Alexandre Rames67555f72014-11-18 10:55:16 +00006191void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006192 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6193 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00006194 InvokeRuntimeCallingConvention calling_convention;
6195 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
6196}
6197
6198void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00006199 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08006200 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00006201}
6202
6203void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
6204 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006205 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006206 DataType::Type input_type = conversion->GetInputType();
6207 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006208 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6209 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006210 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
6211 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006212 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6213 }
6214
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006215 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006216 locations->SetInAt(0, Location::RequiresFpuRegister());
6217 } else {
6218 locations->SetInAt(0, Location::RequiresRegister());
6219 }
6220
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006221 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006222 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6223 } else {
6224 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6225 }
6226}
6227
6228void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006229 DataType::Type result_type = conversion->GetResultType();
6230 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00006231
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006232 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6233 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006234
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006235 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
6236 int result_size = DataType::Size(result_type);
6237 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00006238 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006239 Register output = OutputRegister(conversion);
6240 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006241 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01006242 // 'int' values are used directly as W registers, discarding the top
6243 // bits, so we don't need to sign-extend and can just perform a move.
6244 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
6245 // top 32 bits of the target register. We theoretically could leave those
6246 // bits unchanged, but we would have to make sure that no code uses a
6247 // 32bit input value as a 64bit value assuming that the top 32 bits are
6248 // zero.
6249 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006250 } else if (DataType::IsUnsignedType(result_type) ||
6251 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
6252 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006253 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00006254 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006255 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006256 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006257 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006258 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6259 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006260 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006261 } else if (DataType::IsFloatingPointType(result_type) &&
6262 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006263 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
6264 } else {
6265 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6266 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006267 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00006268}
Alexandre Rames67555f72014-11-18 10:55:16 +00006269
Serban Constantinescu02164b32014-11-13 14:05:07 +00006270void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
6271 HandleShift(ushr);
6272}
6273
6274void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
6275 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00006276}
6277
6278void LocationsBuilderARM64::VisitXor(HXor* instruction) {
6279 HandleBinaryOp(instruction);
6280}
6281
6282void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
6283 HandleBinaryOp(instruction);
6284}
6285
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006286void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006287 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006288 LOG(FATAL) << "Unreachable";
6289}
6290
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006291void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006292 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006293 LOG(FATAL) << "Unreachable";
6294}
6295
Mark Mendellfe57faa2015-09-18 09:26:15 -04006296// Simple implementation of packed switch - generate cascaded compare/jumps.
6297void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6298 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006299 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006300 locations->SetInAt(0, Location::RequiresRegister());
6301}
6302
6303void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6304 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08006305 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006306 Register value_reg = InputRegisterAt(switch_instr, 0);
6307 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6308
Zheng Xu3927c8b2015-11-18 17:46:25 +08006309 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01006310 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08006311 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
6312 // make sure we don't emit it if the target may run out of range.
6313 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
6314 // ranges and emit the tables only as required.
6315 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04006316
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006317 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08006318 // Current instruction id is an upper bound of the number of HIRs in the graph.
6319 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
6320 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006321 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6322 Register temp = temps.AcquireW();
6323 __ Subs(temp, value_reg, Operand(lower_bound));
6324
Zheng Xu3927c8b2015-11-18 17:46:25 +08006325 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006326 // Jump to successors[0] if value == lower_bound.
6327 __ B(eq, codegen_->GetLabelOf(successors[0]));
6328 int32_t last_index = 0;
6329 for (; num_entries - last_index > 2; last_index += 2) {
6330 __ Subs(temp, temp, Operand(2));
6331 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6332 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
6333 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6334 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
6335 }
6336 if (num_entries - last_index == 2) {
6337 // The last missing case_value.
6338 __ Cmp(temp, Operand(1));
6339 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08006340 }
6341
6342 // And the default for any other value.
6343 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6344 __ B(codegen_->GetLabelOf(default_block));
6345 }
6346 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01006347 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08006348
6349 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6350
6351 // Below instructions should use at most one blocked register. Since there are two blocked
6352 // registers, we are free to block one.
6353 Register temp_w = temps.AcquireW();
6354 Register index;
6355 // Remove the bias.
6356 if (lower_bound != 0) {
6357 index = temp_w;
6358 __ Sub(index, value_reg, Operand(lower_bound));
6359 } else {
6360 index = value_reg;
6361 }
6362
6363 // Jump to default block if index is out of the range.
6364 __ Cmp(index, Operand(num_entries));
6365 __ B(hs, codegen_->GetLabelOf(default_block));
6366
6367 // In current VIXL implementation, it won't require any blocked registers to encode the
6368 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
6369 // register pressure.
6370 Register table_base = temps.AcquireX();
6371 // Load jump offset from the table.
6372 __ Adr(table_base, jump_table->GetTableStartLabel());
6373 Register jump_offset = temp_w;
6374 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
6375
6376 // Jump to target block by branching to table_base(pc related) + offset.
6377 Register target_address = table_base;
6378 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
6379 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006380 }
6381}
6382
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006383void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
6384 HInstruction* instruction,
6385 Location out,
6386 uint32_t offset,
6387 Location maybe_temp,
6388 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006389 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006390 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006391 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006392 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006393 if (kUseBakerReadBarrier) {
6394 // Load with fast path based Baker's read barrier.
6395 // /* HeapReference<Object> */ out = *(out + offset)
6396 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6397 out,
6398 out_reg,
6399 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006400 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006401 /* needs_null_check= */ false,
6402 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006403 } else {
6404 // Load with slow path based read barrier.
6405 // Save the value of `out` into `maybe_temp` before overwriting it
6406 // in the following move operation, as we will need it for the
6407 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006408 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00006409 __ Mov(temp_reg, out_reg);
6410 // /* HeapReference<Object> */ out = *(out + offset)
6411 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6412 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6413 }
6414 } else {
6415 // Plain load with no read barrier.
6416 // /* HeapReference<Object> */ out = *(out + offset)
6417 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6418 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6419 }
6420}
6421
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006422void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
6423 HInstruction* instruction,
6424 Location out,
6425 Location obj,
6426 uint32_t offset,
6427 Location maybe_temp,
6428 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006429 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006430 Register out_reg = RegisterFrom(out, type);
6431 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006432 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006433 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006434 if (kUseBakerReadBarrier) {
6435 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00006436 // /* HeapReference<Object> */ out = *(obj + offset)
6437 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6438 out,
6439 obj_reg,
6440 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006441 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006442 /* needs_null_check= */ false,
6443 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006444 } else {
6445 // Load with slow path based read barrier.
6446 // /* HeapReference<Object> */ out = *(obj + offset)
6447 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6448 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6449 }
6450 } else {
6451 // Plain load with no read barrier.
6452 // /* HeapReference<Object> */ out = *(obj + offset)
6453 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6454 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6455 }
6456}
6457
Vladimir Markoca1e0382018-04-11 09:58:41 +00006458void CodeGeneratorARM64::GenerateGcRootFieldLoad(
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006459 HInstruction* instruction,
6460 Location root,
6461 Register obj,
6462 uint32_t offset,
6463 vixl::aarch64::Label* fixup_label,
6464 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006465 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006466 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006467 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006468 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006469 if (kUseBakerReadBarrier) {
6470 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00006471 // Baker's read barrier are used.
Roland Levillain44015862016-01-22 11:47:17 +00006472
Vladimir Marko008e09f32018-08-06 15:42:43 +01006473 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6474 // the Marking Register) to decide whether we need to enter
6475 // the slow path to mark the GC root.
6476 //
6477 // We use shared thunks for the slow path; shared within the method
6478 // for JIT, across methods for AOT. That thunk checks the reference
6479 // and jumps to the entrypoint if needed.
6480 //
6481 // lr = &return_address;
6482 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6483 // if (mr) { // Thread::Current()->GetIsGcMarking()
6484 // goto gc_root_thunk<root_reg>(lr)
6485 // }
6486 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00006487
Vladimir Marko008e09f32018-08-06 15:42:43 +01006488 UseScratchRegisterScope temps(GetVIXLAssembler());
6489 DCHECK(temps.IsAvailable(ip0));
6490 DCHECK(temps.IsAvailable(ip1));
6491 temps.Exclude(ip0, ip1);
6492 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
Roland Levillain44015862016-01-22 11:47:17 +00006493
Vladimir Marko008e09f32018-08-06 15:42:43 +01006494 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6495 vixl::aarch64::Label return_address;
6496 __ adr(lr, &return_address);
6497 if (fixup_label != nullptr) {
6498 __ bind(fixup_label);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006499 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006500 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
Vladimir Marko94796f82018-08-08 15:15:33 +01006501 "GC root LDR must be 2 instructions (8B) before the return address label.");
Vladimir Marko008e09f32018-08-06 15:42:43 +01006502 __ ldr(root_reg, MemOperand(obj.X(), offset));
6503 EmitBakerReadBarrierCbnz(custom_data);
6504 __ bind(&return_address);
Roland Levillain44015862016-01-22 11:47:17 +00006505 } else {
6506 // GC root loaded through a slow path for read barriers other
6507 // than Baker's.
6508 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006509 if (fixup_label == nullptr) {
6510 __ Add(root_reg.X(), obj.X(), offset);
6511 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006512 EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006513 }
Roland Levillain44015862016-01-22 11:47:17 +00006514 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00006515 GenerateReadBarrierForRootSlow(instruction, root, root);
Roland Levillain44015862016-01-22 11:47:17 +00006516 }
6517 } else {
6518 // Plain GC root load with no read barrier.
6519 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006520 if (fixup_label == nullptr) {
6521 __ Ldr(root_reg, MemOperand(obj, offset));
6522 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006523 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006524 }
Roland Levillain44015862016-01-22 11:47:17 +00006525 // Note that GC roots are not affected by heap poisoning, thus we
6526 // do not have to unpoison `root_reg` here.
6527 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006528 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00006529}
6530
Vladimir Markoc8178f52020-11-24 10:38:16 +00006531void CodeGeneratorARM64::GenerateIntrinsicCasMoveWithBakerReadBarrier(
6532 vixl::aarch64::Register marked_old_value,
Vladimir Marko94796f82018-08-08 15:15:33 +01006533 vixl::aarch64::Register old_value) {
6534 DCHECK(kEmitCompilerReadBarrier);
6535 DCHECK(kUseBakerReadBarrier);
6536
6537 // Similar to the Baker RB path in GenerateGcRootFieldLoad(), with a MOV instead of LDR.
Vladimir Markoc8178f52020-11-24 10:38:16 +00006538 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(marked_old_value.GetCode());
Vladimir Marko94796f82018-08-08 15:15:33 +01006539
6540 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6541 vixl::aarch64::Label return_address;
6542 __ adr(lr, &return_address);
6543 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6544 "GC root LDR must be 2 instructions (8B) before the return address label.");
Vladimir Markoc8178f52020-11-24 10:38:16 +00006545 __ mov(marked_old_value, old_value);
Vladimir Marko94796f82018-08-08 15:15:33 +01006546 EmitBakerReadBarrierCbnz(custom_data);
6547 __ bind(&return_address);
6548}
6549
Roland Levillain44015862016-01-22 11:47:17 +00006550void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6551 Location ref,
Vladimir Marko248141f2018-08-10 10:40:07 +01006552 vixl::aarch64::Register obj,
6553 const vixl::aarch64::MemOperand& src,
Roland Levillain44015862016-01-22 11:47:17 +00006554 bool needs_null_check,
6555 bool use_load_acquire) {
6556 DCHECK(kEmitCompilerReadBarrier);
6557 DCHECK(kUseBakerReadBarrier);
6558
Vladimir Marko0ecac682018-08-07 10:40:38 +01006559 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6560 // Marking Register) to decide whether we need to enter the slow
6561 // path to mark the reference. Then, in the slow path, check the
6562 // gray bit in the lock word of the reference's holder (`obj`) to
6563 // decide whether to mark `ref` or not.
6564 //
6565 // We use shared thunks for the slow path; shared within the method
6566 // for JIT, across methods for AOT. That thunk checks the holder
6567 // and jumps to the entrypoint if needed. If the holder is not gray,
6568 // it creates a fake dependency and returns to the LDR instruction.
6569 //
6570 // lr = &gray_return_address;
6571 // if (mr) { // Thread::Current()->GetIsGcMarking()
6572 // goto field_thunk<holder_reg, base_reg, use_load_acquire>(lr)
6573 // }
6574 // not_gray_return_address:
6575 // // Original reference load. If the offset is too large to fit
6576 // // into LDR, we use an adjusted base register here.
6577 // HeapReference<mirror::Object> reference = *(obj+offset);
6578 // gray_return_address:
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006579
Vladimir Marko248141f2018-08-10 10:40:07 +01006580 DCHECK(src.GetAddrMode() == vixl::aarch64::Offset);
6581 DCHECK_ALIGNED(src.GetOffset(), sizeof(mirror::HeapReference<mirror::Object>));
6582
6583 UseScratchRegisterScope temps(GetVIXLAssembler());
6584 DCHECK(temps.IsAvailable(ip0));
6585 DCHECK(temps.IsAvailable(ip1));
6586 temps.Exclude(ip0, ip1);
6587 uint32_t custom_data = use_load_acquire
6588 ? EncodeBakerReadBarrierAcquireData(src.GetBaseRegister().GetCode(), obj.GetCode())
6589 : EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode());
6590
6591 {
6592 ExactAssemblyScope guard(GetVIXLAssembler(),
6593 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6594 vixl::aarch64::Label return_address;
6595 __ adr(lr, &return_address);
6596 EmitBakerReadBarrierCbnz(custom_data);
6597 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6598 "Field LDR must be 1 instruction (4B) before the return address label; "
6599 " 2 instructions (8B) for heap poisoning.");
6600 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
6601 if (use_load_acquire) {
6602 DCHECK_EQ(src.GetOffset(), 0);
6603 __ ldar(ref_reg, src);
6604 } else {
6605 __ ldr(ref_reg, src);
6606 }
6607 if (needs_null_check) {
6608 MaybeRecordImplicitNullCheck(instruction);
6609 }
6610 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6611 // macro instructions disallowed in ExactAssemblyScope.
6612 if (kPoisonHeapReferences) {
6613 __ neg(ref_reg, Operand(ref_reg));
6614 }
6615 __ bind(&return_address);
6616 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006617 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Vladimir Marko248141f2018-08-10 10:40:07 +01006618}
6619
6620void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6621 Location ref,
6622 Register obj,
6623 uint32_t offset,
6624 Location maybe_temp,
6625 bool needs_null_check,
6626 bool use_load_acquire) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006627 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6628 Register base = obj;
6629 if (use_load_acquire) {
6630 DCHECK(maybe_temp.IsRegister());
6631 base = WRegisterFrom(maybe_temp);
6632 __ Add(base, obj, offset);
6633 offset = 0u;
6634 } else if (offset >= kReferenceLoadMinFarOffset) {
6635 DCHECK(maybe_temp.IsRegister());
6636 base = WRegisterFrom(maybe_temp);
6637 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6638 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6639 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006640 }
Vladimir Marko248141f2018-08-10 10:40:07 +01006641 MemOperand src(base.X(), offset);
6642 GenerateFieldLoadWithBakerReadBarrier(
6643 instruction, ref, obj, src, needs_null_check, use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006644}
6645
Artem Serov0806f582018-10-11 20:14:20 +01006646void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HArrayGet* instruction,
6647 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006648 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006649 uint32_t data_offset,
6650 Location index,
Roland Levillain44015862016-01-22 11:47:17 +00006651 bool needs_null_check) {
6652 DCHECK(kEmitCompilerReadBarrier);
6653 DCHECK(kUseBakerReadBarrier);
6654
Vladimir Marko66d691d2017-04-07 17:53:39 +01006655 static_assert(
6656 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6657 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006658 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006659
Vladimir Marko008e09f32018-08-06 15:42:43 +01006660 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6661 // Marking Register) to decide whether we need to enter the slow
6662 // path to mark the reference. Then, in the slow path, check the
6663 // gray bit in the lock word of the reference's holder (`obj`) to
6664 // decide whether to mark `ref` or not.
6665 //
6666 // We use shared thunks for the slow path; shared within the method
6667 // for JIT, across methods for AOT. That thunk checks the holder
6668 // and jumps to the entrypoint if needed. If the holder is not gray,
6669 // it creates a fake dependency and returns to the LDR instruction.
6670 //
6671 // lr = &gray_return_address;
6672 // if (mr) { // Thread::Current()->GetIsGcMarking()
6673 // goto array_thunk<base_reg>(lr)
6674 // }
6675 // not_gray_return_address:
6676 // // Original reference load. If the offset is too large to fit
6677 // // into LDR, we use an adjusted base register here.
6678 // HeapReference<mirror::Object> reference = data[index];
6679 // gray_return_address:
Vladimir Marko66d691d2017-04-07 17:53:39 +01006680
Vladimir Marko008e09f32018-08-06 15:42:43 +01006681 DCHECK(index.IsValid());
6682 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
6683 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006684
Vladimir Marko008e09f32018-08-06 15:42:43 +01006685 UseScratchRegisterScope temps(GetVIXLAssembler());
6686 DCHECK(temps.IsAvailable(ip0));
6687 DCHECK(temps.IsAvailable(ip1));
6688 temps.Exclude(ip0, ip1);
Artem Serov0806f582018-10-11 20:14:20 +01006689
6690 Register temp;
6691 if (instruction->GetArray()->IsIntermediateAddress()) {
6692 // We do not need to compute the intermediate address from the array: the
6693 // input instruction has done it already. See the comment in
6694 // `TryExtractArrayAccessAddress()`.
6695 if (kIsDebugBuild) {
6696 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
6697 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
6698 }
6699 temp = obj;
6700 } else {
6701 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
6702 __ Add(temp.X(), obj.X(), Operand(data_offset));
6703 }
6704
Vladimir Marko008e09f32018-08-06 15:42:43 +01006705 uint32_t custom_data = EncodeBakerReadBarrierArrayData(temp.GetCode());
Vladimir Marko66d691d2017-04-07 17:53:39 +01006706
Vladimir Marko008e09f32018-08-06 15:42:43 +01006707 {
6708 ExactAssemblyScope guard(GetVIXLAssembler(),
6709 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6710 vixl::aarch64::Label return_address;
6711 __ adr(lr, &return_address);
6712 EmitBakerReadBarrierCbnz(custom_data);
6713 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6714 "Array LDR must be 1 instruction (4B) before the return address label; "
6715 " 2 instructions (8B) for heap poisoning.");
6716 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6717 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6718 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6719 // macro instructions disallowed in ExactAssemblyScope.
6720 if (kPoisonHeapReferences) {
6721 __ neg(ref_reg, Operand(ref_reg));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006722 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006723 __ bind(&return_address);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006724 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006725 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Roland Levillain44015862016-01-22 11:47:17 +00006726}
6727
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006728void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6729 // The following condition is a compile-time one, so it does not have a run-time cost.
6730 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
6731 // The following condition is a run-time one; it is executed after the
6732 // previous compile-time test, to avoid penalizing non-debug builds.
6733 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6734 UseScratchRegisterScope temps(GetVIXLAssembler());
6735 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6736 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6737 }
6738 }
6739}
6740
Vladimir Marko1bff99f2020-11-02 15:07:33 +00006741SlowPathCodeARM64* CodeGeneratorARM64::AddReadBarrierSlowPath(HInstruction* instruction,
6742 Location out,
6743 Location ref,
6744 Location obj,
6745 uint32_t offset,
6746 Location index) {
6747 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
6748 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6749 AddSlowPath(slow_path);
6750 return slow_path;
6751}
6752
Roland Levillain44015862016-01-22 11:47:17 +00006753void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6754 Location out,
6755 Location ref,
6756 Location obj,
6757 uint32_t offset,
6758 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006759 DCHECK(kEmitCompilerReadBarrier);
6760
Roland Levillain44015862016-01-22 11:47:17 +00006761 // Insert a slow path based read barrier *after* the reference load.
6762 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006763 // If heap poisoning is enabled, the unpoisoning of the loaded
6764 // reference will be carried out by the runtime within the slow
6765 // path.
6766 //
6767 // Note that `ref` currently does not get unpoisoned (when heap
6768 // poisoning is enabled), which is alright as the `ref` argument is
6769 // not used by the artReadBarrierSlow entry point.
6770 //
6771 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko1bff99f2020-11-02 15:07:33 +00006772 SlowPathCodeARM64* slow_path = AddReadBarrierSlowPath(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006773
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006774 __ B(slow_path->GetEntryLabel());
6775 __ Bind(slow_path->GetExitLabel());
6776}
6777
Roland Levillain44015862016-01-22 11:47:17 +00006778void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6779 Location out,
6780 Location ref,
6781 Location obj,
6782 uint32_t offset,
6783 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006784 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006785 // Baker's read barriers shall be handled by the fast path
6786 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6787 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006788 // If heap poisoning is enabled, unpoisoning will be taken care of
6789 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006790 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006791 } else if (kPoisonHeapReferences) {
6792 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6793 }
6794}
6795
Roland Levillain44015862016-01-22 11:47:17 +00006796void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6797 Location out,
6798 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006799 DCHECK(kEmitCompilerReadBarrier);
6800
Roland Levillain44015862016-01-22 11:47:17 +00006801 // Insert a slow path based read barrier *after* the GC root load.
6802 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006803 // Note that GC roots are not affected by heap poisoning, so we do
6804 // not need to do anything special for this here.
6805 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006806 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006807 AddSlowPath(slow_path);
6808
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006809 __ B(slow_path->GetEntryLabel());
6810 __ Bind(slow_path->GetExitLabel());
6811}
6812
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006813void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6814 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006815 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006816 locations->SetInAt(0, Location::RequiresRegister());
6817 locations->SetOut(Location::RequiresRegister());
6818}
6819
6820void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6821 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006822 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006823 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006824 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006825 __ Ldr(XRegisterFrom(locations->Out()),
6826 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006827 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006828 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006829 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006830 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6831 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006832 __ Ldr(XRegisterFrom(locations->Out()),
6833 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006834 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006835}
6836
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006837static void PatchJitRootUse(uint8_t* code,
6838 const uint8_t* roots_data,
6839 vixl::aarch64::Literal<uint32_t>* literal,
6840 uint64_t index_in_table) {
6841 uint32_t literal_offset = literal->GetOffset();
6842 uintptr_t address =
6843 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6844 uint8_t* data = code + literal_offset;
6845 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6846}
6847
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006848void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6849 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006850 const StringReference& string_reference = entry.first;
6851 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006852 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006853 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006854 }
6855 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006856 const TypeReference& type_reference = entry.first;
6857 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006858 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006859 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006860 }
6861}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006862
Artem Serov1a719e42019-07-18 14:24:55 +01006863MemOperand InstructionCodeGeneratorARM64::VecNeonAddress(
6864 HVecMemoryOperation* instruction,
6865 UseScratchRegisterScope* temps_scope,
6866 size_t size,
6867 bool is_string_char_at,
6868 /*out*/ Register* scratch) {
6869 LocationSummary* locations = instruction->GetLocations();
6870 Register base = InputRegisterAt(instruction, 0);
6871
6872 if (instruction->InputAt(1)->IsIntermediateAddressIndex()) {
6873 DCHECK(!is_string_char_at);
6874 return MemOperand(base.X(), InputRegisterAt(instruction, 1).X());
6875 }
6876
6877 Location index = locations->InAt(1);
6878 uint32_t offset = is_string_char_at
6879 ? mirror::String::ValueOffset().Uint32Value()
6880 : mirror::Array::DataOffset(size).Uint32Value();
6881 size_t shift = ComponentSizeShiftWidth(size);
6882
6883 // HIntermediateAddress optimization is only applied for scalar ArrayGet and ArraySet.
6884 DCHECK(!instruction->InputAt(0)->IsIntermediateAddress());
6885
6886 if (index.IsConstant()) {
6887 offset += Int64FromLocation(index) << shift;
6888 return HeapOperand(base, offset);
6889 } else {
6890 *scratch = temps_scope->AcquireSameSizeAs(base);
6891 __ Add(*scratch, base, Operand(WRegisterFrom(index), LSL, shift));
6892 return HeapOperand(*scratch, offset);
6893 }
6894}
6895
Alexandre Rames67555f72014-11-18 10:55:16 +00006896#undef __
6897#undef QUICK_ENTRY_POINT
6898
Vladimir Markoca1e0382018-04-11 09:58:41 +00006899#define __ assembler.GetVIXLAssembler()->
6900
6901static void EmitGrayCheckAndFastPath(arm64::Arm64Assembler& assembler,
6902 vixl::aarch64::Register base_reg,
6903 vixl::aarch64::MemOperand& lock_word,
Vladimir Marko7a695052018-04-12 10:26:50 +01006904 vixl::aarch64::Label* slow_path,
6905 vixl::aarch64::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006906 // Load the lock word containing the rb_state.
6907 __ Ldr(ip0.W(), lock_word);
6908 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01006909 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Vladimir Markoca1e0382018-04-11 09:58:41 +00006910 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6911 __ Tbnz(ip0.W(), LockWord::kReadBarrierStateShift, slow_path);
6912 static_assert(
6913 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET,
6914 "Field and array LDR offsets must be the same to reuse the same code.");
Vladimir Marko7a695052018-04-12 10:26:50 +01006915 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
6916 if (throw_npe != nullptr) {
6917 __ Bind(throw_npe);
6918 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006919 // Adjust the return address back to the LDR (1 instruction; 2 for heap poisoning).
6920 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6921 "Field LDR must be 1 instruction (4B) before the return address label; "
6922 " 2 instructions (8B) for heap poisoning.");
6923 __ Add(lr, lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6924 // Introduce a dependency on the lock_word including rb_state,
6925 // to prevent load-load reordering, and without using
6926 // a memory barrier (which would be more expensive).
6927 __ Add(base_reg, base_reg, Operand(ip0, LSR, 32));
6928 __ Br(lr); // And return back to the function.
6929 // Note: The fake dependency is unnecessary for the slow path.
6930}
6931
6932// Load the read barrier introspection entrypoint in register `entrypoint`.
6933static void LoadReadBarrierMarkIntrospectionEntrypoint(arm64::Arm64Assembler& assembler,
6934 vixl::aarch64::Register entrypoint) {
6935 // entrypoint = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6936 DCHECK_EQ(ip0.GetCode(), 16u);
6937 const int32_t entry_point_offset =
6938 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6939 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
6940}
6941
6942void CodeGeneratorARM64::CompileBakerReadBarrierThunk(Arm64Assembler& assembler,
6943 uint32_t encoded_data,
6944 /*out*/ std::string* debug_name) {
6945 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
6946 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006947 case BakerReadBarrierKind::kField:
6948 case BakerReadBarrierKind::kAcquire: {
Artem Serova07de552020-11-01 22:42:43 +00006949 Register base_reg =
6950 vixl::aarch64::XRegister(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00006951 CheckValidReg(base_reg.GetCode());
Artem Serova07de552020-11-01 22:42:43 +00006952 Register holder_reg =
6953 vixl::aarch64::XRegister(BakerReadBarrierSecondRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00006954 CheckValidReg(holder_reg.GetCode());
6955 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6956 temps.Exclude(ip0, ip1);
Roland Levillain988c3912019-09-25 19:33:35 +01006957 // In the case of a field load (with relaxed semantic), if `base_reg` differs from
6958 // `holder_reg`, the offset was too large and we must have emitted (during the construction
6959 // of the HIR graph, see `art::HInstructionBuilder::BuildInstanceFieldAccess`) and preserved
6960 // (see `art::PrepareForRegisterAllocation::VisitNullCheck`) an explicit null check before
6961 // the load. Otherwise, for implicit null checks, we need to null-check the holder as we do
6962 // not necessarily do that check before going to the thunk.
6963 //
6964 // In the case of a field load with load-acquire semantics (where `base_reg` always differs
6965 // from `holder_reg`), we also need an explicit null check when implicit null checks are
6966 // allowed, as we do not emit one before going to the thunk.
Vladimir Marko7a695052018-04-12 10:26:50 +01006967 vixl::aarch64::Label throw_npe_label;
6968 vixl::aarch64::Label* throw_npe = nullptr;
Roland Levillain988c3912019-09-25 19:33:35 +01006969 if (GetCompilerOptions().GetImplicitNullChecks() &&
6970 (holder_reg.Is(base_reg) || (kind == BakerReadBarrierKind::kAcquire))) {
Vladimir Marko7a695052018-04-12 10:26:50 +01006971 throw_npe = &throw_npe_label;
6972 __ Cbz(holder_reg.W(), throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006973 }
Vladimir Marko7a695052018-04-12 10:26:50 +01006974 // Check if the holder is gray and, if not, add fake dependency to the base register
6975 // and return to the LDR instruction to load the reference. Otherwise, use introspection
6976 // to load the reference and call the entrypoint that performs further checks on the
6977 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006978 vixl::aarch64::Label slow_path;
6979 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
Vladimir Marko7a695052018-04-12 10:26:50 +01006980 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006981 __ Bind(&slow_path);
Vladimir Marko0ecac682018-08-07 10:40:38 +01006982 if (kind == BakerReadBarrierKind::kField) {
6983 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6984 __ Ldr(ip0.W(), ldr_address); // Load the LDR (immediate) unsigned offset.
6985 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6986 __ Ubfx(ip0.W(), ip0.W(), 10, 12); // Extract the offset.
6987 __ Ldr(ip0.W(), MemOperand(base_reg, ip0, LSL, 2)); // Load the reference.
6988 } else {
6989 DCHECK(kind == BakerReadBarrierKind::kAcquire);
6990 DCHECK(!base_reg.Is(holder_reg));
6991 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6992 __ Ldar(ip0.W(), MemOperand(base_reg));
6993 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006994 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
6995 __ Br(ip1); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006996 break;
6997 }
6998 case BakerReadBarrierKind::kArray: {
Artem Serova07de552020-11-01 22:42:43 +00006999 Register base_reg =
7000 vixl::aarch64::XRegister(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00007001 CheckValidReg(base_reg.GetCode());
7002 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7003 BakerReadBarrierSecondRegField::Decode(encoded_data));
7004 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
7005 temps.Exclude(ip0, ip1);
7006 vixl::aarch64::Label slow_path;
7007 int32_t data_offset =
7008 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
7009 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
7010 DCHECK_LT(lock_word.GetOffset(), 0);
7011 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path);
7012 __ Bind(&slow_path);
7013 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
7014 __ Ldr(ip0.W(), ldr_address); // Load the LDR (register) unsigned offset.
7015 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
7016 __ Ubfx(ip0, ip0, 16, 6); // Extract the index register, plus 32 (bit 21 is set).
7017 __ Bfi(ip1, ip0, 3, 6); // Insert ip0 to the entrypoint address to create
7018 // a switch case target based on the index register.
7019 __ Mov(ip0, base_reg); // Move the base register to ip0.
7020 __ Br(ip1); // Jump to the entrypoint's array switch case.
7021 break;
7022 }
7023 case BakerReadBarrierKind::kGcRoot: {
7024 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
7025 // and it does not have a forwarding address), call the correct introspection entrypoint;
7026 // otherwise return the reference (or the extracted forwarding address).
7027 // There is no gray bit check for GC roots.
Artem Serova07de552020-11-01 22:42:43 +00007028 Register root_reg =
7029 vixl::aarch64::WRegister(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00007030 CheckValidReg(root_reg.GetCode());
7031 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7032 BakerReadBarrierSecondRegField::Decode(encoded_data));
7033 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
7034 temps.Exclude(ip0, ip1);
7035 vixl::aarch64::Label return_label, not_marked, forwarding_address;
7036 __ Cbz(root_reg, &return_label);
7037 MemOperand lock_word(root_reg.X(), mirror::Object::MonitorOffset().Int32Value());
7038 __ Ldr(ip0.W(), lock_word);
7039 __ Tbz(ip0.W(), LockWord::kMarkBitStateShift, &not_marked);
7040 __ Bind(&return_label);
7041 __ Br(lr);
7042 __ Bind(&not_marked);
7043 __ Tst(ip0.W(), Operand(ip0.W(), LSL, 1));
7044 __ B(&forwarding_address, mi);
7045 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
7046 // Adjust the art_quick_read_barrier_mark_introspection address in IP1 to
7047 // art_quick_read_barrier_mark_introspection_gc_roots.
7048 __ Add(ip1, ip1, Operand(BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRYPOINT_OFFSET));
7049 __ Mov(ip0.W(), root_reg);
7050 __ Br(ip1);
7051 __ Bind(&forwarding_address);
7052 __ Lsl(root_reg, ip0.W(), LockWord::kForwardingAddressShift);
7053 __ Br(lr);
7054 break;
7055 }
7056 default:
7057 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
7058 UNREACHABLE();
7059 }
7060
Vladimir Marko966b46f2018-08-03 10:20:19 +00007061 // For JIT, the slow path is considered part of the compiled method,
Vladimir Markof91fc122020-05-13 09:21:00 +01007062 // so JIT should pass null as `debug_name`.
Vladimir Marko695348f2020-05-19 14:42:02 +01007063 DCHECK(!GetCompilerOptions().IsJitCompiler() || debug_name == nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00007064 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00007065 std::ostringstream oss;
7066 oss << "BakerReadBarrierThunk";
7067 switch (kind) {
7068 case BakerReadBarrierKind::kField:
7069 oss << "Field_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
7070 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
7071 break;
Vladimir Marko0ecac682018-08-07 10:40:38 +01007072 case BakerReadBarrierKind::kAcquire:
7073 oss << "Acquire_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
7074 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
7075 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +00007076 case BakerReadBarrierKind::kArray:
7077 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
7078 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7079 BakerReadBarrierSecondRegField::Decode(encoded_data));
7080 break;
7081 case BakerReadBarrierKind::kGcRoot:
7082 oss << "GcRoot_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
7083 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7084 BakerReadBarrierSecondRegField::Decode(encoded_data));
7085 break;
7086 }
7087 *debug_name = oss.str();
7088 }
7089}
7090
7091#undef __
7092
Alexandre Rames5319def2014-10-23 10:03:10 +01007093} // namespace arm64
7094} // namespace art