blob: b34b613849142a30708214b24afc88554db3bf89 [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
Alex Light3a73ffb2021-01-25 14:11:05 +000019#include "aarch64/assembler-aarch64.h"
20#include "aarch64/registers-aarch64.h"
Vladimir Markof4f2daa2017-03-20 18:26:59 +000021#include "arch/arm64/asm_support_arm64.h"
Serban Constantinescu579885a2015-02-22 20:51:33 +000022#include "arch/arm64/instruction_set_features_arm64.h"
Vladimir Marko86c87522020-05-11 16:55:55 +010023#include "arch/arm64/jni_frame_arm64.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000024#include "art_method-inl.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070025#include "base/bit_utils.h"
26#include "base/bit_utils_iterator.h"
Vladimir Marko98873af2020-12-16 12:10:03 +000027#include "class_root-inl.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010028#include "class_table.h"
Zheng Xuc6667102015-05-15 16:08:45 +080029#include "code_generator_utils.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010030#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080031#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010032#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010033#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070034#include "heap_poisoning.h"
Nicolas Geoffray4313ccb2020-08-26 17:01:15 +010035#include "interpreter/mterp/nterp.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080036#include "intrinsics.h"
37#include "intrinsics_arm64.h"
Ulya Trafimovichec696e52022-01-26 10:21:32 +000038#include "intrinsics_utils.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010039#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070040#include "lock_word.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010041#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070042#include "mirror/class-inl.h"
Vladimir Marko2d98dc22020-10-01 11:21:37 +000043#include "mirror/var_handle.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000044#include "offsets.h"
Alex Light3a73ffb2021-01-25 14:11:05 +000045#include "optimizing/common_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010046#include "thread.h"
47#include "utils/arm64/assembler_arm64.h"
48#include "utils/assembler.h"
49#include "utils/stack_checks.h"
50
Scott Wakeling97c72b72016-06-24 16:19:36 +010051using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000052using vixl::ExactAssemblyScope;
53using vixl::CodeBufferCheckScope;
54using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010055
56#ifdef __
57#error "ARM64 Codegen VIXL macro-assembler macro already defined."
58#endif
59
Vladimir Marko0a516052019-10-14 13:00:44 +000060namespace art {
Alexandre Rames5319def2014-10-23 10:03:10 +010061
Roland Levillain22ccc3a2015-11-24 13:10:05 +000062template<class MirrorType>
63class GcRoot;
64
Alexandre Rames5319def2014-10-23 10:03:10 +010065namespace arm64 {
66
Alexandre Ramesbe919d92016-08-23 18:33:36 +010067using helpers::ARM64EncodableConstantOrRegister;
68using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069using helpers::CPURegisterFrom;
70using helpers::DRegisterFrom;
71using helpers::FPRegisterFrom;
72using helpers::HeapOperand;
73using helpers::HeapOperandFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010074using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080075using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080076using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010077using helpers::InputRegisterAt;
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +010078using helpers::Int64FromLocation;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010079using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080080using helpers::LocationFrom;
81using helpers::OperandFromMemOperand;
82using helpers::OutputCPURegister;
83using helpers::OutputFPRegister;
84using helpers::OutputRegister;
85using helpers::RegisterFrom;
86using helpers::StackOperandFrom;
87using helpers::VIXLRegCodeFromART;
88using helpers::WRegisterFrom;
89using helpers::XRegisterFrom;
90
Vladimir Markof3e0ee22015-12-17 15:23:13 +000091// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080092// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
93// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000094static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010095
Vladimir Markof4f2daa2017-03-20 18:26:59 +000096// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
97// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
Vladimir Marko008e09f32018-08-06 15:42:43 +010098// For the Baker read barrier implementation using link-time generated thunks we need to split
Vladimir Markof4f2daa2017-03-20 18:26:59 +000099// the offset explicitly.
100constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
101
Alexandre Rames5319def2014-10-23 10:03:10 +0100102inline Condition ARM64Condition(IfCondition cond) {
103 switch (cond) {
104 case kCondEQ: return eq;
105 case kCondNE: return ne;
106 case kCondLT: return lt;
107 case kCondLE: return le;
108 case kCondGT: return gt;
109 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700110 case kCondB: return lo;
111 case kCondBE: return ls;
112 case kCondA: return hi;
113 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100114 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000115 LOG(FATAL) << "Unreachable";
116 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100117}
118
Vladimir Markod6e069b2016-01-18 11:11:01 +0000119inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
120 // The ARM64 condition codes can express all the necessary branches, see the
121 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
122 // There is no dex instruction or HIR that would need the missing conditions
123 // "equal or unordered" or "not equal".
124 switch (cond) {
125 case kCondEQ: return eq;
126 case kCondNE: return ne /* unordered */;
127 case kCondLT: return gt_bias ? cc : lt /* unordered */;
128 case kCondLE: return gt_bias ? ls : le /* unordered */;
129 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
130 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
131 default:
132 LOG(FATAL) << "UNREACHABLE";
133 UNREACHABLE();
134 }
135}
136
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100137Location ARM64ReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000138 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
139 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
140 // but we use the exact registers for clarity.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100141 if (return_type == DataType::Type::kFloat32) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000142 return LocationFrom(s0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100143 } else if (return_type == DataType::Type::kFloat64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000144 return LocationFrom(d0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100145 } else if (return_type == DataType::Type::kInt64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000146 return LocationFrom(x0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100147 } else if (return_type == DataType::Type::kVoid) {
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100148 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000149 } else {
150 return LocationFrom(w0);
151 }
152}
153
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100154Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000155 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100156}
157
Vladimir Marko3232dbb2018-07-25 15:42:46 +0100158static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
159 InvokeRuntimeCallingConvention calling_convention;
160 RegisterSet caller_saves = RegisterSet::Empty();
161 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
162 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
163 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
164 DataType::Type::kReference).GetCode());
165 return caller_saves;
166}
167
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100168// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
169#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700170#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100171
Zheng Xuda403092015-04-24 17:35:39 +0800172void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800173 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Andreas Gampe3db70682018-12-26 15:12:03 -0800174 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100175 for (uint32_t i : LowToHighBits(core_spills)) {
176 // If the register holds an object, update the stack mask.
177 if (locations->RegisterContainsObject(i)) {
178 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800179 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100180 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
181 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
182 saved_core_stack_offsets_[i] = stack_offset;
183 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800184 }
185
Artem Serovc8150b52019-07-31 18:28:00 +0100186 const size_t fp_reg_size = codegen->GetSlowPathFPWidth();
Andreas Gampe3db70682018-12-26 15:12:03 -0800187 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100188 for (uint32_t i : LowToHighBits(fp_spills)) {
189 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
190 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
191 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serov9df37b92019-07-23 16:41:54 +0100192 stack_offset += fp_reg_size;
Zheng Xuda403092015-04-24 17:35:39 +0800193 }
194
Artem Serov55ab7e82020-04-27 21:02:28 +0100195 InstructionCodeGeneratorARM64* visitor =
196 down_cast<CodeGeneratorARM64*>(codegen)->GetInstructionCodeGeneratorArm64();
197 visitor->SaveLiveRegistersHelper(locations, codegen->GetFirstRegisterSlotInSlowPath());
Zheng Xuda403092015-04-24 17:35:39 +0800198}
199
200void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Artem Serov55ab7e82020-04-27 21:02:28 +0100201 InstructionCodeGeneratorARM64* visitor =
202 down_cast<CodeGeneratorARM64*>(codegen)->GetInstructionCodeGeneratorArm64();
203 visitor->RestoreLiveRegistersHelper(locations, codegen->GetFirstRegisterSlotInSlowPath());
Zheng Xuda403092015-04-24 17:35:39 +0800204}
205
Alexandre Rames5319def2014-10-23 10:03:10 +0100206class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
207 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000208 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100209
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100210 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100211 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000212 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100213
Alexandre Rames5319def2014-10-23 10:03:10 +0100214 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000215 if (instruction_->CanThrowIntoCatchBlock()) {
216 // Live registers will be restored in the catch block if caught.
217 SaveLiveRegisters(codegen, instruction_->GetLocations());
218 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000219 // We're moving two locations to locations that could overlap, so we need a parallel
220 // move resolver.
221 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100222 codegen->EmitParallelMoves(locations->InAt(0),
223 LocationFrom(calling_convention.GetRegisterAt(0)),
224 DataType::Type::kInt32,
225 locations->InAt(1),
226 LocationFrom(calling_convention.GetRegisterAt(1)),
227 DataType::Type::kInt32);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000228 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
229 ? kQuickThrowStringBounds
230 : kQuickThrowArrayBounds;
231 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100232 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800233 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100234 }
235
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100236 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100237
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100238 const char* GetDescription() const override { return "BoundsCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100239
Alexandre Rames5319def2014-10-23 10:03:10 +0100240 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100241 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
242};
243
Alexandre Rames67555f72014-11-18 10:55:16 +0000244class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
245 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000246 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000247
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100248 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000249 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
250 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000251 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800252 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000253 }
254
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100255 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100256
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100257 const char* GetDescription() const override { return "DivZeroCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100258
Alexandre Rames67555f72014-11-18 10:55:16 +0000259 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000260 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
261};
262
263class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
264 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100265 LoadClassSlowPathARM64(HLoadClass* cls, HInstruction* at)
266 : SlowPathCodeARM64(at), cls_(cls) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000267 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100268 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Alexandre Rames67555f72014-11-18 10:55:16 +0000269 }
270
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100271 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000272 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000273 Location out = locations->Out();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100274 const uint32_t dex_pc = instruction_->GetDexPc();
275 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
276 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
Alexandre Rames67555f72014-11-18 10:55:16 +0000277
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100278 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames67555f72014-11-18 10:55:16 +0000279 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000280 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000281
Vladimir Markof3c52b42017-11-17 17:32:12 +0000282 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100283 if (must_resolve_type) {
Santiago Aboy Solanesa0232ad2021-11-08 17:00:06 +0000284 DCHECK(IsSameDexFile(cls_->GetDexFile(), arm64_codegen->GetGraph()->GetDexFile()) ||
Santiago Aboy Solanes69a87e32022-03-08 16:43:54 +0000285 arm64_codegen->GetCompilerOptions().WithinOatFile(&cls_->GetDexFile()) ||
286 ContainsElement(Runtime::Current()->GetClassLinker()->GetBootClassPath(),
287 &cls_->GetDexFile()));
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100288 dex::TypeIndex type_index = cls_->GetTypeIndex();
289 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Vladimir Marko8f63f102020-09-28 12:10:28 +0100290 if (cls_->NeedsAccessCheck()) {
291 CheckEntrypointTypes<kQuickResolveTypeAndVerifyAccess, void*, uint32_t>();
292 arm64_codegen->InvokeRuntime(kQuickResolveTypeAndVerifyAccess, instruction_, dex_pc, this);
293 } else {
294 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
295 arm64_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
296 }
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100297 // If we also must_do_clinit, the resolved type is now in the correct register.
298 } else {
299 DCHECK(must_do_clinit);
300 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
301 arm64_codegen->MoveLocation(LocationFrom(calling_convention.GetRegisterAt(0)),
302 source,
303 cls_->GetType());
304 }
305 if (must_do_clinit) {
306 arm64_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
307 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800308 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000309
310 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000311 if (out.IsValid()) {
312 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100313 DataType::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000314 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000315 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000316 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000317 __ B(GetExitLabel());
318 }
319
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100320 const char* GetDescription() const override { return "LoadClassSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100321
Alexandre Rames67555f72014-11-18 10:55:16 +0000322 private:
323 // The class this slow path will load.
324 HLoadClass* const cls_;
325
Alexandre Rames67555f72014-11-18 10:55:16 +0000326 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
327};
328
Vladimir Markoaad75c62016-10-03 08:46:48 +0000329class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
330 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000331 explicit LoadStringSlowPathARM64(HLoadString* instruction)
332 : SlowPathCodeARM64(instruction) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000333
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100334 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000335 LocationSummary* locations = instruction_->GetLocations();
336 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
337 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
338
339 __ Bind(GetEntryLabel());
340 SaveLiveRegisters(codegen, locations);
341
Vladimir Markof3c52b42017-11-17 17:32:12 +0000342 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000343 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
344 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000345 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
346 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100347 DataType::Type type = instruction_->GetType();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000348 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
349
350 RestoreLiveRegisters(codegen, locations);
351
Vladimir Markoaad75c62016-10-03 08:46:48 +0000352 __ B(GetExitLabel());
353 }
354
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100355 const char* GetDescription() const override { return "LoadStringSlowPathARM64"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000356
357 private:
358 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
359};
360
Alexandre Rames5319def2014-10-23 10:03:10 +0100361class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
362 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000363 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100364
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100365 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000366 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100367 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000368 if (instruction_->CanThrowIntoCatchBlock()) {
369 // Live registers will be restored in the catch block if caught.
370 SaveLiveRegisters(codegen, instruction_->GetLocations());
371 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000372 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
373 instruction_,
374 instruction_->GetDexPc(),
375 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800376 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100377 }
378
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100379 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100380
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100381 const char* GetDescription() const override { return "NullCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100382
Alexandre Rames5319def2014-10-23 10:03:10 +0100383 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100384 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
385};
386
387class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
388 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100389 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000390 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100391
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100392 void EmitNativeCode(CodeGenerator* codegen) override {
Artem Serov7957d952017-04-04 15:44:09 +0100393 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000394 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100395 __ Bind(GetEntryLabel());
Artem Serov1a719e42019-07-18 14:24:55 +0100396 SaveLiveRegisters(codegen, locations); // Only saves live vector regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000397 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800398 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov1a719e42019-07-18 14:24:55 +0100399 RestoreLiveRegisters(codegen, locations); // Only restores live vector regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000400 if (successor_ == nullptr) {
401 __ B(GetReturnLabel());
402 } else {
403 __ B(arm64_codegen->GetLabelOf(successor_));
404 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100405 }
406
Scott Wakeling97c72b72016-06-24 16:19:36 +0100407 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100408 DCHECK(successor_ == nullptr);
409 return &return_label_;
410 }
411
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100412 HBasicBlock* GetSuccessor() const {
413 return successor_;
414 }
415
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100416 const char* GetDescription() const override { return "SuspendCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100417
Alexandre Rames5319def2014-10-23 10:03:10 +0100418 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100419 // If not null, the block to branch to after the suspend check.
420 HBasicBlock* const successor_;
421
422 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100423 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100424
425 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
426};
427
Alexandre Rames67555f72014-11-18 10:55:16 +0000428class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
429 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000430 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000431 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000432
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100433 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000434 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800435
Alexandre Rames3e69f162014-12-10 10:36:50 +0000436 DCHECK(instruction_->IsCheckCast()
437 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
438 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100439 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000440
Alexandre Rames67555f72014-11-18 10:55:16 +0000441 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000442
Vladimir Marko87584542017-12-12 17:47:52 +0000443 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000444 SaveLiveRegisters(codegen, locations);
445 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000446
447 // We're moving two locations to locations that could overlap, so we need a parallel
448 // move resolver.
449 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800450 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800451 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100452 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800453 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800454 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100455 DataType::Type::kReference);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000456 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000457 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800458 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100459 DataType::Type ret_type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000460 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
461 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
462 } else {
463 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800464 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
465 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000466 }
467
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000468 if (!is_fatal_) {
469 RestoreLiveRegisters(codegen, locations);
470 __ B(GetExitLabel());
471 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000472 }
473
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100474 const char* GetDescription() const override { return "TypeCheckSlowPathARM64"; }
475 bool IsFatal() const override { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100476
Alexandre Rames67555f72014-11-18 10:55:16 +0000477 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000478 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000479
Alexandre Rames67555f72014-11-18 10:55:16 +0000480 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
481};
482
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700483class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
484 public:
Aart Bik42249c32016-01-07 15:33:50 -0800485 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000486 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700487
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100488 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bik42249c32016-01-07 15:33:50 -0800489 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700490 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100491 LocationSummary* locations = instruction_->GetLocations();
492 SaveLiveRegisters(codegen, locations);
493 InvokeRuntimeCallingConvention calling_convention;
494 __ Mov(calling_convention.GetRegisterAt(0),
495 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000496 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100497 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700498 }
499
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100500 const char* GetDescription() const override { return "DeoptimizationSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100501
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700502 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700503 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
504};
505
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100506class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
507 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000508 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100509
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100510 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100511 LocationSummary* locations = instruction_->GetLocations();
512 __ Bind(GetEntryLabel());
513 SaveLiveRegisters(codegen, locations);
514
515 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100516 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100517 parallel_move.AddMove(
518 locations->InAt(0),
519 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100520 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100521 nullptr);
522 parallel_move.AddMove(
523 locations->InAt(1),
524 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100525 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100526 nullptr);
527 parallel_move.AddMove(
528 locations->InAt(2),
529 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100530 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100531 nullptr);
532 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
533
534 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000535 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100536 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
537 RestoreLiveRegisters(codegen, locations);
538 __ B(GetExitLabel());
539 }
540
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100541 const char* GetDescription() const override { return "ArraySetSlowPathARM64"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100542
543 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100544 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
545};
546
Zheng Xu3927c8b2015-11-18 17:46:25 +0800547void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
548 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000549 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800550
551 // We are about to use the assembler to place literals directly. Make sure we have enough
552 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000553 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
554 num_entries * sizeof(int32_t),
555 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800556
557 __ Bind(&table_start_);
558 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
559 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100560 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800561 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100562 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800563 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
564 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
565 Literal<int32_t> literal(jump_offset);
566 __ place(&literal);
567 }
568}
569
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000570// Slow path generating a read barrier for a heap reference.
571class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
572 public:
573 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
574 Location out,
575 Location ref,
576 Location obj,
577 uint32_t offset,
578 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000579 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000580 out_(out),
581 ref_(ref),
582 obj_(obj),
583 offset_(offset),
584 index_(index) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +0000585 DCHECK(gUseReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000586 // If `obj` is equal to `out` or `ref`, it means the initial object
587 // has been overwritten by (or after) the heap object reference load
588 // to be instrumented, e.g.:
589 //
590 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000591 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000592 //
593 // In that case, we have lost the information about the original
594 // object, and the emitted read barrier cannot work properly.
595 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
596 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
597 }
598
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100599 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000600 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
601 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100602 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000603 DCHECK(locations->CanCall());
604 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100605 DCHECK(instruction_->IsInstanceFieldGet() ||
Alex Light3a73ffb2021-01-25 14:11:05 +0000606 instruction_->IsPredicatedInstanceFieldGet() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100607 instruction_->IsStaticFieldGet() ||
608 instruction_->IsArrayGet() ||
609 instruction_->IsInstanceOf() ||
610 instruction_->IsCheckCast() ||
Vladimir Markoa41ea272020-09-07 15:24:36 +0000611 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000612 << "Unexpected instruction in read barrier for heap reference slow path: "
613 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000614 // The read barrier instrumentation of object ArrayGet
615 // instructions does not support the HIntermediateAddress
616 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000617 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100618 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000619
620 __ Bind(GetEntryLabel());
621
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000622 SaveLiveRegisters(codegen, locations);
623
624 // We may have to change the index's value, but as `index_` is a
625 // constant member (like other "inputs" of this slow path),
626 // introduce a copy of it, `index`.
627 Location index = index_;
628 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100629 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000630 if (instruction_->IsArrayGet()) {
631 // Compute the actual memory offset and store it in `index`.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100632 Register index_reg = RegisterFrom(index_, DataType::Type::kInt32);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000633 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
634 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
635 // We are about to change the value of `index_reg` (see the
636 // calls to vixl::MacroAssembler::Lsl and
637 // vixl::MacroAssembler::Mov below), but it has
638 // not been saved by the previous call to
639 // art::SlowPathCode::SaveLiveRegisters, as it is a
640 // callee-save register --
641 // art::SlowPathCode::SaveLiveRegisters does not consider
642 // callee-save registers, as it has been designed with the
643 // assumption that callee-save registers are supposed to be
644 // handled by the called function. So, as a callee-save
645 // register, `index_reg` _would_ eventually be saved onto
646 // the stack, but it would be too late: we would have
647 // changed its value earlier. Therefore, we manually save
648 // it here into another freely available register,
649 // `free_reg`, chosen of course among the caller-save
650 // registers (as a callee-save `free_reg` register would
651 // exhibit the same problem).
652 //
653 // Note we could have requested a temporary register from
654 // the register allocator instead; but we prefer not to, as
655 // this is a slow path, and we know we can find a
656 // caller-save register that is available.
657 Register free_reg = FindAvailableCallerSaveRegister(codegen);
658 __ Mov(free_reg.W(), index_reg);
659 index_reg = free_reg;
660 index = LocationFrom(index_reg);
661 } else {
662 // The initial register stored in `index_` has already been
663 // saved in the call to art::SlowPathCode::SaveLiveRegisters
664 // (as it is not a callee-save register), so we can freely
665 // use it.
666 }
667 // Shifting the index value contained in `index_reg` by the scale
668 // factor (2) cannot overflow in practice, as the runtime is
669 // unable to allocate object arrays with a size larger than
670 // 2^26 - 1 (that is, 2^28 - 4 bytes).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100671 __ Lsl(index_reg, index_reg, DataType::SizeShift(type));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000672 static_assert(
673 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
674 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
675 __ Add(index_reg, index_reg, Operand(offset_));
676 } else {
Ulya Trafimovichec696e52022-01-26 10:21:32 +0000677 // In the case of the following intrinsics `index_` is not shifted by a scale factor of 2
678 // (as in the case of ArrayGet), as it is actually an offset to an object field within an
679 // object.
Roland Levillain3d312422016-06-23 13:53:42 +0100680 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000681 DCHECK(instruction_->GetLocations()->Intrinsified());
Ulya Trafimovichec696e52022-01-26 10:21:32 +0000682 HInvoke* invoke = instruction_->AsInvoke();
683 DCHECK(IsUnsafeGetObject(invoke) ||
684 IsVarHandleGet(invoke) ||
685 IsUnsafeCASObject(invoke) ||
686 IsVarHandleCASFamily(invoke)) << invoke->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100687 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100688 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000689 }
690 }
691
692 // We're moving two or three locations to locations that could
693 // overlap, so we need a parallel move resolver.
694 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100695 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000696 parallel_move.AddMove(ref_,
697 LocationFrom(calling_convention.GetRegisterAt(0)),
698 type,
699 nullptr);
700 parallel_move.AddMove(obj_,
701 LocationFrom(calling_convention.GetRegisterAt(1)),
702 type,
703 nullptr);
704 if (index.IsValid()) {
705 parallel_move.AddMove(index,
706 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100707 DataType::Type::kInt32,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000708 nullptr);
709 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
710 } else {
711 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
712 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
713 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000714 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000715 instruction_,
716 instruction_->GetDexPc(),
717 this);
718 CheckEntrypointTypes<
719 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
720 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
721
722 RestoreLiveRegisters(codegen, locations);
723
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000724 __ B(GetExitLabel());
725 }
726
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100727 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000728
729 private:
730 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100731 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
732 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000733 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
734 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
735 return Register(VIXLRegCodeFromART(i), kXRegSize);
736 }
737 }
738 // We shall never fail to find a free caller-save register, as
739 // there are more than two core caller-save registers on ARM64
740 // (meaning it is possible to find one which is different from
741 // `ref` and `obj`).
742 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
743 LOG(FATAL) << "Could not find a free register";
744 UNREACHABLE();
745 }
746
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000747 const Location out_;
748 const Location ref_;
749 const Location obj_;
750 const uint32_t offset_;
751 // An additional location containing an index to an array.
752 // Only used for HArrayGet and the UnsafeGetObject &
753 // UnsafeGetObjectVolatile intrinsics.
754 const Location index_;
755
756 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
757};
758
759// Slow path generating a read barrier for a GC root.
760class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
761 public:
762 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000763 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +0000764 DCHECK(gUseReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +0000765 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000766
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100767 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000768 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100769 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000770 DCHECK(locations->CanCall());
771 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Vladimir Markoa41ea272020-09-07 15:24:36 +0000772 DCHECK(instruction_->IsLoadClass() ||
773 instruction_->IsLoadString() ||
774 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000775 << "Unexpected instruction in read barrier for GC root slow path: "
776 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000777
778 __ Bind(GetEntryLabel());
779 SaveLiveRegisters(codegen, locations);
780
781 InvokeRuntimeCallingConvention calling_convention;
782 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
783 // The argument of the ReadBarrierForRootSlow is not a managed
784 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
785 // thus we need a 64-bit move here, and we cannot use
786 //
787 // arm64_codegen->MoveLocation(
788 // LocationFrom(calling_convention.GetRegisterAt(0)),
789 // root_,
790 // type);
791 //
792 // which would emit a 32-bit move, as `type` is a (32-bit wide)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100793 // reference type (`DataType::Type::kReference`).
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000794 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000795 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000796 instruction_,
797 instruction_->GetDexPc(),
798 this);
799 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
800 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
801
802 RestoreLiveRegisters(codegen, locations);
803 __ B(GetExitLabel());
804 }
805
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100806 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000807
808 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000809 const Location out_;
810 const Location root_;
811
812 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
813};
814
Mythri Alle5097f832021-11-02 14:52:30 +0000815class MethodEntryExitHooksSlowPathARM64 : public SlowPathCodeARM64 {
816 public:
817 explicit MethodEntryExitHooksSlowPathARM64(HInstruction* instruction)
818 : SlowPathCodeARM64(instruction) {}
819
820 void EmitNativeCode(CodeGenerator* codegen) override {
821 LocationSummary* locations = instruction_->GetLocations();
822 QuickEntrypointEnum entry_point =
823 (instruction_->IsMethodEntryHook()) ? kQuickMethodEntryHook : kQuickMethodExitHook;
824 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
825 __ Bind(GetEntryLabel());
826 SaveLiveRegisters(codegen, locations);
827 arm64_codegen->InvokeRuntime(entry_point, instruction_, instruction_->GetDexPc(), this);
828 RestoreLiveRegisters(codegen, locations);
829 __ B(GetExitLabel());
830 }
831
832 const char* GetDescription() const override {
833 return "MethodEntryExitHooksSlowPath";
834 }
835
836 private:
837 DISALLOW_COPY_AND_ASSIGN(MethodEntryExitHooksSlowPathARM64);
838};
839
Nicolas Geoffray9e598902021-11-19 14:53:07 +0000840class CompileOptimizedSlowPathARM64 : public SlowPathCodeARM64 {
841 public:
842 CompileOptimizedSlowPathARM64() : SlowPathCodeARM64(/* instruction= */ nullptr) {}
843
844 void EmitNativeCode(CodeGenerator* codegen) override {
845 uint32_t entrypoint_offset =
846 GetThreadOffset<kArm64PointerSize>(kQuickCompileOptimized).Int32Value();
847 __ Bind(GetEntryLabel());
848 __ Ldr(lr, MemOperand(tr, entrypoint_offset));
849 // Note: we don't record the call here (and therefore don't generate a stack
850 // map), as the entrypoint should never be suspended.
851 __ Blr(lr);
852 __ B(GetExitLabel());
853 }
854
855 const char* GetDescription() const override {
856 return "CompileOptimizedSlowPath";
857 }
858
859 private:
860 DISALLOW_COPY_AND_ASSIGN(CompileOptimizedSlowPathARM64);
861};
862
Alexandre Rames5319def2014-10-23 10:03:10 +0100863#undef __
864
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100865Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100866 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100867 if (type == DataType::Type::kVoid) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100868 LOG(FATAL) << "Unreachable type " << type;
869 }
870
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100871 if (DataType::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100872 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
873 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100874 } else if (!DataType::IsFloatingPointType(type) &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000875 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000876 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
877 } else {
878 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100879 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
880 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100881 }
882
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000883 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100884 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100885 return next_location;
886}
887
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100888Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100889 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100890}
891
Vladimir Marko86c87522020-05-11 16:55:55 +0100892Location CriticalNativeCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
893 DCHECK_NE(type, DataType::Type::kReference);
894
895 Location location = Location::NoLocation();
896 if (DataType::IsFloatingPointType(type)) {
897 if (fpr_index_ < kParameterFPRegistersLength) {
898 location = LocationFrom(kParameterFPRegisters[fpr_index_]);
899 ++fpr_index_;
900 }
901 } else {
902 // Native ABI uses the same registers as managed, except that the method register x0
903 // is a normal argument.
904 if (gpr_index_ < 1u + kParameterCoreRegistersLength) {
905 location = LocationFrom(gpr_index_ == 0u ? x0 : kParameterCoreRegisters[gpr_index_ - 1u]);
906 ++gpr_index_;
907 }
908 }
909 if (location.IsInvalid()) {
910 if (DataType::Is64BitType(type)) {
911 location = Location::DoubleStackSlot(stack_offset_);
912 } else {
913 location = Location::StackSlot(stack_offset_);
914 }
915 stack_offset_ += kFramePointerSize;
916
917 if (for_register_allocation_) {
918 location = Location::Any();
919 }
920 }
921 return location;
922}
923
924Location CriticalNativeCallingConventionVisitorARM64::GetReturnLocation(DataType::Type type) const {
925 // We perform conversion to the managed ABI return register after the call if needed.
926 InvokeDexCallingConventionVisitorARM64 dex_calling_convention;
927 return dex_calling_convention.GetReturnLocation(type);
928}
929
930Location CriticalNativeCallingConventionVisitorARM64::GetMethodLocation() const {
931 // Pass the method in the hidden argument x15.
932 return Location::RegisterLocation(x15.GetCode());
933}
934
Serban Constantinescu579885a2015-02-22 20:51:33 +0000935CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100936 const CompilerOptions& compiler_options,
937 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100938 : CodeGenerator(graph,
939 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000940 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000941 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100942 callee_saved_core_registers.GetList(),
943 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100944 compiler_options,
945 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100946 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
947 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov1a719e42019-07-18 14:24:55 +0100948 location_builder_neon_(graph, this),
949 instruction_visitor_neon_(graph, this),
950 location_builder_sve_(graph, this),
951 instruction_visitor_sve_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100952 move_resolver_(graph->GetAllocator(), this),
Artem Serovaa6f4832018-11-21 18:57:54 +0000953 assembler_(graph->GetAllocator(),
954 compiler_options.GetInstructionSetFeatures()->AsArm64InstructionSetFeatures()),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000955 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100956 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000957 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100958 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko8f63f102020-09-28 12:10:28 +0100959 public_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
960 package_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000961 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100962 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeb9eb002020-10-02 13:54:19 +0100963 boot_image_jni_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +0100964 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100965 call_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100966 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100967 uint32_literals_(std::less<uint32_t>(),
968 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
969 uint64_literals_(std::less<uint64_t>(),
970 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000971 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100972 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000973 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +0000974 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
975 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
976 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000977 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000978 AddAllocatedRegister(LocationFrom(lr));
Artem Serov1a719e42019-07-18 14:24:55 +0100979
980 bool use_sve = ShouldUseSVE();
981 if (use_sve) {
982 location_builder_ = &location_builder_sve_;
983 instruction_visitor_ = &instruction_visitor_sve_;
984 } else {
985 location_builder_ = &location_builder_neon_;
986 instruction_visitor_ = &instruction_visitor_neon_;
987 }
988}
989
990bool CodeGeneratorARM64::ShouldUseSVE() const {
Artem Serov8ba4de12019-12-04 21:10:23 +0000991 return GetInstructionSetFeatures().HasSVE();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000992}
Alexandre Rames5319def2014-10-23 10:03:10 +0100993
Artem Serov55ab7e82020-04-27 21:02:28 +0100994size_t CodeGeneratorARM64::GetSIMDRegisterWidth() const {
995 return SupportsPredicatedSIMD()
996 ? GetInstructionSetFeatures().GetSVEVectorLength() / kBitsPerByte
997 : vixl::aarch64::kQRegSizeInBytes;
998}
999
Alexandre Rames67555f72014-11-18 10:55:16 +00001000#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001001
Zheng Xu3927c8b2015-11-18 17:46:25 +08001002void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001003 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001004 jump_table->EmitTable(this);
1005 }
1006}
1007
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001008void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001009 EmitJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +00001010
1011 // Emit JIT baker read barrier slow paths.
Vladimir Marko695348f2020-05-19 14:42:02 +01001012 DCHECK(GetCompilerOptions().IsJitCompiler() || jit_baker_read_barrier_slow_paths_.empty());
Vladimir Marko966b46f2018-08-03 10:20:19 +00001013 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
1014 uint32_t encoded_data = entry.first;
1015 vixl::aarch64::Label* slow_path_entry = &entry.second.label;
1016 __ Bind(slow_path_entry);
Andreas Gampe3db70682018-12-26 15:12:03 -08001017 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name= */ nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00001018 }
1019
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001020 // Ensure we emit the literal pool.
1021 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001022
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001023 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001024
1025 // Verify Baker read barrier linker patches.
1026 if (kIsDebugBuild) {
1027 ArrayRef<const uint8_t> code = allocator->GetMemory();
1028 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
1029 DCHECK(info.label.IsBound());
1030 uint32_t literal_offset = info.label.GetLocation();
1031 DCHECK_ALIGNED(literal_offset, 4u);
1032
1033 auto GetInsn = [&code](uint32_t offset) {
1034 DCHECK_ALIGNED(offset, 4u);
1035 return
1036 (static_cast<uint32_t>(code[offset + 0]) << 0) +
1037 (static_cast<uint32_t>(code[offset + 1]) << 8) +
1038 (static_cast<uint32_t>(code[offset + 2]) << 16)+
1039 (static_cast<uint32_t>(code[offset + 3]) << 24);
1040 };
1041
1042 const uint32_t encoded_data = info.custom_data;
1043 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
1044 // Check that the next instruction matches the expected LDR.
1045 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01001046 case BakerReadBarrierKind::kField:
1047 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00001048 DCHECK_GE(code.size() - literal_offset, 8u);
1049 uint32_t next_insn = GetInsn(literal_offset + 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001050 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1051 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko0ecac682018-08-07 10:40:38 +01001052 if (kind == BakerReadBarrierKind::kField) {
1053 // LDR (immediate) with correct base_reg.
1054 CHECK_EQ(next_insn & 0xffc003e0u, 0xb9400000u | (base_reg << 5));
1055 } else {
1056 DCHECK(kind == BakerReadBarrierKind::kAcquire);
1057 // LDAR with correct base_reg.
1058 CHECK_EQ(next_insn & 0xffffffe0u, 0x88dffc00u | (base_reg << 5));
1059 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001060 break;
1061 }
1062 case BakerReadBarrierKind::kArray: {
1063 DCHECK_GE(code.size() - literal_offset, 8u);
1064 uint32_t next_insn = GetInsn(literal_offset + 4u);
1065 // LDR (register) with the correct base_reg, size=10 (32-bit), option=011 (extend = LSL),
1066 // and S=1 (shift amount = 2 for 32-bit version), i.e. LDR Wt, [Xn, Xm, LSL #2].
1067 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1068 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1069 CHECK_EQ(next_insn & 0xffe0ffe0u, 0xb8607800u | (base_reg << 5));
1070 CheckValidReg((next_insn >> 16) & 0x1f); // Check index register
1071 break;
1072 }
1073 case BakerReadBarrierKind::kGcRoot: {
1074 DCHECK_GE(literal_offset, 4u);
1075 uint32_t prev_insn = GetInsn(literal_offset - 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001076 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko94796f82018-08-08 15:15:33 +01001077 // Usually LDR (immediate) with correct root_reg but
Vladimir Markoc8178f52020-11-24 10:38:16 +00001078 // we may have a "MOV marked, old_value" for intrinsic CAS.
Vladimir Marko94796f82018-08-08 15:15:33 +01001079 if ((prev_insn & 0xffe0ffff) != (0x2a0003e0 | root_reg)) { // MOV?
1080 CHECK_EQ(prev_insn & 0xffc0001fu, 0xb9400000u | root_reg); // LDR?
1081 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001082 break;
1083 }
1084 default:
1085 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
1086 UNREACHABLE();
1087 }
1088 }
1089 }
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001090}
1091
Zheng Xuad4450e2015-04-17 18:48:56 +08001092void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1093 // Note: There are 6 kinds of moves:
1094 // 1. constant -> GPR/FPR (non-cycle)
1095 // 2. constant -> stack (non-cycle)
1096 // 3. GPR/FPR -> GPR/FPR
1097 // 4. GPR/FPR -> stack
1098 // 5. stack -> GPR/FPR
1099 // 6. stack -> stack (non-cycle)
1100 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1101 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1102 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1103 // dependency.
1104 vixl_temps_.Open(GetVIXLAssembler());
1105}
1106
1107void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1108 vixl_temps_.Close();
1109}
1110
1111Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001112 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1113 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1114 || kind == Location::kSIMDStackSlot);
1115 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1116 ? Location::kFpuRegister
1117 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001118 Location scratch = GetScratchLocation(kind);
1119 if (!scratch.Equals(Location::NoLocation())) {
1120 return scratch;
1121 }
1122 // Allocate from VIXL temp registers.
1123 if (kind == Location::kRegister) {
1124 scratch = LocationFrom(vixl_temps_.AcquireX());
1125 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001126 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serov1a719e42019-07-18 14:24:55 +01001127 scratch = codegen_->GetGraph()->HasSIMD()
1128 ? codegen_->GetInstructionCodeGeneratorArm64()->AllocateSIMDScratchLocation(&vixl_temps_)
1129 : LocationFrom(vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001130 }
1131 AddScratchLocation(scratch);
1132 return scratch;
1133}
1134
1135void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1136 if (loc.IsRegister()) {
1137 vixl_temps_.Release(XRegisterFrom(loc));
1138 } else {
1139 DCHECK(loc.IsFpuRegister());
Artem Serov1a719e42019-07-18 14:24:55 +01001140 if (codegen_->GetGraph()->HasSIMD()) {
1141 codegen_->GetInstructionCodeGeneratorArm64()->FreeSIMDScratchLocation(loc, &vixl_temps_);
1142 } else {
1143 vixl_temps_.Release(DRegisterFrom(loc));
1144 }
Zheng Xuad4450e2015-04-17 18:48:56 +08001145 }
1146 RemoveScratchLocation(loc);
1147}
1148
Alexandre Rames3e69f162014-12-10 10:36:50 +00001149void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001150 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001151 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001152}
1153
Mythri Alle5097f832021-11-02 14:52:30 +00001154void LocationsBuilderARM64::VisitMethodExitHook(HMethodExitHook* method_hook) {
1155 LocationSummary* locations = new (GetGraph()->GetAllocator())
1156 LocationSummary(method_hook, LocationSummary::kCallOnSlowPath);
1157 DataType::Type return_type = method_hook->InputAt(0)->GetType();
1158 locations->SetInAt(0, ARM64ReturnLocation(return_type));
1159}
1160
1161void InstructionCodeGeneratorARM64::GenerateMethodEntryExitHook(HInstruction* instruction) {
1162 MacroAssembler* masm = GetVIXLAssembler();
1163 UseScratchRegisterScope temps(masm);
1164 Register temp = temps.AcquireX();
1165 Register value = temps.AcquireW();
1166
1167 SlowPathCodeARM64* slow_path =
1168 new (codegen_->GetScopedAllocator()) MethodEntryExitHooksSlowPathARM64(instruction);
1169 codegen_->AddSlowPath(slow_path);
1170
1171 uint64_t address = reinterpret_cast64<uint64_t>(Runtime::Current()->GetInstrumentation());
1172 int offset = instrumentation::Instrumentation::NeedsEntryExitHooksOffset().Int32Value();
1173 __ Mov(temp, address + offset);
Mythri Alle9575c122021-11-12 12:04:41 +00001174 __ Ldrb(value, MemOperand(temp, 0));
Mythri Alle5097f832021-11-02 14:52:30 +00001175 __ Cbnz(value, slow_path->GetEntryLabel());
1176 __ Bind(slow_path->GetExitLabel());
1177}
1178
1179void InstructionCodeGeneratorARM64::VisitMethodExitHook(HMethodExitHook* instruction) {
1180 DCHECK(codegen_->GetCompilerOptions().IsJitCompiler() && GetGraph()->IsDebuggable());
1181 DCHECK(codegen_->RequiresCurrentMethod());
1182 GenerateMethodEntryExitHook(instruction);
1183}
1184
1185void LocationsBuilderARM64::VisitMethodEntryHook(HMethodEntryHook* method_hook) {
1186 new (GetGraph()->GetAllocator()) LocationSummary(method_hook, LocationSummary::kCallOnSlowPath);
1187}
1188
1189void InstructionCodeGeneratorARM64::VisitMethodEntryHook(HMethodEntryHook* instruction) {
1190 DCHECK(codegen_->GetCompilerOptions().IsJitCompiler() && GetGraph()->IsDebuggable());
1191 DCHECK(codegen_->RequiresCurrentMethod());
1192 GenerateMethodEntryExitHook(instruction);
1193}
1194
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001195void CodeGeneratorARM64::MaybeIncrementHotness(bool is_frame_entry) {
1196 MacroAssembler* masm = GetVIXLAssembler();
1197 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1198 UseScratchRegisterScope temps(masm);
1199 Register counter = temps.AcquireX();
1200 Register method = is_frame_entry ? kArtMethodRegister : temps.AcquireX();
1201 if (!is_frame_entry) {
1202 __ Ldr(method, MemOperand(sp, 0));
1203 }
1204 __ Ldrh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
Nicolas Geoffray61673dc2021-11-06 13:58:31 +00001205 vixl::aarch64::Label done;
1206 DCHECK_EQ(0u, interpreter::kNterpHotnessValue);
1207 __ Cbz(counter, &done);
1208 __ Add(counter, counter, -1);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001209 __ Strh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
Nicolas Geoffray61673dc2021-11-06 13:58:31 +00001210 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001211 }
1212
1213 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffray9e598902021-11-19 14:53:07 +00001214 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) CompileOptimizedSlowPathARM64();
1215 AddSlowPath(slow_path);
1216 ProfilingInfo* info = GetGraph()->GetProfilingInfo();
1217 DCHECK(info != nullptr);
1218 DCHECK(!HasEmptyFrame());
1219 uint64_t address = reinterpret_cast64<uint64_t>(info);
1220 vixl::aarch64::Label done;
1221 UseScratchRegisterScope temps(masm);
1222 Register temp = temps.AcquireX();
1223 Register counter = temps.AcquireW();
1224 __ Ldr(temp, DeduplicateUint64Literal(address));
1225 __ Ldrh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
1226 __ Cbz(counter, slow_path->GetEntryLabel());
1227 __ Add(counter, counter, -1);
1228 __ Strh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
1229 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001230 }
1231}
1232
Alexandre Rames5319def2014-10-23 10:03:10 +01001233void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001234 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffrayab3f8d22022-07-23 15:49:51 +00001235
1236 // Check if we need to generate the clinit check. We will jump to the
1237 // resolution stub if the class is not initialized and the executing thread is
1238 // not the thread initializing it.
1239 // We do this before constructing the frame to get the correct stack trace if
1240 // an exception is thrown.
1241 if (GetCompilerOptions().ShouldCompileWithClinitCheck(GetGraph()->GetArtMethod())) {
1242 UseScratchRegisterScope temps(masm);
1243 vixl::aarch64::Label resolution;
Nicolas Geoffrayf8537902022-09-13 15:22:29 +01001244 vixl::aarch64::Label memory_barrier;
Nicolas Geoffrayab3f8d22022-07-23 15:49:51 +00001245
1246 Register temp1 = temps.AcquireW();
1247 Register temp2 = temps.AcquireW();
1248
1249 // Check if we're visibly initialized.
1250
1251 // We don't emit a read barrier here to save on code size. We rely on the
1252 // resolution trampoline to do a suspend check before re-entering this code.
1253 __ Ldr(temp1, MemOperand(kArtMethodRegister, ArtMethod::DeclaringClassOffset().Int32Value()));
1254 __ Ldrb(temp2, HeapOperand(temp1, status_byte_offset));
1255 __ Cmp(temp2, shifted_visibly_initialized_value);
1256 __ B(hs, &frame_entry_label_);
1257
Nicolas Geoffrayf8537902022-09-13 15:22:29 +01001258 // Check if we're initialized and jump to code that does a memory barrier if
1259 // so.
1260 __ Cmp(temp2, shifted_initialized_value);
1261 __ B(hs, &memory_barrier);
1262
Nicolas Geoffrayab3f8d22022-07-23 15:49:51 +00001263 // Check if we're initializing and the thread initializing is the one
1264 // executing the code.
1265 __ Cmp(temp2, shifted_initializing_value);
1266 __ B(lo, &resolution);
1267
1268 __ Ldr(temp1, HeapOperand(temp1, mirror::Class::ClinitThreadIdOffset().Int32Value()));
1269 __ Ldr(temp2, MemOperand(tr, Thread::TidOffset<kArm64PointerSize>().Int32Value()));
1270 __ Cmp(temp1, temp2);
1271 __ B(eq, &frame_entry_label_);
1272 __ Bind(&resolution);
1273
1274 // Jump to the resolution stub.
1275 ThreadOffset64 entrypoint_offset =
1276 GetThreadOffset<kArm64PointerSize>(kQuickQuickResolutionTrampoline);
1277 __ Ldr(temp1.X(), MemOperand(tr, entrypoint_offset.Int32Value()));
1278 __ Br(temp1.X());
Nicolas Geoffrayf8537902022-09-13 15:22:29 +01001279
1280 __ Bind(&memory_barrier);
1281 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Nicolas Geoffrayab3f8d22022-07-23 15:49:51 +00001282 }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001283 __ Bind(&frame_entry_label_);
1284
Vladimir Marko33bff252017-11-01 14:35:42 +00001285 bool do_overflow_check =
1286 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001287 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001288 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001289 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001290 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001291 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001292 {
1293 // Ensure that between load and RecordPcInfo there are no pools emitted.
1294 ExactAssemblyScope eas(GetVIXLAssembler(),
1295 kInstructionSize,
1296 CodeBufferCheckScope::kExactSize);
1297 __ ldr(wzr, MemOperand(temp, 0));
1298 RecordPcInfo(nullptr, 0);
1299 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001300 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001301
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001302 if (!HasEmptyFrame()) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001303 // Stack layout:
1304 // sp[frame_size - 8] : lr.
1305 // ... : other preserved core registers.
1306 // ... : other preserved fp registers.
1307 // ... : reserved frame space.
1308 // sp[0] : current method.
Vladimir Marko1a225a72019-07-05 13:37:42 +01001309 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1310 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1311 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1312 DCHECK(!preserved_core_registers.IsEmpty());
1313 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1314 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001315
Vladimir Marko1a225a72019-07-05 13:37:42 +01001316 // Save the current method if we need it, or if using STP reduces code
1317 // size. Note that we do not do this in HCurrentMethod, as the
1318 // instruction might have been removed in the SSA graph.
1319 CPURegister lowest_spill;
1320 if (core_spills_offset == kXRegSizeInBytes) {
1321 // If there is no gap between the method and the lowest core spill, use
1322 // aligned STP pre-index to store both. Max difference is 512. We do
1323 // that to reduce code size even if we do not have to save the method.
1324 DCHECK_LE(frame_size, 512); // 32 core registers are only 256 bytes.
1325 lowest_spill = preserved_core_registers.PopLowestIndex();
1326 __ Stp(kArtMethodRegister, lowest_spill, MemOperand(sp, -frame_size, PreIndex));
1327 } else if (RequiresCurrentMethod()) {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001328 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001329 } else {
1330 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001331 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001332 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Vladimir Marko1a225a72019-07-05 13:37:42 +01001333 if (lowest_spill.IsValid()) {
1334 GetAssembler()->cfi().RelOffset(DWARFReg(lowest_spill), core_spills_offset);
1335 core_spills_offset += kXRegSizeInBytes;
1336 }
1337 GetAssembler()->SpillRegisters(preserved_core_registers, core_spills_offset);
1338 GetAssembler()->SpillRegisters(preserved_fp_registers, fp_spills_offset);
Mingyao Yang063fc772016-08-02 11:02:54 -07001339
1340 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1341 // Initialize should_deoptimize flag to 0.
1342 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1343 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1344 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001345 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001346 MaybeIncrementHotness(/* is_frame_entry= */ true);
Andreas Gampe3db70682018-12-26 15:12:03 -08001347 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001348}
1349
1350void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001351 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001352 if (!HasEmptyFrame()) {
Vladimir Marko1a225a72019-07-05 13:37:42 +01001353 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1354 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1355 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1356 DCHECK(!preserved_core_registers.IsEmpty());
1357 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1358 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
1359
1360 CPURegister lowest_spill;
1361 if (core_spills_offset == kXRegSizeInBytes) {
1362 // If there is no gap between the method and the lowest core spill, use
1363 // aligned LDP pre-index to pop both. Max difference is 504. We do
1364 // that to reduce code size even though the loaded method is unused.
1365 DCHECK_LE(frame_size, 504); // 32 core registers are only 256 bytes.
1366 lowest_spill = preserved_core_registers.PopLowestIndex();
1367 core_spills_offset += kXRegSizeInBytes;
1368 }
1369 GetAssembler()->UnspillRegisters(preserved_fp_registers, fp_spills_offset);
1370 GetAssembler()->UnspillRegisters(preserved_core_registers, core_spills_offset);
1371 if (lowest_spill.IsValid()) {
1372 __ Ldp(xzr, lowest_spill, MemOperand(sp, frame_size, PostIndex));
1373 GetAssembler()->cfi().Restore(DWARFReg(lowest_spill));
1374 } else {
1375 __ Drop(frame_size);
1376 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001377 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001378 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001379 __ Ret();
1380 GetAssembler()->cfi().RestoreState();
1381 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001382}
1383
Scott Wakeling97c72b72016-06-24 16:19:36 +01001384CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001385 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001386 return CPURegList(CPURegister::kRegister, kXRegSize,
1387 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001388}
1389
Scott Wakeling97c72b72016-06-24 16:19:36 +01001390CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001391 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1392 GetNumberOfFloatingPointRegisters()));
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001393 return CPURegList(CPURegister::kVRegister, kDRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001394 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001395}
1396
Alexandre Rames5319def2014-10-23 10:03:10 +01001397void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1398 __ Bind(GetLabelOf(block));
1399}
1400
Calin Juravle175dc732015-08-25 15:42:32 +01001401void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1402 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001403 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001404}
1405
Calin Juravlee460d1d2015-09-29 04:52:17 +01001406void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1407 if (location.IsRegister()) {
1408 locations->AddTemp(location);
1409 } else {
1410 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1411 }
1412}
1413
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001414void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001415 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001416 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001417 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001418 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001419 if (value_can_be_null) {
1420 __ Cbz(value, &done);
1421 }
Roland Levillainc73f0522018-08-14 15:16:50 +01001422 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07001423 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01001424 // Calculate the offset (in the card table) of the card corresponding to
1425 // `object`.
Alexandre Rames5319def2014-10-23 10:03:10 +01001426 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Roland Levillainc73f0522018-08-14 15:16:50 +01001427 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
1428 // `object`'s card.
1429 //
1430 // Register `card` contains the address of the card table. Note that the card
1431 // table's base is biased during its creation so that it always starts at an
1432 // address whose least-significant byte is equal to `kCardDirty` (see
1433 // art::gc::accounting::CardTable::Create). Therefore the STRB instruction
1434 // below writes the `kCardDirty` (byte) value into the `object`'s card
1435 // (located at `card + object >> kCardShift`).
1436 //
1437 // This dual use of the value in register `card` (1. to calculate the location
1438 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
1439 // (no need to explicitly load `kCardDirty` as an immediate value).
Serban Constantinescu02164b32014-11-13 14:05:07 +00001440 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001441 if (value_can_be_null) {
1442 __ Bind(&done);
1443 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001444}
1445
David Brazdil58282f42016-01-14 12:45:10 +00001446void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001447 // Blocked core registers:
1448 // lr : Runtime reserved.
1449 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001450 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001451 // ip1 : VIXL core temp.
1452 // ip0 : VIXL core temp.
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001453 // x18 : Platform register.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001454 //
1455 // Blocked fp registers:
1456 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001457 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1458 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001459 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001460 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001461 }
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001462 blocked_core_registers_[X18] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001463
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001464 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001465 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001466 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001467 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001468
David Brazdil58282f42016-01-14 12:45:10 +00001469 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001470 // Stubs do not save callee-save floating point registers. If the graph
1471 // is debuggable, we need to deal with these registers differently. For
1472 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001473 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1474 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001475 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001476 }
1477 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001478}
1479
Alexandre Rames3e69f162014-12-10 10:36:50 +00001480size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1481 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1482 __ Str(reg, MemOperand(sp, stack_index));
1483 return kArm64WordSize;
1484}
1485
1486size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1487 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1488 __ Ldr(reg, MemOperand(sp, stack_index));
1489 return kArm64WordSize;
1490}
1491
Artem Serov9df37b92019-07-23 16:41:54 +01001492size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1493 uint32_t reg_id ATTRIBUTE_UNUSED) {
1494 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1495 << "use SaveRestoreLiveRegistersHelper";
1496 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001497}
1498
Artem Serov9df37b92019-07-23 16:41:54 +01001499size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1500 uint32_t reg_id ATTRIBUTE_UNUSED) {
1501 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1502 << "use SaveRestoreLiveRegistersHelper";
1503 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001504}
1505
Alexandre Rames5319def2014-10-23 10:03:10 +01001506void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001507 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001508}
1509
1510void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001511 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001512}
1513
Vladimir Markoa0431112018-06-25 09:32:54 +01001514const Arm64InstructionSetFeatures& CodeGeneratorARM64::GetInstructionSetFeatures() const {
1515 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArm64InstructionSetFeatures();
1516}
1517
Alexandre Rames67555f72014-11-18 10:55:16 +00001518void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001519 if (constant->IsIntConstant()) {
1520 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1521 } else if (constant->IsLongConstant()) {
1522 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1523 } else if (constant->IsNullConstant()) {
1524 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001525 } else if (constant->IsFloatConstant()) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001526 __ Fmov(VRegister(destination), constant->AsFloatConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001527 } else {
1528 DCHECK(constant->IsDoubleConstant());
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001529 __ Fmov(VRegister(destination), constant->AsDoubleConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001530 }
1531}
1532
Alexandre Rames3e69f162014-12-10 10:36:50 +00001533
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001534static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001535 DCHECK(constant.IsConstant());
1536 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001537 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001538 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001539 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1540 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1541 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1542 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001543}
1544
Roland Levillain952b2352017-05-03 19:49:14 +01001545// Allocate a scratch register from the VIXL pool, querying first
1546// the floating-point register pool, and then the core register
1547// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001548// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1549// using a different allocation strategy.
1550static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1551 vixl::aarch64::UseScratchRegisterScope* temps,
1552 int size_in_bits) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001553 return masm->GetScratchVRegisterList()->IsEmpty()
Roland Levillain558dea12017-01-27 19:40:44 +00001554 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1555 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1556}
1557
Calin Juravlee460d1d2015-09-29 04:52:17 +01001558void CodeGeneratorARM64::MoveLocation(Location destination,
1559 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001560 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001561 if (source.Equals(destination)) {
1562 return;
1563 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001564
1565 // A valid move can always be inferred from the destination and source
1566 // locations. When moving from and to a register, the argument type can be
1567 // used to generate 32bit instead of 64bit moves. In debug mode we also
1568 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001569 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001570
1571 if (destination.IsRegister() || destination.IsFpuRegister()) {
1572 if (unspecified_type) {
1573 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1574 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001575 (src_cst != nullptr && (src_cst->IsIntConstant()
1576 || src_cst->IsFloatConstant()
1577 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001578 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001579 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001580 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001581 // If the source is a double stack slot or a 64bit constant, a 64bit
1582 // type is appropriate. Else the source is a register, and since the
1583 // type has not been specified, we chose a 64bit type to force a 64bit
1584 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001585 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001586 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001587 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001588 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1589 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001590 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001591 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1592 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1593 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001594 } else if (source.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001595 GetInstructionCodeGeneratorArm64()->LoadSIMDRegFromStack(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001596 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001597 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001598 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001599 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001600 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001601 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001602 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001603 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001604 DataType::Type source_type = DataType::Is64BitType(dst_type)
1605 ? DataType::Type::kInt64
1606 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001607 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1608 }
1609 } else {
1610 DCHECK(source.IsFpuRegister());
1611 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001612 DataType::Type source_type = DataType::Is64BitType(dst_type)
1613 ? DataType::Type::kFloat64
1614 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001615 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1616 } else {
1617 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001618 if (GetGraph()->HasSIMD()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001619 GetInstructionCodeGeneratorArm64()->MoveSIMDRegToSIMDReg(destination, source);
Artem Serovd4bccf12017-04-03 18:47:32 +01001620 } else {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001621 __ Fmov(VRegister(dst), FPRegisterFrom(source, dst_type));
Artem Serovd4bccf12017-04-03 18:47:32 +01001622 }
1623 }
1624 }
1625 } else if (destination.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001626 GetInstructionCodeGeneratorArm64()->MoveToSIMDStackSlot(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001627 } else { // The destination is not a register. It must be a stack slot.
1628 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1629 if (source.IsRegister() || source.IsFpuRegister()) {
1630 if (unspecified_type) {
1631 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001632 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001633 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001634 dst_type =
1635 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001636 }
1637 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001638 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1639 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001640 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001641 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001642 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1643 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001644 UseScratchRegisterScope temps(GetVIXLAssembler());
1645 HConstant* src_cst = source.GetConstant();
1646 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001647 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001648 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1649 ? Register(xzr)
1650 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001651 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001652 if (src_cst->IsIntConstant()) {
1653 temp = temps.AcquireW();
1654 } else if (src_cst->IsLongConstant()) {
1655 temp = temps.AcquireX();
1656 } else if (src_cst->IsFloatConstant()) {
1657 temp = temps.AcquireS();
1658 } else {
1659 DCHECK(src_cst->IsDoubleConstant());
1660 temp = temps.AcquireD();
1661 }
1662 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001663 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001664 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001665 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001666 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001667 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001668 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001669 // Use any scratch register (a core or a floating-point one)
1670 // from VIXL scratch register pools as a temporary.
1671 //
1672 // We used to only use the FP scratch register pool, but in some
1673 // rare cases the only register from this pool (D31) would
1674 // already be used (e.g. within a ParallelMove instruction, when
1675 // a move is blocked by a another move requiring a scratch FP
1676 // register, which would reserve D31). To prevent this issue, we
1677 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001678 //
1679 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001680 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001681 // use AcquireFPOrCoreCPURegisterOfSize instead of
1682 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1683 // allocates core scratch registers first.
1684 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1685 GetVIXLAssembler(),
1686 &temps,
1687 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001688 __ Ldr(temp, StackOperandFrom(source));
1689 __ Str(temp, StackOperandFrom(destination));
1690 }
1691 }
1692}
1693
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001694void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001695 CPURegister dst,
1696 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001697 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001698 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001699 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001700 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001701 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001702 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001703 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001704 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001705 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001706 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001707 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001708 case DataType::Type::kInt16:
1709 __ Ldrsh(Register(dst), src);
1710 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001711 case DataType::Type::kInt32:
1712 case DataType::Type::kReference:
1713 case DataType::Type::kInt64:
1714 case DataType::Type::kFloat32:
1715 case DataType::Type::kFloat64:
1716 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001717 __ Ldr(dst, src);
1718 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001719 case DataType::Type::kUint32:
1720 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001721 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001722 LOG(FATAL) << "Unreachable type " << type;
1723 }
1724}
1725
Calin Juravle77520bc2015-01-12 18:45:46 +00001726void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Vladimir Marko98873af2020-12-16 12:10:03 +00001727 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001728 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001729 const MemOperand& src,
1730 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001731 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001732 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001733 Register temp_base = temps.AcquireX();
1734
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001735 DCHECK(!src.IsPreIndex());
1736 DCHECK(!src.IsPostIndex());
1737
1738 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001739 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001740 {
1741 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1742 MemOperand base = MemOperand(temp_base);
1743 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001744 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001745 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001746 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001747 {
1748 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1749 __ ldarb(Register(dst), base);
1750 if (needs_null_check) {
1751 MaybeRecordImplicitNullCheck(instruction);
1752 }
1753 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001754 if (type == DataType::Type::kInt8) {
1755 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001756 }
1757 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001758 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001759 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001760 {
1761 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1762 __ ldarh(Register(dst), base);
1763 if (needs_null_check) {
1764 MaybeRecordImplicitNullCheck(instruction);
1765 }
1766 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001767 if (type == DataType::Type::kInt16) {
1768 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
1769 }
Artem Serov914d7a82017-02-07 14:33:49 +00001770 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001771 case DataType::Type::kInt32:
1772 case DataType::Type::kReference:
1773 case DataType::Type::kInt64:
1774 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001775 {
1776 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1777 __ ldar(Register(dst), base);
1778 if (needs_null_check) {
1779 MaybeRecordImplicitNullCheck(instruction);
1780 }
1781 }
1782 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001783 case DataType::Type::kFloat32:
1784 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00001785 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001786 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001787
Artem Serov914d7a82017-02-07 14:33:49 +00001788 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1789 {
1790 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1791 __ ldar(temp, base);
1792 if (needs_null_check) {
1793 MaybeRecordImplicitNullCheck(instruction);
1794 }
1795 }
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001796 __ Fmov(VRegister(dst), temp);
Artem Serov914d7a82017-02-07 14:33:49 +00001797 break;
Roland Levillain44015862016-01-22 11:47:17 +00001798 }
Aart Bik66c158e2018-01-31 12:55:04 -08001799 case DataType::Type::kUint32:
1800 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001801 case DataType::Type::kVoid:
Artem Serov914d7a82017-02-07 14:33:49 +00001802 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001803 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001804 }
1805}
1806
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001807void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001808 CPURegister src,
1809 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001810 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001811 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001812 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001813 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001814 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001815 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001816 case DataType::Type::kUint16:
1817 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001818 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001819 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001820 case DataType::Type::kInt32:
1821 case DataType::Type::kReference:
1822 case DataType::Type::kInt64:
1823 case DataType::Type::kFloat32:
1824 case DataType::Type::kFloat64:
1825 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001826 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001827 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001828 case DataType::Type::kUint32:
1829 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001830 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001831 LOG(FATAL) << "Unreachable type " << type;
1832 }
1833}
1834
Artem Serov914d7a82017-02-07 14:33:49 +00001835void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001836 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001837 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00001838 const MemOperand& dst,
1839 bool needs_null_check) {
1840 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001841 UseScratchRegisterScope temps(GetVIXLAssembler());
1842 Register temp_base = temps.AcquireX();
1843
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001844 DCHECK(!dst.IsPreIndex());
1845 DCHECK(!dst.IsPostIndex());
1846
1847 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001848 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001849 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001850 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00001851 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001852 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001853 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001854 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001855 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001856 {
1857 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1858 __ stlrb(Register(src), base);
1859 if (needs_null_check) {
1860 MaybeRecordImplicitNullCheck(instruction);
1861 }
1862 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001863 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001864 case DataType::Type::kUint16:
1865 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001866 {
1867 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1868 __ stlrh(Register(src), base);
1869 if (needs_null_check) {
1870 MaybeRecordImplicitNullCheck(instruction);
1871 }
1872 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001873 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001874 case DataType::Type::kInt32:
1875 case DataType::Type::kReference:
1876 case DataType::Type::kInt64:
1877 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001878 {
1879 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1880 __ stlr(Register(src), base);
1881 if (needs_null_check) {
1882 MaybeRecordImplicitNullCheck(instruction);
1883 }
1884 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001885 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001886 case DataType::Type::kFloat32:
1887 case DataType::Type::kFloat64: {
1888 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001889 Register temp_src;
1890 if (src.IsZero()) {
1891 // The zero register is used to avoid synthesizing zero constants.
1892 temp_src = Register(src);
1893 } else {
1894 DCHECK(src.IsFPRegister());
1895 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001896 __ Fmov(temp_src, VRegister(src));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001897 }
Artem Serov914d7a82017-02-07 14:33:49 +00001898 {
1899 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1900 __ stlr(temp_src, base);
1901 if (needs_null_check) {
1902 MaybeRecordImplicitNullCheck(instruction);
1903 }
1904 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001905 break;
1906 }
Aart Bik66c158e2018-01-31 12:55:04 -08001907 case DataType::Type::kUint32:
1908 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001909 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001910 LOG(FATAL) << "Unreachable type " << type;
1911 }
1912}
1913
Calin Juravle175dc732015-08-25 15:42:32 +01001914void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1915 HInstruction* instruction,
1916 uint32_t dex_pc,
1917 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001918 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00001919
Vladimir Markof6675082019-05-17 12:05:28 +01001920 ThreadOffset64 entrypoint_offset = GetThreadOffset<kArm64PointerSize>(entrypoint);
1921 // Reduce code size for AOT by using shared trampolines for slow path runtime calls across the
1922 // entire oat file. This adds an extra branch and we do not want to slow down the main path.
1923 // For JIT, thunk sharing is per-method, so the gains would be smaller or even negative.
Vladimir Marko695348f2020-05-19 14:42:02 +01001924 if (slow_path == nullptr || GetCompilerOptions().IsJitCompiler()) {
Vladimir Markof6675082019-05-17 12:05:28 +01001925 __ Ldr(lr, MemOperand(tr, entrypoint_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00001926 // Ensure the pc position is recorded immediately after the `blr` instruction.
1927 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1928 __ blr(lr);
1929 if (EntrypointRequiresStackMap(entrypoint)) {
1930 RecordPcInfo(instruction, dex_pc, slow_path);
1931 }
Vladimir Markof6675082019-05-17 12:05:28 +01001932 } else {
1933 // Ensure the pc position is recorded immediately after the `bl` instruction.
1934 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1935 EmitEntrypointThunkCall(entrypoint_offset);
1936 if (EntrypointRequiresStackMap(entrypoint)) {
1937 RecordPcInfo(instruction, dex_pc, slow_path);
1938 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001939 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001940}
1941
Roland Levillaindec8f632016-07-22 17:10:06 +01001942void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1943 HInstruction* instruction,
1944 SlowPathCode* slow_path) {
1945 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01001946 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1947 __ Blr(lr);
1948}
1949
Alexandre Rames67555f72014-11-18 10:55:16 +00001950void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001951 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001952 UseScratchRegisterScope temps(GetVIXLAssembler());
1953 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001954
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001955 // CMP (immediate) is limited to imm12 or imm12<<12, so we would need to materialize
1956 // the constant 0xf0000000 for comparison with the full 32-bit field. To reduce the code
1957 // size, load only the high byte of the field and compare with 0xf0.
1958 // Note: The same code size could be achieved with LDR+MNV(asr #24)+CBNZ but benchmarks
1959 // show that this pattern is slower (tested on little cores).
1960 __ Ldrb(temp, HeapOperand(class_reg, status_byte_offset));
1961 __ Cmp(temp, shifted_visibly_initialized_value);
1962 __ B(lo, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001963 __ Bind(slow_path->GetExitLabel());
1964}
Alexandre Rames5319def2014-10-23 10:03:10 +01001965
Vladimir Marko175e7862018-03-27 09:03:13 +00001966void InstructionCodeGeneratorARM64::GenerateBitstringTypeCheckCompare(
1967 HTypeCheckInstruction* check, vixl::aarch64::Register temp) {
1968 uint32_t path_to_root = check->GetBitstringPathToRoot();
1969 uint32_t mask = check->GetBitstringMask();
1970 DCHECK(IsPowerOfTwo(mask + 1));
1971 size_t mask_bits = WhichPowerOf2(mask + 1);
1972
1973 if (mask_bits == 16u) {
1974 // Load only the bitstring part of the status word.
1975 __ Ldrh(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1976 } else {
1977 // /* uint32_t */ temp = temp->status_
1978 __ Ldr(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1979 // Extract the bitstring bits.
1980 __ Ubfx(temp, temp, 0, mask_bits);
1981 }
1982 // Compare the bitstring bits to `path_to_root`.
1983 __ Cmp(temp, path_to_root);
1984}
1985
Roland Levillain44015862016-01-22 11:47:17 +00001986void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001987 BarrierType type = BarrierAll;
1988
1989 switch (kind) {
1990 case MemBarrierKind::kAnyAny:
1991 case MemBarrierKind::kAnyStore: {
1992 type = BarrierAll;
1993 break;
1994 }
1995 case MemBarrierKind::kLoadAny: {
1996 type = BarrierReads;
1997 break;
1998 }
1999 case MemBarrierKind::kStoreStore: {
2000 type = BarrierWrites;
2001 break;
2002 }
2003 default:
2004 LOG(FATAL) << "Unexpected memory barrier " << kind;
2005 }
2006 __ Dmb(InnerShareable, type);
2007}
2008
Vladimir Marko7039c112018-04-05 17:59:39 +01002009bool CodeGeneratorARM64::CanUseImplicitSuspendCheck() const {
2010 // Use implicit suspend checks if requested in compiler options unless there are SIMD
2011 // instructions in the graph. The implicit suspend check saves all FP registers as
2012 // 64-bit (in line with the calling convention) but SIMD instructions can use 128-bit
2013 // registers, so they need to be saved in an explicit slow path.
2014 return GetCompilerOptions().GetImplicitSuspendChecks() && !GetGraph()->HasSIMD();
2015}
2016
Serban Constantinescu02164b32014-11-13 14:05:07 +00002017void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2018 HBasicBlock* successor) {
Stelios Ioannouc54cc7c2021-07-09 17:06:03 +01002019 if (instruction->IsNoOp()) {
2020 if (successor != nullptr) {
2021 __ B(codegen_->GetLabelOf(successor));
2022 }
2023 return;
2024 }
2025
Vladimir Marko7039c112018-04-05 17:59:39 +01002026 if (codegen_->CanUseImplicitSuspendCheck()) {
2027 __ Ldr(kImplicitSuspendCheckRegister, MemOperand(kImplicitSuspendCheckRegister));
2028 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Vladimir Markoe45883e2022-01-11 12:38:35 +00002029 if (successor != nullptr) {
2030 __ B(codegen_->GetLabelOf(successor));
2031 }
Vladimir Marko7039c112018-04-05 17:59:39 +01002032 return;
2033 }
2034
Serban Constantinescu02164b32014-11-13 14:05:07 +00002035 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002036 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2037 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002038 slow_path =
2039 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002040 instruction->SetSlowPath(slow_path);
2041 codegen_->AddSlowPath(slow_path);
2042 if (successor != nullptr) {
2043 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002044 }
2045 } else {
2046 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2047 }
2048
Serban Constantinescu02164b32014-11-13 14:05:07 +00002049 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2050 Register temp = temps.AcquireW();
2051
Vladimir Markoddf4fd32021-11-22 16:31:57 +00002052 __ Ldr(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Vladimir Marko254a8582021-11-29 14:08:37 +00002053 __ Tst(temp, Thread::SuspendOrCheckpointRequestFlags());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002054 if (successor == nullptr) {
Vladimir Marko254a8582021-11-29 14:08:37 +00002055 __ B(ne, slow_path->GetEntryLabel());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002056 __ Bind(slow_path->GetReturnLabel());
2057 } else {
Vladimir Marko254a8582021-11-29 14:08:37 +00002058 __ B(eq, codegen_->GetLabelOf(successor));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002059 __ B(slow_path->GetEntryLabel());
2060 // slow_path will return to GetLabelOf(successor).
2061 }
2062}
2063
Alexandre Rames5319def2014-10-23 10:03:10 +01002064InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2065 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002066 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002067 assembler_(codegen->GetAssembler()),
2068 codegen_(codegen) {}
2069
Alexandre Rames67555f72014-11-18 10:55:16 +00002070void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002071 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002072 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002073 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002074 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002075 case DataType::Type::kInt32:
2076 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01002077 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002078 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002079 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002080 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002081
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002082 case DataType::Type::kFloat32:
2083 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002084 locations->SetInAt(0, Location::RequiresFpuRegister());
2085 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002086 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002087 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002088
Alexandre Rames5319def2014-10-23 10:03:10 +01002089 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002090 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002091 }
2092}
2093
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002094void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2095 const FieldInfo& field_info) {
Alex Light3a73ffb2021-01-25 14:11:05 +00002096 DCHECK(instruction->IsInstanceFieldGet() ||
2097 instruction->IsStaticFieldGet() ||
2098 instruction->IsPredicatedInstanceFieldGet());
2099
2100 bool is_predicated = instruction->IsPredicatedInstanceFieldGet();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002101
2102 bool object_field_get_with_read_barrier =
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00002103 gUseReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01002104 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002105 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2106 object_field_get_with_read_barrier
2107 ? LocationSummary::kCallOnSlowPath
2108 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002109 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002110 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko0ecac682018-08-07 10:40:38 +01002111 // We need a temporary register for the read barrier load in
2112 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2113 // only if the field is volatile or the offset is too big.
2114 if (field_info.IsVolatile() ||
2115 field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2116 locations->AddTemp(FixedTempLocation());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002117 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002118 }
Alex Light3a73ffb2021-01-25 14:11:05 +00002119 // Input for object receiver.
2120 locations->SetInAt(is_predicated ? 1 : 0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002121 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alex Light3a73ffb2021-01-25 14:11:05 +00002122 if (is_predicated) {
2123 locations->SetInAt(0, Location::RequiresFpuRegister());
2124 locations->SetOut(Location::SameAsFirstInput());
2125 } else {
2126 locations->SetOut(Location::RequiresFpuRegister());
2127 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002128 } else {
Alex Light3a73ffb2021-01-25 14:11:05 +00002129 if (is_predicated) {
2130 locations->SetInAt(0, Location::RequiresRegister());
2131 locations->SetOut(Location::SameAsFirstInput());
2132 } else {
2133 // The output overlaps for an object field get when read barriers
2134 // are enabled: we do not want the load to overwrite the object's
2135 // location, as we need it to emit the read barrier.
2136 locations->SetOut(Location::RequiresRegister(),
2137 object_field_get_with_read_barrier ? Location::kOutputOverlap
2138 : Location::kNoOutputOverlap);
2139 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002140 }
2141}
2142
2143void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2144 const FieldInfo& field_info) {
Alex Light3a73ffb2021-01-25 14:11:05 +00002145 DCHECK(instruction->IsInstanceFieldGet() ||
2146 instruction->IsStaticFieldGet() ||
2147 instruction->IsPredicatedInstanceFieldGet());
2148 bool is_predicated = instruction->IsPredicatedInstanceFieldGet();
Roland Levillain44015862016-01-22 11:47:17 +00002149 LocationSummary* locations = instruction->GetLocations();
Alex Light3a73ffb2021-01-25 14:11:05 +00002150 uint32_t receiver_input = is_predicated ? 1 : 0;
2151 Location base_loc = locations->InAt(receiver_input);
Roland Levillain44015862016-01-22 11:47:17 +00002152 Location out = locations->Out();
2153 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01002154 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
2155 DataType::Type load_type = instruction->GetType();
Alex Light3a73ffb2021-01-25 14:11:05 +00002156 MemOperand field =
2157 HeapOperand(InputRegisterAt(instruction, receiver_input), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002158
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00002159 if (gUseReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01002160 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002161 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002162 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002163 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002164 Location maybe_temp =
2165 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002166 // Note that potential implicit null checks are handled in this
2167 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2168 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2169 instruction,
2170 out,
2171 base,
2172 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002173 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002174 /* needs_null_check= */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002175 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002176 } else {
2177 // General case.
2178 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002179 // Note that a potential implicit null check is handled in this
2180 // CodeGeneratorARM64::LoadAcquire call.
2181 // NB: LoadAcquire will record the pc info if needed.
Vladimir Marko98873af2020-12-16 12:10:03 +00002182 codegen_->LoadAcquire(instruction,
2183 load_type,
2184 OutputCPURegister(instruction),
2185 field,
2186 /* needs_null_check= */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002187 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002188 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2189 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01002190 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002191 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002192 }
Vladimir Marko61b92282017-10-11 13:23:17 +01002193 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002194 // If read barriers are enabled, emit read barriers other than
2195 // Baker's using a slow path (and also unpoison the loaded
2196 // reference, if heap poisoning is enabled).
2197 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2198 }
Roland Levillain4d027112015-07-01 15:41:14 +01002199 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002200}
2201
2202void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2203 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002204 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01002205 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002206 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2207 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002208 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002209 locations->SetInAt(1, Location::RequiresFpuRegister());
2210 } else {
2211 locations->SetInAt(1, Location::RequiresRegister());
2212 }
2213}
2214
2215void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002216 const FieldInfo& field_info,
2217 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002218 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alex Light3a73ffb2021-01-25 14:11:05 +00002219 bool is_predicated =
2220 instruction->IsInstanceFieldSet() && instruction->AsInstanceFieldSet()->GetIsPredicatedSet();
Alexandre Rames09a99962015-04-15 11:47:56 +01002221
2222 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002223 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002224 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002225 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002226 DataType::Type field_type = field_info.GetFieldType();
Alex Light3a73ffb2021-01-25 14:11:05 +00002227 std::optional<vixl::aarch64::Label> pred_is_null;
2228 if (is_predicated) {
2229 pred_is_null.emplace();
2230 __ Cbz(obj, &*pred_is_null);
2231 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002232
Roland Levillain4d027112015-07-01 15:41:14 +01002233 {
2234 // We use a block to end the scratch scope before the write barrier, thus
2235 // freeing the temporary registers so they can be used in `MarkGCCard`.
2236 UseScratchRegisterScope temps(GetVIXLAssembler());
2237
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002238 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002239 DCHECK(value.IsW());
2240 Register temp = temps.AcquireW();
2241 __ Mov(temp, value.W());
2242 GetAssembler()->PoisonHeapReference(temp.W());
2243 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002244 }
Roland Levillain4d027112015-07-01 15:41:14 +01002245
2246 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002247 codegen_->StoreRelease(
Andreas Gampe3db70682018-12-26 15:12:03 -08002248 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check= */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002249 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002250 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2251 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002252 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2253 codegen_->MaybeRecordImplicitNullCheck(instruction);
2254 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002255 }
2256
2257 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002258 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002259 }
Alex Light3a73ffb2021-01-25 14:11:05 +00002260
2261 if (is_predicated) {
2262 __ Bind(&*pred_is_null);
2263 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002264}
2265
Alexandre Rames67555f72014-11-18 10:55:16 +00002266void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002267 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002268
2269 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002270 case DataType::Type::kInt32:
2271 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002272 Register dst = OutputRegister(instr);
2273 Register lhs = InputRegisterAt(instr, 0);
2274 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002275 if (instr->IsAdd()) {
2276 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002277 } else if (instr->IsAnd()) {
2278 __ And(dst, lhs, rhs);
2279 } else if (instr->IsOr()) {
2280 __ Orr(dst, lhs, rhs);
2281 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002282 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002283 } else if (instr->IsRor()) {
2284 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002285 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002286 __ Ror(dst, lhs, shift);
2287 } else {
2288 // Ensure shift distance is in the same size register as the result. If
2289 // we are rotating a long and the shift comes in a w register originally,
2290 // we don't need to sxtw for use as an x since the shift distances are
2291 // all & reg_bits - 1.
2292 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2293 }
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002294 } else if (instr->IsMin() || instr->IsMax()) {
2295 __ Cmp(lhs, rhs);
2296 __ Csel(dst, lhs, rhs, instr->IsMin() ? lt : gt);
Alexandre Rames67555f72014-11-18 10:55:16 +00002297 } else {
2298 DCHECK(instr->IsXor());
2299 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002300 }
2301 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002302 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002303 case DataType::Type::kFloat32:
2304 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002305 VRegister dst = OutputFPRegister(instr);
2306 VRegister lhs = InputFPRegisterAt(instr, 0);
2307 VRegister rhs = InputFPRegisterAt(instr, 1);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002308 if (instr->IsAdd()) {
2309 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002310 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002311 __ Fsub(dst, lhs, rhs);
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002312 } else if (instr->IsMin()) {
2313 __ Fmin(dst, lhs, rhs);
2314 } else if (instr->IsMax()) {
2315 __ Fmax(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002316 } else {
2317 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002318 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002319 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002320 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002321 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002322 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002323 }
2324}
2325
Serban Constantinescu02164b32014-11-13 14:05:07 +00002326void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2327 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2328
Vladimir Markoca6fff82017-10-03 14:49:14 +01002329 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002330 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002331 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002332 case DataType::Type::kInt32:
2333 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002334 locations->SetInAt(0, Location::RequiresRegister());
2335 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002336 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002337 break;
2338 }
2339 default:
2340 LOG(FATAL) << "Unexpected shift type " << type;
2341 }
2342}
2343
2344void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2345 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2346
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002347 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002348 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002349 case DataType::Type::kInt32:
2350 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002351 Register dst = OutputRegister(instr);
2352 Register lhs = InputRegisterAt(instr, 0);
2353 Operand rhs = InputOperandAt(instr, 1);
2354 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002355 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002356 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002357 if (instr->IsShl()) {
2358 __ Lsl(dst, lhs, shift_value);
2359 } else if (instr->IsShr()) {
2360 __ Asr(dst, lhs, shift_value);
2361 } else {
2362 __ Lsr(dst, lhs, shift_value);
2363 }
2364 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002365 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002366
2367 if (instr->IsShl()) {
2368 __ Lsl(dst, lhs, rhs_reg);
2369 } else if (instr->IsShr()) {
2370 __ Asr(dst, lhs, rhs_reg);
2371 } else {
2372 __ Lsr(dst, lhs, rhs_reg);
2373 }
2374 }
2375 break;
2376 }
2377 default:
2378 LOG(FATAL) << "Unexpected shift operation type " << type;
2379 }
2380}
2381
Alexandre Rames5319def2014-10-23 10:03:10 +01002382void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002383 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002384}
2385
2386void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002387 HandleBinaryOp(instruction);
2388}
2389
2390void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2391 HandleBinaryOp(instruction);
2392}
2393
2394void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2395 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002396}
2397
Artem Serov7fc63502016-02-09 17:15:29 +00002398void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002399 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002400 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002401 locations->SetInAt(0, Location::RequiresRegister());
2402 // There is no immediate variant of negated bitwise instructions in AArch64.
2403 locations->SetInAt(1, Location::RequiresRegister());
2404 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2405}
2406
Artem Serov7fc63502016-02-09 17:15:29 +00002407void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002408 Register dst = OutputRegister(instr);
2409 Register lhs = InputRegisterAt(instr, 0);
2410 Register rhs = InputRegisterAt(instr, 1);
2411
2412 switch (instr->GetOpKind()) {
2413 case HInstruction::kAnd:
2414 __ Bic(dst, lhs, rhs);
2415 break;
2416 case HInstruction::kOr:
2417 __ Orn(dst, lhs, rhs);
2418 break;
2419 case HInstruction::kXor:
2420 __ Eon(dst, lhs, rhs);
2421 break;
2422 default:
2423 LOG(FATAL) << "Unreachable";
2424 }
2425}
2426
Anton Kirilov74234da2017-01-13 14:42:47 +00002427void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2428 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002429 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2430 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002431 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002432 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002433 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2434 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2435 } else {
2436 locations->SetInAt(0, Location::RequiresRegister());
2437 }
2438 locations->SetInAt(1, Location::RequiresRegister());
2439 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2440}
2441
Anton Kirilov74234da2017-01-13 14:42:47 +00002442void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2443 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002444 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002445 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002446 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002447 Register out = OutputRegister(instruction);
2448 Register left;
2449 if (kind != HInstruction::kNeg) {
2450 left = InputRegisterAt(instruction, 0);
2451 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002452 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002453 // shifter operand operation, the IR generating `right_reg` (input to the type
2454 // conversion) can have a different type from the current instruction's type,
2455 // so we manually indicate the type.
2456 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002457 Operand right_operand(0);
2458
Anton Kirilov74234da2017-01-13 14:42:47 +00002459 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2460 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002461 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2462 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002463 right_operand = Operand(right_reg,
2464 helpers::ShiftFromOpKind(op_kind),
2465 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002466 }
2467
2468 // Logical binary operations do not support extension operations in the
2469 // operand. Note that VIXL would still manage if it was passed by generating
2470 // the extension as a separate instruction.
2471 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
Santiago Aboy Solanes872ec722022-02-18 14:10:25 +00002472 DCHECK_IMPLIES(right_operand.IsExtendedRegister(),
2473 kind != HInstruction::kAnd && kind != HInstruction::kOr &&
2474 kind != HInstruction::kXor && kind != HInstruction::kNeg);
Alexandre Rames8626b742015-11-25 16:28:08 +00002475 switch (kind) {
2476 case HInstruction::kAdd:
2477 __ Add(out, left, right_operand);
2478 break;
2479 case HInstruction::kAnd:
2480 __ And(out, left, right_operand);
2481 break;
2482 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002483 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002484 __ Neg(out, right_operand);
2485 break;
2486 case HInstruction::kOr:
2487 __ Orr(out, left, right_operand);
2488 break;
2489 case HInstruction::kSub:
2490 __ Sub(out, left, right_operand);
2491 break;
2492 case HInstruction::kXor:
2493 __ Eor(out, left, right_operand);
2494 break;
2495 default:
2496 LOG(FATAL) << "Unexpected operation kind: " << kind;
2497 UNREACHABLE();
2498 }
2499}
2500
Artem Serov328429f2016-07-06 16:23:04 +01002501void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002502 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002503 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002504 locations->SetInAt(0, Location::RequiresRegister());
2505 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002506 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002507}
2508
Roland Levillain19c54192016-11-04 13:44:09 +00002509void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002510 __ Add(OutputRegister(instruction),
2511 InputRegisterAt(instruction, 0),
2512 Operand(InputOperandAt(instruction, 1)));
2513}
2514
Artem Serove1811ed2017-04-27 16:50:47 +01002515void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2516 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002517 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002518
2519 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2520
2521 locations->SetInAt(0, Location::RequiresRegister());
2522 // For byte case we don't need to shift the index variable so we can encode the data offset into
2523 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2524 // data offset constant generation out of the loop and reduce the critical path length in the
2525 // loop.
2526 locations->SetInAt(1, shift->GetValue() == 0
2527 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2528 : Location::RequiresRegister());
2529 locations->SetInAt(2, Location::ConstantLocation(shift));
2530 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2531}
2532
2533void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2534 HIntermediateAddressIndex* instruction) {
2535 Register index_reg = InputRegisterAt(instruction, 0);
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002536 uint32_t shift = Int64FromLocation(instruction->GetLocations()->InAt(2));
Artem Serove1811ed2017-04-27 16:50:47 +01002537 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2538
2539 if (shift == 0) {
2540 __ Add(OutputRegister(instruction), index_reg, offset);
2541 } else {
2542 Register offset_reg = InputRegisterAt(instruction, 1);
2543 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2544 }
2545}
2546
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002547void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002548 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002549 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002550 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2551 if (instr->GetOpKind() == HInstruction::kSub &&
2552 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002553 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002554 // Don't allocate register for Mneg instruction.
2555 } else {
2556 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2557 Location::RequiresRegister());
2558 }
2559 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2560 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002561 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2562}
2563
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002564void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002565 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002566 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2567 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002568
2569 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2570 // This fixup should be carried out for all multiply-accumulate instructions:
2571 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002572 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002573 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2574 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Florian Mayer6f572b72022-01-10 13:03:57 -08002575 ptrdiff_t off = masm->GetCursorOffset();
2576 if (off >= static_cast<ptrdiff_t>(kInstructionSize) &&
2577 masm->GetInstructionAt(off - static_cast<ptrdiff_t>(kInstructionSize))->IsLoadOrStore()) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002578 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002579 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002580 __ nop();
2581 }
2582 }
2583
2584 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002585 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002586 __ Madd(res, mul_left, mul_right, accumulator);
2587 } else {
2588 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002589 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002590 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002591 __ Mneg(res, mul_left, mul_right);
2592 } else {
2593 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2594 __ Msub(res, mul_left, mul_right, accumulator);
2595 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002596 }
2597}
2598
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002599void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002600 bool object_array_get_with_read_barrier =
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00002601 gUseReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002602 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002603 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2604 object_array_get_with_read_barrier
2605 ? LocationSummary::kCallOnSlowPath
2606 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002607 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002608 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002609 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002610 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002611 // We need a temporary register for the read barrier load in
2612 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2613 // only if the offset is too big.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002614 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2615 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002616 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002617 if (offset >= kReferenceLoadMinFarOffset) {
2618 locations->AddTemp(FixedTempLocation());
2619 }
Artem Serov0806f582018-10-11 20:14:20 +01002620 } else if (!instruction->GetArray()->IsIntermediateAddress()) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01002621 // We need a non-scratch temporary for the array data pointer in
Artem Serov0806f582018-10-11 20:14:20 +01002622 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier() for the case with no
2623 // intermediate address.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002624 locations->AddTemp(Location::RequiresRegister());
2625 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002626 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002627 locations->SetInAt(0, Location::RequiresRegister());
2628 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002629 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002630 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2631 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002632 // The output overlaps in the case of an object array get with
2633 // read barriers enabled: we do not want the move to overwrite the
2634 // array's location, as we need it to emit the read barrier.
2635 locations->SetOut(
2636 Location::RequiresRegister(),
2637 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002638 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002639}
2640
2641void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002642 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002643 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002644 LocationSummary* locations = instruction->GetLocations();
2645 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002646 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002647 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002648 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2649 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002650 MacroAssembler* masm = GetVIXLAssembler();
2651 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002652
Artem Serov0806f582018-10-11 20:14:20 +01002653 // The non-Baker read barrier instrumentation of object ArrayGet instructions
Roland Levillain19c54192016-11-04 13:44:09 +00002654 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002655 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002656 instruction->GetArray()->IsIntermediateAddress() &&
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00002657 gUseReadBarrier &&
Artem Serov0806f582018-10-11 20:14:20 +01002658 !kUseBakerReadBarrier));
Roland Levillain19c54192016-11-04 13:44:09 +00002659
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00002660 if (type == DataType::Type::kReference && gUseReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002661 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002662 // Note that a potential implicit null check is handled in the
2663 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002664 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002665 if (index.IsConstant()) {
Artem Serov0806f582018-10-11 20:14:20 +01002666 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002667 // Array load with a constant index can be treated as a field load.
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002668 offset += Int64FromLocation(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002669 Location maybe_temp =
2670 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2671 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2672 out,
2673 obj.W(),
2674 offset,
2675 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002676 /* needs_null_check= */ false,
2677 /* use_load_acquire= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002678 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002679 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08002680 instruction, out, obj.W(), offset, index, /* needs_null_check= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002681 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002682 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002683 // General case.
2684 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002685 Register length;
2686 if (maybe_compressed_char_at) {
2687 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2688 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002689 {
2690 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2691 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2692
2693 if (instruction->GetArray()->IsIntermediateAddress()) {
2694 DCHECK_LT(count_offset, offset);
2695 int64_t adjusted_offset =
2696 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2697 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2698 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2699 } else {
2700 __ Ldr(length, HeapOperand(obj, count_offset));
2701 }
2702 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002703 }
jessicahandojo05765752016-09-09 19:01:32 -07002704 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002705 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002706 if (maybe_compressed_char_at) {
2707 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002708 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2709 "Expecting 0=compressed, 1=uncompressed");
2710 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002711 __ Ldrb(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002712 HeapOperand(obj, offset + Int64FromLocation(index)));
jessicahandojo05765752016-09-09 19:01:32 -07002713 __ B(&done);
2714 __ Bind(&uncompressed_load);
2715 __ Ldrh(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002716 HeapOperand(obj, offset + (Int64FromLocation(index) << 1)));
jessicahandojo05765752016-09-09 19:01:32 -07002717 __ Bind(&done);
2718 } else {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002719 offset += Int64FromLocation(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002720 source = HeapOperand(obj, offset);
2721 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002722 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002723 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002724 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002725 // We do not need to compute the intermediate address from the array: the
2726 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002727 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002728 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002729 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2730 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
Roland Levillain44015862016-01-22 11:47:17 +00002731 }
2732 temp = obj;
2733 } else {
2734 __ Add(temp, obj, offset);
2735 }
jessicahandojo05765752016-09-09 19:01:32 -07002736 if (maybe_compressed_char_at) {
2737 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002738 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2739 "Expecting 0=compressed, 1=uncompressed");
2740 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002741 __ Ldrb(Register(OutputCPURegister(instruction)),
2742 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2743 __ B(&done);
2744 __ Bind(&uncompressed_load);
2745 __ Ldrh(Register(OutputCPURegister(instruction)),
2746 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2747 __ Bind(&done);
2748 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002749 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002750 }
Roland Levillain44015862016-01-22 11:47:17 +00002751 }
jessicahandojo05765752016-09-09 19:01:32 -07002752 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002753 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2754 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002755 codegen_->Load(type, OutputCPURegister(instruction), source);
2756 codegen_->MaybeRecordImplicitNullCheck(instruction);
2757 }
Roland Levillain44015862016-01-22 11:47:17 +00002758
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002759 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002760 static_assert(
2761 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2762 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2763 Location obj_loc = locations->InAt(0);
2764 if (index.IsConstant()) {
2765 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2766 } else {
2767 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2768 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002769 }
Roland Levillain4d027112015-07-01 15:41:14 +01002770 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002771}
2772
Alexandre Rames5319def2014-10-23 10:03:10 +01002773void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002774 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002775 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002776 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002777}
2778
2779void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002780 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002781 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002782 {
2783 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2784 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2785 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2786 codegen_->MaybeRecordImplicitNullCheck(instruction);
2787 }
jessicahandojo05765752016-09-09 19:01:32 -07002788 // Mask out compression flag from String's array length.
2789 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002790 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002791 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002792}
2793
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002794void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002795 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002796
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002797 bool needs_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002798 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002799 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002800 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002801 locations->SetInAt(0, Location::RequiresRegister());
2802 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002803 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2804 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002805 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002806 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002807 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002808 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002809 }
2810}
2811
2812void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002813 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002814 LocationSummary* locations = instruction->GetLocations();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002815 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002816 bool needs_write_barrier =
2817 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002818
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002819 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002820 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002821 CPURegister source = value;
2822 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002823 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002824 MemOperand destination = HeapOperand(array);
2825 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002826
2827 if (!needs_write_barrier) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002828 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002829 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002830 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002831 destination = HeapOperand(array, offset);
2832 } else {
2833 UseScratchRegisterScope temps(masm);
2834 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002835 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002836 // We do not need to compute the intermediate address from the array: the
2837 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002838 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002839 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002840 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2841 DCHECK(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002842 }
2843 temp = array;
2844 } else {
2845 __ Add(temp, array, offset);
2846 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002847 destination = HeapOperand(temp,
2848 XRegisterFrom(index),
2849 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002850 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002851 }
Artem Serov914d7a82017-02-07 14:33:49 +00002852 {
2853 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2854 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2855 codegen_->Store(value_type, value, destination);
2856 codegen_->MaybeRecordImplicitNullCheck(instruction);
2857 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002858 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002859 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002860
2861 bool can_value_be_null = instruction->GetValueCanBeNull();
2862 vixl::aarch64::Label do_store;
2863 if (can_value_be_null) {
2864 __ Cbz(Register(value), &do_store);
2865 }
2866
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002867 SlowPathCodeARM64* slow_path = nullptr;
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002868 if (needs_type_check) {
2869 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
2870 codegen_->AddSlowPath(slow_path);
2871
2872 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2873 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2874 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2875
Alexandre Rames97833a02015-04-16 15:07:12 +01002876 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002877 Register temp = temps.AcquireSameSizeAs(array);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002878 Register temp2 = temps.AcquireSameSizeAs(array);
2879
2880 // Note that when Baker read barriers are enabled, the type
2881 // checks are performed without read barriers. This is fine,
2882 // even in the case where a class object is in the from-space
2883 // after the flip, as a comparison involving such a type would
2884 // not produce a false positive; it may of course produce a
2885 // false negative, in which case we would take the ArraySet
2886 // slow path.
2887
2888 // /* HeapReference<Class> */ temp = array->klass_
2889 {
2890 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2891 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2892 __ Ldr(temp, HeapOperand(array, class_offset));
2893 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames97833a02015-04-16 15:07:12 +01002894 }
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002895 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Alexandre Rames97833a02015-04-16 15:07:12 +01002896
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002897 // /* HeapReference<Class> */ temp = temp->component_type_
2898 __ Ldr(temp, HeapOperand(temp, component_offset));
2899 // /* HeapReference<Class> */ temp2 = value->klass_
2900 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2901 // If heap poisoning is enabled, no need to unpoison `temp`
2902 // nor `temp2`, as we are comparing two poisoned references.
2903 __ Cmp(temp, temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002904
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002905 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2906 vixl::aarch64::Label do_put;
2907 __ B(eq, &do_put);
2908 // If heap poisoning is enabled, the `temp` reference has
2909 // not been unpoisoned yet; unpoison it now.
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002910 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002911
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002912 // /* HeapReference<Class> */ temp = temp->super_class_
2913 __ Ldr(temp, HeapOperand(temp, super_offset));
2914 // If heap poisoning is enabled, no need to unpoison
2915 // `temp`, as we are comparing against null below.
2916 __ Cbnz(temp, slow_path->GetEntryLabel());
2917 __ Bind(&do_put);
Vladimir Markod1ef8732017-04-18 13:55:13 +01002918 } else {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002919 __ B(ne, slow_path->GetEntryLabel());
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002920 }
2921 }
2922
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002923 codegen_->MarkGCCard(array, value.W(), /* value_can_be_null= */ false);
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002924
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002925 if (can_value_be_null) {
2926 DCHECK(do_store.IsLinked());
2927 __ Bind(&do_store);
2928 }
2929
2930 UseScratchRegisterScope temps(masm);
2931 if (kPoisonHeapReferences) {
2932 Register temp_source = temps.AcquireSameSizeAs(array);
2933 DCHECK(value.IsW());
2934 __ Mov(temp_source, value.W());
2935 GetAssembler()->PoisonHeapReference(temp_source);
2936 source = temp_source;
2937 }
2938
2939 if (index.IsConstant()) {
2940 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
2941 destination = HeapOperand(array, offset);
2942 } else {
2943 Register temp_base = temps.AcquireSameSizeAs(array);
2944 __ Add(temp_base, array, offset);
2945 destination = HeapOperand(temp_base,
2946 XRegisterFrom(index),
2947 LSL,
2948 DataType::SizeShift(value_type));
2949 }
2950
2951 {
2952 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2953 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2954 __ Str(source, destination);
2955
2956 if (can_value_be_null || !needs_type_check) {
2957 codegen_->MaybeRecordImplicitNullCheck(instruction);
2958 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002959 }
2960
2961 if (slow_path != nullptr) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002962 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002963 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002964 }
2965}
2966
Alexandre Rames67555f72014-11-18 10:55:16 +00002967void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002968 RegisterSet caller_saves = RegisterSet::Empty();
2969 InvokeRuntimeCallingConvention calling_convention;
2970 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2971 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2972 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002973
2974 // If both index and length are constant, we can check the bounds statically and
2975 // generate code accordingly. We want to make sure we generate constant locations
2976 // in that case, regardless of whether they are encodable in the comparison or not.
2977 HInstruction* index = instruction->InputAt(0);
2978 HInstruction* length = instruction->InputAt(1);
2979 bool both_const = index->IsConstant() && length->IsConstant();
2980 locations->SetInAt(0, both_const
2981 ? Location::ConstantLocation(index->AsConstant())
2982 : ARM64EncodableConstantOrRegister(index, instruction));
2983 locations->SetInAt(1, both_const
2984 ? Location::ConstantLocation(length->AsConstant())
2985 : ARM64EncodableConstantOrRegister(length, instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002986}
2987
2988void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Georgia Kouvelibe530852019-01-17 10:46:41 +00002989 LocationSummary* locations = instruction->GetLocations();
2990 Location index_loc = locations->InAt(0);
2991 Location length_loc = locations->InAt(1);
2992
2993 int cmp_first_input = 0;
2994 int cmp_second_input = 1;
2995 Condition cond = hs;
2996
2997 if (index_loc.IsConstant()) {
2998 int64_t index = Int64FromLocation(index_loc);
2999 if (length_loc.IsConstant()) {
3000 int64_t length = Int64FromLocation(length_loc);
3001 if (index < 0 || index >= length) {
3002 BoundsCheckSlowPathARM64* slow_path =
3003 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
3004 codegen_->AddSlowPath(slow_path);
3005 __ B(slow_path->GetEntryLabel());
3006 } else {
3007 // BCE will remove the bounds check if we are guaranteed to pass.
3008 // However, some optimization after BCE may have generated this, and we should not
3009 // generate a bounds check if it is a valid range.
3010 }
3011 return;
3012 }
3013 // Only the index is constant: change the order of the operands and commute the condition
3014 // so we can use an immediate constant for the index (only the second input to a cmp
3015 // instruction can be an immediate).
3016 cmp_first_input = 1;
3017 cmp_second_input = 0;
3018 cond = ls;
3019 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003020 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003021 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Georgia Kouvelibe530852019-01-17 10:46:41 +00003022 __ Cmp(InputRegisterAt(instruction, cmp_first_input),
3023 InputOperandAt(instruction, cmp_second_input));
Alexandre Rames67555f72014-11-18 10:55:16 +00003024 codegen_->AddSlowPath(slow_path);
Georgia Kouvelibe530852019-01-17 10:46:41 +00003025 __ B(slow_path->GetEntryLabel(), cond);
Alexandre Rames67555f72014-11-18 10:55:16 +00003026}
3027
Alexandre Rames67555f72014-11-18 10:55:16 +00003028void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3029 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003030 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00003031 locations->SetInAt(0, Location::RequiresRegister());
3032 if (check->HasUses()) {
3033 locations->SetOut(Location::SameAsFirstInput());
3034 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01003035 // Rely on the type initialization to save everything we need.
3036 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Alexandre Rames67555f72014-11-18 10:55:16 +00003037}
3038
3039void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3040 // We assume the class is not null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01003041 SlowPathCodeARM64* slow_path =
3042 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(check->GetLoadClass(), check);
Alexandre Rames67555f72014-11-18 10:55:16 +00003043 codegen_->AddSlowPath(slow_path);
3044 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3045}
3046
Roland Levillain1a653882016-03-18 18:05:57 +00003047static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3048 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3049 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3050}
3051
3052void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01003053 VRegister lhs_reg = InputFPRegisterAt(instruction, 0);
Roland Levillain1a653882016-03-18 18:05:57 +00003054 Location rhs_loc = instruction->GetLocations()->InAt(1);
3055 if (rhs_loc.IsConstant()) {
3056 // 0.0 is the only immediate that can be encoded directly in
3057 // an FCMP instruction.
3058 //
3059 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3060 // specify that in a floating-point comparison, positive zero
3061 // and negative zero are considered equal, so we can use the
3062 // literal 0.0 for both cases here.
3063 //
3064 // Note however that some methods (Float.equal, Float.compare,
3065 // Float.compareTo, Double.equal, Double.compare,
3066 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3067 // StrictMath.min) consider 0.0 to be (strictly) greater than
3068 // -0.0. So if we ever translate calls to these methods into a
3069 // HCompare instruction, we must handle the -0.0 case with
3070 // care here.
3071 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3072 __ Fcmp(lhs_reg, 0.0);
3073 } else {
3074 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3075 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003076}
3077
Serban Constantinescu02164b32014-11-13 14:05:07 +00003078void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003079 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003080 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003081 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003082 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003083 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003084 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003085 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003086 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003087 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003088 case DataType::Type::kInt32:
3089 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003090 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003091 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003092 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3093 break;
3094 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003095 case DataType::Type::kFloat32:
3096 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003097 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003098 locations->SetInAt(1,
3099 IsFloatingPointZeroConstant(compare->InputAt(1))
3100 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3101 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003102 locations->SetOut(Location::RequiresRegister());
3103 break;
3104 }
3105 default:
3106 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3107 }
3108}
3109
3110void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003111 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00003112
3113 // 0 if: left == right
3114 // 1 if: left > right
3115 // -1 if: left < right
3116 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003117 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003118 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003119 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003120 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003121 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003122 case DataType::Type::kInt32:
3123 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003124 Register result = OutputRegister(compare);
3125 Register left = InputRegisterAt(compare, 0);
3126 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003127 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003128 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3129 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003130 break;
3131 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003132 case DataType::Type::kFloat32:
3133 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003134 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003135 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003136 __ Cset(result, ne);
3137 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003138 break;
3139 }
3140 default:
3141 LOG(FATAL) << "Unimplemented compare type " << in_type;
3142 }
3143}
3144
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003145void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003146 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003147
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003148 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003149 locations->SetInAt(0, Location::RequiresFpuRegister());
3150 locations->SetInAt(1,
3151 IsFloatingPointZeroConstant(instruction->InputAt(1))
3152 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3153 : Location::RequiresFpuRegister());
3154 } else {
3155 // Integer cases.
3156 locations->SetInAt(0, Location::RequiresRegister());
3157 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3158 }
3159
David Brazdilb3e773e2016-01-26 11:28:37 +00003160 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003161 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003162 }
3163}
3164
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003165void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003166 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003167 return;
3168 }
3169
3170 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003171 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003172 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003173
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003174 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003175 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003176 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003177 } else {
3178 // Integer cases.
3179 Register lhs = InputRegisterAt(instruction, 0);
3180 Operand rhs = InputOperandAt(instruction, 1);
3181 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003182 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003183 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003184}
3185
3186#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3187 M(Equal) \
3188 M(NotEqual) \
3189 M(LessThan) \
3190 M(LessThanOrEqual) \
3191 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003192 M(GreaterThanOrEqual) \
3193 M(Below) \
3194 M(BelowOrEqual) \
3195 M(Above) \
3196 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003197#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003198void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3199void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003200FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003201#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003202#undef FOR_EACH_CONDITION_INSTRUCTION
3203
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003204void InstructionCodeGeneratorARM64::GenerateIntDivForPower2Denom(HDiv* instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003205 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003206 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003207 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
3208
3209 Register out = OutputRegister(instruction);
3210 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003211
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003212 Register final_dividend;
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003213 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003214 // No need to adjust the result for non-negative dividends or the INT32_MIN/INT64_MIN dividends.
3215 // NOTE: The generated code for HDiv correctly works for the INT32_MIN/INT64_MIN dividends:
3216 // imm == 2
3217 // add out, dividend(0x80000000), dividend(0x80000000), lsr #31 => out = 0x80000001
3218 // asr out, out(0x80000001), #1 => out = 0xc0000000
3219 // This is the same as 'asr out, 0x80000000, #1'
3220 //
3221 // imm > 2
3222 // add temp, dividend(0x80000000), imm - 1 => temp = 0b10..01..1, where the number
3223 // of the rightmost 1s is ctz_imm.
3224 // cmp dividend(0x80000000), 0 => N = 1, V = 0 (lt is true)
3225 // csel out, temp(0b10..01..1), dividend(0x80000000), lt => out = 0b10..01..1
3226 // asr out, out(0b10..01..1), #ctz_imm => out = 0b1..10..0, where the number of the
3227 // leftmost 1s is ctz_imm + 1.
3228 // This is the same as 'asr out, dividend(0x80000000), #ctz_imm'.
3229 //
3230 // imm == INT32_MIN
3231 // add tmp, dividend(0x80000000), #0x7fffffff => tmp = -1
3232 // cmp dividend(0x80000000), 0 => N = 1, V = 0 (lt is true)
3233 // csel out, temp(-1), dividend(0x80000000), lt => out = -1
3234 // neg out, out(-1), asr #31 => out = 1
3235 // This is the same as 'neg out, dividend(0x80000000), asr #31'.
3236 final_dividend = dividend;
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003237 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003238 if (abs_imm == 2) {
3239 int bits = DataType::Size(instruction->GetResultType()) * kBitsPerByte;
3240 __ Add(out, dividend, Operand(dividend, LSR, bits - 1));
3241 } else {
3242 UseScratchRegisterScope temps(GetVIXLAssembler());
3243 Register temp = temps.AcquireSameSizeAs(out);
3244 __ Add(temp, dividend, abs_imm - 1);
3245 __ Cmp(dividend, 0);
3246 __ Csel(out, temp, dividend, lt);
3247 }
3248 final_dividend = out;
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003249 }
3250
Zheng Xuc6667102015-05-15 16:08:45 +08003251 int ctz_imm = CTZ(abs_imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003252 if (imm > 0) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003253 __ Asr(out, final_dividend, ctz_imm);
Zheng Xuc6667102015-05-15 16:08:45 +08003254 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003255 __ Neg(out, Operand(final_dividend, ASR, ctz_imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003256 }
3257}
3258
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003259// Return true if the magic number was modified by subtracting 2^32(Int32 div) or 2^64(Int64 div).
3260// So dividend needs to be added.
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003261static inline bool NeedToAddDividend(int64_t magic_number, int64_t divisor) {
3262 return divisor > 0 && magic_number < 0;
3263}
3264
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003265// Return true if the magic number was modified by adding 2^32(Int32 div) or 2^64(Int64 div).
3266// So dividend needs to be subtracted.
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003267static inline bool NeedToSubDividend(int64_t magic_number, int64_t divisor) {
3268 return divisor < 0 && magic_number > 0;
3269}
3270
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003271// Generate code which increments the value in register 'in' by 1 if the value is negative.
3272// It is done with 'add out, in, in, lsr #31 or #63'.
3273// If the value is a result of an operation setting the N flag, CINC MI can be used
3274// instead of ADD. 'use_cond_inc' controls this.
3275void InstructionCodeGeneratorARM64::GenerateIncrementNegativeByOne(
3276 Register out,
3277 Register in,
3278 bool use_cond_inc) {
3279 if (use_cond_inc) {
3280 __ Cinc(out, in, mi);
3281 } else {
3282 __ Add(out, in, Operand(in, LSR, in.GetSizeInBits() - 1));
3283 }
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003284}
3285
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003286// Helper to generate code producing the result of HRem with a constant divisor.
3287void InstructionCodeGeneratorARM64::GenerateResultRemWithAnyConstant(
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003288 Register out,
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003289 Register dividend,
3290 Register quotient,
3291 int64_t divisor,
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003292 UseScratchRegisterScope* temps_scope) {
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003293 Register temp_imm = temps_scope->AcquireSameSizeAs(out);
3294 __ Mov(temp_imm, divisor);
3295 __ Msub(out, quotient, temp_imm, dividend);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003296}
3297
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003298// Helper to generate code for HDiv/HRem instructions when a dividend is non-negative and
3299// a divisor is a positive constant, not power of 2.
3300void InstructionCodeGeneratorARM64::GenerateInt64UnsignedDivRemWithAnyPositiveConstant(
3301 HBinaryOperation* instruction) {
3302 DCHECK(instruction->IsDiv() || instruction->IsRem());
3303 DCHECK(instruction->GetResultType() == DataType::Type::kInt64);
3304
3305 LocationSummary* locations = instruction->GetLocations();
3306 Location second = locations->InAt(1);
3307 DCHECK(second.IsConstant());
3308
3309 Register out = OutputRegister(instruction);
3310 Register dividend = InputRegisterAt(instruction, 0);
3311 int64_t imm = Int64FromConstant(second.GetConstant());
3312 DCHECK_GT(imm, 0);
3313
3314 int64_t magic;
3315 int shift;
3316 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ true, &magic, &shift);
3317
3318 UseScratchRegisterScope temps(GetVIXLAssembler());
3319 Register temp = temps.AcquireSameSizeAs(out);
3320
3321 auto generate_unsigned_div_code = [this, magic, shift](Register out,
3322 Register dividend,
3323 Register temp) {
3324 // temp = get_high(dividend * magic)
3325 __ Mov(temp, magic);
3326 if (magic > 0 && shift == 0) {
3327 __ Smulh(out, dividend, temp);
3328 } else {
3329 __ Smulh(temp, dividend, temp);
3330 if (magic < 0) {
3331 // The negative magic means that the multiplier m is greater than INT64_MAX.
3332 // In such a case shift is never 0. See the proof in
3333 // InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant.
3334 __ Add(temp, temp, dividend);
3335 }
3336 DCHECK_NE(shift, 0);
3337 __ Lsr(out, temp, shift);
3338 }
3339 };
3340
3341 if (instruction->IsDiv()) {
3342 generate_unsigned_div_code(out, dividend, temp);
3343 } else {
3344 generate_unsigned_div_code(temp, dividend, temp);
3345 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3346 }
3347}
3348
3349// Helper to generate code for HDiv/HRem instructions for any dividend and a constant divisor
3350// (not power of 2).
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003351void InstructionCodeGeneratorARM64::GenerateInt64DivRemWithAnyConstant(
3352 HBinaryOperation* instruction) {
Zheng Xuc6667102015-05-15 16:08:45 +08003353 DCHECK(instruction->IsDiv() || instruction->IsRem());
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003354 DCHECK(instruction->GetResultType() == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003355
3356 LocationSummary* locations = instruction->GetLocations();
3357 Location second = locations->InAt(1);
3358 DCHECK(second.IsConstant());
3359
3360 Register out = OutputRegister(instruction);
3361 Register dividend = InputRegisterAt(instruction, 0);
3362 int64_t imm = Int64FromConstant(second.GetConstant());
3363
Zheng Xuc6667102015-05-15 16:08:45 +08003364 int64_t magic;
3365 int shift;
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003366 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ true, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08003367
3368 UseScratchRegisterScope temps(GetVIXLAssembler());
3369 Register temp = temps.AcquireSameSizeAs(out);
3370
3371 // temp = get_high(dividend * magic)
3372 __ Mov(temp, magic);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003373 __ Smulh(temp, dividend, temp);
3374
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003375 // The multiplication result might need some corrections to be finalized.
3376 // The last correction is to increment by 1, if the result is negative.
3377 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3378 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3379 // However if one of the corrections is ADD or SUB, the sign can be detected
3380 // with ADDS/SUBS. They set the N flag if the result is negative.
3381 // This allows to use CINC MI which has latency 1.
3382 bool use_cond_inc = false;
3383
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003384 // Some combinations of magic_number and the divisor require to correct the result.
3385 // Check whether the correction is needed.
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003386 if (NeedToAddDividend(magic, imm)) {
3387 __ Adds(temp, temp, dividend);
3388 use_cond_inc = true;
3389 } else if (NeedToSubDividend(magic, imm)) {
3390 __ Subs(temp, temp, dividend);
3391 use_cond_inc = true;
3392 }
3393
3394 if (shift != 0) {
3395 __ Asr(temp, temp, shift);
3396 }
3397
3398 if (instruction->IsRem()) {
3399 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3400 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3401 } else {
3402 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3403 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003404}
3405
3406void InstructionCodeGeneratorARM64::GenerateInt32DivRemWithAnyConstant(
3407 HBinaryOperation* instruction) {
3408 DCHECK(instruction->IsDiv() || instruction->IsRem());
3409 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
3410
3411 LocationSummary* locations = instruction->GetLocations();
3412 Location second = locations->InAt(1);
3413 DCHECK(second.IsConstant());
3414
3415 Register out = OutputRegister(instruction);
3416 Register dividend = InputRegisterAt(instruction, 0);
3417 int64_t imm = Int64FromConstant(second.GetConstant());
3418
3419 int64_t magic;
3420 int shift;
3421 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
3422 UseScratchRegisterScope temps(GetVIXLAssembler());
3423 Register temp = temps.AcquireSameSizeAs(out);
3424
3425 // temp = get_high(dividend * magic)
3426 __ Mov(temp, magic);
3427 __ Smull(temp.X(), dividend, temp);
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003428
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003429 // The multiplication result might need some corrections to be finalized.
3430 // The last correction is to increment by 1, if the result is negative.
3431 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3432 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3433 // However if one of the corrections is ADD or SUB, the sign can be detected
3434 // with ADDS/SUBS. They set the N flag if the result is negative.
3435 // This allows to use CINC MI which has latency 1.
3436 bool use_cond_inc = false;
3437
3438 // ADD/SUB correction is performed in the high 32 bits
3439 // as high 32 bits are ignored because type are kInt32.
3440 if (NeedToAddDividend(magic, imm)) {
3441 __ Adds(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3442 use_cond_inc = true;
3443 } else if (NeedToSubDividend(magic, imm)) {
3444 __ Subs(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3445 use_cond_inc = true;
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003446 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003447
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003448 // Extract the result from the high 32 bits and apply the final right shift.
3449 DCHECK_LT(shift, 32);
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003450 if (imm > 0 && HasNonNegativeInputAt(instruction, 0)) {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01003451 // No need to adjust the result for a non-negative dividend and a positive divisor.
3452 if (instruction->IsDiv()) {
3453 __ Lsr(out.X(), temp.X(), 32 + shift);
3454 } else {
3455 __ Lsr(temp.X(), temp.X(), 32 + shift);
3456 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3457 }
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003458 } else {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01003459 __ Asr(temp.X(), temp.X(), 32 + shift);
3460
3461 if (instruction->IsRem()) {
3462 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3463 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3464 } else {
3465 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3466 }
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003467 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003468}
3469
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003470void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction,
3471 int64_t divisor) {
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003472 DCHECK(instruction->IsDiv() || instruction->IsRem());
3473 if (instruction->GetResultType() == DataType::Type::kInt64) {
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003474 if (divisor > 0 && HasNonNegativeInputAt(instruction, 0)) {
3475 GenerateInt64UnsignedDivRemWithAnyPositiveConstant(instruction);
3476 } else {
3477 GenerateInt64DivRemWithAnyConstant(instruction);
3478 }
Zheng Xuc6667102015-05-15 16:08:45 +08003479 } else {
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003480 GenerateInt32DivRemWithAnyConstant(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003481 }
3482}
3483
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003484void InstructionCodeGeneratorARM64::GenerateIntDivForConstDenom(HDiv *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003485 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Zheng Xuc6667102015-05-15 16:08:45 +08003486
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003487 if (imm == 0) {
3488 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3489 return;
3490 }
Zheng Xuc6667102015-05-15 16:08:45 +08003491
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003492 if (IsPowerOfTwo(AbsOrMin(imm))) {
3493 GenerateIntDivForPower2Denom(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003494 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003495 // Cases imm == -1 or imm == 1 are handled by InstructionSimplifier.
3496 DCHECK(imm < -2 || imm > 2) << imm;
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003497 GenerateDivRemWithAnyConstant(instruction, imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003498 }
3499}
3500
3501void InstructionCodeGeneratorARM64::GenerateIntDiv(HDiv *instruction) {
3502 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
3503 << instruction->GetResultType();
3504
3505 if (instruction->GetLocations()->InAt(1).IsConstant()) {
3506 GenerateIntDivForConstDenom(instruction);
3507 } else {
3508 Register out = OutputRegister(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003509 Register dividend = InputRegisterAt(instruction, 0);
3510 Register divisor = InputRegisterAt(instruction, 1);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003511 __ Sdiv(out, dividend, divisor);
Zheng Xuc6667102015-05-15 16:08:45 +08003512 }
3513}
3514
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003515void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3516 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003517 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003518 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003519 case DataType::Type::kInt32:
3520 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003521 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003522 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003523 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3524 break;
3525
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003526 case DataType::Type::kFloat32:
3527 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003528 locations->SetInAt(0, Location::RequiresFpuRegister());
3529 locations->SetInAt(1, Location::RequiresFpuRegister());
3530 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3531 break;
3532
3533 default:
3534 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3535 }
3536}
3537
3538void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003539 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003540 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003541 case DataType::Type::kInt32:
3542 case DataType::Type::kInt64:
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003543 GenerateIntDiv(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003544 break;
3545
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003546 case DataType::Type::kFloat32:
3547 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003548 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3549 break;
3550
3551 default:
3552 LOG(FATAL) << "Unexpected div type " << type;
3553 }
3554}
3555
Alexandre Rames67555f72014-11-18 10:55:16 +00003556void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003557 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003558 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003559}
3560
3561void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3562 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003563 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003564 codegen_->AddSlowPath(slow_path);
3565 Location value = instruction->GetLocations()->InAt(0);
3566
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003567 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003568
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003569 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003570 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Elliott Hughesc1896c92018-11-29 11:33:18 -08003571 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003572 }
3573
Alexandre Rames67555f72014-11-18 10:55:16 +00003574 if (value.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003575 int64_t divisor = Int64FromLocation(value);
Alexandre Rames67555f72014-11-18 10:55:16 +00003576 if (divisor == 0) {
3577 __ B(slow_path->GetEntryLabel());
3578 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003579 // A division by a non-null constant is valid. We don't need to perform
3580 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003581 }
3582 } else {
3583 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3584 }
3585}
3586
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003587void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3588 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003589 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003590 locations->SetOut(Location::ConstantLocation(constant));
3591}
3592
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003593void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3594 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003595 // Will be generated at use site.
3596}
3597
Alexandre Rames5319def2014-10-23 10:03:10 +01003598void LocationsBuilderARM64::VisitExit(HExit* exit) {
3599 exit->SetLocations(nullptr);
3600}
3601
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003602void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003603}
3604
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003605void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3606 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003607 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003608 locations->SetOut(Location::ConstantLocation(constant));
3609}
3610
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003611void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003612 // Will be generated at use site.
3613}
3614
David Brazdilfc6a86a2015-06-26 10:33:45 +00003615void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003616 if (successor->IsExitBlock()) {
3617 DCHECK(got->GetPrevious()->AlwaysThrows());
3618 return; // no code needed
3619 }
3620
Serban Constantinescu02164b32014-11-13 14:05:07 +00003621 HBasicBlock* block = got->GetBlock();
3622 HInstruction* previous = got->GetPrevious();
3623 HLoopInformation* info = block->GetLoopInformation();
3624
David Brazdil46e2a392015-03-16 17:31:52 +00003625 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00003626 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003627 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
Vladimir Markoe45883e2022-01-11 12:38:35 +00003628 return; // `GenerateSuspendCheck()` emitted the jump.
Serban Constantinescu02164b32014-11-13 14:05:07 +00003629 }
3630 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3631 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08003632 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003633 }
3634 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003635 __ B(codegen_->GetLabelOf(successor));
3636 }
3637}
3638
David Brazdilfc6a86a2015-06-26 10:33:45 +00003639void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3640 got->SetLocations(nullptr);
3641}
3642
3643void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3644 HandleGoto(got, got->GetSuccessor());
3645}
3646
3647void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3648 try_boundary->SetLocations(nullptr);
3649}
3650
3651void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3652 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3653 if (!successor->IsExitBlock()) {
3654 HandleGoto(try_boundary, successor);
3655 }
3656}
3657
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003658void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003659 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003660 vixl::aarch64::Label* true_target,
3661 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003662 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003663
David Brazdil0debae72015-11-12 18:37:00 +00003664 if (true_target == nullptr && false_target == nullptr) {
3665 // Nothing to do. The code always falls through.
3666 return;
3667 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003668 // Constant condition, statically compared against "true" (integer value 1).
3669 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003670 if (true_target != nullptr) {
3671 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003672 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003673 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003674 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003675 if (false_target != nullptr) {
3676 __ B(false_target);
3677 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003678 }
David Brazdil0debae72015-11-12 18:37:00 +00003679 return;
3680 }
3681
3682 // The following code generates these patterns:
3683 // (1) true_target == nullptr && false_target != nullptr
3684 // - opposite condition true => branch to false_target
3685 // (2) true_target != nullptr && false_target == nullptr
3686 // - condition true => branch to true_target
3687 // (3) true_target != nullptr && false_target != nullptr
3688 // - condition true => branch to true_target
3689 // - branch to false_target
3690 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003691 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003692 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003693 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003694 if (true_target == nullptr) {
3695 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3696 } else {
3697 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3698 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003699 } else {
3700 // The condition instruction has not been materialized, use its inputs as
3701 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003702 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003703
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003704 DataType::Type type = condition->InputAt(0)->GetType();
3705 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003706 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003707 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003708 IfCondition opposite_condition = condition->GetOppositeCondition();
3709 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003710 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003711 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003712 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003713 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003714 // Integer cases.
3715 Register lhs = InputRegisterAt(condition, 0);
3716 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003717
3718 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003719 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003720 if (true_target == nullptr) {
3721 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3722 non_fallthrough_target = false_target;
3723 } else {
3724 arm64_cond = ARM64Condition(condition->GetCondition());
3725 non_fallthrough_target = true_target;
3726 }
3727
Aart Bik086d27e2016-01-20 17:02:00 -08003728 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003729 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003730 switch (arm64_cond) {
3731 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003732 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003733 break;
3734 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003735 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003736 break;
3737 case lt:
3738 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003739 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003740 break;
3741 case ge:
3742 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003743 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003744 break;
3745 default:
3746 // Without the `static_cast` the compiler throws an error for
3747 // `-Werror=sign-promo`.
3748 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3749 }
3750 } else {
3751 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003752 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003753 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003754 }
3755 }
David Brazdil0debae72015-11-12 18:37:00 +00003756
3757 // If neither branch falls through (case 3), the conditional branch to `true_target`
3758 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3759 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003760 __ B(false_target);
3761 }
3762}
3763
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003764void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003765 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003766 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003767 locations->SetInAt(0, Location::RequiresRegister());
3768 }
3769}
3770
3771void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003772 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3773 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003774 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3775 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3776 true_target = nullptr;
3777 }
3778 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3779 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3780 false_target = nullptr;
3781 }
Andreas Gampe3db70682018-12-26 15:12:03 -08003782 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003783}
3784
3785void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003786 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003787 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003788 InvokeRuntimeCallingConvention calling_convention;
3789 RegisterSet caller_saves = RegisterSet::Empty();
3790 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3791 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003792 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003793 locations->SetInAt(0, Location::RequiresRegister());
3794 }
3795}
3796
3797void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003798 SlowPathCodeARM64* slow_path =
3799 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003800 GenerateTestAndBranch(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08003801 /* condition_input_index= */ 0,
David Brazdil0debae72015-11-12 18:37:00 +00003802 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08003803 /* false_target= */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003804}
3805
Mingyao Yang063fc772016-08-02 11:02:54 -07003806void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003807 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003808 LocationSummary(flag, LocationSummary::kNoCall);
3809 locations->SetOut(Location::RequiresRegister());
3810}
3811
3812void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3813 __ Ldr(OutputRegister(flag),
3814 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3815}
3816
David Brazdilc0b601b2016-02-08 14:20:45 +00003817static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3818 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003819 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003820}
3821
Alexandre Rames880f1192016-06-13 16:04:50 +01003822static inline Condition GetConditionForSelect(HCondition* condition) {
3823 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003824 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3825 : ARM64Condition(cond);
3826}
3827
David Brazdil74eb1b22015-12-14 11:44:01 +00003828void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003829 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003830 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003831 locations->SetInAt(0, Location::RequiresFpuRegister());
3832 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003833 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003834 } else {
3835 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3836 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3837 bool is_true_value_constant = cst_true_value != nullptr;
3838 bool is_false_value_constant = cst_false_value != nullptr;
3839 // Ask VIXL whether we should synthesize constants in registers.
3840 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3841 Operand true_op = is_true_value_constant ?
3842 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3843 Operand false_op = is_false_value_constant ?
3844 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3845 bool true_value_in_register = false;
3846 bool false_value_in_register = false;
3847 MacroAssembler::GetCselSynthesisInformation(
3848 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3849 true_value_in_register |= !is_true_value_constant;
3850 false_value_in_register |= !is_false_value_constant;
3851
3852 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3853 : Location::ConstantLocation(cst_true_value));
3854 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3855 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003856 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003857 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003858
David Brazdil74eb1b22015-12-14 11:44:01 +00003859 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3860 locations->SetInAt(2, Location::RequiresRegister());
3861 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003862}
3863
3864void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003865 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003866 Condition csel_cond;
3867
3868 if (IsBooleanValueOrMaterializedCondition(cond)) {
3869 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003870 // Use the condition flags set by the previous instruction.
3871 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003872 } else {
3873 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003874 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003875 }
3876 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003877 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003878 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003879 } else {
3880 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003881 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003882 }
3883
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003884 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003885 __ Fcsel(OutputFPRegister(select),
3886 InputFPRegisterAt(select, 1),
3887 InputFPRegisterAt(select, 0),
3888 csel_cond);
3889 } else {
3890 __ Csel(OutputRegister(select),
3891 InputOperandAt(select, 1),
3892 InputOperandAt(select, 0),
3893 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003894 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003895}
3896
Santiago Aboy Solanescaf9b652022-06-24 10:03:30 +01003897void LocationsBuilderARM64::VisitNop(HNop* nop) {
3898 new (GetGraph()->GetAllocator()) LocationSummary(nop);
David Srbecky0cf44932015-12-09 14:09:59 +00003899}
3900
Santiago Aboy Solanescaf9b652022-06-24 10:03:30 +01003901void InstructionCodeGeneratorARM64::VisitNop(HNop*) {
3902 // The environment recording already happened in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003903}
3904
Vladimir Markodec78172020-06-19 15:31:23 +01003905void CodeGeneratorARM64::IncreaseFrame(size_t adjustment) {
3906 __ Claim(adjustment);
3907 GetAssembler()->cfi().AdjustCFAOffset(adjustment);
3908}
3909
3910void CodeGeneratorARM64::DecreaseFrame(size_t adjustment) {
3911 __ Drop(adjustment);
3912 GetAssembler()->cfi().AdjustCFAOffset(-adjustment);
3913}
3914
David Srbeckyc7098ff2016-02-09 14:30:11 +00003915void CodeGeneratorARM64::GenerateNop() {
3916 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003917}
3918
Alex Light3a73ffb2021-01-25 14:11:05 +00003919void LocationsBuilderARM64::VisitPredicatedInstanceFieldGet(
3920 HPredicatedInstanceFieldGet* instruction) {
3921 HandleFieldGet(instruction, instruction->GetFieldInfo());
3922}
3923
Alexandre Rames5319def2014-10-23 10:03:10 +01003924void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003925 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003926}
3927
Alex Light3a73ffb2021-01-25 14:11:05 +00003928void InstructionCodeGeneratorARM64::VisitPredicatedInstanceFieldGet(
3929 HPredicatedInstanceFieldGet* instruction) {
3930 vixl::aarch64::Label finish;
3931 __ Cbz(InputRegisterAt(instruction, 1), &finish);
3932 HandleFieldGet(instruction, instruction->GetFieldInfo());
3933 __ Bind(&finish);
3934}
3935
Alexandre Rames5319def2014-10-23 10:03:10 +01003936void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003937 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003938}
3939
3940void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003941 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003942}
3943
3944void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003945 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003946}
3947
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003948// Temp is used for read barrier.
3949static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00003950 if (gUseReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003951 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003952 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3953 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3954 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3955 return 1;
3956 }
3957 return 0;
3958}
3959
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003960// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003961// interface pointer, one for loading the current interface.
3962// The other checks have one temp for loading the object's class.
3963static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3964 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3965 return 3;
3966 }
3967 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003968}
3969
Alexandre Rames67555f72014-11-18 10:55:16 +00003970void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003971 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003972 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003973 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003974 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003975 case TypeCheckKind::kExactCheck:
3976 case TypeCheckKind::kAbstractClassCheck:
3977 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00003978 case TypeCheckKind::kArrayObjectCheck: {
3979 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
3980 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
3981 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003982 break;
Vladimir Marko87584542017-12-12 17:47:52 +00003983 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003984 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003985 case TypeCheckKind::kUnresolvedCheck:
3986 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003987 call_kind = LocationSummary::kCallOnSlowPath;
3988 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00003989 case TypeCheckKind::kBitstringCheck:
3990 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003991 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003992
Vladimir Markoca6fff82017-10-03 14:49:14 +01003993 LocationSummary* locations =
3994 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003995 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003996 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003997 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003998 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003999 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
4000 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
4001 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
4002 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
4003 } else {
4004 locations->SetInAt(1, Location::RequiresRegister());
4005 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004006 // The "out" register is used as a temporary, so it overlaps with the inputs.
4007 // Note that TypeCheckSlowPathARM64 uses this register too.
4008 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004009 // Add temps if necessary for read barriers.
4010 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00004011}
4012
4013void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004014 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00004015 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004016 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004017 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00004018 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
4019 ? Register()
4020 : InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004021 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00004022 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004023 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
4024 DCHECK_LE(num_temps, 1u);
4025 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004026 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4027 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4028 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4029 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00004030
Scott Wakeling97c72b72016-06-24 16:19:36 +01004031 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004032 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004033
4034 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004035 // Avoid null check if we know `obj` is not null.
4036 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004037 __ Cbz(obj, &zero);
4038 }
4039
Roland Levillain44015862016-01-22 11:47:17 +00004040 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004041 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004042 ReadBarrierOption read_barrier_option =
4043 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004044 // /* HeapReference<Class> */ out = obj->klass_
4045 GenerateReferenceLoadTwoRegisters(instruction,
4046 out_loc,
4047 obj_loc,
4048 class_offset,
4049 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004050 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004051 __ Cmp(out, cls);
4052 __ Cset(out, eq);
4053 if (zero.IsLinked()) {
4054 __ B(&done);
4055 }
4056 break;
4057 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004058
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004059 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004060 ReadBarrierOption read_barrier_option =
4061 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004062 // /* HeapReference<Class> */ out = obj->klass_
4063 GenerateReferenceLoadTwoRegisters(instruction,
4064 out_loc,
4065 obj_loc,
4066 class_offset,
4067 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004068 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004069 // If the class is abstract, we eagerly fetch the super class of the
4070 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004071 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004072 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004073 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004074 GenerateReferenceLoadOneRegister(instruction,
4075 out_loc,
4076 super_offset,
4077 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004078 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004079 // If `out` is null, we use it for the result, and jump to `done`.
4080 __ Cbz(out, &done);
4081 __ Cmp(out, cls);
4082 __ B(ne, &loop);
4083 __ Mov(out, 1);
4084 if (zero.IsLinked()) {
4085 __ B(&done);
4086 }
4087 break;
4088 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004089
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004090 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004091 ReadBarrierOption read_barrier_option =
4092 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004093 // /* HeapReference<Class> */ out = obj->klass_
4094 GenerateReferenceLoadTwoRegisters(instruction,
4095 out_loc,
4096 obj_loc,
4097 class_offset,
4098 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004099 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004100 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004101 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004102 __ Bind(&loop);
4103 __ Cmp(out, cls);
4104 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004105 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004106 GenerateReferenceLoadOneRegister(instruction,
4107 out_loc,
4108 super_offset,
4109 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004110 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004111 __ Cbnz(out, &loop);
4112 // If `out` is null, we use it for the result, and jump to `done`.
4113 __ B(&done);
4114 __ Bind(&success);
4115 __ Mov(out, 1);
4116 if (zero.IsLinked()) {
4117 __ B(&done);
4118 }
4119 break;
4120 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004121
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004122 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004123 ReadBarrierOption read_barrier_option =
4124 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004125 // /* HeapReference<Class> */ out = obj->klass_
4126 GenerateReferenceLoadTwoRegisters(instruction,
4127 out_loc,
4128 obj_loc,
4129 class_offset,
4130 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004131 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004132 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004133 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004134 __ Cmp(out, cls);
4135 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004136 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004137 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004138 GenerateReferenceLoadOneRegister(instruction,
4139 out_loc,
4140 component_offset,
4141 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004142 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004143 // If `out` is null, we use it for the result, and jump to `done`.
4144 __ Cbz(out, &done);
4145 __ Ldrh(out, HeapOperand(out, primitive_offset));
4146 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4147 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004148 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004149 __ Mov(out, 1);
4150 __ B(&done);
4151 break;
4152 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004153
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004154 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004155 // No read barrier since the slow path will retry upon failure.
4156 // /* HeapReference<Class> */ out = obj->klass_
4157 GenerateReferenceLoadTwoRegisters(instruction,
4158 out_loc,
4159 obj_loc,
4160 class_offset,
4161 maybe_temp_loc,
4162 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004163 __ Cmp(out, cls);
4164 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004165 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08004166 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004167 codegen_->AddSlowPath(slow_path);
4168 __ B(ne, slow_path->GetEntryLabel());
4169 __ Mov(out, 1);
4170 if (zero.IsLinked()) {
4171 __ B(&done);
4172 }
4173 break;
4174 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004175
Calin Juravle98893e12015-10-02 21:05:03 +01004176 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004177 case TypeCheckKind::kInterfaceCheck: {
4178 // Note that we indeed only call on slow path, but we always go
4179 // into the slow path for the unresolved and interface check
4180 // cases.
4181 //
4182 // We cannot directly call the InstanceofNonTrivial runtime
4183 // entry point without resorting to a type checking slow path
4184 // here (i.e. by calling InvokeRuntime directly), as it would
4185 // require to assign fixed registers for the inputs of this
4186 // HInstanceOf instruction (following the runtime calling
4187 // convention), which might be cluttered by the potential first
4188 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004189 //
4190 // TODO: Introduce a new runtime entry point taking the object
4191 // to test (instead of its class) as argument, and let it deal
4192 // with the read barrier issues. This will let us refactor this
4193 // case of the `switch` code as it was previously (with a direct
4194 // call to the runtime not using a type checking slow path).
4195 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004196 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004197 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08004198 instruction, /* is_fatal= */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004199 codegen_->AddSlowPath(slow_path);
4200 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004201 if (zero.IsLinked()) {
4202 __ B(&done);
4203 }
4204 break;
4205 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004206
4207 case TypeCheckKind::kBitstringCheck: {
4208 // /* HeapReference<Class> */ temp = obj->klass_
4209 GenerateReferenceLoadTwoRegisters(instruction,
4210 out_loc,
4211 obj_loc,
4212 class_offset,
4213 maybe_temp_loc,
4214 kWithoutReadBarrier);
4215
4216 GenerateBitstringTypeCheckCompare(instruction, out);
4217 __ Cset(out, eq);
4218 if (zero.IsLinked()) {
4219 __ B(&done);
4220 }
4221 break;
4222 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004223 }
4224
4225 if (zero.IsLinked()) {
4226 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004227 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004228 }
4229
4230 if (done.IsLinked()) {
4231 __ Bind(&done);
4232 }
4233
4234 if (slow_path != nullptr) {
4235 __ Bind(slow_path->GetExitLabel());
4236 }
4237}
4238
4239void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004240 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00004241 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004242 LocationSummary* locations =
4243 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004244 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00004245 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
4246 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
4247 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
4248 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
4249 } else {
4250 locations->SetInAt(1, Location::RequiresRegister());
4251 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004252 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4253 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004254}
4255
4256void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004257 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004258 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004259 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004260 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00004261 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
4262 ? Register()
4263 : InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004264 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4265 DCHECK_GE(num_temps, 1u);
4266 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004267 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004268 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4269 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004270 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004271 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4272 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4273 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4274 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4275 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4276 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4277 const uint32_t object_array_data_offset =
4278 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004279
Vladimir Marko87584542017-12-12 17:47:52 +00004280 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004281 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004282 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4283 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004284 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004285
Scott Wakeling97c72b72016-06-24 16:19:36 +01004286 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004287 // Avoid null check if we know obj is not null.
4288 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004289 __ Cbz(obj, &done);
4290 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004291
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004292 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004293 case TypeCheckKind::kExactCheck:
4294 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004295 // /* HeapReference<Class> */ temp = obj->klass_
4296 GenerateReferenceLoadTwoRegisters(instruction,
4297 temp_loc,
4298 obj_loc,
4299 class_offset,
4300 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004301 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004302
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004303 __ Cmp(temp, cls);
4304 // Jump to slow path for throwing the exception or doing a
4305 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004306 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004307 break;
4308 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004309
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004310 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004311 // /* HeapReference<Class> */ temp = obj->klass_
4312 GenerateReferenceLoadTwoRegisters(instruction,
4313 temp_loc,
4314 obj_loc,
4315 class_offset,
4316 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004317 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004318
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004319 // If the class is abstract, we eagerly fetch the super class of the
4320 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004321 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004322 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004323 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004324 GenerateReferenceLoadOneRegister(instruction,
4325 temp_loc,
4326 super_offset,
4327 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004328 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004329
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004330 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4331 // exception.
4332 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4333 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004334 __ Cmp(temp, cls);
4335 __ B(ne, &loop);
4336 break;
4337 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004338
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004339 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004340 // /* HeapReference<Class> */ temp = obj->klass_
4341 GenerateReferenceLoadTwoRegisters(instruction,
4342 temp_loc,
4343 obj_loc,
4344 class_offset,
4345 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004346 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004347
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004348 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004349 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004350 __ Bind(&loop);
4351 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004352 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004353
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004354 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004355 GenerateReferenceLoadOneRegister(instruction,
4356 temp_loc,
4357 super_offset,
4358 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004359 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004360
4361 // If the class reference currently in `temp` is not null, jump
4362 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004363 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004364 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004365 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004366 break;
4367 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004368
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004369 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004370 // /* HeapReference<Class> */ temp = obj->klass_
4371 GenerateReferenceLoadTwoRegisters(instruction,
4372 temp_loc,
4373 obj_loc,
4374 class_offset,
4375 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004376 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004377
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004378 // Do an exact check.
4379 __ Cmp(temp, cls);
4380 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004381
4382 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004383 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004384 GenerateReferenceLoadOneRegister(instruction,
4385 temp_loc,
4386 component_offset,
4387 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004388 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004389
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004390 // If the component type is null, jump to the slow path to throw the exception.
4391 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4392 // Otherwise, the object is indeed an array. Further check that this component type is not a
4393 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004394 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4395 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004396 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004397 break;
4398 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004399
Calin Juravle98893e12015-10-02 21:05:03 +01004400 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004401 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004402 //
4403 // We cannot directly call the CheckCast runtime entry point
4404 // without resorting to a type checking slow path here (i.e. by
4405 // calling InvokeRuntime directly), as it would require to
4406 // assign fixed registers for the inputs of this HInstanceOf
4407 // instruction (following the runtime calling convention), which
4408 // might be cluttered by the potential first read barrier
4409 // emission at the beginning of this method.
4410 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004411 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004412 case TypeCheckKind::kInterfaceCheck: {
4413 // /* HeapReference<Class> */ temp = obj->klass_
4414 GenerateReferenceLoadTwoRegisters(instruction,
4415 temp_loc,
4416 obj_loc,
4417 class_offset,
4418 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004419 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004420
4421 // /* HeapReference<Class> */ temp = temp->iftable_
4422 GenerateReferenceLoadTwoRegisters(instruction,
4423 temp_loc,
4424 temp_loc,
4425 iftable_offset,
4426 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004427 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004428 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004429 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004430 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004431 vixl::aarch64::Label start_loop;
4432 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004433 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004434 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4435 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004436 // Go to next interface.
4437 __ Add(temp, temp, 2 * kHeapReferenceSize);
4438 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004439 // Compare the classes and continue the loop if they do not match.
4440 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4441 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004442 break;
4443 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004444
4445 case TypeCheckKind::kBitstringCheck: {
4446 // /* HeapReference<Class> */ temp = obj->klass_
4447 GenerateReferenceLoadTwoRegisters(instruction,
4448 temp_loc,
4449 obj_loc,
4450 class_offset,
4451 maybe_temp2_loc,
4452 kWithoutReadBarrier);
4453
4454 GenerateBitstringTypeCheckCompare(instruction, temp);
4455 __ B(ne, type_check_slow_path->GetEntryLabel());
4456 break;
4457 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004458 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004459 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004460
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004461 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004462}
4463
Alexandre Rames5319def2014-10-23 10:03:10 +01004464void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004465 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004466 locations->SetOut(Location::ConstantLocation(constant));
4467}
4468
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004469void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004470 // Will be generated at use site.
4471}
4472
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004473void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004474 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004475 locations->SetOut(Location::ConstantLocation(constant));
4476}
4477
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004478void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004479 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004480}
4481
Calin Juravle175dc732015-08-25 15:42:32 +01004482void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4483 // The trampoline uses the same calling convention as dex calling conventions,
4484 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4485 // the method_idx.
4486 HandleInvoke(invoke);
4487}
4488
4489void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4490 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004491 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004492}
4493
Alexandre Rames5319def2014-10-23 10:03:10 +01004494void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004495 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004496 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004497}
4498
Alexandre Rames67555f72014-11-18 10:55:16 +00004499void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4500 HandleInvoke(invoke);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004501 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
4502 // We cannot request ip1 as it's blocked by the register allocator.
4503 invoke->GetLocations()->SetInAt(invoke->GetNumberOfArguments() - 1, Location::Any());
4504 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004505}
4506
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004507void CodeGeneratorARM64::MaybeGenerateInlineCacheCheck(HInstruction* instruction,
4508 Register klass) {
4509 DCHECK_EQ(klass.GetCode(), 0u);
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004510 // We know the destination of an intrinsic, so no need to record inline
4511 // caches.
4512 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00004513 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004514 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004515 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
Nicolas Geoffray9e598902021-11-19 14:53:07 +00004516 ProfilingInfo* info = GetGraph()->GetProfilingInfo();
4517 DCHECK(info != nullptr);
4518 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
4519 uint64_t address = reinterpret_cast64<uint64_t>(cache);
4520 vixl::aarch64::Label done;
4521 __ Mov(x8, address);
4522 __ Ldr(x9, MemOperand(x8, InlineCache::ClassesOffset().Int32Value()));
4523 // Fast path for a monomorphic cache.
4524 __ Cmp(klass, x9);
4525 __ B(eq, &done);
4526 InvokeRuntime(kQuickUpdateInlineCache, instruction, instruction->GetDexPc());
4527 __ Bind(&done);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004528 }
4529}
4530
Alexandre Rames67555f72014-11-18 10:55:16 +00004531void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4532 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004533 LocationSummary* locations = invoke->GetLocations();
4534 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004535 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004536 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004537 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004538
Artem Serov914d7a82017-02-07 14:33:49 +00004539 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004540 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004541 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004542 {
4543 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4544 // /* HeapReference<Class> */ temp = temp->klass_
4545 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4546 codegen_->MaybeRecordImplicitNullCheck(invoke);
4547 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004548 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004549 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004550 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004551 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004552 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004553 }
Artem Serov914d7a82017-02-07 14:33:49 +00004554
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004555 // Instead of simply (possibly) unpoisoning `temp` here, we should
4556 // emit a read barrier for the previous class reference load.
4557 // However this is not required in practice, as this is an
4558 // intermediate/temporary reference and because the current
4559 // concurrent copying collector keeps the from-space memory
4560 // intact/accessible until the end of the marking phase (the
4561 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004562 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004563
4564 // If we're compiling baseline, update the inline cache.
4565 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
4566
4567 // The register ip1 is required to be used for the hidden argument in
4568 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
4569 MacroAssembler* masm = GetVIXLAssembler();
4570 UseScratchRegisterScope scratch_scope(masm);
4571 scratch_scope.Exclude(ip1);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004572 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
4573 Location interface_method = locations->InAt(invoke->GetNumberOfArguments() - 1);
4574 if (interface_method.IsStackSlot()) {
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00004575 __ Ldr(ip1, StackOperandFrom(interface_method));
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004576 } else {
4577 __ Mov(ip1, XRegisterFrom(interface_method));
4578 }
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00004579 // If the load kind is through a runtime call, we will pass the method we
4580 // fetch the IMT, which will either be a no-op if we don't hit the conflict
4581 // stub, or will make us always go through the trampoline when there is a
4582 // conflict.
4583 } else if (invoke->GetHiddenArgumentLoadKind() != MethodLoadKind::kRuntimeCall) {
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004584 codegen_->LoadMethod(
4585 invoke->GetHiddenArgumentLoadKind(), Location::RegisterLocation(ip1.GetCode()), invoke);
4586 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004587
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004588 __ Ldr(temp,
4589 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4590 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004591 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004592 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004593 __ Ldr(temp, MemOperand(temp, method_offset));
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00004594 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRuntimeCall) {
4595 // We pass the method from the IMT in case of a conflict. This will ensure
4596 // we go into the runtime to resolve the actual method.
4597 __ Mov(ip1, temp);
4598 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004599 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004600 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004601
4602 {
4603 // Ensure the pc position is recorded immediately after the `blr` instruction.
4604 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4605
4606 // lr();
4607 __ blr(lr);
4608 DCHECK(!codegen_->IsLeafMethod());
4609 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4610 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004611
Andreas Gampe3db70682018-12-26 15:12:03 -08004612 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004613}
4614
4615void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004616 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004617 if (intrinsic.TryDispatch(invoke)) {
4618 return;
4619 }
4620
Alexandre Rames67555f72014-11-18 10:55:16 +00004621 HandleInvoke(invoke);
4622}
4623
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004624void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004625 // Explicit clinit checks triggered by static invokes must have been pruned by
4626 // art::PrepareForRegisterAllocation.
4627 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004628
Vladimir Markoca6fff82017-10-03 14:49:14 +01004629 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004630 if (intrinsic.TryDispatch(invoke)) {
4631 return;
4632 }
4633
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004634 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
Vladimir Marko86c87522020-05-11 16:55:55 +01004635 CriticalNativeCallingConventionVisitorARM64 calling_convention_visitor(
4636 /*for_register_allocation=*/ true);
4637 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4638 } else {
4639 HandleInvoke(invoke);
4640 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004641}
4642
Andreas Gampe878d58c2015-01-15 23:24:00 -08004643static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4644 if (invoke->GetLocations()->Intrinsified()) {
4645 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4646 intrinsic.Dispatch(invoke);
4647 return true;
4648 }
4649 return false;
4650}
4651
Vladimir Markodc151b22015-10-15 18:02:30 +01004652HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4653 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004654 ArtMethod* method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004655 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004656 return desired_dispatch_info;
4657}
4658
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004659void CodeGeneratorARM64::LoadMethod(MethodLoadKind load_kind, Location temp, HInvoke* invoke) {
4660 switch (load_kind) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004661 case MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01004662 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01004663 // Add ADRP with its PC-relative method patch.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004664 vixl::aarch64::Label* adrp_label =
4665 NewBootImageMethodPatch(invoke->GetResolvedMethodReference());
Vladimir Marko65979462017-05-19 17:25:12 +01004666 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4667 // Add ADD with its PC-relative method patch.
4668 vixl::aarch64::Label* add_label =
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004669 NewBootImageMethodPatch(invoke->GetResolvedMethodReference(), adrp_label);
Vladimir Marko65979462017-05-19 17:25:12 +01004670 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4671 break;
4672 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004673 case MethodLoadKind::kBootImageRelRo: {
Vladimir Markob066d432018-01-03 13:14:37 +00004674 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
Vladimir Marko9d31daa2022-04-14 10:48:44 +01004675 uint32_t boot_image_offset = GetBootImageOffset(invoke);
4676 LoadBootImageRelRoEntry(WRegisterFrom(temp), boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00004677 break;
4678 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004679 case MethodLoadKind::kBssEntry: {
Vladimir Markob066d432018-01-03 13:14:37 +00004680 // Add ADRP with its PC-relative .bss entry patch.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004681 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(invoke->GetMethodReference());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004682 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Markob066d432018-01-03 13:14:37 +00004683 // Add LDR with its PC-relative .bss entry patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004684 vixl::aarch64::Label* ldr_label =
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004685 NewMethodBssEntryPatch(invoke->GetMethodReference(), adrp_label);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01004686 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004687 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004688 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004689 }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004690 case MethodLoadKind::kJitDirectAddress: {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004691 // Load method address from literal pool.
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004692 __ Ldr(XRegisterFrom(temp),
4693 DeduplicateUint64Literal(reinterpret_cast<uint64_t>(invoke->GetResolvedMethod())));
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004694 break;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004695 }
4696 case MethodLoadKind::kRuntimeCall: {
4697 // Test situation, don't do anything.
4698 break;
4699 }
4700 default: {
4701 LOG(FATAL) << "Load kind should have already been handled " << load_kind;
4702 UNREACHABLE();
4703 }
4704 }
4705}
4706
4707void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4708 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
4709 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
4710 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4711 switch (invoke->GetMethodLoadKind()) {
4712 case MethodLoadKind::kStringInit: {
4713 uint32_t offset =
4714 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4715 // temp = thread->string_init_entrypoint
4716 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
4717 break;
4718 }
4719 case MethodLoadKind::kRecursive: {
4720 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
4721 break;
4722 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004723 case MethodLoadKind::kRuntimeCall: {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004724 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4725 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004726 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004727 case MethodLoadKind::kBootImageLinkTimePcRelative:
4728 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
4729 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
4730 // Do not materialize the method pointer, load directly the entrypoint.
4731 // Add ADRP with its PC-relative JNI entrypoint patch.
4732 vixl::aarch64::Label* adrp_label =
4733 NewBootImageJniEntrypointPatch(invoke->GetResolvedMethodReference());
4734 EmitAdrpPlaceholder(adrp_label, lr);
4735 // Add the LDR with its PC-relative method patch.
4736 vixl::aarch64::Label* add_label =
4737 NewBootImageJniEntrypointPatch(invoke->GetResolvedMethodReference(), adrp_label);
4738 EmitLdrOffsetPlaceholder(add_label, lr, lr);
4739 break;
4740 }
4741 FALLTHROUGH_INTENDED;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004742 default: {
4743 LoadMethod(invoke->GetMethodLoadKind(), temp, invoke);
4744 break;
4745 }
Vladimir Marko58155012015-08-19 12:49:41 +00004746 }
4747
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004748 auto call_lr = [&]() {
4749 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4750 ExactAssemblyScope eas(GetVIXLAssembler(),
4751 kInstructionSize,
4752 CodeBufferCheckScope::kExactSize);
4753 // lr()
4754 __ blr(lr);
4755 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko86c87522020-05-11 16:55:55 +01004756 };
Vladimir Marko58155012015-08-19 12:49:41 +00004757 switch (invoke->GetCodePtrLocation()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004758 case CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004759 {
Nicolas Geoffray282795c2021-09-24 18:16:41 +01004760 DCHECK(!GetGraph()->HasShouldDeoptimizeFlag());
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004761 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4762 ExactAssemblyScope eas(GetVIXLAssembler(),
4763 kInstructionSize,
4764 CodeBufferCheckScope::kExactSize);
4765 __ bl(&frame_entry_label_);
4766 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4767 }
Vladimir Marko58155012015-08-19 12:49:41 +00004768 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004769 case CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01004770 size_t out_frame_size =
4771 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorARM64,
4772 kAapcs64StackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01004773 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004774 if (invoke->GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative) {
4775 call_lr();
4776 } else {
4777 // LR = callee_method->ptr_sized_fields_.data_; // EntryPointFromJni
4778 MemberOffset offset = ArtMethod::EntryPointFromJniOffset(kArm64PointerSize);
4779 __ Ldr(lr, MemOperand(XRegisterFrom(callee_method), offset.Int32Value()));
4780 // lr()
4781 call_lr();
4782 }
Vladimir Marko86c87522020-05-11 16:55:55 +01004783 // Zero-/sign-extend the result when needed due to native and managed ABI mismatch.
4784 switch (invoke->GetType()) {
4785 case DataType::Type::kBool:
4786 __ Ubfx(w0, w0, 0, 8);
4787 break;
4788 case DataType::Type::kInt8:
4789 __ Sbfx(w0, w0, 0, 8);
4790 break;
4791 case DataType::Type::kUint16:
4792 __ Ubfx(w0, w0, 0, 16);
4793 break;
4794 case DataType::Type::kInt16:
4795 __ Sbfx(w0, w0, 0, 16);
4796 break;
4797 case DataType::Type::kInt32:
4798 case DataType::Type::kInt64:
4799 case DataType::Type::kFloat32:
4800 case DataType::Type::kFloat64:
4801 case DataType::Type::kVoid:
4802 break;
4803 default:
4804 DCHECK(false) << invoke->GetType();
4805 break;
4806 }
4807 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01004808 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01004809 }
4810 break;
4811 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004812 case CodePtrLocation::kCallArtMethod: {
4813 // LR = callee_method->ptr_sized_fields_.entry_point_from_quick_compiled_code_;
4814 MemberOffset offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
4815 __ Ldr(lr, MemOperand(XRegisterFrom(callee_method), offset.Int32Value()));
4816 // lr()
4817 call_lr();
Vladimir Marko58155012015-08-19 12:49:41 +00004818 break;
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004819 }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004820 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004821
Andreas Gampe878d58c2015-01-15 23:24:00 -08004822 DCHECK(!IsLeafMethod());
4823}
4824
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004825void CodeGeneratorARM64::GenerateVirtualCall(
4826 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004827 // Use the calling convention instead of the location of the receiver, as
4828 // intrinsics may have put the receiver in a different register. In the intrinsics
4829 // slow path, the arguments have been moved to the right place, so here we are
4830 // guaranteed that the receiver is the first register of the calling convention.
4831 InvokeDexCallingConvention calling_convention;
4832 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004833 Register temp = XRegisterFrom(temp_in);
4834 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4835 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4836 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004837 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004838
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004839 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004840
4841 {
4842 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4843 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4844 // /* HeapReference<Class> */ temp = receiver->klass_
4845 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4846 MaybeRecordImplicitNullCheck(invoke);
4847 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004848 // Instead of simply (possibly) unpoisoning `temp` here, we should
4849 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004850 // intermediate/temporary reference and because the current
4851 // concurrent copying collector keeps the from-space memory
4852 // intact/accessible until the end of the marking phase (the
4853 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004854 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004855
4856 // If we're compiling baseline, update the inline cache.
4857 MaybeGenerateInlineCacheCheck(invoke, temp);
4858
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004859 // temp = temp->GetMethodAt(method_offset);
4860 __ Ldr(temp, MemOperand(temp, method_offset));
4861 // lr = temp->GetEntryPoint();
4862 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004863 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004864 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004865 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4866 // lr();
4867 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004868 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004869 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004870}
4871
Vladimir Marko9922f002020-06-08 15:05:15 +01004872void CodeGeneratorARM64::MoveFromReturnRegister(Location trg, DataType::Type type) {
4873 if (!trg.IsValid()) {
4874 DCHECK(type == DataType::Type::kVoid);
4875 return;
4876 }
4877
4878 DCHECK_NE(type, DataType::Type::kVoid);
4879
4880 if (DataType::IsIntegralType(type) || type == DataType::Type::kReference) {
4881 Register trg_reg = RegisterFrom(trg, type);
4882 Register res_reg = RegisterFrom(ARM64ReturnLocation(type), type);
4883 __ Mov(trg_reg, res_reg, kDiscardForSameWReg);
4884 } else {
4885 VRegister trg_reg = FPRegisterFrom(trg, type);
4886 VRegister res_reg = FPRegisterFrom(ARM64ReturnLocation(type), type);
4887 __ Fmov(trg_reg, res_reg);
4888 }
4889}
4890
Orion Hodsonac141392017-01-13 11:53:47 +00004891void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00004892 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
4893 if (intrinsic.TryDispatch(invoke)) {
4894 return;
4895 }
Orion Hodsonac141392017-01-13 11:53:47 +00004896 HandleInvoke(invoke);
4897}
4898
4899void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00004900 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4901 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
4902 return;
4903 }
Orion Hodsonac141392017-01-13 11:53:47 +00004904 codegen_->GenerateInvokePolymorphicCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004905 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004906}
4907
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004908void LocationsBuilderARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4909 HandleInvoke(invoke);
4910}
4911
4912void InstructionCodeGeneratorARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4913 codegen_->GenerateInvokeCustomCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004914 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004915}
4916
Vladimir Marko6fd16062018-06-26 11:02:04 +01004917vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageIntrinsicPatch(
4918 uint32_t intrinsic_data,
4919 vixl::aarch64::Label* adrp_label) {
4920 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004921 /* dex_file= */ nullptr, intrinsic_data, adrp_label, &boot_image_other_patches_);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004922}
4923
Vladimir Markob066d432018-01-03 13:14:37 +00004924vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageRelRoPatch(
4925 uint32_t boot_image_offset,
4926 vixl::aarch64::Label* adrp_label) {
4927 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004928 /* dex_file= */ nullptr, boot_image_offset, adrp_label, &boot_image_other_patches_);
Vladimir Markob066d432018-01-03 13:14:37 +00004929}
4930
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004931vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004932 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004933 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004934 return NewPcRelativePatch(
4935 target_method.dex_file, target_method.index, adrp_label, &boot_image_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004936}
4937
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004938vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4939 MethodReference target_method,
4940 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004941 return NewPcRelativePatch(
4942 target_method.dex_file, target_method.index, adrp_label, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004943}
4944
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004945vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageTypePatch(
Scott Wakeling97c72b72016-06-24 16:19:36 +01004946 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004947 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004948 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004949 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &boot_image_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004950}
4951
Vladimir Marko1998cd02017-01-13 13:02:58 +00004952vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
Vladimir Marko8f63f102020-09-28 12:10:28 +01004953 HLoadClass* load_class,
Vladimir Marko1998cd02017-01-13 13:02:58 +00004954 vixl::aarch64::Label* adrp_label) {
Vladimir Marko8f63f102020-09-28 12:10:28 +01004955 const DexFile& dex_file = load_class->GetDexFile();
4956 dex::TypeIndex type_index = load_class->GetTypeIndex();
4957 ArenaDeque<PcRelativePatchInfo>* patches = nullptr;
4958 switch (load_class->GetLoadKind()) {
4959 case HLoadClass::LoadKind::kBssEntry:
4960 patches = &type_bss_entry_patches_;
4961 break;
4962 case HLoadClass::LoadKind::kBssEntryPublic:
4963 patches = &public_type_bss_entry_patches_;
4964 break;
4965 case HLoadClass::LoadKind::kBssEntryPackage:
4966 patches = &package_type_bss_entry_patches_;
4967 break;
4968 default:
4969 LOG(FATAL) << "Unexpected load kind: " << load_class->GetLoadKind();
4970 UNREACHABLE();
4971 }
4972 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004973}
4974
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004975vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004976 const DexFile& dex_file,
4977 dex::StringIndex string_index,
4978 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004979 return NewPcRelativePatch(
4980 &dex_file, string_index.index_, adrp_label, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01004981}
4982
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004983vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4984 const DexFile& dex_file,
4985 dex::StringIndex string_index,
4986 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004987 return NewPcRelativePatch(&dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004988}
4989
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004990vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageJniEntrypointPatch(
4991 MethodReference target_method,
4992 vixl::aarch64::Label* adrp_label) {
4993 return NewPcRelativePatch(
4994 target_method.dex_file, target_method.index, adrp_label, &boot_image_jni_entrypoint_patches_);
4995}
4996
Vladimir Markof6675082019-05-17 12:05:28 +01004997void CodeGeneratorARM64::EmitEntrypointThunkCall(ThreadOffset64 entrypoint_offset) {
4998 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01004999 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markof6675082019-05-17 12:05:28 +01005000 call_entrypoint_patches_.emplace_back(/*dex_file*/ nullptr, entrypoint_offset.Uint32Value());
5001 vixl::aarch64::Label* bl_label = &call_entrypoint_patches_.back().label;
5002 __ bind(bl_label);
5003 __ bl(static_cast<int64_t>(0)); // Placeholder, patched at link-time.
5004}
5005
Vladimir Marko966b46f2018-08-03 10:20:19 +00005006void CodeGeneratorARM64::EmitBakerReadBarrierCbnz(uint32_t custom_data) {
Vladimir Marko94796f82018-08-08 15:15:33 +01005007 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01005008 if (GetCompilerOptions().IsJitCompiler()) {
Vladimir Marko966b46f2018-08-03 10:20:19 +00005009 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
5010 vixl::aarch64::Label* slow_path_entry = &it->second.label;
5011 __ cbnz(mr, slow_path_entry);
5012 } else {
5013 baker_read_barrier_patches_.emplace_back(custom_data);
5014 vixl::aarch64::Label* cbnz_label = &baker_read_barrier_patches_.back().label;
5015 __ bind(cbnz_label);
5016 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
5017 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005018}
5019
Scott Wakeling97c72b72016-06-24 16:19:36 +01005020vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005021 const DexFile* dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005022 uint32_t offset_or_index,
5023 vixl::aarch64::Label* adrp_label,
5024 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005025 // Add a patch entry and return the label.
5026 patches->emplace_back(dex_file, offset_or_index);
5027 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01005028 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005029 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
5030 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
5031 return label;
5032}
5033
Scott Wakeling97c72b72016-06-24 16:19:36 +01005034vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
5035 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005036 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005037}
5038
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005039vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005040 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005041 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005042 return jit_string_patches_.GetOrCreate(
5043 StringReference(&dex_file, string_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08005044 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005045}
5046
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005047vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005048 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005049 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005050 return jit_class_patches_.GetOrCreate(
5051 TypeReference(&dex_file, type_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08005052 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005053}
5054
Vladimir Markoaad75c62016-10-03 08:46:48 +00005055void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
5056 vixl::aarch64::Register reg) {
5057 DCHECK(reg.IsX());
5058 SingleEmissionCheckScope guard(GetVIXLAssembler());
5059 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005060 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005061}
5062
5063void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
5064 vixl::aarch64::Register out,
5065 vixl::aarch64::Register base) {
5066 DCHECK(out.IsX());
5067 DCHECK(base.IsX());
5068 SingleEmissionCheckScope guard(GetVIXLAssembler());
5069 __ Bind(fixup_label);
5070 __ add(out, base, Operand(/* offset placeholder */ 0));
5071}
5072
5073void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
5074 vixl::aarch64::Register out,
5075 vixl::aarch64::Register base) {
5076 DCHECK(base.IsX());
5077 SingleEmissionCheckScope guard(GetVIXLAssembler());
5078 __ Bind(fixup_label);
5079 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
5080}
5081
Vladimir Marko9d31daa2022-04-14 10:48:44 +01005082void CodeGeneratorARM64::LoadBootImageRelRoEntry(vixl::aarch64::Register reg,
5083 uint32_t boot_image_offset) {
5084 DCHECK(reg.IsW());
5085 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5086 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_offset);
5087 EmitAdrpPlaceholder(adrp_label, reg.X());
5088 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
5089 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_offset, adrp_label);
5090 EmitLdrOffsetPlaceholder(ldr_label, reg.W(), reg.X());
5091}
5092
Vladimir Markoeebb8212018-06-05 14:57:24 +01005093void CodeGeneratorARM64::LoadBootImageAddress(vixl::aarch64::Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01005094 uint32_t boot_image_reference) {
5095 if (GetCompilerOptions().IsBootImage()) {
5096 // Add ADRP with its PC-relative type patch.
5097 vixl::aarch64::Label* adrp_label = NewBootImageIntrinsicPatch(boot_image_reference);
5098 EmitAdrpPlaceholder(adrp_label, reg.X());
5099 // Add ADD with its PC-relative type patch.
5100 vixl::aarch64::Label* add_label = NewBootImageIntrinsicPatch(boot_image_reference, adrp_label);
5101 EmitAddPlaceholder(add_label, reg.X(), reg.X());
Vladimir Markoa2da9b92018-10-10 14:21:55 +01005102 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Marko9d31daa2022-04-14 10:48:44 +01005103 LoadBootImageRelRoEntry(reg, boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01005104 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01005105 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01005106 gc::Heap* heap = Runtime::Current()->GetHeap();
5107 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005108 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01005109 __ Ldr(reg.W(), DeduplicateBootImageAddressLiteral(reinterpret_cast<uintptr_t>(address)));
5110 }
5111}
5112
Vladimir Marko98873af2020-12-16 12:10:03 +00005113void CodeGeneratorARM64::LoadTypeForBootImageIntrinsic(vixl::aarch64::Register reg,
5114 TypeReference target_type) {
5115 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
Vladimir Marko9d31daa2022-04-14 10:48:44 +01005116 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko98873af2020-12-16 12:10:03 +00005117 // Add ADRP with its PC-relative type patch.
5118 vixl::aarch64::Label* adrp_label =
5119 NewBootImageTypePatch(*target_type.dex_file, target_type.TypeIndex());
5120 EmitAdrpPlaceholder(adrp_label, reg.X());
5121 // Add ADD with its PC-relative type patch.
5122 vixl::aarch64::Label* add_label =
5123 NewBootImageTypePatch(*target_type.dex_file, target_type.TypeIndex(), adrp_label);
5124 EmitAddPlaceholder(add_label, reg.X(), reg.X());
5125}
5126
Vladimir Markode91ca92020-10-27 13:41:40 +00005127void CodeGeneratorARM64::LoadIntrinsicDeclaringClass(vixl::aarch64::Register reg, HInvoke* invoke) {
5128 DCHECK_NE(invoke->GetIntrinsic(), Intrinsics::kNone);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005129 if (GetCompilerOptions().IsBootImage()) {
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005130 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01005131 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
Vladimir Marko98873af2020-12-16 12:10:03 +00005132 LoadTypeForBootImageIntrinsic(reg, TypeReference(target_method.dex_file, type_idx));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005133 } else {
Vladimir Markode91ca92020-10-27 13:41:40 +00005134 uint32_t boot_image_offset = GetBootImageOffsetOfIntrinsicDeclaringClass(invoke);
5135 LoadBootImageAddress(reg, boot_image_offset);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005136 }
Vladimir Marko6fd16062018-06-26 11:02:04 +01005137}
5138
Vladimir Marko98873af2020-12-16 12:10:03 +00005139void CodeGeneratorARM64::LoadClassRootForIntrinsic(vixl::aarch64::Register reg,
5140 ClassRoot class_root) {
5141 if (GetCompilerOptions().IsBootImage()) {
5142 ScopedObjectAccess soa(Thread::Current());
5143 ObjPtr<mirror::Class> klass = GetClassRoot(class_root);
5144 TypeReference target_type(&klass->GetDexFile(), klass->GetDexTypeIndex());
5145 LoadTypeForBootImageIntrinsic(reg, target_type);
5146 } else {
5147 uint32_t boot_image_offset = GetBootImageOffset(class_root);
5148 LoadBootImageAddress(reg, boot_image_offset);
5149 }
5150}
5151
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005152template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005153inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
5154 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005155 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005156 for (const PcRelativePatchInfo& info : infos) {
5157 linker_patches->push_back(Factory(info.label.GetLocation(),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005158 info.target_dex_file,
Vladimir Markoaad75c62016-10-03 08:46:48 +00005159 info.pc_insn_label->GetLocation(),
5160 info.offset_or_index));
5161 }
5162}
5163
Vladimir Marko6fd16062018-06-26 11:02:04 +01005164template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5165linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5166 const DexFile* target_dex_file,
5167 uint32_t pc_insn_offset,
5168 uint32_t boot_image_offset) {
5169 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5170 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005171}
5172
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005173void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005174 DCHECK(linker_patches->empty());
5175 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005176 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005177 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005178 boot_image_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005179 type_bss_entry_patches_.size() +
Vladimir Marko8f63f102020-09-28 12:10:28 +01005180 public_type_bss_entry_patches_.size() +
5181 package_type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005182 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005183 string_bss_entry_patches_.size() +
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005184 boot_image_jni_entrypoint_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005185 boot_image_other_patches_.size() +
Vladimir Markof6675082019-05-17 12:05:28 +01005186 call_entrypoint_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005187 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00005188 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005189 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005190 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005191 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005192 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005193 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005194 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005195 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01005196 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005197 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005198 DCHECK(boot_image_type_patches_.empty());
5199 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005200 }
5201 if (GetCompilerOptions().IsBootImage()) {
5202 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5203 boot_image_other_patches_, linker_patches);
5204 } else {
5205 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5206 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005207 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005208 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5209 method_bss_entry_patches_, linker_patches);
5210 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5211 type_bss_entry_patches_, linker_patches);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005212 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PublicTypeBssEntryPatch>(
5213 public_type_bss_entry_patches_, linker_patches);
5214 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PackageTypeBssEntryPatch>(
5215 package_type_bss_entry_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005216 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5217 string_bss_entry_patches_, linker_patches);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005218 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeJniEntrypointPatch>(
5219 boot_image_jni_entrypoint_patches_, linker_patches);
Vladimir Markof6675082019-05-17 12:05:28 +01005220 for (const PatchInfo<vixl::aarch64::Label>& info : call_entrypoint_patches_) {
5221 DCHECK(info.target_dex_file == nullptr);
5222 linker_patches->push_back(linker::LinkerPatch::CallEntrypointPatch(
5223 info.label.GetLocation(), info.offset_or_index));
5224 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005225 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005226 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
5227 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005228 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00005229 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005230}
5231
Vladimir Markoca1e0382018-04-11 09:58:41 +00005232bool CodeGeneratorARM64::NeedsThunkCode(const linker::LinkerPatch& patch) const {
Vladimir Markof6675082019-05-17 12:05:28 +01005233 return patch.GetType() == linker::LinkerPatch::Type::kCallEntrypoint ||
5234 patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
Vladimir Markoca1e0382018-04-11 09:58:41 +00005235 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
5236}
5237
5238void CodeGeneratorARM64::EmitThunkCode(const linker::LinkerPatch& patch,
5239 /*out*/ ArenaVector<uint8_t>* code,
5240 /*out*/ std::string* debug_name) {
5241 Arm64Assembler assembler(GetGraph()->GetAllocator());
5242 switch (patch.GetType()) {
5243 case linker::LinkerPatch::Type::kCallRelative: {
5244 // The thunk just uses the entry point in the ArtMethod. This works even for calls
5245 // to the generic JNI and interpreter trampolines.
5246 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
5247 kArm64PointerSize).Int32Value());
5248 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
David Srbecky889da942021-04-30 13:03:14 +01005249 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00005250 *debug_name = "MethodCallThunk";
5251 }
5252 break;
5253 }
Vladimir Markof6675082019-05-17 12:05:28 +01005254 case linker::LinkerPatch::Type::kCallEntrypoint: {
5255 Offset offset(patch.EntrypointOffset());
5256 assembler.JumpTo(ManagedRegister(arm64::TR), offset, ManagedRegister(arm64::IP0));
David Srbecky889da942021-04-30 13:03:14 +01005257 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markof6675082019-05-17 12:05:28 +01005258 *debug_name = "EntrypointCallThunk_" + std::to_string(offset.Uint32Value());
5259 }
5260 break;
5261 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00005262 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
5263 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
5264 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
5265 break;
5266 }
5267 default:
5268 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
5269 UNREACHABLE();
5270 }
5271
5272 // Ensure we emit the literal pool if any.
5273 assembler.FinalizeCode();
5274 code->resize(assembler.CodeSize());
5275 MemoryRegion code_region(code->data(), code->size());
5276 assembler.FinalizeInstructions(code_region);
5277}
5278
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005279vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
5280 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005281 value,
5282 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
5283}
5284
Scott Wakeling97c72b72016-06-24 16:19:36 +01005285vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005286 return uint64_literals_.GetOrCreate(
5287 value,
5288 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00005289}
5290
Andreas Gampe878d58c2015-01-15 23:24:00 -08005291void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005292 // Explicit clinit checks triggered by static invokes must have been pruned by
5293 // art::PrepareForRegisterAllocation.
5294 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01005295
Andreas Gampe878d58c2015-01-15 23:24:00 -08005296 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08005297 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08005298 return;
5299 }
5300
Vladimir Marko86c87522020-05-11 16:55:55 +01005301 LocationSummary* locations = invoke->GetLocations();
5302 codegen_->GenerateStaticOrDirectCall(
5303 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005304
Andreas Gampe3db70682018-12-26 15:12:03 -08005305 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005306}
5307
5308void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08005309 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08005310 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08005311 return;
5312 }
5313
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005314 {
5315 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
5316 // are no pools emitted.
5317 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
5318 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
5319 DCHECK(!codegen_->IsLeafMethod());
5320 }
5321
Andreas Gampe3db70682018-12-26 15:12:03 -08005322 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005323}
5324
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005325HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
5326 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005327 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005328 case HLoadClass::LoadKind::kInvalid:
5329 LOG(FATAL) << "UNREACHABLE";
5330 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005331 case HLoadClass::LoadKind::kReferrersClass:
5332 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005333 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005334 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005335 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8f63f102020-09-28 12:10:28 +01005336 case HLoadClass::LoadKind::kBssEntryPublic:
5337 case HLoadClass::LoadKind::kBssEntryPackage:
Vladimir Marko695348f2020-05-19 14:42:02 +01005338 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005339 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005340 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005341 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01005342 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005343 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005344 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005345 break;
5346 }
5347 return desired_class_load_kind;
5348}
5349
Alexandre Rames67555f72014-11-18 10:55:16 +00005350void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005351 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005352 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005353 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005354 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005355 cls,
5356 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005357 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00005358 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005359 return;
5360 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005361 DCHECK_EQ(cls->NeedsAccessCheck(),
5362 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
5363 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005364
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00005365 const bool requires_read_barrier = gUseReadBarrier && !cls->IsInBootImage();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005366 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005367 ? LocationSummary::kCallOnSlowPath
5368 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005369 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005370 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005371 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005372 }
5373
Vladimir Marko41559982017-01-06 14:04:23 +00005374 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005375 locations->SetInAt(0, Location::RequiresRegister());
5376 }
5377 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005378 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00005379 if (!gUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005380 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005381 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005382 } else {
5383 // For non-Baker read barrier we have a temp-clobbering call.
5384 }
5385 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005386}
5387
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005388// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5389// move.
5390void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005391 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005392 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005393 codegen_->GenerateLoadClassRuntimeCall(cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08005394 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01005395 return;
5396 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005397 DCHECK_EQ(cls->NeedsAccessCheck(),
5398 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
5399 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Calin Juravle580b6092015-10-06 17:35:58 +01005400
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005401 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01005402 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00005403
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005404 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5405 ? kWithoutReadBarrier
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00005406 : gCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005407 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005408 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005409 case HLoadClass::LoadKind::kReferrersClass: {
5410 DCHECK(!cls->CanCallRuntime());
5411 DCHECK(!cls->MustGenerateClinitCheck());
5412 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5413 Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00005414 codegen_->GenerateGcRootFieldLoad(cls,
5415 out_loc,
5416 current_method,
5417 ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005418 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005419 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005420 break;
5421 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005422 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005423 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5424 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005425 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005426 // Add ADRP with its PC-relative type patch.
5427 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08005428 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005429 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005430 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005431 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005432 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005433 codegen_->NewBootImageTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005434 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005435 break;
5436 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005437 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005438 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markode91ca92020-10-27 13:41:40 +00005439 uint32_t boot_image_offset = CodeGenerator::GetBootImageOffset(cls);
Vladimir Marko9d31daa2022-04-14 10:48:44 +01005440 codegen_->LoadBootImageRelRoEntry(out.W(), boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005441 break;
5442 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005443 case HLoadClass::LoadKind::kBssEntry:
5444 case HLoadClass::LoadKind::kBssEntryPublic:
5445 case HLoadClass::LoadKind::kBssEntryPackage: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005446 // Add ADRP with its PC-relative Class .bss entry patch.
Vladimir Markof3c52b42017-11-17 17:32:12 +00005447 vixl::aarch64::Register temp = XRegisterFrom(out_loc);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005448 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(cls);
Vladimir Markof3c52b42017-11-17 17:32:12 +00005449 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005450 // Add LDR with its PC-relative Class .bss entry patch.
Vladimir Marko8f63f102020-09-28 12:10:28 +01005451 vixl::aarch64::Label* ldr_label = codegen_->NewBssEntryTypePatch(cls, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005452 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005453 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005454 codegen_->GenerateGcRootFieldLoad(cls,
5455 out_loc,
5456 temp,
5457 /* offset placeholder */ 0u,
5458 ldr_label,
5459 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005460 generate_null_check = true;
5461 break;
5462 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005463 case HLoadClass::LoadKind::kJitBootImageAddress: {
5464 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5465 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
5466 DCHECK_NE(address, 0u);
5467 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5468 break;
5469 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005470 case HLoadClass::LoadKind::kJitTableAddress: {
5471 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5472 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005473 cls->GetClass()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005474 codegen_->GenerateGcRootFieldLoad(cls,
5475 out_loc,
5476 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005477 /* offset= */ 0,
5478 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005479 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005480 break;
5481 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005482 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005483 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005484 LOG(FATAL) << "UNREACHABLE";
5485 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005486 }
5487
Vladimir Markoea4c1262017-02-06 19:59:33 +00005488 bool do_clinit = cls->MustGenerateClinitCheck();
5489 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005490 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01005491 SlowPathCodeARM64* slow_path =
5492 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005493 codegen_->AddSlowPath(slow_path);
5494 if (generate_null_check) {
5495 __ Cbz(out, slow_path->GetEntryLabel());
5496 }
5497 if (cls->MustGenerateClinitCheck()) {
5498 GenerateClassInitializationCheck(slow_path, out);
5499 } else {
5500 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005501 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005502 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005503 }
5504}
5505
Orion Hodsondbaa5c72018-05-10 08:22:46 +01005506void LocationsBuilderARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5507 InvokeRuntimeCallingConvention calling_convention;
5508 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5509 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
5510}
5511
5512void InstructionCodeGeneratorARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5513 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
5514}
5515
Orion Hodson18259d72018-04-12 11:18:23 +01005516void LocationsBuilderARM64::VisitLoadMethodType(HLoadMethodType* load) {
5517 InvokeRuntimeCallingConvention calling_convention;
5518 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5519 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
5520}
5521
5522void InstructionCodeGeneratorARM64::VisitLoadMethodType(HLoadMethodType* load) {
5523 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
5524}
5525
David Brazdilcb1c0552015-08-04 16:22:25 +01005526static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005527 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005528}
5529
Alexandre Rames67555f72014-11-18 10:55:16 +00005530void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5531 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005532 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00005533 locations->SetOut(Location::RequiresRegister());
5534}
5535
5536void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005537 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5538}
5539
5540void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005541 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005542}
5543
5544void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5545 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005546}
5547
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005548HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5549 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005550 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005551 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005552 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005553 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01005554 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005555 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005556 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005557 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01005558 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005559 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005560 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005561 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005562 }
5563 return desired_string_load_kind;
5564}
5565
Alexandre Rames67555f72014-11-18 10:55:16 +00005566void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005567 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005568 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005569 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005570 InvokeRuntimeCallingConvention calling_convention;
5571 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5572 } else {
5573 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005574 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00005575 if (!gUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005576 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005577 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005578 } else {
5579 // For non-Baker read barrier we have a temp-clobbering call.
5580 }
5581 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005582 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005583}
5584
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005585// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5586// move.
5587void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005588 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005589 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005590
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005591 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005592 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005593 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5594 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005595 // Add ADRP with its PC-relative String patch.
5596 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005597 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005598 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005599 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005600 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005601 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005602 codegen_->NewBootImageStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005603 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005604 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005605 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005606 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005607 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markode91ca92020-10-27 13:41:40 +00005608 uint32_t boot_image_offset = CodeGenerator::GetBootImageOffset(load);
Vladimir Marko9d31daa2022-04-14 10:48:44 +01005609 codegen_->LoadBootImageRelRoEntry(out.W(), boot_image_offset);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005610 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005611 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005612 case HLoadString::LoadKind::kBssEntry: {
5613 // Add ADRP with its PC-relative String .bss entry patch.
5614 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005615 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00005616 Register temp = XRegisterFrom(out_loc);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005617 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005618 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005619 // Add LDR with its PC-relative String .bss entry patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005620 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005621 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005622 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005623 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005624 codegen_->GenerateGcRootFieldLoad(load,
5625 out_loc,
5626 temp,
5627 /* offset placeholder */ 0u,
5628 ldr_label,
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00005629 gCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005630 SlowPathCodeARM64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005631 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005632 codegen_->AddSlowPath(slow_path);
5633 __ Cbz(out.X(), slow_path->GetEntryLabel());
5634 __ Bind(slow_path->GetExitLabel());
Andreas Gampe3db70682018-12-26 15:12:03 -08005635 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005636 return;
5637 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005638 case HLoadString::LoadKind::kJitBootImageAddress: {
5639 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
5640 DCHECK_NE(address, 0u);
5641 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5642 return;
5643 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005644 case HLoadString::LoadKind::kJitTableAddress: {
5645 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005646 load->GetStringIndex(),
5647 load->GetString()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005648 codegen_->GenerateGcRootFieldLoad(load,
5649 out_loc,
5650 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005651 /* offset= */ 0,
5652 /* fixup_label= */ nullptr,
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00005653 gCompilerReadBarrierOption);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005654 return;
5655 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005656 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005657 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005658 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005659
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005660 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005661 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005662 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005663 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005664 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5665 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005666 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005667}
5668
Alexandre Rames5319def2014-10-23 10:03:10 +01005669void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005670 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01005671 locations->SetOut(Location::ConstantLocation(constant));
5672}
5673
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005674void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005675 // Will be generated at use site.
5676}
5677
Alexandre Rames67555f72014-11-18 10:55:16 +00005678void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005679 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5680 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005681 InvokeRuntimeCallingConvention calling_convention;
5682 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5683}
5684
5685void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005686 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005687 instruction,
5688 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005689 if (instruction->IsEnter()) {
5690 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5691 } else {
5692 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5693 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005694 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005695}
5696
Alexandre Rames42d641b2014-10-27 14:00:51 +00005697void LocationsBuilderARM64::VisitMul(HMul* mul) {
5698 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005699 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005700 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005701 case DataType::Type::kInt32:
5702 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005703 locations->SetInAt(0, Location::RequiresRegister());
5704 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005705 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005706 break;
5707
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005708 case DataType::Type::kFloat32:
5709 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005710 locations->SetInAt(0, Location::RequiresFpuRegister());
5711 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005712 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005713 break;
5714
5715 default:
5716 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5717 }
5718}
5719
5720void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5721 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005722 case DataType::Type::kInt32:
5723 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005724 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5725 break;
5726
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005727 case DataType::Type::kFloat32:
5728 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005729 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005730 break;
5731
5732 default:
5733 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5734 }
5735}
5736
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005737void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5738 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005739 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005740 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005741 case DataType::Type::kInt32:
5742 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005743 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005744 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005745 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005746
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005747 case DataType::Type::kFloat32:
5748 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005749 locations->SetInAt(0, Location::RequiresFpuRegister());
5750 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005751 break;
5752
5753 default:
5754 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5755 }
5756}
5757
5758void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5759 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005760 case DataType::Type::kInt32:
5761 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005762 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5763 break;
5764
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005765 case DataType::Type::kFloat32:
5766 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005767 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005768 break;
5769
5770 default:
5771 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5772 }
5773}
5774
5775void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005776 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5777 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005778 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005779 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005780 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5781 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005782}
5783
5784void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005785 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5786 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005787 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005788 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005789 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005790}
5791
Alexandre Rames5319def2014-10-23 10:03:10 +01005792void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005793 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5794 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005795 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07005796 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005797 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005798}
5799
5800void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005801 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5802 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005803 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005804}
5805
5806void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005807 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005808 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005809 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005810}
5811
5812void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005813 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005814 case DataType::Type::kInt32:
5815 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005816 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005817 break;
5818
5819 default:
5820 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5821 }
5822}
5823
David Brazdil66d126e2015-04-03 16:02:44 +01005824void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005825 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005826 locations->SetInAt(0, Location::RequiresRegister());
5827 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5828}
5829
5830void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005831 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005832}
5833
Alexandre Rames5319def2014-10-23 10:03:10 +01005834void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005835 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5836 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005837}
5838
Calin Juravle2ae48182016-03-16 14:05:09 +00005839void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5840 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005841 return;
5842 }
Artem Serov914d7a82017-02-07 14:33:49 +00005843 {
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01005844 // Ensure that between load and RecordPcInfo there are no pools emitted.
Artem Serov914d7a82017-02-07 14:33:49 +00005845 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5846 Location obj = instruction->GetLocations()->InAt(0);
5847 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5848 RecordPcInfo(instruction, instruction->GetDexPc());
5849 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005850}
5851
Calin Juravle2ae48182016-03-16 14:05:09 +00005852void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005853 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005854 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005855
5856 LocationSummary* locations = instruction->GetLocations();
5857 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005858
5859 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005860}
5861
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005862void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005863 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005864}
5865
Alexandre Rames67555f72014-11-18 10:55:16 +00005866void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5867 HandleBinaryOp(instruction);
5868}
5869
5870void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5871 HandleBinaryOp(instruction);
5872}
5873
Alexandre Rames3e69f162014-12-10 10:36:50 +00005874void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5875 LOG(FATAL) << "Unreachable";
5876}
5877
5878void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005879 if (instruction->GetNext()->IsSuspendCheck() &&
5880 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5881 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5882 // The back edge will generate the suspend check.
5883 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5884 }
5885
Alexandre Rames3e69f162014-12-10 10:36:50 +00005886 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5887}
5888
Alexandre Rames5319def2014-10-23 10:03:10 +01005889void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005890 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005891 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5892 if (location.IsStackSlot()) {
5893 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5894 } else if (location.IsDoubleStackSlot()) {
5895 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5896 }
5897 locations->SetOut(location);
5898}
5899
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005900void InstructionCodeGeneratorARM64::VisitParameterValue(
5901 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005902 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005903}
5904
5905void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5906 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005907 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005908 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005909}
5910
5911void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5912 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5913 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005914}
5915
5916void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005917 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005918 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005919 locations->SetInAt(i, Location::Any());
5920 }
5921 locations->SetOut(Location::Any());
5922}
5923
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005924void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005925 LOG(FATAL) << "Unreachable";
5926}
5927
Serban Constantinescu02164b32014-11-13 14:05:07 +00005928void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005929 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005930 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005931 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005932 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005933 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005934
5935 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005936 case DataType::Type::kInt32:
5937 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005938 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005939 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005940 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5941 break;
5942
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005943 case DataType::Type::kFloat32:
5944 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005945 InvokeRuntimeCallingConvention calling_convention;
5946 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5947 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5948 locations->SetOut(calling_convention.GetReturnLocation(type));
5949
5950 break;
5951 }
5952
Serban Constantinescu02164b32014-11-13 14:05:07 +00005953 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005954 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005955 }
5956}
5957
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005958void InstructionCodeGeneratorARM64::GenerateIntRemForPower2Denom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005959 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005960 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
5961 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
5962
5963 Register out = OutputRegister(instruction);
5964 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005965
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01005966 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01005967 // No need to adjust the result for non-negative dividends or the INT32_MIN/INT64_MIN dividends.
5968 // NOTE: The generated code for HRem correctly works for the INT32_MIN/INT64_MIN dividends.
5969 // INT*_MIN % imm must be 0 for any imm of power 2. 'and' works only with bits
5970 // 0..30 (Int32 case)/0..62 (Int64 case) of a dividend. For INT32_MIN/INT64_MIN they are zeros.
5971 // So 'and' always produces zero.
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005972 __ And(out, dividend, abs_imm - 1);
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01005973 } else {
5974 if (abs_imm == 2) {
5975 __ Cmp(dividend, 0);
5976 __ And(out, dividend, 1);
5977 __ Csneg(out, out, out, ge);
5978 } else {
5979 UseScratchRegisterScope temps(GetVIXLAssembler());
5980 Register temp = temps.AcquireSameSizeAs(out);
5981
5982 __ Negs(temp, dividend);
5983 __ And(out, dividend, abs_imm - 1);
5984 __ And(temp, temp, abs_imm - 1);
5985 __ Csneg(out, out, temp, mi);
5986 }
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005987 }
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005988}
5989
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005990void InstructionCodeGeneratorARM64::GenerateIntRemForConstDenom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005991 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005992
5993 if (imm == 0) {
5994 // Do not generate anything.
5995 // DivZeroCheck would prevent any code to be executed.
5996 return;
5997 }
5998
Evgeny Astigeevichf58dc652018-06-25 17:54:07 +01005999 if (IsPowerOfTwo(AbsOrMin(imm))) {
6000 // Cases imm == -1 or imm == 1 are handled in constant folding by
6001 // InstructionWithAbsorbingInputSimplifier.
6002 // If the cases have survided till code generation they are handled in
6003 // GenerateIntRemForPower2Denom becauses -1 and 1 are the power of 2 (2^0).
6004 // The correct code is generated for them, just more instructions.
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01006005 GenerateIntRemForPower2Denom(instruction);
6006 } else {
6007 DCHECK(imm < -2 || imm > 2) << imm;
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01006008 GenerateDivRemWithAnyConstant(instruction, imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01006009 }
6010}
6011
6012void InstructionCodeGeneratorARM64::GenerateIntRem(HRem* instruction) {
6013 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
6014 << instruction->GetResultType();
6015
6016 if (instruction->GetLocations()->InAt(1).IsConstant()) {
6017 GenerateIntRemForConstDenom(instruction);
6018 } else {
6019 Register out = OutputRegister(instruction);
6020 Register dividend = InputRegisterAt(instruction, 0);
6021 Register divisor = InputRegisterAt(instruction, 1);
6022 UseScratchRegisterScope temps(GetVIXLAssembler());
6023 Register temp = temps.AcquireSameSizeAs(out);
6024 __ Sdiv(temp, dividend, divisor);
6025 __ Msub(out, temp, divisor, dividend);
6026 }
6027}
6028
Serban Constantinescu02164b32014-11-13 14:05:07 +00006029void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006030 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00006031
Serban Constantinescu02164b32014-11-13 14:05:07 +00006032 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006033 case DataType::Type::kInt32:
6034 case DataType::Type::kInt64: {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01006035 GenerateIntRem(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006036 break;
6037 }
6038
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006039 case DataType::Type::kFloat32:
6040 case DataType::Type::kFloat64: {
6041 QuickEntrypointEnum entrypoint =
6042 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00006043 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006044 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00006045 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6046 } else {
6047 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6048 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00006049 break;
6050 }
6051
Serban Constantinescu02164b32014-11-13 14:05:07 +00006052 default:
6053 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00006054 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00006055 }
6056}
6057
Aart Bik1f8d51b2018-02-15 10:42:37 -08006058void LocationsBuilderARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01006059 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08006060}
6061
Aart Bik1f8d51b2018-02-15 10:42:37 -08006062void InstructionCodeGeneratorARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01006063 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08006064}
6065
6066void LocationsBuilderARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01006067 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08006068}
6069
6070void InstructionCodeGeneratorARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01006071 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08006072}
6073
Aart Bik3dad3412018-02-28 12:01:46 -08006074void LocationsBuilderARM64::VisitAbs(HAbs* abs) {
6075 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
6076 switch (abs->GetResultType()) {
6077 case DataType::Type::kInt32:
6078 case DataType::Type::kInt64:
6079 locations->SetInAt(0, Location::RequiresRegister());
6080 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6081 break;
6082 case DataType::Type::kFloat32:
6083 case DataType::Type::kFloat64:
6084 locations->SetInAt(0, Location::RequiresFpuRegister());
6085 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6086 break;
6087 default:
6088 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
6089 }
6090}
6091
6092void InstructionCodeGeneratorARM64::VisitAbs(HAbs* abs) {
6093 switch (abs->GetResultType()) {
6094 case DataType::Type::kInt32:
6095 case DataType::Type::kInt64: {
6096 Register in_reg = InputRegisterAt(abs, 0);
6097 Register out_reg = OutputRegister(abs);
6098 __ Cmp(in_reg, Operand(0));
6099 __ Cneg(out_reg, in_reg, lt);
6100 break;
6101 }
6102 case DataType::Type::kFloat32:
6103 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01006104 VRegister in_reg = InputFPRegisterAt(abs, 0);
6105 VRegister out_reg = OutputFPRegister(abs);
Aart Bik3dad3412018-02-28 12:01:46 -08006106 __ Fabs(out_reg, in_reg);
6107 break;
6108 }
6109 default:
6110 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
6111 }
6112}
6113
Igor Murashkind01745e2017-04-05 16:40:31 -07006114void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
6115 constructor_fence->SetLocations(nullptr);
6116}
6117
6118void InstructionCodeGeneratorARM64::VisitConstructorFence(
6119 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
6120 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
6121}
6122
Calin Juravle27df7582015-04-17 19:12:31 +01006123void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6124 memory_barrier->SetLocations(nullptr);
6125}
6126
6127void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006128 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01006129}
6130
Alexandre Rames5319def2014-10-23 10:03:10 +01006131void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006132 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006133 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00006134 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01006135}
6136
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00006137void InstructionCodeGeneratorARM64::VisitReturn(HReturn* ret) {
6138 if (GetGraph()->IsCompilingOsr()) {
6139 // To simplify callers of an OSR method, we put the return value in both
6140 // floating point and core register.
6141 switch (ret->InputAt(0)->GetType()) {
6142 case DataType::Type::kFloat32:
6143 __ Fmov(w0, s0);
6144 break;
6145 case DataType::Type::kFloat64:
6146 __ Fmov(x0, d0);
6147 break;
6148 default:
6149 break;
6150 }
6151 }
Alexandre Rames5319def2014-10-23 10:03:10 +01006152 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01006153}
6154
6155void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
6156 instruction->SetLocations(nullptr);
6157}
6158
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006159void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01006160 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01006161}
6162
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006163void LocationsBuilderARM64::VisitRor(HRor* ror) {
6164 HandleBinaryOp(ror);
6165}
6166
6167void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
6168 HandleBinaryOp(ror);
6169}
6170
Serban Constantinescu02164b32014-11-13 14:05:07 +00006171void LocationsBuilderARM64::VisitShl(HShl* shl) {
6172 HandleShift(shl);
6173}
6174
6175void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
6176 HandleShift(shl);
6177}
6178
6179void LocationsBuilderARM64::VisitShr(HShr* shr) {
6180 HandleShift(shr);
6181}
6182
6183void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
6184 HandleShift(shr);
6185}
6186
Alexandre Rames5319def2014-10-23 10:03:10 +01006187void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006188 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006189}
6190
6191void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006192 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006193}
6194
Alexandre Rames67555f72014-11-18 10:55:16 +00006195void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006196 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00006197}
6198
6199void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01006200 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00006201}
6202
6203void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01006204 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006205}
6206
Alexandre Rames67555f72014-11-18 10:55:16 +00006207void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006208 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01006209}
6210
Vladimir Marko552a1342017-10-31 10:56:47 +00006211void LocationsBuilderARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6212 codegen_->CreateStringBuilderAppendLocations(instruction, LocationFrom(x0));
6213}
6214
6215void InstructionCodeGeneratorARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6216 __ Mov(w0, instruction->GetFormat()->GetValue());
6217 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
6218}
6219
Calin Juravlee460d1d2015-09-29 04:52:17 +01006220void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
6221 HUnresolvedInstanceFieldGet* instruction) {
6222 FieldAccessCallingConventionARM64 calling_convention;
6223 codegen_->CreateUnresolvedFieldLocationSummary(
6224 instruction, instruction->GetFieldType(), calling_convention);
6225}
6226
6227void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
6228 HUnresolvedInstanceFieldGet* instruction) {
6229 FieldAccessCallingConventionARM64 calling_convention;
6230 codegen_->GenerateUnresolvedFieldAccess(instruction,
6231 instruction->GetFieldType(),
6232 instruction->GetFieldIndex(),
6233 instruction->GetDexPc(),
6234 calling_convention);
6235}
6236
6237void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
6238 HUnresolvedInstanceFieldSet* instruction) {
6239 FieldAccessCallingConventionARM64 calling_convention;
6240 codegen_->CreateUnresolvedFieldLocationSummary(
6241 instruction, instruction->GetFieldType(), calling_convention);
6242}
6243
6244void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
6245 HUnresolvedInstanceFieldSet* instruction) {
6246 FieldAccessCallingConventionARM64 calling_convention;
6247 codegen_->GenerateUnresolvedFieldAccess(instruction,
6248 instruction->GetFieldType(),
6249 instruction->GetFieldIndex(),
6250 instruction->GetDexPc(),
6251 calling_convention);
6252}
6253
6254void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
6255 HUnresolvedStaticFieldGet* instruction) {
6256 FieldAccessCallingConventionARM64 calling_convention;
6257 codegen_->CreateUnresolvedFieldLocationSummary(
6258 instruction, instruction->GetFieldType(), calling_convention);
6259}
6260
6261void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
6262 HUnresolvedStaticFieldGet* instruction) {
6263 FieldAccessCallingConventionARM64 calling_convention;
6264 codegen_->GenerateUnresolvedFieldAccess(instruction,
6265 instruction->GetFieldType(),
6266 instruction->GetFieldIndex(),
6267 instruction->GetDexPc(),
6268 calling_convention);
6269}
6270
6271void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
6272 HUnresolvedStaticFieldSet* instruction) {
6273 FieldAccessCallingConventionARM64 calling_convention;
6274 codegen_->CreateUnresolvedFieldLocationSummary(
6275 instruction, instruction->GetFieldType(), calling_convention);
6276}
6277
6278void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
6279 HUnresolvedStaticFieldSet* instruction) {
6280 FieldAccessCallingConventionARM64 calling_convention;
6281 codegen_->GenerateUnresolvedFieldAccess(instruction,
6282 instruction->GetFieldType(),
6283 instruction->GetFieldIndex(),
6284 instruction->GetDexPc(),
6285 calling_convention);
6286}
6287
Alexandre Rames5319def2014-10-23 10:03:10 +01006288void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006289 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6290 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01006291 // In suspend check slow path, usually there are no caller-save registers at all.
6292 // If SIMD instructions are present, however, we force spilling all live SIMD
6293 // registers in full width (since the runtime only saves/restores lower part).
6294 locations->SetCustomSlowPathCallerSaves(
6295 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01006296}
6297
6298void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006299 HBasicBlock* block = instruction->GetBlock();
6300 if (block->GetLoopInformation() != nullptr) {
6301 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6302 // The back edge will generate the suspend check.
6303 return;
6304 }
6305 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6306 // The goto will generate the suspend check.
6307 return;
6308 }
6309 GenerateSuspendCheck(instruction, nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08006310 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01006311}
6312
Alexandre Rames67555f72014-11-18 10:55:16 +00006313void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006314 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6315 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00006316 InvokeRuntimeCallingConvention calling_convention;
6317 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
6318}
6319
6320void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00006321 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08006322 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00006323}
6324
6325void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
6326 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006327 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006328 DataType::Type input_type = conversion->GetInputType();
6329 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006330 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6331 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006332 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
6333 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006334 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6335 }
6336
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006337 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006338 locations->SetInAt(0, Location::RequiresFpuRegister());
6339 } else {
6340 locations->SetInAt(0, Location::RequiresRegister());
6341 }
6342
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006343 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006344 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6345 } else {
6346 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6347 }
6348}
6349
6350void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006351 DataType::Type result_type = conversion->GetResultType();
6352 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00006353
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006354 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6355 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006356
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006357 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
6358 int result_size = DataType::Size(result_type);
6359 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00006360 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006361 Register output = OutputRegister(conversion);
6362 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006363 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01006364 // 'int' values are used directly as W registers, discarding the top
6365 // bits, so we don't need to sign-extend and can just perform a move.
6366 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
6367 // top 32 bits of the target register. We theoretically could leave those
6368 // bits unchanged, but we would have to make sure that no code uses a
6369 // 32bit input value as a 64bit value assuming that the top 32 bits are
6370 // zero.
6371 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006372 } else if (DataType::IsUnsignedType(result_type) ||
6373 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
6374 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006375 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00006376 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006377 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006378 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006379 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006380 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6381 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006382 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006383 } else if (DataType::IsFloatingPointType(result_type) &&
6384 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006385 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
6386 } else {
6387 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6388 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006389 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00006390}
Alexandre Rames67555f72014-11-18 10:55:16 +00006391
Serban Constantinescu02164b32014-11-13 14:05:07 +00006392void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
6393 HandleShift(ushr);
6394}
6395
6396void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
6397 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00006398}
6399
6400void LocationsBuilderARM64::VisitXor(HXor* instruction) {
6401 HandleBinaryOp(instruction);
6402}
6403
6404void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
6405 HandleBinaryOp(instruction);
6406}
6407
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006408void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006409 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006410 LOG(FATAL) << "Unreachable";
6411}
6412
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006413void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006414 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006415 LOG(FATAL) << "Unreachable";
6416}
6417
Mark Mendellfe57faa2015-09-18 09:26:15 -04006418// Simple implementation of packed switch - generate cascaded compare/jumps.
6419void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6420 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006421 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006422 locations->SetInAt(0, Location::RequiresRegister());
6423}
6424
6425void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6426 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08006427 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006428 Register value_reg = InputRegisterAt(switch_instr, 0);
6429 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6430
Zheng Xu3927c8b2015-11-18 17:46:25 +08006431 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01006432 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08006433 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
6434 // make sure we don't emit it if the target may run out of range.
6435 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
6436 // ranges and emit the tables only as required.
6437 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04006438
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006439 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08006440 // Current instruction id is an upper bound of the number of HIRs in the graph.
6441 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
6442 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006443 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6444 Register temp = temps.AcquireW();
6445 __ Subs(temp, value_reg, Operand(lower_bound));
6446
Zheng Xu3927c8b2015-11-18 17:46:25 +08006447 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006448 // Jump to successors[0] if value == lower_bound.
6449 __ B(eq, codegen_->GetLabelOf(successors[0]));
6450 int32_t last_index = 0;
6451 for (; num_entries - last_index > 2; last_index += 2) {
6452 __ Subs(temp, temp, Operand(2));
6453 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6454 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
6455 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6456 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
6457 }
6458 if (num_entries - last_index == 2) {
6459 // The last missing case_value.
6460 __ Cmp(temp, Operand(1));
6461 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08006462 }
6463
6464 // And the default for any other value.
6465 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6466 __ B(codegen_->GetLabelOf(default_block));
6467 }
6468 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01006469 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08006470
6471 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6472
6473 // Below instructions should use at most one blocked register. Since there are two blocked
6474 // registers, we are free to block one.
6475 Register temp_w = temps.AcquireW();
6476 Register index;
6477 // Remove the bias.
6478 if (lower_bound != 0) {
6479 index = temp_w;
6480 __ Sub(index, value_reg, Operand(lower_bound));
6481 } else {
6482 index = value_reg;
6483 }
6484
6485 // Jump to default block if index is out of the range.
6486 __ Cmp(index, Operand(num_entries));
6487 __ B(hs, codegen_->GetLabelOf(default_block));
6488
6489 // In current VIXL implementation, it won't require any blocked registers to encode the
6490 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
6491 // register pressure.
6492 Register table_base = temps.AcquireX();
6493 // Load jump offset from the table.
6494 __ Adr(table_base, jump_table->GetTableStartLabel());
6495 Register jump_offset = temp_w;
6496 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
6497
6498 // Jump to target block by branching to table_base(pc related) + offset.
6499 Register target_address = table_base;
6500 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
6501 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006502 }
6503}
6504
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006505void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
6506 HInstruction* instruction,
6507 Location out,
6508 uint32_t offset,
6509 Location maybe_temp,
6510 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006511 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006512 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006513 if (read_barrier_option == kWithReadBarrier) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006514 CHECK(gUseReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006515 if (kUseBakerReadBarrier) {
6516 // Load with fast path based Baker's read barrier.
6517 // /* HeapReference<Object> */ out = *(out + offset)
6518 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6519 out,
6520 out_reg,
6521 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006522 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006523 /* needs_null_check= */ false,
6524 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006525 } else {
6526 // Load with slow path based read barrier.
6527 // Save the value of `out` into `maybe_temp` before overwriting it
6528 // in the following move operation, as we will need it for the
6529 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006530 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00006531 __ Mov(temp_reg, out_reg);
6532 // /* HeapReference<Object> */ out = *(out + offset)
6533 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6534 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6535 }
6536 } else {
6537 // Plain load with no read barrier.
6538 // /* HeapReference<Object> */ out = *(out + offset)
6539 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6540 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6541 }
6542}
6543
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006544void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
6545 HInstruction* instruction,
6546 Location out,
6547 Location obj,
6548 uint32_t offset,
6549 Location maybe_temp,
6550 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006551 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006552 Register out_reg = RegisterFrom(out, type);
6553 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006554 if (read_barrier_option == kWithReadBarrier) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006555 CHECK(gUseReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006556 if (kUseBakerReadBarrier) {
6557 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00006558 // /* HeapReference<Object> */ out = *(obj + offset)
6559 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6560 out,
6561 obj_reg,
6562 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006563 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006564 /* needs_null_check= */ false,
6565 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006566 } else {
6567 // Load with slow path based read barrier.
6568 // /* HeapReference<Object> */ out = *(obj + offset)
6569 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6570 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6571 }
6572 } else {
6573 // Plain load with no read barrier.
6574 // /* HeapReference<Object> */ out = *(obj + offset)
6575 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6576 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6577 }
6578}
6579
Vladimir Markoca1e0382018-04-11 09:58:41 +00006580void CodeGeneratorARM64::GenerateGcRootFieldLoad(
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006581 HInstruction* instruction,
6582 Location root,
6583 Register obj,
6584 uint32_t offset,
6585 vixl::aarch64::Label* fixup_label,
6586 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006587 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006588 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006589 if (read_barrier_option == kWithReadBarrier) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006590 DCHECK(gUseReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006591 if (kUseBakerReadBarrier) {
6592 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00006593 // Baker's read barrier are used.
Roland Levillain44015862016-01-22 11:47:17 +00006594
Vladimir Marko008e09f32018-08-06 15:42:43 +01006595 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6596 // the Marking Register) to decide whether we need to enter
6597 // the slow path to mark the GC root.
6598 //
6599 // We use shared thunks for the slow path; shared within the method
6600 // for JIT, across methods for AOT. That thunk checks the reference
6601 // and jumps to the entrypoint if needed.
6602 //
6603 // lr = &return_address;
6604 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6605 // if (mr) { // Thread::Current()->GetIsGcMarking()
6606 // goto gc_root_thunk<root_reg>(lr)
6607 // }
6608 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00006609
Vladimir Marko008e09f32018-08-06 15:42:43 +01006610 UseScratchRegisterScope temps(GetVIXLAssembler());
6611 DCHECK(temps.IsAvailable(ip0));
6612 DCHECK(temps.IsAvailable(ip1));
6613 temps.Exclude(ip0, ip1);
6614 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
Roland Levillain44015862016-01-22 11:47:17 +00006615
Vladimir Marko008e09f32018-08-06 15:42:43 +01006616 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6617 vixl::aarch64::Label return_address;
6618 __ adr(lr, &return_address);
6619 if (fixup_label != nullptr) {
6620 __ bind(fixup_label);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006621 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006622 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
Vladimir Marko94796f82018-08-08 15:15:33 +01006623 "GC root LDR must be 2 instructions (8B) before the return address label.");
Vladimir Marko008e09f32018-08-06 15:42:43 +01006624 __ ldr(root_reg, MemOperand(obj.X(), offset));
6625 EmitBakerReadBarrierCbnz(custom_data);
6626 __ bind(&return_address);
Roland Levillain44015862016-01-22 11:47:17 +00006627 } else {
6628 // GC root loaded through a slow path for read barriers other
6629 // than Baker's.
6630 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006631 if (fixup_label == nullptr) {
6632 __ Add(root_reg.X(), obj.X(), offset);
6633 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006634 EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006635 }
Roland Levillain44015862016-01-22 11:47:17 +00006636 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00006637 GenerateReadBarrierForRootSlow(instruction, root, root);
Roland Levillain44015862016-01-22 11:47:17 +00006638 }
6639 } else {
6640 // Plain GC root load with no read barrier.
6641 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006642 if (fixup_label == nullptr) {
6643 __ Ldr(root_reg, MemOperand(obj, offset));
6644 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006645 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006646 }
Roland Levillain44015862016-01-22 11:47:17 +00006647 // Note that GC roots are not affected by heap poisoning, thus we
6648 // do not have to unpoison `root_reg` here.
6649 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006650 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00006651}
6652
Vladimir Markoc8178f52020-11-24 10:38:16 +00006653void CodeGeneratorARM64::GenerateIntrinsicCasMoveWithBakerReadBarrier(
6654 vixl::aarch64::Register marked_old_value,
Vladimir Marko94796f82018-08-08 15:15:33 +01006655 vixl::aarch64::Register old_value) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006656 DCHECK(gUseReadBarrier);
Vladimir Marko94796f82018-08-08 15:15:33 +01006657 DCHECK(kUseBakerReadBarrier);
6658
6659 // Similar to the Baker RB path in GenerateGcRootFieldLoad(), with a MOV instead of LDR.
Vladimir Markoc8178f52020-11-24 10:38:16 +00006660 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(marked_old_value.GetCode());
Vladimir Marko94796f82018-08-08 15:15:33 +01006661
6662 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6663 vixl::aarch64::Label return_address;
6664 __ adr(lr, &return_address);
6665 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6666 "GC root LDR must be 2 instructions (8B) before the return address label.");
Vladimir Markoc8178f52020-11-24 10:38:16 +00006667 __ mov(marked_old_value, old_value);
Vladimir Marko94796f82018-08-08 15:15:33 +01006668 EmitBakerReadBarrierCbnz(custom_data);
6669 __ bind(&return_address);
6670}
6671
Roland Levillain44015862016-01-22 11:47:17 +00006672void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6673 Location ref,
Vladimir Marko248141f2018-08-10 10:40:07 +01006674 vixl::aarch64::Register obj,
6675 const vixl::aarch64::MemOperand& src,
Roland Levillain44015862016-01-22 11:47:17 +00006676 bool needs_null_check,
6677 bool use_load_acquire) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006678 DCHECK(gUseReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006679 DCHECK(kUseBakerReadBarrier);
6680
Vladimir Marko0ecac682018-08-07 10:40:38 +01006681 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6682 // Marking Register) to decide whether we need to enter the slow
6683 // path to mark the reference. Then, in the slow path, check the
6684 // gray bit in the lock word of the reference's holder (`obj`) to
6685 // decide whether to mark `ref` or not.
6686 //
6687 // We use shared thunks for the slow path; shared within the method
6688 // for JIT, across methods for AOT. That thunk checks the holder
6689 // and jumps to the entrypoint if needed. If the holder is not gray,
6690 // it creates a fake dependency and returns to the LDR instruction.
6691 //
6692 // lr = &gray_return_address;
6693 // if (mr) { // Thread::Current()->GetIsGcMarking()
6694 // goto field_thunk<holder_reg, base_reg, use_load_acquire>(lr)
6695 // }
6696 // not_gray_return_address:
6697 // // Original reference load. If the offset is too large to fit
6698 // // into LDR, we use an adjusted base register here.
6699 // HeapReference<mirror::Object> reference = *(obj+offset);
6700 // gray_return_address:
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006701
Vladimir Marko248141f2018-08-10 10:40:07 +01006702 DCHECK(src.GetAddrMode() == vixl::aarch64::Offset);
6703 DCHECK_ALIGNED(src.GetOffset(), sizeof(mirror::HeapReference<mirror::Object>));
6704
6705 UseScratchRegisterScope temps(GetVIXLAssembler());
6706 DCHECK(temps.IsAvailable(ip0));
6707 DCHECK(temps.IsAvailable(ip1));
6708 temps.Exclude(ip0, ip1);
6709 uint32_t custom_data = use_load_acquire
6710 ? EncodeBakerReadBarrierAcquireData(src.GetBaseRegister().GetCode(), obj.GetCode())
6711 : EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode());
6712
6713 {
6714 ExactAssemblyScope guard(GetVIXLAssembler(),
6715 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6716 vixl::aarch64::Label return_address;
6717 __ adr(lr, &return_address);
6718 EmitBakerReadBarrierCbnz(custom_data);
6719 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6720 "Field LDR must be 1 instruction (4B) before the return address label; "
6721 " 2 instructions (8B) for heap poisoning.");
6722 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
6723 if (use_load_acquire) {
6724 DCHECK_EQ(src.GetOffset(), 0);
6725 __ ldar(ref_reg, src);
6726 } else {
6727 __ ldr(ref_reg, src);
6728 }
6729 if (needs_null_check) {
6730 MaybeRecordImplicitNullCheck(instruction);
6731 }
6732 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6733 // macro instructions disallowed in ExactAssemblyScope.
6734 if (kPoisonHeapReferences) {
6735 __ neg(ref_reg, Operand(ref_reg));
6736 }
6737 __ bind(&return_address);
6738 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006739 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Vladimir Marko248141f2018-08-10 10:40:07 +01006740}
6741
6742void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6743 Location ref,
6744 Register obj,
6745 uint32_t offset,
6746 Location maybe_temp,
6747 bool needs_null_check,
6748 bool use_load_acquire) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006749 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6750 Register base = obj;
6751 if (use_load_acquire) {
6752 DCHECK(maybe_temp.IsRegister());
6753 base = WRegisterFrom(maybe_temp);
6754 __ Add(base, obj, offset);
6755 offset = 0u;
6756 } else if (offset >= kReferenceLoadMinFarOffset) {
6757 DCHECK(maybe_temp.IsRegister());
6758 base = WRegisterFrom(maybe_temp);
6759 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6760 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6761 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006762 }
Vladimir Marko248141f2018-08-10 10:40:07 +01006763 MemOperand src(base.X(), offset);
6764 GenerateFieldLoadWithBakerReadBarrier(
6765 instruction, ref, obj, src, needs_null_check, use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006766}
6767
Artem Serov0806f582018-10-11 20:14:20 +01006768void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HArrayGet* instruction,
6769 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006770 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006771 uint32_t data_offset,
6772 Location index,
Roland Levillain44015862016-01-22 11:47:17 +00006773 bool needs_null_check) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006774 DCHECK(gUseReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006775 DCHECK(kUseBakerReadBarrier);
6776
Vladimir Marko66d691d2017-04-07 17:53:39 +01006777 static_assert(
6778 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6779 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006780 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006781
Vladimir Marko008e09f32018-08-06 15:42:43 +01006782 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6783 // Marking Register) to decide whether we need to enter the slow
6784 // path to mark the reference. Then, in the slow path, check the
6785 // gray bit in the lock word of the reference's holder (`obj`) to
6786 // decide whether to mark `ref` or not.
6787 //
6788 // We use shared thunks for the slow path; shared within the method
6789 // for JIT, across methods for AOT. That thunk checks the holder
6790 // and jumps to the entrypoint if needed. If the holder is not gray,
6791 // it creates a fake dependency and returns to the LDR instruction.
6792 //
6793 // lr = &gray_return_address;
6794 // if (mr) { // Thread::Current()->GetIsGcMarking()
6795 // goto array_thunk<base_reg>(lr)
6796 // }
6797 // not_gray_return_address:
6798 // // Original reference load. If the offset is too large to fit
6799 // // into LDR, we use an adjusted base register here.
6800 // HeapReference<mirror::Object> reference = data[index];
6801 // gray_return_address:
Vladimir Marko66d691d2017-04-07 17:53:39 +01006802
Vladimir Marko008e09f32018-08-06 15:42:43 +01006803 DCHECK(index.IsValid());
6804 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
6805 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006806
Vladimir Marko008e09f32018-08-06 15:42:43 +01006807 UseScratchRegisterScope temps(GetVIXLAssembler());
6808 DCHECK(temps.IsAvailable(ip0));
6809 DCHECK(temps.IsAvailable(ip1));
6810 temps.Exclude(ip0, ip1);
Artem Serov0806f582018-10-11 20:14:20 +01006811
6812 Register temp;
6813 if (instruction->GetArray()->IsIntermediateAddress()) {
6814 // We do not need to compute the intermediate address from the array: the
6815 // input instruction has done it already. See the comment in
6816 // `TryExtractArrayAccessAddress()`.
6817 if (kIsDebugBuild) {
6818 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
6819 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
6820 }
6821 temp = obj;
6822 } else {
6823 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
6824 __ Add(temp.X(), obj.X(), Operand(data_offset));
6825 }
6826
Vladimir Marko008e09f32018-08-06 15:42:43 +01006827 uint32_t custom_data = EncodeBakerReadBarrierArrayData(temp.GetCode());
Vladimir Marko66d691d2017-04-07 17:53:39 +01006828
Vladimir Marko008e09f32018-08-06 15:42:43 +01006829 {
6830 ExactAssemblyScope guard(GetVIXLAssembler(),
6831 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6832 vixl::aarch64::Label return_address;
6833 __ adr(lr, &return_address);
6834 EmitBakerReadBarrierCbnz(custom_data);
6835 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6836 "Array LDR must be 1 instruction (4B) before the return address label; "
6837 " 2 instructions (8B) for heap poisoning.");
6838 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6839 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6840 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6841 // macro instructions disallowed in ExactAssemblyScope.
6842 if (kPoisonHeapReferences) {
6843 __ neg(ref_reg, Operand(ref_reg));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006844 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006845 __ bind(&return_address);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006846 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006847 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Roland Levillain44015862016-01-22 11:47:17 +00006848}
6849
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006850void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6851 // The following condition is a compile-time one, so it does not have a run-time cost.
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006852 if (kIsDebugBuild && gUseReadBarrier && kUseBakerReadBarrier) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006853 // The following condition is a run-time one; it is executed after the
6854 // previous compile-time test, to avoid penalizing non-debug builds.
6855 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6856 UseScratchRegisterScope temps(GetVIXLAssembler());
6857 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6858 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6859 }
6860 }
6861}
6862
Vladimir Marko1bff99f2020-11-02 15:07:33 +00006863SlowPathCodeARM64* CodeGeneratorARM64::AddReadBarrierSlowPath(HInstruction* instruction,
6864 Location out,
6865 Location ref,
6866 Location obj,
6867 uint32_t offset,
6868 Location index) {
6869 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
6870 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6871 AddSlowPath(slow_path);
6872 return slow_path;
6873}
6874
Roland Levillain44015862016-01-22 11:47:17 +00006875void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6876 Location out,
6877 Location ref,
6878 Location obj,
6879 uint32_t offset,
6880 Location index) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006881 DCHECK(gUseReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006882
Roland Levillain44015862016-01-22 11:47:17 +00006883 // Insert a slow path based read barrier *after* the reference load.
6884 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006885 // If heap poisoning is enabled, the unpoisoning of the loaded
6886 // reference will be carried out by the runtime within the slow
6887 // path.
6888 //
6889 // Note that `ref` currently does not get unpoisoned (when heap
6890 // poisoning is enabled), which is alright as the `ref` argument is
6891 // not used by the artReadBarrierSlow entry point.
6892 //
6893 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko1bff99f2020-11-02 15:07:33 +00006894 SlowPathCodeARM64* slow_path = AddReadBarrierSlowPath(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006895
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006896 __ B(slow_path->GetEntryLabel());
6897 __ Bind(slow_path->GetExitLabel());
6898}
6899
Roland Levillain44015862016-01-22 11:47:17 +00006900void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6901 Location out,
6902 Location ref,
6903 Location obj,
6904 uint32_t offset,
6905 Location index) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006906 if (gUseReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006907 // Baker's read barriers shall be handled by the fast path
6908 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6909 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006910 // If heap poisoning is enabled, unpoisoning will be taken care of
6911 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006912 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006913 } else if (kPoisonHeapReferences) {
6914 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6915 }
6916}
6917
Roland Levillain44015862016-01-22 11:47:17 +00006918void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6919 Location out,
6920 Location root) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006921 DCHECK(gUseReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006922
Roland Levillain44015862016-01-22 11:47:17 +00006923 // Insert a slow path based read barrier *after* the GC root load.
6924 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006925 // Note that GC roots are not affected by heap poisoning, so we do
6926 // not need to do anything special for this here.
6927 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006928 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006929 AddSlowPath(slow_path);
6930
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006931 __ B(slow_path->GetEntryLabel());
6932 __ Bind(slow_path->GetExitLabel());
6933}
6934
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006935void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6936 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006937 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006938 locations->SetInAt(0, Location::RequiresRegister());
6939 locations->SetOut(Location::RequiresRegister());
6940}
6941
6942void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6943 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006944 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006945 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006946 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006947 __ Ldr(XRegisterFrom(locations->Out()),
6948 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006949 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006950 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006951 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006952 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6953 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006954 __ Ldr(XRegisterFrom(locations->Out()),
6955 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006956 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006957}
6958
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006959static void PatchJitRootUse(uint8_t* code,
6960 const uint8_t* roots_data,
6961 vixl::aarch64::Literal<uint32_t>* literal,
6962 uint64_t index_in_table) {
6963 uint32_t literal_offset = literal->GetOffset();
6964 uintptr_t address =
6965 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6966 uint8_t* data = code + literal_offset;
6967 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6968}
6969
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006970void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6971 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006972 const StringReference& string_reference = entry.first;
6973 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006974 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006975 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006976 }
6977 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006978 const TypeReference& type_reference = entry.first;
6979 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006980 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006981 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006982 }
6983}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006984
Artem Serov8ba4de12019-12-04 21:10:23 +00006985MemOperand InstructionCodeGeneratorARM64::VecNEONAddress(
Artem Serov1a719e42019-07-18 14:24:55 +01006986 HVecMemoryOperation* instruction,
6987 UseScratchRegisterScope* temps_scope,
6988 size_t size,
6989 bool is_string_char_at,
6990 /*out*/ Register* scratch) {
6991 LocationSummary* locations = instruction->GetLocations();
6992 Register base = InputRegisterAt(instruction, 0);
6993
6994 if (instruction->InputAt(1)->IsIntermediateAddressIndex()) {
6995 DCHECK(!is_string_char_at);
6996 return MemOperand(base.X(), InputRegisterAt(instruction, 1).X());
6997 }
6998
6999 Location index = locations->InAt(1);
7000 uint32_t offset = is_string_char_at
7001 ? mirror::String::ValueOffset().Uint32Value()
7002 : mirror::Array::DataOffset(size).Uint32Value();
7003 size_t shift = ComponentSizeShiftWidth(size);
7004
7005 // HIntermediateAddress optimization is only applied for scalar ArrayGet and ArraySet.
7006 DCHECK(!instruction->InputAt(0)->IsIntermediateAddress());
7007
7008 if (index.IsConstant()) {
7009 offset += Int64FromLocation(index) << shift;
7010 return HeapOperand(base, offset);
7011 } else {
7012 *scratch = temps_scope->AcquireSameSizeAs(base);
7013 __ Add(*scratch, base, Operand(WRegisterFrom(index), LSL, shift));
7014 return HeapOperand(*scratch, offset);
7015 }
7016}
7017
Artem Serov8ba4de12019-12-04 21:10:23 +00007018SVEMemOperand InstructionCodeGeneratorARM64::VecSVEAddress(
7019 HVecMemoryOperation* instruction,
7020 UseScratchRegisterScope* temps_scope,
7021 size_t size,
7022 bool is_string_char_at,
7023 /*out*/ Register* scratch) {
7024 LocationSummary* locations = instruction->GetLocations();
7025 Register base = InputRegisterAt(instruction, 0);
7026 Location index = locations->InAt(1);
7027
Artem Serov8ba4de12019-12-04 21:10:23 +00007028 DCHECK(!instruction->InputAt(1)->IsIntermediateAddressIndex());
Artem Serov8ba4de12019-12-04 21:10:23 +00007029 DCHECK(!index.IsConstant());
7030
7031 uint32_t offset = is_string_char_at
7032 ? mirror::String::ValueOffset().Uint32Value()
7033 : mirror::Array::DataOffset(size).Uint32Value();
7034 size_t shift = ComponentSizeShiftWidth(size);
7035
Raphael Gault0700b692020-09-30 08:33:10 +00007036 if (instruction->InputAt(0)->IsIntermediateAddress()) {
7037 return SVEMemOperand(base.X(), XRegisterFrom(index), LSL, shift);
7038 }
7039
Artem Serov8ba4de12019-12-04 21:10:23 +00007040 *scratch = temps_scope->AcquireSameSizeAs(base);
7041 __ Add(*scratch, base, offset);
7042 return SVEMemOperand(scratch->X(), XRegisterFrom(index), LSL, shift);
7043}
7044
Alexandre Rames67555f72014-11-18 10:55:16 +00007045#undef __
7046#undef QUICK_ENTRY_POINT
7047
Vladimir Markoca1e0382018-04-11 09:58:41 +00007048#define __ assembler.GetVIXLAssembler()->
7049
7050static void EmitGrayCheckAndFastPath(arm64::Arm64Assembler& assembler,
7051 vixl::aarch64::Register base_reg,
7052 vixl::aarch64::MemOperand& lock_word,
Vladimir Marko7a695052018-04-12 10:26:50 +01007053 vixl::aarch64::Label* slow_path,
7054 vixl::aarch64::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00007055 // Load the lock word containing the rb_state.
7056 __ Ldr(ip0.W(), lock_word);
7057 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01007058 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Vladimir Markoca1e0382018-04-11 09:58:41 +00007059 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7060 __ Tbnz(ip0.W(), LockWord::kReadBarrierStateShift, slow_path);
7061 static_assert(
7062 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET,
7063 "Field and array LDR offsets must be the same to reuse the same code.");
Vladimir Marko7a695052018-04-12 10:26:50 +01007064 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
7065 if (throw_npe != nullptr) {
7066 __ Bind(throw_npe);
7067 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00007068 // Adjust the return address back to the LDR (1 instruction; 2 for heap poisoning).
7069 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
7070 "Field LDR must be 1 instruction (4B) before the return address label; "
7071 " 2 instructions (8B) for heap poisoning.");
7072 __ Add(lr, lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
7073 // Introduce a dependency on the lock_word including rb_state,
7074 // to prevent load-load reordering, and without using
7075 // a memory barrier (which would be more expensive).
7076 __ Add(base_reg, base_reg, Operand(ip0, LSR, 32));
7077 __ Br(lr); // And return back to the function.
7078 // Note: The fake dependency is unnecessary for the slow path.
7079}
7080
7081// Load the read barrier introspection entrypoint in register `entrypoint`.
7082static void LoadReadBarrierMarkIntrospectionEntrypoint(arm64::Arm64Assembler& assembler,
7083 vixl::aarch64::Register entrypoint) {
7084 // entrypoint = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
7085 DCHECK_EQ(ip0.GetCode(), 16u);
7086 const int32_t entry_point_offset =
7087 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
7088 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
7089}
7090
7091void CodeGeneratorARM64::CompileBakerReadBarrierThunk(Arm64Assembler& assembler,
7092 uint32_t encoded_data,
7093 /*out*/ std::string* debug_name) {
7094 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
7095 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01007096 case BakerReadBarrierKind::kField:
7097 case BakerReadBarrierKind::kAcquire: {
Artem Serova07de552020-11-01 22:42:43 +00007098 Register base_reg =
7099 vixl::aarch64::XRegister(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00007100 CheckValidReg(base_reg.GetCode());
Artem Serova07de552020-11-01 22:42:43 +00007101 Register holder_reg =
7102 vixl::aarch64::XRegister(BakerReadBarrierSecondRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00007103 CheckValidReg(holder_reg.GetCode());
7104 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
7105 temps.Exclude(ip0, ip1);
Roland Levillain988c3912019-09-25 19:33:35 +01007106 // In the case of a field load (with relaxed semantic), if `base_reg` differs from
7107 // `holder_reg`, the offset was too large and we must have emitted (during the construction
7108 // of the HIR graph, see `art::HInstructionBuilder::BuildInstanceFieldAccess`) and preserved
7109 // (see `art::PrepareForRegisterAllocation::VisitNullCheck`) an explicit null check before
7110 // the load. Otherwise, for implicit null checks, we need to null-check the holder as we do
7111 // not necessarily do that check before going to the thunk.
7112 //
7113 // In the case of a field load with load-acquire semantics (where `base_reg` always differs
7114 // from `holder_reg`), we also need an explicit null check when implicit null checks are
7115 // allowed, as we do not emit one before going to the thunk.
Vladimir Marko7a695052018-04-12 10:26:50 +01007116 vixl::aarch64::Label throw_npe_label;
7117 vixl::aarch64::Label* throw_npe = nullptr;
Roland Levillain988c3912019-09-25 19:33:35 +01007118 if (GetCompilerOptions().GetImplicitNullChecks() &&
7119 (holder_reg.Is(base_reg) || (kind == BakerReadBarrierKind::kAcquire))) {
Vladimir Marko7a695052018-04-12 10:26:50 +01007120 throw_npe = &throw_npe_label;
7121 __ Cbz(holder_reg.W(), throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00007122 }
Vladimir Marko7a695052018-04-12 10:26:50 +01007123 // Check if the holder is gray and, if not, add fake dependency to the base register
7124 // and return to the LDR instruction to load the reference. Otherwise, use introspection
7125 // to load the reference and call the entrypoint that performs further checks on the
7126 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00007127 vixl::aarch64::Label slow_path;
7128 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
Vladimir Marko7a695052018-04-12 10:26:50 +01007129 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00007130 __ Bind(&slow_path);
Vladimir Marko0ecac682018-08-07 10:40:38 +01007131 if (kind == BakerReadBarrierKind::kField) {
7132 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
7133 __ Ldr(ip0.W(), ldr_address); // Load the LDR (immediate) unsigned offset.
7134 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
7135 __ Ubfx(ip0.W(), ip0.W(), 10, 12); // Extract the offset.
7136 __ Ldr(ip0.W(), MemOperand(base_reg, ip0, LSL, 2)); // Load the reference.
7137 } else {
7138 DCHECK(kind == BakerReadBarrierKind::kAcquire);
7139 DCHECK(!base_reg.Is(holder_reg));
7140 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
7141 __ Ldar(ip0.W(), MemOperand(base_reg));
7142 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00007143 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
7144 __ Br(ip1); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00007145 break;
7146 }
7147 case BakerReadBarrierKind::kArray: {
Artem Serova07de552020-11-01 22:42:43 +00007148 Register base_reg =
7149 vixl::aarch64::XRegister(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00007150 CheckValidReg(base_reg.GetCode());
7151 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7152 BakerReadBarrierSecondRegField::Decode(encoded_data));
7153 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
7154 temps.Exclude(ip0, ip1);
7155 vixl::aarch64::Label slow_path;
7156 int32_t data_offset =
7157 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
7158 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
7159 DCHECK_LT(lock_word.GetOffset(), 0);
7160 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path);
7161 __ Bind(&slow_path);
7162 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
7163 __ Ldr(ip0.W(), ldr_address); // Load the LDR (register) unsigned offset.
7164 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
7165 __ Ubfx(ip0, ip0, 16, 6); // Extract the index register, plus 32 (bit 21 is set).
7166 __ Bfi(ip1, ip0, 3, 6); // Insert ip0 to the entrypoint address to create
7167 // a switch case target based on the index register.
7168 __ Mov(ip0, base_reg); // Move the base register to ip0.
7169 __ Br(ip1); // Jump to the entrypoint's array switch case.
7170 break;
7171 }
7172 case BakerReadBarrierKind::kGcRoot: {
7173 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
7174 // and it does not have a forwarding address), call the correct introspection entrypoint;
7175 // otherwise return the reference (or the extracted forwarding address).
7176 // There is no gray bit check for GC roots.
Artem Serova07de552020-11-01 22:42:43 +00007177 Register root_reg =
7178 vixl::aarch64::WRegister(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00007179 CheckValidReg(root_reg.GetCode());
7180 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7181 BakerReadBarrierSecondRegField::Decode(encoded_data));
7182 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
7183 temps.Exclude(ip0, ip1);
7184 vixl::aarch64::Label return_label, not_marked, forwarding_address;
7185 __ Cbz(root_reg, &return_label);
7186 MemOperand lock_word(root_reg.X(), mirror::Object::MonitorOffset().Int32Value());
7187 __ Ldr(ip0.W(), lock_word);
7188 __ Tbz(ip0.W(), LockWord::kMarkBitStateShift, &not_marked);
7189 __ Bind(&return_label);
7190 __ Br(lr);
7191 __ Bind(&not_marked);
7192 __ Tst(ip0.W(), Operand(ip0.W(), LSL, 1));
7193 __ B(&forwarding_address, mi);
7194 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
7195 // Adjust the art_quick_read_barrier_mark_introspection address in IP1 to
7196 // art_quick_read_barrier_mark_introspection_gc_roots.
7197 __ Add(ip1, ip1, Operand(BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRYPOINT_OFFSET));
7198 __ Mov(ip0.W(), root_reg);
7199 __ Br(ip1);
7200 __ Bind(&forwarding_address);
7201 __ Lsl(root_reg, ip0.W(), LockWord::kForwardingAddressShift);
7202 __ Br(lr);
7203 break;
7204 }
7205 default:
7206 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
7207 UNREACHABLE();
7208 }
7209
Vladimir Marko966b46f2018-08-03 10:20:19 +00007210 // For JIT, the slow path is considered part of the compiled method,
Vladimir Markof91fc122020-05-13 09:21:00 +01007211 // so JIT should pass null as `debug_name`.
Santiago Aboy Solanes872ec722022-02-18 14:10:25 +00007212 DCHECK_IMPLIES(GetCompilerOptions().IsJitCompiler(), debug_name == nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00007213 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00007214 std::ostringstream oss;
7215 oss << "BakerReadBarrierThunk";
7216 switch (kind) {
7217 case BakerReadBarrierKind::kField:
7218 oss << "Field_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
7219 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
7220 break;
Vladimir Marko0ecac682018-08-07 10:40:38 +01007221 case BakerReadBarrierKind::kAcquire:
7222 oss << "Acquire_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
7223 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
7224 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +00007225 case BakerReadBarrierKind::kArray:
7226 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
7227 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7228 BakerReadBarrierSecondRegField::Decode(encoded_data));
7229 break;
7230 case BakerReadBarrierKind::kGcRoot:
7231 oss << "GcRoot_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
7232 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7233 BakerReadBarrierSecondRegField::Decode(encoded_data));
7234 break;
7235 }
7236 *debug_name = oss.str();
7237 }
7238}
7239
7240#undef __
7241
Alexandre Rames5319def2014-10-23 10:03:10 +01007242} // namespace arm64
7243} // namespace art