blob: 079c440033c0ac83954bf3a30c0e6b4aefef171c [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"
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +000046#include "optimizing/nodes.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010047#include "thread.h"
48#include "utils/arm64/assembler_arm64.h"
49#include "utils/assembler.h"
50#include "utils/stack_checks.h"
51
Scott Wakeling97c72b72016-06-24 16:19:36 +010052using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000053using vixl::ExactAssemblyScope;
54using vixl::CodeBufferCheckScope;
55using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010056
57#ifdef __
58#error "ARM64 Codegen VIXL macro-assembler macro already defined."
59#endif
60
VladimĂ­r Marko434d9682022-11-04 14:04:17 +000061namespace art HIDDEN {
Alexandre Rames5319def2014-10-23 10:03:10 +010062
Roland Levillain22ccc3a2015-11-24 13:10:05 +000063template<class MirrorType>
64class GcRoot;
65
Alexandre Rames5319def2014-10-23 10:03:10 +010066namespace arm64 {
67
Alexandre Ramesbe919d92016-08-23 18:33:36 +010068using helpers::ARM64EncodableConstantOrRegister;
69using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080070using helpers::CPURegisterFrom;
71using helpers::DRegisterFrom;
72using helpers::FPRegisterFrom;
73using helpers::HeapOperand;
74using helpers::HeapOperandFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010075using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080076using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080077using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010078using helpers::InputRegisterAt;
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +010079using helpers::Int64FromLocation;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010080using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080081using helpers::LocationFrom;
82using helpers::OperandFromMemOperand;
83using helpers::OutputCPURegister;
84using helpers::OutputFPRegister;
85using helpers::OutputRegister;
86using helpers::RegisterFrom;
87using helpers::StackOperandFrom;
88using helpers::VIXLRegCodeFromART;
89using helpers::WRegisterFrom;
90using helpers::XRegisterFrom;
91
Vladimir Markof3e0ee22015-12-17 15:23:13 +000092// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080093// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
94// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000095static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010096
Vladimir Markof4f2daa2017-03-20 18:26:59 +000097// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
98// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
Vladimir Marko008e09f32018-08-06 15:42:43 +010099// For the Baker read barrier implementation using link-time generated thunks we need to split
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000100// the offset explicitly.
101constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
102
Alexandre Rames5319def2014-10-23 10:03:10 +0100103inline Condition ARM64Condition(IfCondition cond) {
104 switch (cond) {
105 case kCondEQ: return eq;
106 case kCondNE: return ne;
107 case kCondLT: return lt;
108 case kCondLE: return le;
109 case kCondGT: return gt;
110 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700111 case kCondB: return lo;
112 case kCondBE: return ls;
113 case kCondA: return hi;
114 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100115 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000116 LOG(FATAL) << "Unreachable";
117 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100118}
119
Vladimir Markod6e069b2016-01-18 11:11:01 +0000120inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
121 // The ARM64 condition codes can express all the necessary branches, see the
122 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
123 // There is no dex instruction or HIR that would need the missing conditions
124 // "equal or unordered" or "not equal".
125 switch (cond) {
126 case kCondEQ: return eq;
127 case kCondNE: return ne /* unordered */;
128 case kCondLT: return gt_bias ? cc : lt /* unordered */;
129 case kCondLE: return gt_bias ? ls : le /* unordered */;
130 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
131 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
132 default:
133 LOG(FATAL) << "UNREACHABLE";
134 UNREACHABLE();
135 }
136}
137
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100138Location ARM64ReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000139 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
140 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
141 // but we use the exact registers for clarity.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100142 if (return_type == DataType::Type::kFloat32) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000143 return LocationFrom(s0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100144 } else if (return_type == DataType::Type::kFloat64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000145 return LocationFrom(d0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100146 } else if (return_type == DataType::Type::kInt64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000147 return LocationFrom(x0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100148 } else if (return_type == DataType::Type::kVoid) {
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100149 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000150 } else {
151 return LocationFrom(w0);
152 }
153}
154
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100155Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000156 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100157}
158
Vladimir Marko3232dbb2018-07-25 15:42:46 +0100159static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
160 InvokeRuntimeCallingConvention calling_convention;
161 RegisterSet caller_saves = RegisterSet::Empty();
162 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
163 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
164 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
165 DataType::Type::kReference).GetCode());
166 return caller_saves;
167}
168
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100169// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
170#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700171#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100172
Zheng Xuda403092015-04-24 17:35:39 +0800173void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800174 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Andreas Gampe3db70682018-12-26 15:12:03 -0800175 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100176 for (uint32_t i : LowToHighBits(core_spills)) {
177 // If the register holds an object, update the stack mask.
178 if (locations->RegisterContainsObject(i)) {
179 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800180 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100181 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
182 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
183 saved_core_stack_offsets_[i] = stack_offset;
184 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800185 }
186
Artem Serovc8150b52019-07-31 18:28:00 +0100187 const size_t fp_reg_size = codegen->GetSlowPathFPWidth();
Andreas Gampe3db70682018-12-26 15:12:03 -0800188 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100189 for (uint32_t i : LowToHighBits(fp_spills)) {
190 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
191 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
192 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serov9df37b92019-07-23 16:41:54 +0100193 stack_offset += fp_reg_size;
Zheng Xuda403092015-04-24 17:35:39 +0800194 }
195
Artem Serov55ab7e82020-04-27 21:02:28 +0100196 InstructionCodeGeneratorARM64* visitor =
197 down_cast<CodeGeneratorARM64*>(codegen)->GetInstructionCodeGeneratorArm64();
198 visitor->SaveLiveRegistersHelper(locations, codegen->GetFirstRegisterSlotInSlowPath());
Zheng Xuda403092015-04-24 17:35:39 +0800199}
200
201void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Artem Serov55ab7e82020-04-27 21:02:28 +0100202 InstructionCodeGeneratorARM64* visitor =
203 down_cast<CodeGeneratorARM64*>(codegen)->GetInstructionCodeGeneratorArm64();
204 visitor->RestoreLiveRegistersHelper(locations, codegen->GetFirstRegisterSlotInSlowPath());
Zheng Xuda403092015-04-24 17:35:39 +0800205}
206
Alexandre Rames5319def2014-10-23 10:03:10 +0100207class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
208 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000209 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100210
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100211 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100212 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000213 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100214
Alexandre Rames5319def2014-10-23 10:03:10 +0100215 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000216 if (instruction_->CanThrowIntoCatchBlock()) {
217 // Live registers will be restored in the catch block if caught.
218 SaveLiveRegisters(codegen, instruction_->GetLocations());
219 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000220 // We're moving two locations to locations that could overlap, so we need a parallel
221 // move resolver.
222 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100223 codegen->EmitParallelMoves(locations->InAt(0),
224 LocationFrom(calling_convention.GetRegisterAt(0)),
225 DataType::Type::kInt32,
226 locations->InAt(1),
227 LocationFrom(calling_convention.GetRegisterAt(1)),
228 DataType::Type::kInt32);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000229 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
230 ? kQuickThrowStringBounds
231 : kQuickThrowArrayBounds;
232 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100233 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800234 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100235 }
236
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100237 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100238
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100239 const char* GetDescription() const override { return "BoundsCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100240
Alexandre Rames5319def2014-10-23 10:03:10 +0100241 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100242 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
243};
244
Alexandre Rames67555f72014-11-18 10:55:16 +0000245class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
246 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000247 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000248
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100249 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000250 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
251 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000252 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800253 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000254 }
255
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100256 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100257
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100258 const char* GetDescription() const override { return "DivZeroCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100259
Alexandre Rames67555f72014-11-18 10:55:16 +0000260 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000261 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
262};
263
264class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
265 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100266 LoadClassSlowPathARM64(HLoadClass* cls, HInstruction* at)
267 : SlowPathCodeARM64(at), cls_(cls) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000268 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100269 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Alexandre Rames67555f72014-11-18 10:55:16 +0000270 }
271
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100272 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000273 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000274 Location out = locations->Out();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100275 const uint32_t dex_pc = instruction_->GetDexPc();
276 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
277 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
Alexandre Rames67555f72014-11-18 10:55:16 +0000278
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100279 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames67555f72014-11-18 10:55:16 +0000280 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000281 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000282
Vladimir Markof3c52b42017-11-17 17:32:12 +0000283 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100284 if (must_resolve_type) {
Santiago Aboy Solanesa0232ad2021-11-08 17:00:06 +0000285 DCHECK(IsSameDexFile(cls_->GetDexFile(), arm64_codegen->GetGraph()->GetDexFile()) ||
Santiago Aboy Solanes69a87e32022-03-08 16:43:54 +0000286 arm64_codegen->GetCompilerOptions().WithinOatFile(&cls_->GetDexFile()) ||
287 ContainsElement(Runtime::Current()->GetClassLinker()->GetBootClassPath(),
288 &cls_->GetDexFile()));
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100289 dex::TypeIndex type_index = cls_->GetTypeIndex();
290 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Vladimir Marko8f63f102020-09-28 12:10:28 +0100291 if (cls_->NeedsAccessCheck()) {
292 CheckEntrypointTypes<kQuickResolveTypeAndVerifyAccess, void*, uint32_t>();
293 arm64_codegen->InvokeRuntime(kQuickResolveTypeAndVerifyAccess, instruction_, dex_pc, this);
294 } else {
295 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
296 arm64_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
297 }
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100298 // If we also must_do_clinit, the resolved type is now in the correct register.
299 } else {
300 DCHECK(must_do_clinit);
301 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
302 arm64_codegen->MoveLocation(LocationFrom(calling_convention.GetRegisterAt(0)),
303 source,
304 cls_->GetType());
305 }
306 if (must_do_clinit) {
307 arm64_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
308 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800309 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000310
311 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000312 if (out.IsValid()) {
313 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100314 DataType::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000315 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000316 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000317 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000318 __ B(GetExitLabel());
319 }
320
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100321 const char* GetDescription() const override { return "LoadClassSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100322
Alexandre Rames67555f72014-11-18 10:55:16 +0000323 private:
324 // The class this slow path will load.
325 HLoadClass* const cls_;
326
Alexandre Rames67555f72014-11-18 10:55:16 +0000327 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
328};
329
Vladimir Markoaad75c62016-10-03 08:46:48 +0000330class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
331 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000332 explicit LoadStringSlowPathARM64(HLoadString* instruction)
333 : SlowPathCodeARM64(instruction) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000334
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100335 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000336 LocationSummary* locations = instruction_->GetLocations();
337 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
338 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
339
340 __ Bind(GetEntryLabel());
341 SaveLiveRegisters(codegen, locations);
342
Vladimir Markof3c52b42017-11-17 17:32:12 +0000343 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000344 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
345 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000346 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
347 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100348 DataType::Type type = instruction_->GetType();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000349 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
350
351 RestoreLiveRegisters(codegen, locations);
352
Vladimir Markoaad75c62016-10-03 08:46:48 +0000353 __ B(GetExitLabel());
354 }
355
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100356 const char* GetDescription() const override { return "LoadStringSlowPathARM64"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000357
358 private:
359 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
360};
361
Alexandre Rames5319def2014-10-23 10:03:10 +0100362class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
363 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000364 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100365
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100366 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000367 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100368 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000369 if (instruction_->CanThrowIntoCatchBlock()) {
370 // Live registers will be restored in the catch block if caught.
371 SaveLiveRegisters(codegen, instruction_->GetLocations());
372 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000373 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
374 instruction_,
375 instruction_->GetDexPc(),
376 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800377 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100378 }
379
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100380 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100381
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100382 const char* GetDescription() const override { return "NullCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100383
Alexandre Rames5319def2014-10-23 10:03:10 +0100384 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100385 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
386};
387
388class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
389 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100390 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000391 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100392
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100393 void EmitNativeCode(CodeGenerator* codegen) override {
Artem Serov7957d952017-04-04 15:44:09 +0100394 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000395 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100396 __ Bind(GetEntryLabel());
Artem Serov1a719e42019-07-18 14:24:55 +0100397 SaveLiveRegisters(codegen, locations); // Only saves live vector regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000398 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800399 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov1a719e42019-07-18 14:24:55 +0100400 RestoreLiveRegisters(codegen, locations); // Only restores live vector regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000401 if (successor_ == nullptr) {
402 __ B(GetReturnLabel());
403 } else {
404 __ B(arm64_codegen->GetLabelOf(successor_));
405 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100406 }
407
Scott Wakeling97c72b72016-06-24 16:19:36 +0100408 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100409 DCHECK(successor_ == nullptr);
410 return &return_label_;
411 }
412
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100413 HBasicBlock* GetSuccessor() const {
414 return successor_;
415 }
416
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100417 const char* GetDescription() const override { return "SuspendCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100418
Alexandre Rames5319def2014-10-23 10:03:10 +0100419 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100420 // If not null, the block to branch to after the suspend check.
421 HBasicBlock* const successor_;
422
423 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100424 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100425
426 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
427};
428
Alexandre Rames67555f72014-11-18 10:55:16 +0000429class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
430 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000431 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000432 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000433
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100434 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000435 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800436
Alexandre Rames3e69f162014-12-10 10:36:50 +0000437 DCHECK(instruction_->IsCheckCast()
438 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
439 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100440 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000441
Alexandre Rames67555f72014-11-18 10:55:16 +0000442 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000443
Vladimir Marko87584542017-12-12 17:47:52 +0000444 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000445 SaveLiveRegisters(codegen, locations);
446 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000447
448 // We're moving two locations to locations that could overlap, so we need a parallel
449 // move resolver.
450 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800451 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800452 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100453 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800454 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800455 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100456 DataType::Type::kReference);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000457 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000458 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800459 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100460 DataType::Type ret_type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000461 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
462 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
463 } else {
464 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800465 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
466 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000467 }
468
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000469 if (!is_fatal_) {
470 RestoreLiveRegisters(codegen, locations);
471 __ B(GetExitLabel());
472 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000473 }
474
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100475 const char* GetDescription() const override { return "TypeCheckSlowPathARM64"; }
476 bool IsFatal() const override { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100477
Alexandre Rames67555f72014-11-18 10:55:16 +0000478 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000479 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000480
Alexandre Rames67555f72014-11-18 10:55:16 +0000481 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
482};
483
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700484class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
485 public:
Aart Bik42249c32016-01-07 15:33:50 -0800486 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000487 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700488
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100489 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bik42249c32016-01-07 15:33:50 -0800490 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700491 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100492 LocationSummary* locations = instruction_->GetLocations();
493 SaveLiveRegisters(codegen, locations);
494 InvokeRuntimeCallingConvention calling_convention;
495 __ Mov(calling_convention.GetRegisterAt(0),
496 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000497 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100498 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700499 }
500
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100501 const char* GetDescription() const override { return "DeoptimizationSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100502
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700503 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700504 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
505};
506
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100507class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
508 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000509 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100510
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100511 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100512 LocationSummary* locations = instruction_->GetLocations();
513 __ Bind(GetEntryLabel());
514 SaveLiveRegisters(codegen, locations);
515
516 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100517 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100518 parallel_move.AddMove(
519 locations->InAt(0),
520 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100521 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100522 nullptr);
523 parallel_move.AddMove(
524 locations->InAt(1),
525 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100526 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100527 nullptr);
528 parallel_move.AddMove(
529 locations->InAt(2),
530 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100531 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100532 nullptr);
533 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
534
535 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000536 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100537 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
538 RestoreLiveRegisters(codegen, locations);
539 __ B(GetExitLabel());
540 }
541
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100542 const char* GetDescription() const override { return "ArraySetSlowPathARM64"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100543
544 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100545 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
546};
547
Zheng Xu3927c8b2015-11-18 17:46:25 +0800548void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
549 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000550 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800551
552 // We are about to use the assembler to place literals directly. Make sure we have enough
553 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000554 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
555 num_entries * sizeof(int32_t),
556 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800557
558 __ Bind(&table_start_);
559 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
560 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100561 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800562 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100563 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800564 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
565 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
566 Literal<int32_t> literal(jump_offset);
567 __ place(&literal);
568 }
569}
570
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000571// Slow path generating a read barrier for a heap reference.
572class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
573 public:
574 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
575 Location out,
576 Location ref,
577 Location obj,
578 uint32_t offset,
579 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000580 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000581 out_(out),
582 ref_(ref),
583 obj_(obj),
584 offset_(offset),
585 index_(index) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +0000586 DCHECK(gUseReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000587 // If `obj` is equal to `out` or `ref`, it means the initial object
588 // has been overwritten by (or after) the heap object reference load
589 // to be instrumented, e.g.:
590 //
591 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000592 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000593 //
594 // In that case, we have lost the information about the original
595 // object, and the emitted read barrier cannot work properly.
596 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
597 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
598 }
599
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100600 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000601 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
602 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100603 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000604 DCHECK(locations->CanCall());
605 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100606 DCHECK(instruction_->IsInstanceFieldGet() ||
Alex Light3a73ffb2021-01-25 14:11:05 +0000607 instruction_->IsPredicatedInstanceFieldGet() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100608 instruction_->IsStaticFieldGet() ||
609 instruction_->IsArrayGet() ||
610 instruction_->IsInstanceOf() ||
611 instruction_->IsCheckCast() ||
Vladimir Markoa41ea272020-09-07 15:24:36 +0000612 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000613 << "Unexpected instruction in read barrier for heap reference slow path: "
614 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000615 // The read barrier instrumentation of object ArrayGet
616 // instructions does not support the HIntermediateAddress
617 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000618 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100619 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000620
621 __ Bind(GetEntryLabel());
622
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000623 SaveLiveRegisters(codegen, locations);
624
625 // We may have to change the index's value, but as `index_` is a
626 // constant member (like other "inputs" of this slow path),
627 // introduce a copy of it, `index`.
628 Location index = index_;
629 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100630 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000631 if (instruction_->IsArrayGet()) {
632 // Compute the actual memory offset and store it in `index`.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100633 Register index_reg = RegisterFrom(index_, DataType::Type::kInt32);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000634 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
635 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
636 // We are about to change the value of `index_reg` (see the
637 // calls to vixl::MacroAssembler::Lsl and
638 // vixl::MacroAssembler::Mov below), but it has
639 // not been saved by the previous call to
640 // art::SlowPathCode::SaveLiveRegisters, as it is a
641 // callee-save register --
642 // art::SlowPathCode::SaveLiveRegisters does not consider
643 // callee-save registers, as it has been designed with the
644 // assumption that callee-save registers are supposed to be
645 // handled by the called function. So, as a callee-save
646 // register, `index_reg` _would_ eventually be saved onto
647 // the stack, but it would be too late: we would have
648 // changed its value earlier. Therefore, we manually save
649 // it here into another freely available register,
650 // `free_reg`, chosen of course among the caller-save
651 // registers (as a callee-save `free_reg` register would
652 // exhibit the same problem).
653 //
654 // Note we could have requested a temporary register from
655 // the register allocator instead; but we prefer not to, as
656 // this is a slow path, and we know we can find a
657 // caller-save register that is available.
658 Register free_reg = FindAvailableCallerSaveRegister(codegen);
659 __ Mov(free_reg.W(), index_reg);
660 index_reg = free_reg;
661 index = LocationFrom(index_reg);
662 } else {
663 // The initial register stored in `index_` has already been
664 // saved in the call to art::SlowPathCode::SaveLiveRegisters
665 // (as it is not a callee-save register), so we can freely
666 // use it.
667 }
668 // Shifting the index value contained in `index_reg` by the scale
669 // factor (2) cannot overflow in practice, as the runtime is
670 // unable to allocate object arrays with a size larger than
671 // 2^26 - 1 (that is, 2^28 - 4 bytes).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100672 __ Lsl(index_reg, index_reg, DataType::SizeShift(type));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000673 static_assert(
674 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
675 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
676 __ Add(index_reg, index_reg, Operand(offset_));
677 } else {
Ulya Trafimovichec696e52022-01-26 10:21:32 +0000678 // In the case of the following intrinsics `index_` is not shifted by a scale factor of 2
679 // (as in the case of ArrayGet), as it is actually an offset to an object field within an
680 // object.
Roland Levillain3d312422016-06-23 13:53:42 +0100681 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000682 DCHECK(instruction_->GetLocations()->Intrinsified());
Ulya Trafimovichec696e52022-01-26 10:21:32 +0000683 HInvoke* invoke = instruction_->AsInvoke();
684 DCHECK(IsUnsafeGetObject(invoke) ||
685 IsVarHandleGet(invoke) ||
686 IsUnsafeCASObject(invoke) ||
687 IsVarHandleCASFamily(invoke)) << invoke->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100688 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100689 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000690 }
691 }
692
693 // We're moving two or three locations to locations that could
694 // overlap, so we need a parallel move resolver.
695 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100696 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000697 parallel_move.AddMove(ref_,
698 LocationFrom(calling_convention.GetRegisterAt(0)),
699 type,
700 nullptr);
701 parallel_move.AddMove(obj_,
702 LocationFrom(calling_convention.GetRegisterAt(1)),
703 type,
704 nullptr);
705 if (index.IsValid()) {
706 parallel_move.AddMove(index,
707 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100708 DataType::Type::kInt32,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000709 nullptr);
710 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
711 } else {
712 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
713 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
714 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000715 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000716 instruction_,
717 instruction_->GetDexPc(),
718 this);
719 CheckEntrypointTypes<
720 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
721 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
722
723 RestoreLiveRegisters(codegen, locations);
724
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000725 __ B(GetExitLabel());
726 }
727
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100728 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000729
730 private:
731 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100732 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
733 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000734 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
735 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
736 return Register(VIXLRegCodeFromART(i), kXRegSize);
737 }
738 }
739 // We shall never fail to find a free caller-save register, as
740 // there are more than two core caller-save registers on ARM64
741 // (meaning it is possible to find one which is different from
742 // `ref` and `obj`).
743 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
744 LOG(FATAL) << "Could not find a free register";
745 UNREACHABLE();
746 }
747
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000748 const Location out_;
749 const Location ref_;
750 const Location obj_;
751 const uint32_t offset_;
752 // An additional location containing an index to an array.
753 // Only used for HArrayGet and the UnsafeGetObject &
754 // UnsafeGetObjectVolatile intrinsics.
755 const Location index_;
756
757 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
758};
759
760// Slow path generating a read barrier for a GC root.
761class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
762 public:
763 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000764 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +0000765 DCHECK(gUseReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +0000766 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000767
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100768 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000769 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100770 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000771 DCHECK(locations->CanCall());
772 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Vladimir Markoa41ea272020-09-07 15:24:36 +0000773 DCHECK(instruction_->IsLoadClass() ||
774 instruction_->IsLoadString() ||
775 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000776 << "Unexpected instruction in read barrier for GC root slow path: "
777 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000778
779 __ Bind(GetEntryLabel());
780 SaveLiveRegisters(codegen, locations);
781
782 InvokeRuntimeCallingConvention calling_convention;
783 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
784 // The argument of the ReadBarrierForRootSlow is not a managed
785 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
786 // thus we need a 64-bit move here, and we cannot use
787 //
788 // arm64_codegen->MoveLocation(
789 // LocationFrom(calling_convention.GetRegisterAt(0)),
790 // root_,
791 // type);
792 //
793 // which would emit a 32-bit move, as `type` is a (32-bit wide)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100794 // reference type (`DataType::Type::kReference`).
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000795 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000796 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000797 instruction_,
798 instruction_->GetDexPc(),
799 this);
800 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
801 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
802
803 RestoreLiveRegisters(codegen, locations);
804 __ B(GetExitLabel());
805 }
806
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100807 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000808
809 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000810 const Location out_;
811 const Location root_;
812
813 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
814};
815
Mythri Alle5097f832021-11-02 14:52:30 +0000816class MethodEntryExitHooksSlowPathARM64 : public SlowPathCodeARM64 {
817 public:
818 explicit MethodEntryExitHooksSlowPathARM64(HInstruction* instruction)
819 : SlowPathCodeARM64(instruction) {}
820
821 void EmitNativeCode(CodeGenerator* codegen) override {
822 LocationSummary* locations = instruction_->GetLocations();
823 QuickEntrypointEnum entry_point =
824 (instruction_->IsMethodEntryHook()) ? kQuickMethodEntryHook : kQuickMethodExitHook;
825 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
826 __ Bind(GetEntryLabel());
827 SaveLiveRegisters(codegen, locations);
Mythri Allebab6beb2022-10-21 13:28:05 +0000828 if (instruction_->IsMethodExitHook()) {
829 __ Mov(vixl::aarch64::x4, arm64_codegen->GetFrameSize());
830 }
Mythri Alle5097f832021-11-02 14:52:30 +0000831 arm64_codegen->InvokeRuntime(entry_point, instruction_, instruction_->GetDexPc(), this);
832 RestoreLiveRegisters(codegen, locations);
833 __ B(GetExitLabel());
834 }
835
836 const char* GetDescription() const override {
837 return "MethodEntryExitHooksSlowPath";
838 }
839
840 private:
841 DISALLOW_COPY_AND_ASSIGN(MethodEntryExitHooksSlowPathARM64);
842};
843
Nicolas Geoffray9e598902021-11-19 14:53:07 +0000844class CompileOptimizedSlowPathARM64 : public SlowPathCodeARM64 {
845 public:
846 CompileOptimizedSlowPathARM64() : SlowPathCodeARM64(/* instruction= */ nullptr) {}
847
848 void EmitNativeCode(CodeGenerator* codegen) override {
849 uint32_t entrypoint_offset =
850 GetThreadOffset<kArm64PointerSize>(kQuickCompileOptimized).Int32Value();
851 __ Bind(GetEntryLabel());
852 __ Ldr(lr, MemOperand(tr, entrypoint_offset));
853 // Note: we don't record the call here (and therefore don't generate a stack
854 // map), as the entrypoint should never be suspended.
855 __ Blr(lr);
856 __ B(GetExitLabel());
857 }
858
859 const char* GetDescription() const override {
860 return "CompileOptimizedSlowPath";
861 }
862
863 private:
864 DISALLOW_COPY_AND_ASSIGN(CompileOptimizedSlowPathARM64);
865};
866
Alexandre Rames5319def2014-10-23 10:03:10 +0100867#undef __
868
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100869Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100870 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100871 if (type == DataType::Type::kVoid) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100872 LOG(FATAL) << "Unreachable type " << type;
873 }
874
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100875 if (DataType::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100876 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
877 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100878 } else if (!DataType::IsFloatingPointType(type) &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000879 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000880 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
881 } else {
882 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100883 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
884 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100885 }
886
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000887 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100888 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100889 return next_location;
890}
891
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100892Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100893 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100894}
895
Vladimir Marko86c87522020-05-11 16:55:55 +0100896Location CriticalNativeCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
897 DCHECK_NE(type, DataType::Type::kReference);
898
899 Location location = Location::NoLocation();
900 if (DataType::IsFloatingPointType(type)) {
901 if (fpr_index_ < kParameterFPRegistersLength) {
902 location = LocationFrom(kParameterFPRegisters[fpr_index_]);
903 ++fpr_index_;
904 }
905 } else {
906 // Native ABI uses the same registers as managed, except that the method register x0
907 // is a normal argument.
908 if (gpr_index_ < 1u + kParameterCoreRegistersLength) {
909 location = LocationFrom(gpr_index_ == 0u ? x0 : kParameterCoreRegisters[gpr_index_ - 1u]);
910 ++gpr_index_;
911 }
912 }
913 if (location.IsInvalid()) {
914 if (DataType::Is64BitType(type)) {
915 location = Location::DoubleStackSlot(stack_offset_);
916 } else {
917 location = Location::StackSlot(stack_offset_);
918 }
919 stack_offset_ += kFramePointerSize;
920
921 if (for_register_allocation_) {
922 location = Location::Any();
923 }
924 }
925 return location;
926}
927
928Location CriticalNativeCallingConventionVisitorARM64::GetReturnLocation(DataType::Type type) const {
929 // We perform conversion to the managed ABI return register after the call if needed.
930 InvokeDexCallingConventionVisitorARM64 dex_calling_convention;
931 return dex_calling_convention.GetReturnLocation(type);
932}
933
934Location CriticalNativeCallingConventionVisitorARM64::GetMethodLocation() const {
935 // Pass the method in the hidden argument x15.
936 return Location::RegisterLocation(x15.GetCode());
937}
938
Serban Constantinescu579885a2015-02-22 20:51:33 +0000939CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100940 const CompilerOptions& compiler_options,
941 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100942 : CodeGenerator(graph,
943 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000944 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000945 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100946 callee_saved_core_registers.GetList(),
947 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100948 compiler_options,
949 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100950 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
951 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov1a719e42019-07-18 14:24:55 +0100952 location_builder_neon_(graph, this),
953 instruction_visitor_neon_(graph, this),
954 location_builder_sve_(graph, this),
955 instruction_visitor_sve_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100956 move_resolver_(graph->GetAllocator(), this),
Artem Serovaa6f4832018-11-21 18:57:54 +0000957 assembler_(graph->GetAllocator(),
958 compiler_options.GetInstructionSetFeatures()->AsArm64InstructionSetFeatures()),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000959 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100960 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000961 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100962 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko8f63f102020-09-28 12:10:28 +0100963 public_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
964 package_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000965 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100966 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeb9eb002020-10-02 13:54:19 +0100967 boot_image_jni_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +0100968 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100969 call_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100970 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100971 uint32_literals_(std::less<uint32_t>(),
972 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
973 uint64_literals_(std::less<uint64_t>(),
974 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000975 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100976 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000977 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +0000978 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
979 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
980 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000981 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000982 AddAllocatedRegister(LocationFrom(lr));
Artem Serov1a719e42019-07-18 14:24:55 +0100983
984 bool use_sve = ShouldUseSVE();
985 if (use_sve) {
986 location_builder_ = &location_builder_sve_;
987 instruction_visitor_ = &instruction_visitor_sve_;
988 } else {
989 location_builder_ = &location_builder_neon_;
990 instruction_visitor_ = &instruction_visitor_neon_;
991 }
992}
993
994bool CodeGeneratorARM64::ShouldUseSVE() const {
Artem Serov8ba4de12019-12-04 21:10:23 +0000995 return GetInstructionSetFeatures().HasSVE();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000996}
Alexandre Rames5319def2014-10-23 10:03:10 +0100997
Artem Serov55ab7e82020-04-27 21:02:28 +0100998size_t CodeGeneratorARM64::GetSIMDRegisterWidth() const {
999 return SupportsPredicatedSIMD()
1000 ? GetInstructionSetFeatures().GetSVEVectorLength() / kBitsPerByte
1001 : vixl::aarch64::kQRegSizeInBytes;
1002}
1003
Alexandre Rames67555f72014-11-18 10:55:16 +00001004#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001005
Zheng Xu3927c8b2015-11-18 17:46:25 +08001006void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001007 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001008 jump_table->EmitTable(this);
1009 }
1010}
1011
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001012void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001013 EmitJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +00001014
1015 // Emit JIT baker read barrier slow paths.
Vladimir Marko695348f2020-05-19 14:42:02 +01001016 DCHECK(GetCompilerOptions().IsJitCompiler() || jit_baker_read_barrier_slow_paths_.empty());
Vladimir Marko966b46f2018-08-03 10:20:19 +00001017 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
1018 uint32_t encoded_data = entry.first;
1019 vixl::aarch64::Label* slow_path_entry = &entry.second.label;
1020 __ Bind(slow_path_entry);
Andreas Gampe3db70682018-12-26 15:12:03 -08001021 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name= */ nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00001022 }
1023
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001024 // Ensure we emit the literal pool.
1025 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001026
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001027 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001028
1029 // Verify Baker read barrier linker patches.
1030 if (kIsDebugBuild) {
1031 ArrayRef<const uint8_t> code = allocator->GetMemory();
1032 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
1033 DCHECK(info.label.IsBound());
1034 uint32_t literal_offset = info.label.GetLocation();
1035 DCHECK_ALIGNED(literal_offset, 4u);
1036
1037 auto GetInsn = [&code](uint32_t offset) {
1038 DCHECK_ALIGNED(offset, 4u);
1039 return
1040 (static_cast<uint32_t>(code[offset + 0]) << 0) +
1041 (static_cast<uint32_t>(code[offset + 1]) << 8) +
1042 (static_cast<uint32_t>(code[offset + 2]) << 16)+
1043 (static_cast<uint32_t>(code[offset + 3]) << 24);
1044 };
1045
1046 const uint32_t encoded_data = info.custom_data;
1047 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
1048 // Check that the next instruction matches the expected LDR.
1049 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01001050 case BakerReadBarrierKind::kField:
1051 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00001052 DCHECK_GE(code.size() - literal_offset, 8u);
1053 uint32_t next_insn = GetInsn(literal_offset + 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001054 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1055 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko0ecac682018-08-07 10:40:38 +01001056 if (kind == BakerReadBarrierKind::kField) {
1057 // LDR (immediate) with correct base_reg.
1058 CHECK_EQ(next_insn & 0xffc003e0u, 0xb9400000u | (base_reg << 5));
1059 } else {
1060 DCHECK(kind == BakerReadBarrierKind::kAcquire);
1061 // LDAR with correct base_reg.
1062 CHECK_EQ(next_insn & 0xffffffe0u, 0x88dffc00u | (base_reg << 5));
1063 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001064 break;
1065 }
1066 case BakerReadBarrierKind::kArray: {
1067 DCHECK_GE(code.size() - literal_offset, 8u);
1068 uint32_t next_insn = GetInsn(literal_offset + 4u);
1069 // LDR (register) with the correct base_reg, size=10 (32-bit), option=011 (extend = LSL),
1070 // and S=1 (shift amount = 2 for 32-bit version), i.e. LDR Wt, [Xn, Xm, LSL #2].
1071 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1072 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1073 CHECK_EQ(next_insn & 0xffe0ffe0u, 0xb8607800u | (base_reg << 5));
1074 CheckValidReg((next_insn >> 16) & 0x1f); // Check index register
1075 break;
1076 }
1077 case BakerReadBarrierKind::kGcRoot: {
1078 DCHECK_GE(literal_offset, 4u);
1079 uint32_t prev_insn = GetInsn(literal_offset - 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001080 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko94796f82018-08-08 15:15:33 +01001081 // Usually LDR (immediate) with correct root_reg but
Vladimir Markoc8178f52020-11-24 10:38:16 +00001082 // we may have a "MOV marked, old_value" for intrinsic CAS.
Vladimir Marko94796f82018-08-08 15:15:33 +01001083 if ((prev_insn & 0xffe0ffff) != (0x2a0003e0 | root_reg)) { // MOV?
1084 CHECK_EQ(prev_insn & 0xffc0001fu, 0xb9400000u | root_reg); // LDR?
1085 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001086 break;
1087 }
1088 default:
1089 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
1090 UNREACHABLE();
1091 }
1092 }
1093 }
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001094}
1095
Zheng Xuad4450e2015-04-17 18:48:56 +08001096void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1097 // Note: There are 6 kinds of moves:
1098 // 1. constant -> GPR/FPR (non-cycle)
1099 // 2. constant -> stack (non-cycle)
1100 // 3. GPR/FPR -> GPR/FPR
1101 // 4. GPR/FPR -> stack
1102 // 5. stack -> GPR/FPR
1103 // 6. stack -> stack (non-cycle)
1104 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1105 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1106 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1107 // dependency.
1108 vixl_temps_.Open(GetVIXLAssembler());
1109}
1110
1111void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1112 vixl_temps_.Close();
1113}
1114
1115Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001116 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1117 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1118 || kind == Location::kSIMDStackSlot);
1119 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1120 ? Location::kFpuRegister
1121 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001122 Location scratch = GetScratchLocation(kind);
1123 if (!scratch.Equals(Location::NoLocation())) {
1124 return scratch;
1125 }
1126 // Allocate from VIXL temp registers.
1127 if (kind == Location::kRegister) {
1128 scratch = LocationFrom(vixl_temps_.AcquireX());
1129 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001130 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serov1a719e42019-07-18 14:24:55 +01001131 scratch = codegen_->GetGraph()->HasSIMD()
1132 ? codegen_->GetInstructionCodeGeneratorArm64()->AllocateSIMDScratchLocation(&vixl_temps_)
1133 : LocationFrom(vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001134 }
1135 AddScratchLocation(scratch);
1136 return scratch;
1137}
1138
1139void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1140 if (loc.IsRegister()) {
1141 vixl_temps_.Release(XRegisterFrom(loc));
1142 } else {
1143 DCHECK(loc.IsFpuRegister());
Artem Serov1a719e42019-07-18 14:24:55 +01001144 if (codegen_->GetGraph()->HasSIMD()) {
1145 codegen_->GetInstructionCodeGeneratorArm64()->FreeSIMDScratchLocation(loc, &vixl_temps_);
1146 } else {
1147 vixl_temps_.Release(DRegisterFrom(loc));
1148 }
Zheng Xuad4450e2015-04-17 18:48:56 +08001149 }
1150 RemoveScratchLocation(loc);
1151}
1152
Alexandre Rames3e69f162014-12-10 10:36:50 +00001153void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001154 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001155 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001156}
1157
Mythri Alle5097f832021-11-02 14:52:30 +00001158void LocationsBuilderARM64::VisitMethodExitHook(HMethodExitHook* method_hook) {
1159 LocationSummary* locations = new (GetGraph()->GetAllocator())
1160 LocationSummary(method_hook, LocationSummary::kCallOnSlowPath);
1161 DataType::Type return_type = method_hook->InputAt(0)->GetType();
1162 locations->SetInAt(0, ARM64ReturnLocation(return_type));
1163}
1164
1165void InstructionCodeGeneratorARM64::GenerateMethodEntryExitHook(HInstruction* instruction) {
1166 MacroAssembler* masm = GetVIXLAssembler();
1167 UseScratchRegisterScope temps(masm);
1168 Register temp = temps.AcquireX();
1169 Register value = temps.AcquireW();
1170
1171 SlowPathCodeARM64* slow_path =
1172 new (codegen_->GetScopedAllocator()) MethodEntryExitHooksSlowPathARM64(instruction);
1173 codegen_->AddSlowPath(slow_path);
1174
Mythri Alle5eff6b32022-11-04 10:57:53 +00001175 if (instruction->IsMethodExitHook()) {
1176 // Check if we are required to check if the caller needs a deoptimization. Strictly speaking it
1177 // would be sufficient to check if CheckCallerForDeopt bit is set. Though it is faster to check
1178 // if it is just non-zero. kCHA bit isn't used in debuggable runtimes as cha optimization is
1179 // disabled in debuggable runtime. The other bit is used when this method itself requires a
1180 // deoptimization due to redefinition. So it is safe to just check for non-zero value here.
1181 __ Ldr(value, MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1182 __ Cbnz(value, slow_path->GetEntryLabel());
1183 }
1184
Mythri Alle5097f832021-11-02 14:52:30 +00001185 uint64_t address = reinterpret_cast64<uint64_t>(Runtime::Current()->GetInstrumentation());
Mythri Alle5b263ae2022-10-21 15:07:44 +00001186 MemberOffset offset = instruction->IsMethodExitHook() ?
Mythri Alle5eff6b32022-11-04 10:57:53 +00001187 instrumentation::Instrumentation::HaveMethodExitListenersOffset() :
Mythri Alle5b263ae2022-10-21 15:07:44 +00001188 instrumentation::Instrumentation::HaveMethodEntryListenersOffset();
1189 __ Mov(temp, address + offset.Int32Value());
Mythri Alle9575c122021-11-12 12:04:41 +00001190 __ Ldrb(value, MemOperand(temp, 0));
Mythri Alle5097f832021-11-02 14:52:30 +00001191 __ Cbnz(value, slow_path->GetEntryLabel());
1192 __ Bind(slow_path->GetExitLabel());
1193}
1194
1195void InstructionCodeGeneratorARM64::VisitMethodExitHook(HMethodExitHook* instruction) {
1196 DCHECK(codegen_->GetCompilerOptions().IsJitCompiler() && GetGraph()->IsDebuggable());
1197 DCHECK(codegen_->RequiresCurrentMethod());
1198 GenerateMethodEntryExitHook(instruction);
1199}
1200
1201void LocationsBuilderARM64::VisitMethodEntryHook(HMethodEntryHook* method_hook) {
1202 new (GetGraph()->GetAllocator()) LocationSummary(method_hook, LocationSummary::kCallOnSlowPath);
1203}
1204
1205void InstructionCodeGeneratorARM64::VisitMethodEntryHook(HMethodEntryHook* instruction) {
1206 DCHECK(codegen_->GetCompilerOptions().IsJitCompiler() && GetGraph()->IsDebuggable());
1207 DCHECK(codegen_->RequiresCurrentMethod());
1208 GenerateMethodEntryExitHook(instruction);
1209}
1210
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001211void CodeGeneratorARM64::MaybeIncrementHotness(bool is_frame_entry) {
1212 MacroAssembler* masm = GetVIXLAssembler();
1213 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1214 UseScratchRegisterScope temps(masm);
1215 Register counter = temps.AcquireX();
1216 Register method = is_frame_entry ? kArtMethodRegister : temps.AcquireX();
1217 if (!is_frame_entry) {
1218 __ Ldr(method, MemOperand(sp, 0));
1219 }
1220 __ Ldrh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
Nicolas Geoffray61673dc2021-11-06 13:58:31 +00001221 vixl::aarch64::Label done;
1222 DCHECK_EQ(0u, interpreter::kNterpHotnessValue);
1223 __ Cbz(counter, &done);
1224 __ Add(counter, counter, -1);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001225 __ Strh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
Nicolas Geoffray61673dc2021-11-06 13:58:31 +00001226 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001227 }
1228
1229 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffray9e598902021-11-19 14:53:07 +00001230 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) CompileOptimizedSlowPathARM64();
1231 AddSlowPath(slow_path);
1232 ProfilingInfo* info = GetGraph()->GetProfilingInfo();
1233 DCHECK(info != nullptr);
1234 DCHECK(!HasEmptyFrame());
1235 uint64_t address = reinterpret_cast64<uint64_t>(info);
1236 vixl::aarch64::Label done;
1237 UseScratchRegisterScope temps(masm);
1238 Register temp = temps.AcquireX();
1239 Register counter = temps.AcquireW();
1240 __ Ldr(temp, DeduplicateUint64Literal(address));
1241 __ Ldrh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
1242 __ Cbz(counter, slow_path->GetEntryLabel());
1243 __ Add(counter, counter, -1);
1244 __ Strh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
1245 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001246 }
1247}
1248
Alexandre Rames5319def2014-10-23 10:03:10 +01001249void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001250 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffrayab3f8d22022-07-23 15:49:51 +00001251
1252 // Check if we need to generate the clinit check. We will jump to the
1253 // resolution stub if the class is not initialized and the executing thread is
1254 // not the thread initializing it.
1255 // We do this before constructing the frame to get the correct stack trace if
1256 // an exception is thrown.
1257 if (GetCompilerOptions().ShouldCompileWithClinitCheck(GetGraph()->GetArtMethod())) {
1258 UseScratchRegisterScope temps(masm);
1259 vixl::aarch64::Label resolution;
Nicolas Geoffrayf8537902022-09-13 15:22:29 +01001260 vixl::aarch64::Label memory_barrier;
Nicolas Geoffrayab3f8d22022-07-23 15:49:51 +00001261
1262 Register temp1 = temps.AcquireW();
1263 Register temp2 = temps.AcquireW();
1264
1265 // Check if we're visibly initialized.
1266
1267 // We don't emit a read barrier here to save on code size. We rely on the
1268 // resolution trampoline to do a suspend check before re-entering this code.
1269 __ Ldr(temp1, MemOperand(kArtMethodRegister, ArtMethod::DeclaringClassOffset().Int32Value()));
1270 __ Ldrb(temp2, HeapOperand(temp1, status_byte_offset));
1271 __ Cmp(temp2, shifted_visibly_initialized_value);
1272 __ B(hs, &frame_entry_label_);
1273
Nicolas Geoffrayf8537902022-09-13 15:22:29 +01001274 // Check if we're initialized and jump to code that does a memory barrier if
1275 // so.
1276 __ Cmp(temp2, shifted_initialized_value);
1277 __ B(hs, &memory_barrier);
1278
Nicolas Geoffrayab3f8d22022-07-23 15:49:51 +00001279 // Check if we're initializing and the thread initializing is the one
1280 // executing the code.
1281 __ Cmp(temp2, shifted_initializing_value);
1282 __ B(lo, &resolution);
1283
1284 __ Ldr(temp1, HeapOperand(temp1, mirror::Class::ClinitThreadIdOffset().Int32Value()));
1285 __ Ldr(temp2, MemOperand(tr, Thread::TidOffset<kArm64PointerSize>().Int32Value()));
1286 __ Cmp(temp1, temp2);
1287 __ B(eq, &frame_entry_label_);
1288 __ Bind(&resolution);
1289
1290 // Jump to the resolution stub.
1291 ThreadOffset64 entrypoint_offset =
1292 GetThreadOffset<kArm64PointerSize>(kQuickQuickResolutionTrampoline);
1293 __ Ldr(temp1.X(), MemOperand(tr, entrypoint_offset.Int32Value()));
1294 __ Br(temp1.X());
Nicolas Geoffrayf8537902022-09-13 15:22:29 +01001295
1296 __ Bind(&memory_barrier);
1297 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Nicolas Geoffrayab3f8d22022-07-23 15:49:51 +00001298 }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001299 __ Bind(&frame_entry_label_);
1300
Vladimir Marko33bff252017-11-01 14:35:42 +00001301 bool do_overflow_check =
1302 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001303 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001304 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001305 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001306 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001307 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001308 {
1309 // Ensure that between load and RecordPcInfo there are no pools emitted.
1310 ExactAssemblyScope eas(GetVIXLAssembler(),
1311 kInstructionSize,
1312 CodeBufferCheckScope::kExactSize);
1313 __ ldr(wzr, MemOperand(temp, 0));
1314 RecordPcInfo(nullptr, 0);
1315 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001316 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001317
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001318 if (!HasEmptyFrame()) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001319 // Stack layout:
1320 // sp[frame_size - 8] : lr.
1321 // ... : other preserved core registers.
1322 // ... : other preserved fp registers.
1323 // ... : reserved frame space.
1324 // sp[0] : current method.
Vladimir Marko1a225a72019-07-05 13:37:42 +01001325 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1326 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1327 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1328 DCHECK(!preserved_core_registers.IsEmpty());
1329 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1330 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001331
Vladimir Marko1a225a72019-07-05 13:37:42 +01001332 // Save the current method if we need it, or if using STP reduces code
1333 // size. Note that we do not do this in HCurrentMethod, as the
1334 // instruction might have been removed in the SSA graph.
1335 CPURegister lowest_spill;
1336 if (core_spills_offset == kXRegSizeInBytes) {
1337 // If there is no gap between the method and the lowest core spill, use
1338 // aligned STP pre-index to store both. Max difference is 512. We do
1339 // that to reduce code size even if we do not have to save the method.
1340 DCHECK_LE(frame_size, 512); // 32 core registers are only 256 bytes.
1341 lowest_spill = preserved_core_registers.PopLowestIndex();
1342 __ Stp(kArtMethodRegister, lowest_spill, MemOperand(sp, -frame_size, PreIndex));
1343 } else if (RequiresCurrentMethod()) {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001344 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001345 } else {
1346 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001347 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001348 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Vladimir Marko1a225a72019-07-05 13:37:42 +01001349 if (lowest_spill.IsValid()) {
1350 GetAssembler()->cfi().RelOffset(DWARFReg(lowest_spill), core_spills_offset);
1351 core_spills_offset += kXRegSizeInBytes;
1352 }
1353 GetAssembler()->SpillRegisters(preserved_core_registers, core_spills_offset);
1354 GetAssembler()->SpillRegisters(preserved_fp_registers, fp_spills_offset);
Mingyao Yang063fc772016-08-02 11:02:54 -07001355
1356 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1357 // Initialize should_deoptimize flag to 0.
1358 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1359 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1360 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001361 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001362 MaybeIncrementHotness(/* is_frame_entry= */ true);
Andreas Gampe3db70682018-12-26 15:12:03 -08001363 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001364}
1365
1366void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001367 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001368 if (!HasEmptyFrame()) {
Vladimir Marko1a225a72019-07-05 13:37:42 +01001369 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1370 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1371 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1372 DCHECK(!preserved_core_registers.IsEmpty());
1373 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1374 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
1375
1376 CPURegister lowest_spill;
1377 if (core_spills_offset == kXRegSizeInBytes) {
1378 // If there is no gap between the method and the lowest core spill, use
1379 // aligned LDP pre-index to pop both. Max difference is 504. We do
1380 // that to reduce code size even though the loaded method is unused.
1381 DCHECK_LE(frame_size, 504); // 32 core registers are only 256 bytes.
1382 lowest_spill = preserved_core_registers.PopLowestIndex();
1383 core_spills_offset += kXRegSizeInBytes;
1384 }
1385 GetAssembler()->UnspillRegisters(preserved_fp_registers, fp_spills_offset);
1386 GetAssembler()->UnspillRegisters(preserved_core_registers, core_spills_offset);
1387 if (lowest_spill.IsValid()) {
1388 __ Ldp(xzr, lowest_spill, MemOperand(sp, frame_size, PostIndex));
1389 GetAssembler()->cfi().Restore(DWARFReg(lowest_spill));
1390 } else {
1391 __ Drop(frame_size);
1392 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001393 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001394 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001395 __ Ret();
1396 GetAssembler()->cfi().RestoreState();
1397 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001398}
1399
Scott Wakeling97c72b72016-06-24 16:19:36 +01001400CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001401 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001402 return CPURegList(CPURegister::kRegister, kXRegSize,
1403 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001404}
1405
Scott Wakeling97c72b72016-06-24 16:19:36 +01001406CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001407 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1408 GetNumberOfFloatingPointRegisters()));
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001409 return CPURegList(CPURegister::kVRegister, kDRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001410 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001411}
1412
Alexandre Rames5319def2014-10-23 10:03:10 +01001413void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1414 __ Bind(GetLabelOf(block));
1415}
1416
Calin Juravle175dc732015-08-25 15:42:32 +01001417void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1418 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001419 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001420}
1421
Calin Juravlee460d1d2015-09-29 04:52:17 +01001422void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1423 if (location.IsRegister()) {
1424 locations->AddTemp(location);
1425 } else {
1426 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1427 }
1428}
1429
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00001430void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool emit_null_check) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001431 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001432 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001433 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001434 vixl::aarch64::Label done;
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00001435 if (emit_null_check) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001436 __ Cbz(value, &done);
1437 }
Roland Levillainc73f0522018-08-14 15:16:50 +01001438 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07001439 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01001440 // Calculate the offset (in the card table) of the card corresponding to
1441 // `object`.
Alexandre Rames5319def2014-10-23 10:03:10 +01001442 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Roland Levillainc73f0522018-08-14 15:16:50 +01001443 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
1444 // `object`'s card.
1445 //
1446 // Register `card` contains the address of the card table. Note that the card
1447 // table's base is biased during its creation so that it always starts at an
1448 // address whose least-significant byte is equal to `kCardDirty` (see
1449 // art::gc::accounting::CardTable::Create). Therefore the STRB instruction
1450 // below writes the `kCardDirty` (byte) value into the `object`'s card
1451 // (located at `card + object >> kCardShift`).
1452 //
1453 // This dual use of the value in register `card` (1. to calculate the location
1454 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
1455 // (no need to explicitly load `kCardDirty` as an immediate value).
Serban Constantinescu02164b32014-11-13 14:05:07 +00001456 __ Strb(card, MemOperand(card, temp.X()));
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00001457 if (emit_null_check) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001458 __ Bind(&done);
1459 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001460}
1461
David Brazdil58282f42016-01-14 12:45:10 +00001462void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001463 // Blocked core registers:
1464 // lr : Runtime reserved.
1465 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001466 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001467 // ip1 : VIXL core temp.
1468 // ip0 : VIXL core temp.
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001469 // x18 : Platform register.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001470 //
1471 // Blocked fp registers:
1472 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001473 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1474 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001475 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001476 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001477 }
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001478 blocked_core_registers_[X18] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001479
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001480 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001481 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001482 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001483 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001484
David Brazdil58282f42016-01-14 12:45:10 +00001485 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001486 // Stubs do not save callee-save floating point registers. If the graph
1487 // is debuggable, we need to deal with these registers differently. For
1488 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001489 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1490 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001491 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001492 }
1493 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001494}
1495
Alexandre Rames3e69f162014-12-10 10:36:50 +00001496size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1497 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1498 __ Str(reg, MemOperand(sp, stack_index));
1499 return kArm64WordSize;
1500}
1501
1502size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1503 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1504 __ Ldr(reg, MemOperand(sp, stack_index));
1505 return kArm64WordSize;
1506}
1507
Artem Serov9df37b92019-07-23 16:41:54 +01001508size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1509 uint32_t reg_id ATTRIBUTE_UNUSED) {
1510 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1511 << "use SaveRestoreLiveRegistersHelper";
1512 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001513}
1514
Artem Serov9df37b92019-07-23 16:41:54 +01001515size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1516 uint32_t reg_id ATTRIBUTE_UNUSED) {
1517 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1518 << "use SaveRestoreLiveRegistersHelper";
1519 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001520}
1521
Alexandre Rames5319def2014-10-23 10:03:10 +01001522void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001523 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001524}
1525
1526void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001527 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001528}
1529
Vladimir Markoa0431112018-06-25 09:32:54 +01001530const Arm64InstructionSetFeatures& CodeGeneratorARM64::GetInstructionSetFeatures() const {
1531 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArm64InstructionSetFeatures();
1532}
1533
Alexandre Rames67555f72014-11-18 10:55:16 +00001534void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001535 if (constant->IsIntConstant()) {
1536 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1537 } else if (constant->IsLongConstant()) {
1538 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1539 } else if (constant->IsNullConstant()) {
1540 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001541 } else if (constant->IsFloatConstant()) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001542 __ Fmov(VRegister(destination), constant->AsFloatConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001543 } else {
1544 DCHECK(constant->IsDoubleConstant());
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001545 __ Fmov(VRegister(destination), constant->AsDoubleConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001546 }
1547}
1548
Alexandre Rames3e69f162014-12-10 10:36:50 +00001549
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001550static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001551 DCHECK(constant.IsConstant());
1552 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001553 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001554 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001555 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1556 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1557 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1558 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001559}
1560
Roland Levillain952b2352017-05-03 19:49:14 +01001561// Allocate a scratch register from the VIXL pool, querying first
1562// the floating-point register pool, and then the core register
1563// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001564// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1565// using a different allocation strategy.
1566static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1567 vixl::aarch64::UseScratchRegisterScope* temps,
1568 int size_in_bits) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001569 return masm->GetScratchVRegisterList()->IsEmpty()
Roland Levillain558dea12017-01-27 19:40:44 +00001570 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1571 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1572}
1573
Calin Juravlee460d1d2015-09-29 04:52:17 +01001574void CodeGeneratorARM64::MoveLocation(Location destination,
1575 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001576 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001577 if (source.Equals(destination)) {
1578 return;
1579 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001580
1581 // A valid move can always be inferred from the destination and source
1582 // locations. When moving from and to a register, the argument type can be
1583 // used to generate 32bit instead of 64bit moves. In debug mode we also
1584 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001585 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001586
1587 if (destination.IsRegister() || destination.IsFpuRegister()) {
1588 if (unspecified_type) {
1589 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1590 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001591 (src_cst != nullptr && (src_cst->IsIntConstant()
1592 || src_cst->IsFloatConstant()
1593 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001594 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001595 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001596 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001597 // If the source is a double stack slot or a 64bit constant, a 64bit
1598 // type is appropriate. Else the source is a register, and since the
1599 // type has not been specified, we chose a 64bit type to force a 64bit
1600 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001601 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001602 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001603 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001604 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1605 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001606 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001607 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1608 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1609 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001610 } else if (source.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001611 GetInstructionCodeGeneratorArm64()->LoadSIMDRegFromStack(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001612 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001613 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001614 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001615 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001616 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001617 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001618 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001619 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001620 DataType::Type source_type = DataType::Is64BitType(dst_type)
1621 ? DataType::Type::kInt64
1622 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001623 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1624 }
1625 } else {
1626 DCHECK(source.IsFpuRegister());
1627 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001628 DataType::Type source_type = DataType::Is64BitType(dst_type)
1629 ? DataType::Type::kFloat64
1630 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001631 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1632 } else {
1633 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001634 if (GetGraph()->HasSIMD()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001635 GetInstructionCodeGeneratorArm64()->MoveSIMDRegToSIMDReg(destination, source);
Artem Serovd4bccf12017-04-03 18:47:32 +01001636 } else {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001637 __ Fmov(VRegister(dst), FPRegisterFrom(source, dst_type));
Artem Serovd4bccf12017-04-03 18:47:32 +01001638 }
1639 }
1640 }
1641 } else if (destination.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001642 GetInstructionCodeGeneratorArm64()->MoveToSIMDStackSlot(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001643 } else { // The destination is not a register. It must be a stack slot.
1644 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1645 if (source.IsRegister() || source.IsFpuRegister()) {
1646 if (unspecified_type) {
1647 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001648 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001649 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001650 dst_type =
1651 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001652 }
1653 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001654 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1655 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001656 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001657 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001658 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1659 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001660 UseScratchRegisterScope temps(GetVIXLAssembler());
1661 HConstant* src_cst = source.GetConstant();
1662 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001663 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001664 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1665 ? Register(xzr)
1666 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001667 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001668 if (src_cst->IsIntConstant()) {
1669 temp = temps.AcquireW();
1670 } else if (src_cst->IsLongConstant()) {
1671 temp = temps.AcquireX();
1672 } else if (src_cst->IsFloatConstant()) {
1673 temp = temps.AcquireS();
1674 } else {
1675 DCHECK(src_cst->IsDoubleConstant());
1676 temp = temps.AcquireD();
1677 }
1678 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001679 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001680 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001681 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001682 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001683 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001684 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001685 // Use any scratch register (a core or a floating-point one)
1686 // from VIXL scratch register pools as a temporary.
1687 //
1688 // We used to only use the FP scratch register pool, but in some
1689 // rare cases the only register from this pool (D31) would
1690 // already be used (e.g. within a ParallelMove instruction, when
1691 // a move is blocked by a another move requiring a scratch FP
1692 // register, which would reserve D31). To prevent this issue, we
1693 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001694 //
1695 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001696 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001697 // use AcquireFPOrCoreCPURegisterOfSize instead of
1698 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1699 // allocates core scratch registers first.
1700 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1701 GetVIXLAssembler(),
1702 &temps,
1703 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001704 __ Ldr(temp, StackOperandFrom(source));
1705 __ Str(temp, StackOperandFrom(destination));
1706 }
1707 }
1708}
1709
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001710void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001711 CPURegister dst,
1712 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001713 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001714 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001715 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001716 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001717 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001718 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001719 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001720 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001721 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001722 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001723 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001724 case DataType::Type::kInt16:
1725 __ Ldrsh(Register(dst), src);
1726 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001727 case DataType::Type::kInt32:
1728 case DataType::Type::kReference:
1729 case DataType::Type::kInt64:
1730 case DataType::Type::kFloat32:
1731 case DataType::Type::kFloat64:
1732 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001733 __ Ldr(dst, src);
1734 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001735 case DataType::Type::kUint32:
1736 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001737 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001738 LOG(FATAL) << "Unreachable type " << type;
1739 }
1740}
1741
Calin Juravle77520bc2015-01-12 18:45:46 +00001742void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Vladimir Marko98873af2020-12-16 12:10:03 +00001743 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001744 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001745 const MemOperand& src,
1746 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001747 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001748 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001749 Register temp_base = temps.AcquireX();
1750
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001751 DCHECK(!src.IsPreIndex());
1752 DCHECK(!src.IsPostIndex());
1753
1754 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001755 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001756 {
1757 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1758 MemOperand base = MemOperand(temp_base);
1759 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001760 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001761 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001762 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001763 {
1764 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1765 __ ldarb(Register(dst), base);
1766 if (needs_null_check) {
1767 MaybeRecordImplicitNullCheck(instruction);
1768 }
1769 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001770 if (type == DataType::Type::kInt8) {
1771 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001772 }
1773 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001774 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001775 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001776 {
1777 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1778 __ ldarh(Register(dst), base);
1779 if (needs_null_check) {
1780 MaybeRecordImplicitNullCheck(instruction);
1781 }
1782 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001783 if (type == DataType::Type::kInt16) {
1784 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
1785 }
Artem Serov914d7a82017-02-07 14:33:49 +00001786 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001787 case DataType::Type::kInt32:
1788 case DataType::Type::kReference:
1789 case DataType::Type::kInt64:
1790 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001791 {
1792 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1793 __ ldar(Register(dst), base);
1794 if (needs_null_check) {
1795 MaybeRecordImplicitNullCheck(instruction);
1796 }
1797 }
1798 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001799 case DataType::Type::kFloat32:
1800 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00001801 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001802 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001803
Artem Serov914d7a82017-02-07 14:33:49 +00001804 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1805 {
1806 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1807 __ ldar(temp, base);
1808 if (needs_null_check) {
1809 MaybeRecordImplicitNullCheck(instruction);
1810 }
1811 }
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001812 __ Fmov(VRegister(dst), temp);
Artem Serov914d7a82017-02-07 14:33:49 +00001813 break;
Roland Levillain44015862016-01-22 11:47:17 +00001814 }
Aart Bik66c158e2018-01-31 12:55:04 -08001815 case DataType::Type::kUint32:
1816 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001817 case DataType::Type::kVoid:
Artem Serov914d7a82017-02-07 14:33:49 +00001818 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001819 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001820 }
1821}
1822
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001823void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001824 CPURegister src,
1825 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001826 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001827 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001828 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001829 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001830 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001831 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001832 case DataType::Type::kUint16:
1833 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001834 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001835 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001836 case DataType::Type::kInt32:
1837 case DataType::Type::kReference:
1838 case DataType::Type::kInt64:
1839 case DataType::Type::kFloat32:
1840 case DataType::Type::kFloat64:
1841 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001842 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001843 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001844 case DataType::Type::kUint32:
1845 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001846 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001847 LOG(FATAL) << "Unreachable type " << type;
1848 }
1849}
1850
Artem Serov914d7a82017-02-07 14:33:49 +00001851void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001852 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001853 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00001854 const MemOperand& dst,
1855 bool needs_null_check) {
1856 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001857 UseScratchRegisterScope temps(GetVIXLAssembler());
1858 Register temp_base = temps.AcquireX();
1859
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001860 DCHECK(!dst.IsPreIndex());
1861 DCHECK(!dst.IsPostIndex());
1862
1863 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001864 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001865 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001866 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00001867 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001868 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001869 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001870 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001871 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001872 {
1873 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1874 __ stlrb(Register(src), base);
1875 if (needs_null_check) {
1876 MaybeRecordImplicitNullCheck(instruction);
1877 }
1878 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001879 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001880 case DataType::Type::kUint16:
1881 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001882 {
1883 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1884 __ stlrh(Register(src), base);
1885 if (needs_null_check) {
1886 MaybeRecordImplicitNullCheck(instruction);
1887 }
1888 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001889 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001890 case DataType::Type::kInt32:
1891 case DataType::Type::kReference:
1892 case DataType::Type::kInt64:
1893 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001894 {
1895 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1896 __ stlr(Register(src), base);
1897 if (needs_null_check) {
1898 MaybeRecordImplicitNullCheck(instruction);
1899 }
1900 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001901 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001902 case DataType::Type::kFloat32:
1903 case DataType::Type::kFloat64: {
1904 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001905 Register temp_src;
1906 if (src.IsZero()) {
1907 // The zero register is used to avoid synthesizing zero constants.
1908 temp_src = Register(src);
1909 } else {
1910 DCHECK(src.IsFPRegister());
1911 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001912 __ Fmov(temp_src, VRegister(src));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001913 }
Artem Serov914d7a82017-02-07 14:33:49 +00001914 {
1915 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1916 __ stlr(temp_src, base);
1917 if (needs_null_check) {
1918 MaybeRecordImplicitNullCheck(instruction);
1919 }
1920 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001921 break;
1922 }
Aart Bik66c158e2018-01-31 12:55:04 -08001923 case DataType::Type::kUint32:
1924 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001925 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001926 LOG(FATAL) << "Unreachable type " << type;
1927 }
1928}
1929
Calin Juravle175dc732015-08-25 15:42:32 +01001930void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1931 HInstruction* instruction,
1932 uint32_t dex_pc,
1933 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001934 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00001935
Vladimir Markof6675082019-05-17 12:05:28 +01001936 ThreadOffset64 entrypoint_offset = GetThreadOffset<kArm64PointerSize>(entrypoint);
1937 // Reduce code size for AOT by using shared trampolines for slow path runtime calls across the
1938 // entire oat file. This adds an extra branch and we do not want to slow down the main path.
1939 // For JIT, thunk sharing is per-method, so the gains would be smaller or even negative.
Vladimir Marko695348f2020-05-19 14:42:02 +01001940 if (slow_path == nullptr || GetCompilerOptions().IsJitCompiler()) {
Vladimir Markof6675082019-05-17 12:05:28 +01001941 __ Ldr(lr, MemOperand(tr, entrypoint_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00001942 // Ensure the pc position is recorded immediately after the `blr` instruction.
1943 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1944 __ blr(lr);
1945 if (EntrypointRequiresStackMap(entrypoint)) {
1946 RecordPcInfo(instruction, dex_pc, slow_path);
1947 }
Vladimir Markof6675082019-05-17 12:05:28 +01001948 } else {
1949 // Ensure the pc position is recorded immediately after the `bl` instruction.
1950 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1951 EmitEntrypointThunkCall(entrypoint_offset);
1952 if (EntrypointRequiresStackMap(entrypoint)) {
1953 RecordPcInfo(instruction, dex_pc, slow_path);
1954 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001955 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001956}
1957
Roland Levillaindec8f632016-07-22 17:10:06 +01001958void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1959 HInstruction* instruction,
1960 SlowPathCode* slow_path) {
1961 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01001962 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1963 __ Blr(lr);
1964}
1965
Alexandre Rames67555f72014-11-18 10:55:16 +00001966void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001967 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001968 UseScratchRegisterScope temps(GetVIXLAssembler());
1969 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001970
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001971 // CMP (immediate) is limited to imm12 or imm12<<12, so we would need to materialize
1972 // the constant 0xf0000000 for comparison with the full 32-bit field. To reduce the code
1973 // size, load only the high byte of the field and compare with 0xf0.
1974 // Note: The same code size could be achieved with LDR+MNV(asr #24)+CBNZ but benchmarks
1975 // show that this pattern is slower (tested on little cores).
1976 __ Ldrb(temp, HeapOperand(class_reg, status_byte_offset));
1977 __ Cmp(temp, shifted_visibly_initialized_value);
1978 __ B(lo, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001979 __ Bind(slow_path->GetExitLabel());
1980}
Alexandre Rames5319def2014-10-23 10:03:10 +01001981
Vladimir Marko175e7862018-03-27 09:03:13 +00001982void InstructionCodeGeneratorARM64::GenerateBitstringTypeCheckCompare(
1983 HTypeCheckInstruction* check, vixl::aarch64::Register temp) {
1984 uint32_t path_to_root = check->GetBitstringPathToRoot();
1985 uint32_t mask = check->GetBitstringMask();
1986 DCHECK(IsPowerOfTwo(mask + 1));
1987 size_t mask_bits = WhichPowerOf2(mask + 1);
1988
1989 if (mask_bits == 16u) {
1990 // Load only the bitstring part of the status word.
1991 __ Ldrh(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1992 } else {
1993 // /* uint32_t */ temp = temp->status_
1994 __ Ldr(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1995 // Extract the bitstring bits.
1996 __ Ubfx(temp, temp, 0, mask_bits);
1997 }
1998 // Compare the bitstring bits to `path_to_root`.
1999 __ Cmp(temp, path_to_root);
2000}
2001
Roland Levillain44015862016-01-22 11:47:17 +00002002void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002003 BarrierType type = BarrierAll;
2004
2005 switch (kind) {
2006 case MemBarrierKind::kAnyAny:
2007 case MemBarrierKind::kAnyStore: {
2008 type = BarrierAll;
2009 break;
2010 }
2011 case MemBarrierKind::kLoadAny: {
2012 type = BarrierReads;
2013 break;
2014 }
2015 case MemBarrierKind::kStoreStore: {
2016 type = BarrierWrites;
2017 break;
2018 }
2019 default:
2020 LOG(FATAL) << "Unexpected memory barrier " << kind;
2021 }
2022 __ Dmb(InnerShareable, type);
2023}
2024
Vladimir Marko7039c112018-04-05 17:59:39 +01002025bool CodeGeneratorARM64::CanUseImplicitSuspendCheck() const {
2026 // Use implicit suspend checks if requested in compiler options unless there are SIMD
2027 // instructions in the graph. The implicit suspend check saves all FP registers as
2028 // 64-bit (in line with the calling convention) but SIMD instructions can use 128-bit
2029 // registers, so they need to be saved in an explicit slow path.
2030 return GetCompilerOptions().GetImplicitSuspendChecks() && !GetGraph()->HasSIMD();
2031}
2032
Serban Constantinescu02164b32014-11-13 14:05:07 +00002033void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2034 HBasicBlock* successor) {
Stelios Ioannouc54cc7c2021-07-09 17:06:03 +01002035 if (instruction->IsNoOp()) {
2036 if (successor != nullptr) {
2037 __ B(codegen_->GetLabelOf(successor));
2038 }
2039 return;
2040 }
2041
Vladimir Marko7039c112018-04-05 17:59:39 +01002042 if (codegen_->CanUseImplicitSuspendCheck()) {
2043 __ Ldr(kImplicitSuspendCheckRegister, MemOperand(kImplicitSuspendCheckRegister));
2044 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Vladimir Markoe45883e2022-01-11 12:38:35 +00002045 if (successor != nullptr) {
2046 __ B(codegen_->GetLabelOf(successor));
2047 }
Vladimir Marko7039c112018-04-05 17:59:39 +01002048 return;
2049 }
2050
Serban Constantinescu02164b32014-11-13 14:05:07 +00002051 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002052 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2053 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002054 slow_path =
2055 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002056 instruction->SetSlowPath(slow_path);
2057 codegen_->AddSlowPath(slow_path);
2058 if (successor != nullptr) {
2059 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002060 }
2061 } else {
2062 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2063 }
2064
Serban Constantinescu02164b32014-11-13 14:05:07 +00002065 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2066 Register temp = temps.AcquireW();
2067
Vladimir Markoddf4fd32021-11-22 16:31:57 +00002068 __ Ldr(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Vladimir Marko254a8582021-11-29 14:08:37 +00002069 __ Tst(temp, Thread::SuspendOrCheckpointRequestFlags());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002070 if (successor == nullptr) {
Vladimir Marko254a8582021-11-29 14:08:37 +00002071 __ B(ne, slow_path->GetEntryLabel());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002072 __ Bind(slow_path->GetReturnLabel());
2073 } else {
Vladimir Marko254a8582021-11-29 14:08:37 +00002074 __ B(eq, codegen_->GetLabelOf(successor));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002075 __ B(slow_path->GetEntryLabel());
2076 // slow_path will return to GetLabelOf(successor).
2077 }
2078}
2079
Alexandre Rames5319def2014-10-23 10:03:10 +01002080InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2081 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002082 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002083 assembler_(codegen->GetAssembler()),
2084 codegen_(codegen) {}
2085
Alexandre Rames67555f72014-11-18 10:55:16 +00002086void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002087 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002088 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002089 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002090 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002091 case DataType::Type::kInt32:
2092 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01002093 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002094 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002095 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002096 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002097
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002098 case DataType::Type::kFloat32:
2099 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002100 locations->SetInAt(0, Location::RequiresFpuRegister());
2101 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002102 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002103 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002104
Alexandre Rames5319def2014-10-23 10:03:10 +01002105 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002106 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002107 }
2108}
2109
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002110void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2111 const FieldInfo& field_info) {
Alex Light3a73ffb2021-01-25 14:11:05 +00002112 DCHECK(instruction->IsInstanceFieldGet() ||
2113 instruction->IsStaticFieldGet() ||
2114 instruction->IsPredicatedInstanceFieldGet());
2115
2116 bool is_predicated = instruction->IsPredicatedInstanceFieldGet();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002117
2118 bool object_field_get_with_read_barrier =
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00002119 gUseReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01002120 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002121 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2122 object_field_get_with_read_barrier
2123 ? LocationSummary::kCallOnSlowPath
2124 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002125 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002126 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko0ecac682018-08-07 10:40:38 +01002127 // We need a temporary register for the read barrier load in
2128 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2129 // only if the field is volatile or the offset is too big.
2130 if (field_info.IsVolatile() ||
2131 field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2132 locations->AddTemp(FixedTempLocation());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002133 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002134 }
Alex Light3a73ffb2021-01-25 14:11:05 +00002135 // Input for object receiver.
2136 locations->SetInAt(is_predicated ? 1 : 0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002137 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alex Light3a73ffb2021-01-25 14:11:05 +00002138 if (is_predicated) {
2139 locations->SetInAt(0, Location::RequiresFpuRegister());
2140 locations->SetOut(Location::SameAsFirstInput());
2141 } else {
2142 locations->SetOut(Location::RequiresFpuRegister());
2143 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002144 } else {
Alex Light3a73ffb2021-01-25 14:11:05 +00002145 if (is_predicated) {
2146 locations->SetInAt(0, Location::RequiresRegister());
2147 locations->SetOut(Location::SameAsFirstInput());
2148 } else {
2149 // The output overlaps for an object field get when read barriers
2150 // are enabled: we do not want the load to overwrite the object's
2151 // location, as we need it to emit the read barrier.
2152 locations->SetOut(Location::RequiresRegister(),
2153 object_field_get_with_read_barrier ? Location::kOutputOverlap
2154 : Location::kNoOutputOverlap);
2155 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002156 }
2157}
2158
2159void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2160 const FieldInfo& field_info) {
Alex Light3a73ffb2021-01-25 14:11:05 +00002161 DCHECK(instruction->IsInstanceFieldGet() ||
2162 instruction->IsStaticFieldGet() ||
2163 instruction->IsPredicatedInstanceFieldGet());
2164 bool is_predicated = instruction->IsPredicatedInstanceFieldGet();
Roland Levillain44015862016-01-22 11:47:17 +00002165 LocationSummary* locations = instruction->GetLocations();
Alex Light3a73ffb2021-01-25 14:11:05 +00002166 uint32_t receiver_input = is_predicated ? 1 : 0;
2167 Location base_loc = locations->InAt(receiver_input);
Roland Levillain44015862016-01-22 11:47:17 +00002168 Location out = locations->Out();
2169 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01002170 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
2171 DataType::Type load_type = instruction->GetType();
Alex Light3a73ffb2021-01-25 14:11:05 +00002172 MemOperand field =
2173 HeapOperand(InputRegisterAt(instruction, receiver_input), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002174
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00002175 if (gUseReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01002176 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002177 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002178 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002179 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002180 Location maybe_temp =
2181 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002182 // Note that potential implicit null checks are handled in this
2183 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2184 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2185 instruction,
2186 out,
2187 base,
2188 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002189 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002190 /* needs_null_check= */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002191 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002192 } else {
2193 // General case.
2194 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002195 // Note that a potential implicit null check is handled in this
2196 // CodeGeneratorARM64::LoadAcquire call.
2197 // NB: LoadAcquire will record the pc info if needed.
Vladimir Marko98873af2020-12-16 12:10:03 +00002198 codegen_->LoadAcquire(instruction,
2199 load_type,
2200 OutputCPURegister(instruction),
2201 field,
2202 /* needs_null_check= */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002203 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002204 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2205 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01002206 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002207 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002208 }
Vladimir Marko61b92282017-10-11 13:23:17 +01002209 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002210 // If read barriers are enabled, emit read barriers other than
2211 // Baker's using a slow path (and also unpoison the loaded
2212 // reference, if heap poisoning is enabled).
2213 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2214 }
Roland Levillain4d027112015-07-01 15:41:14 +01002215 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002216}
2217
2218void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2219 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002220 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01002221 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002222 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2223 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002224 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002225 locations->SetInAt(1, Location::RequiresFpuRegister());
2226 } else {
2227 locations->SetInAt(1, Location::RequiresRegister());
2228 }
2229}
2230
2231void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002232 const FieldInfo& field_info,
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00002233 bool value_can_be_null,
2234 WriteBarrierKind write_barrier_kind) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002235 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alex Light3a73ffb2021-01-25 14:11:05 +00002236 bool is_predicated =
2237 instruction->IsInstanceFieldSet() && instruction->AsInstanceFieldSet()->GetIsPredicatedSet();
Alexandre Rames09a99962015-04-15 11:47:56 +01002238
2239 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002240 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002241 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002242 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002243 DataType::Type field_type = field_info.GetFieldType();
Alex Light3a73ffb2021-01-25 14:11:05 +00002244 std::optional<vixl::aarch64::Label> pred_is_null;
2245 if (is_predicated) {
2246 pred_is_null.emplace();
2247 __ Cbz(obj, &*pred_is_null);
2248 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002249
Roland Levillain4d027112015-07-01 15:41:14 +01002250 {
2251 // We use a block to end the scratch scope before the write barrier, thus
2252 // freeing the temporary registers so they can be used in `MarkGCCard`.
2253 UseScratchRegisterScope temps(GetVIXLAssembler());
2254
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002255 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002256 DCHECK(value.IsW());
2257 Register temp = temps.AcquireW();
2258 __ Mov(temp, value.W());
2259 GetAssembler()->PoisonHeapReference(temp.W());
2260 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002261 }
Roland Levillain4d027112015-07-01 15:41:14 +01002262
2263 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002264 codegen_->StoreRelease(
Andreas Gampe3db70682018-12-26 15:12:03 -08002265 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check= */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002266 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002267 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2268 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002269 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2270 codegen_->MaybeRecordImplicitNullCheck(instruction);
2271 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002272 }
2273
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00002274 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1)) &&
2275 write_barrier_kind != WriteBarrierKind::kDontEmit) {
2276 codegen_->MarkGCCard(
2277 obj,
2278 Register(value),
2279 value_can_be_null && write_barrier_kind == WriteBarrierKind::kEmitWithNullCheck);
Alexandre Rames09a99962015-04-15 11:47:56 +01002280 }
Alex Light3a73ffb2021-01-25 14:11:05 +00002281
2282 if (is_predicated) {
2283 __ Bind(&*pred_is_null);
2284 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002285}
2286
Alexandre Rames67555f72014-11-18 10:55:16 +00002287void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002288 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002289
2290 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002291 case DataType::Type::kInt32:
2292 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002293 Register dst = OutputRegister(instr);
2294 Register lhs = InputRegisterAt(instr, 0);
2295 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002296 if (instr->IsAdd()) {
2297 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002298 } else if (instr->IsAnd()) {
2299 __ And(dst, lhs, rhs);
2300 } else if (instr->IsOr()) {
2301 __ Orr(dst, lhs, rhs);
2302 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002303 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002304 } else if (instr->IsRor()) {
2305 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002306 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002307 __ Ror(dst, lhs, shift);
2308 } else {
2309 // Ensure shift distance is in the same size register as the result. If
2310 // we are rotating a long and the shift comes in a w register originally,
2311 // we don't need to sxtw for use as an x since the shift distances are
2312 // all & reg_bits - 1.
2313 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2314 }
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002315 } else if (instr->IsMin() || instr->IsMax()) {
2316 __ Cmp(lhs, rhs);
2317 __ Csel(dst, lhs, rhs, instr->IsMin() ? lt : gt);
Alexandre Rames67555f72014-11-18 10:55:16 +00002318 } else {
2319 DCHECK(instr->IsXor());
2320 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002321 }
2322 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002323 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002324 case DataType::Type::kFloat32:
2325 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002326 VRegister dst = OutputFPRegister(instr);
2327 VRegister lhs = InputFPRegisterAt(instr, 0);
2328 VRegister rhs = InputFPRegisterAt(instr, 1);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002329 if (instr->IsAdd()) {
2330 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002331 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002332 __ Fsub(dst, lhs, rhs);
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002333 } else if (instr->IsMin()) {
2334 __ Fmin(dst, lhs, rhs);
2335 } else if (instr->IsMax()) {
2336 __ Fmax(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002337 } else {
2338 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002339 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002340 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002341 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002342 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002343 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002344 }
2345}
2346
Serban Constantinescu02164b32014-11-13 14:05:07 +00002347void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2348 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2349
Vladimir Markoca6fff82017-10-03 14:49:14 +01002350 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002351 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002352 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002353 case DataType::Type::kInt32:
2354 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002355 locations->SetInAt(0, Location::RequiresRegister());
2356 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002357 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002358 break;
2359 }
2360 default:
2361 LOG(FATAL) << "Unexpected shift type " << type;
2362 }
2363}
2364
2365void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2366 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2367
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002368 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002369 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002370 case DataType::Type::kInt32:
2371 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002372 Register dst = OutputRegister(instr);
2373 Register lhs = InputRegisterAt(instr, 0);
2374 Operand rhs = InputOperandAt(instr, 1);
2375 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002376 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002377 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002378 if (instr->IsShl()) {
2379 __ Lsl(dst, lhs, shift_value);
2380 } else if (instr->IsShr()) {
2381 __ Asr(dst, lhs, shift_value);
2382 } else {
2383 __ Lsr(dst, lhs, shift_value);
2384 }
2385 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002386 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002387
2388 if (instr->IsShl()) {
2389 __ Lsl(dst, lhs, rhs_reg);
2390 } else if (instr->IsShr()) {
2391 __ Asr(dst, lhs, rhs_reg);
2392 } else {
2393 __ Lsr(dst, lhs, rhs_reg);
2394 }
2395 }
2396 break;
2397 }
2398 default:
2399 LOG(FATAL) << "Unexpected shift operation type " << type;
2400 }
2401}
2402
Alexandre Rames5319def2014-10-23 10:03:10 +01002403void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002404 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002405}
2406
2407void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002408 HandleBinaryOp(instruction);
2409}
2410
2411void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2412 HandleBinaryOp(instruction);
2413}
2414
2415void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2416 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002417}
2418
Artem Serov7fc63502016-02-09 17:15:29 +00002419void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002420 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002421 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002422 locations->SetInAt(0, Location::RequiresRegister());
2423 // There is no immediate variant of negated bitwise instructions in AArch64.
2424 locations->SetInAt(1, Location::RequiresRegister());
2425 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2426}
2427
Artem Serov7fc63502016-02-09 17:15:29 +00002428void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002429 Register dst = OutputRegister(instr);
2430 Register lhs = InputRegisterAt(instr, 0);
2431 Register rhs = InputRegisterAt(instr, 1);
2432
2433 switch (instr->GetOpKind()) {
2434 case HInstruction::kAnd:
2435 __ Bic(dst, lhs, rhs);
2436 break;
2437 case HInstruction::kOr:
2438 __ Orn(dst, lhs, rhs);
2439 break;
2440 case HInstruction::kXor:
2441 __ Eon(dst, lhs, rhs);
2442 break;
2443 default:
2444 LOG(FATAL) << "Unreachable";
2445 }
2446}
2447
Anton Kirilov74234da2017-01-13 14:42:47 +00002448void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2449 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002450 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2451 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002452 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002453 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002454 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2455 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2456 } else {
2457 locations->SetInAt(0, Location::RequiresRegister());
2458 }
2459 locations->SetInAt(1, Location::RequiresRegister());
2460 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2461}
2462
Anton Kirilov74234da2017-01-13 14:42:47 +00002463void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2464 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002465 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002466 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002467 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002468 Register out = OutputRegister(instruction);
2469 Register left;
2470 if (kind != HInstruction::kNeg) {
2471 left = InputRegisterAt(instruction, 0);
2472 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002473 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002474 // shifter operand operation, the IR generating `right_reg` (input to the type
2475 // conversion) can have a different type from the current instruction's type,
2476 // so we manually indicate the type.
2477 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002478 Operand right_operand(0);
2479
Anton Kirilov74234da2017-01-13 14:42:47 +00002480 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2481 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002482 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2483 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002484 right_operand = Operand(right_reg,
2485 helpers::ShiftFromOpKind(op_kind),
2486 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002487 }
2488
2489 // Logical binary operations do not support extension operations in the
2490 // operand. Note that VIXL would still manage if it was passed by generating
2491 // the extension as a separate instruction.
2492 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
Santiago Aboy Solanes872ec722022-02-18 14:10:25 +00002493 DCHECK_IMPLIES(right_operand.IsExtendedRegister(),
2494 kind != HInstruction::kAnd && kind != HInstruction::kOr &&
2495 kind != HInstruction::kXor && kind != HInstruction::kNeg);
Alexandre Rames8626b742015-11-25 16:28:08 +00002496 switch (kind) {
2497 case HInstruction::kAdd:
2498 __ Add(out, left, right_operand);
2499 break;
2500 case HInstruction::kAnd:
2501 __ And(out, left, right_operand);
2502 break;
2503 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002504 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002505 __ Neg(out, right_operand);
2506 break;
2507 case HInstruction::kOr:
2508 __ Orr(out, left, right_operand);
2509 break;
2510 case HInstruction::kSub:
2511 __ Sub(out, left, right_operand);
2512 break;
2513 case HInstruction::kXor:
2514 __ Eor(out, left, right_operand);
2515 break;
2516 default:
2517 LOG(FATAL) << "Unexpected operation kind: " << kind;
2518 UNREACHABLE();
2519 }
2520}
2521
Artem Serov328429f2016-07-06 16:23:04 +01002522void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002523 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002524 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002525 locations->SetInAt(0, Location::RequiresRegister());
2526 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002527 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002528}
2529
Roland Levillain19c54192016-11-04 13:44:09 +00002530void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002531 __ Add(OutputRegister(instruction),
2532 InputRegisterAt(instruction, 0),
2533 Operand(InputOperandAt(instruction, 1)));
2534}
2535
Artem Serove1811ed2017-04-27 16:50:47 +01002536void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2537 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002538 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002539
2540 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2541
2542 locations->SetInAt(0, Location::RequiresRegister());
2543 // For byte case we don't need to shift the index variable so we can encode the data offset into
2544 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2545 // data offset constant generation out of the loop and reduce the critical path length in the
2546 // loop.
2547 locations->SetInAt(1, shift->GetValue() == 0
2548 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2549 : Location::RequiresRegister());
2550 locations->SetInAt(2, Location::ConstantLocation(shift));
2551 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2552}
2553
2554void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2555 HIntermediateAddressIndex* instruction) {
2556 Register index_reg = InputRegisterAt(instruction, 0);
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002557 uint32_t shift = Int64FromLocation(instruction->GetLocations()->InAt(2));
Artem Serove1811ed2017-04-27 16:50:47 +01002558 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2559
2560 if (shift == 0) {
2561 __ Add(OutputRegister(instruction), index_reg, offset);
2562 } else {
2563 Register offset_reg = InputRegisterAt(instruction, 1);
2564 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2565 }
2566}
2567
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002568void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002569 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002570 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002571 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2572 if (instr->GetOpKind() == HInstruction::kSub &&
2573 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002574 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002575 // Don't allocate register for Mneg instruction.
2576 } else {
2577 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2578 Location::RequiresRegister());
2579 }
2580 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2581 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002582 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2583}
2584
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002585void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002586 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002587 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2588 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002589
2590 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2591 // This fixup should be carried out for all multiply-accumulate instructions:
2592 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002593 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002594 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2595 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Florian Mayer6f572b72022-01-10 13:03:57 -08002596 ptrdiff_t off = masm->GetCursorOffset();
2597 if (off >= static_cast<ptrdiff_t>(kInstructionSize) &&
2598 masm->GetInstructionAt(off - static_cast<ptrdiff_t>(kInstructionSize))->IsLoadOrStore()) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002599 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002600 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002601 __ nop();
2602 }
2603 }
2604
2605 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002606 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002607 __ Madd(res, mul_left, mul_right, accumulator);
2608 } else {
2609 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002610 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002611 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002612 __ Mneg(res, mul_left, mul_right);
2613 } else {
2614 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2615 __ Msub(res, mul_left, mul_right, accumulator);
2616 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002617 }
2618}
2619
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002620void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002621 bool object_array_get_with_read_barrier =
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00002622 gUseReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002623 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002624 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2625 object_array_get_with_read_barrier
2626 ? LocationSummary::kCallOnSlowPath
2627 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002628 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002629 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002630 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002631 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002632 // We need a temporary register for the read barrier load in
2633 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2634 // only if the offset is too big.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002635 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2636 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002637 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002638 if (offset >= kReferenceLoadMinFarOffset) {
2639 locations->AddTemp(FixedTempLocation());
2640 }
Artem Serov0806f582018-10-11 20:14:20 +01002641 } else if (!instruction->GetArray()->IsIntermediateAddress()) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01002642 // We need a non-scratch temporary for the array data pointer in
Artem Serov0806f582018-10-11 20:14:20 +01002643 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier() for the case with no
2644 // intermediate address.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002645 locations->AddTemp(Location::RequiresRegister());
2646 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002647 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002648 locations->SetInAt(0, Location::RequiresRegister());
2649 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002650 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002651 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2652 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002653 // The output overlaps in the case of an object array get with
2654 // read barriers enabled: we do not want the move to overwrite the
2655 // array's location, as we need it to emit the read barrier.
2656 locations->SetOut(
2657 Location::RequiresRegister(),
2658 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002659 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002660}
2661
2662void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002663 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002664 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002665 LocationSummary* locations = instruction->GetLocations();
2666 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002667 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002668 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002669 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2670 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002671 MacroAssembler* masm = GetVIXLAssembler();
2672 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002673
Artem Serov0806f582018-10-11 20:14:20 +01002674 // The non-Baker read barrier instrumentation of object ArrayGet instructions
Roland Levillain19c54192016-11-04 13:44:09 +00002675 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002676 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002677 instruction->GetArray()->IsIntermediateAddress() &&
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00002678 gUseReadBarrier &&
Artem Serov0806f582018-10-11 20:14:20 +01002679 !kUseBakerReadBarrier));
Roland Levillain19c54192016-11-04 13:44:09 +00002680
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00002681 if (type == DataType::Type::kReference && gUseReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002682 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002683 // Note that a potential implicit null check is handled in the
2684 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002685 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002686 if (index.IsConstant()) {
Artem Serov0806f582018-10-11 20:14:20 +01002687 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002688 // Array load with a constant index can be treated as a field load.
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002689 offset += Int64FromLocation(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002690 Location maybe_temp =
2691 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2692 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2693 out,
2694 obj.W(),
2695 offset,
2696 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002697 /* needs_null_check= */ false,
2698 /* use_load_acquire= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002699 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002700 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08002701 instruction, out, obj.W(), offset, index, /* needs_null_check= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002702 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002703 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002704 // General case.
2705 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002706 Register length;
2707 if (maybe_compressed_char_at) {
2708 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2709 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002710 {
2711 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2712 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2713
2714 if (instruction->GetArray()->IsIntermediateAddress()) {
2715 DCHECK_LT(count_offset, offset);
2716 int64_t adjusted_offset =
2717 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2718 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2719 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2720 } else {
2721 __ Ldr(length, HeapOperand(obj, count_offset));
2722 }
2723 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002724 }
jessicahandojo05765752016-09-09 19:01:32 -07002725 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002726 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002727 if (maybe_compressed_char_at) {
2728 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002729 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2730 "Expecting 0=compressed, 1=uncompressed");
2731 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002732 __ Ldrb(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002733 HeapOperand(obj, offset + Int64FromLocation(index)));
jessicahandojo05765752016-09-09 19:01:32 -07002734 __ B(&done);
2735 __ Bind(&uncompressed_load);
2736 __ Ldrh(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002737 HeapOperand(obj, offset + (Int64FromLocation(index) << 1)));
jessicahandojo05765752016-09-09 19:01:32 -07002738 __ Bind(&done);
2739 } else {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002740 offset += Int64FromLocation(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002741 source = HeapOperand(obj, offset);
2742 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002743 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002744 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002745 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002746 // We do not need to compute the intermediate address from the array: the
2747 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002748 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002749 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002750 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2751 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
Roland Levillain44015862016-01-22 11:47:17 +00002752 }
2753 temp = obj;
2754 } else {
2755 __ Add(temp, obj, offset);
2756 }
jessicahandojo05765752016-09-09 19:01:32 -07002757 if (maybe_compressed_char_at) {
2758 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002759 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2760 "Expecting 0=compressed, 1=uncompressed");
2761 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002762 __ Ldrb(Register(OutputCPURegister(instruction)),
2763 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2764 __ B(&done);
2765 __ Bind(&uncompressed_load);
2766 __ Ldrh(Register(OutputCPURegister(instruction)),
2767 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2768 __ Bind(&done);
2769 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002770 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002771 }
Roland Levillain44015862016-01-22 11:47:17 +00002772 }
jessicahandojo05765752016-09-09 19:01:32 -07002773 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002774 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2775 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002776 codegen_->Load(type, OutputCPURegister(instruction), source);
2777 codegen_->MaybeRecordImplicitNullCheck(instruction);
2778 }
Roland Levillain44015862016-01-22 11:47:17 +00002779
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002780 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002781 static_assert(
2782 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2783 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2784 Location obj_loc = locations->InAt(0);
2785 if (index.IsConstant()) {
2786 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2787 } else {
2788 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2789 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002790 }
Roland Levillain4d027112015-07-01 15:41:14 +01002791 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002792}
2793
Alexandre Rames5319def2014-10-23 10:03:10 +01002794void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002795 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002796 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002797 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002798}
2799
2800void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002801 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002802 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002803 {
2804 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2805 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2806 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2807 codegen_->MaybeRecordImplicitNullCheck(instruction);
2808 }
jessicahandojo05765752016-09-09 19:01:32 -07002809 // Mask out compression flag from String's array length.
2810 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002811 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002812 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002813}
2814
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002815void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002816 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002817
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002818 bool needs_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002819 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002820 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002821 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002822 locations->SetInAt(0, Location::RequiresRegister());
2823 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002824 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2825 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002826 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002827 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002828 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002829 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002830 }
2831}
2832
2833void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002834 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002835 LocationSummary* locations = instruction->GetLocations();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002836 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002837 bool needs_write_barrier =
2838 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002839
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002840 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002841 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002842 CPURegister source = value;
2843 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002844 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002845 MemOperand destination = HeapOperand(array);
2846 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002847
2848 if (!needs_write_barrier) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002849 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002850 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002851 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002852 destination = HeapOperand(array, offset);
2853 } else {
2854 UseScratchRegisterScope temps(masm);
2855 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002856 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002857 // We do not need to compute the intermediate address from the array: the
2858 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002859 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002860 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002861 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2862 DCHECK(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002863 }
2864 temp = array;
2865 } else {
2866 __ Add(temp, array, offset);
2867 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002868 destination = HeapOperand(temp,
2869 XRegisterFrom(index),
2870 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002871 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002872 }
Artem Serov914d7a82017-02-07 14:33:49 +00002873 {
2874 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2875 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2876 codegen_->Store(value_type, value, destination);
2877 codegen_->MaybeRecordImplicitNullCheck(instruction);
2878 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002879 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002880 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002881
2882 bool can_value_be_null = instruction->GetValueCanBeNull();
2883 vixl::aarch64::Label do_store;
2884 if (can_value_be_null) {
2885 __ Cbz(Register(value), &do_store);
2886 }
2887
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002888 SlowPathCodeARM64* slow_path = nullptr;
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002889 if (needs_type_check) {
2890 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
2891 codegen_->AddSlowPath(slow_path);
2892
2893 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2894 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2895 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2896
Alexandre Rames97833a02015-04-16 15:07:12 +01002897 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002898 Register temp = temps.AcquireSameSizeAs(array);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002899 Register temp2 = temps.AcquireSameSizeAs(array);
2900
2901 // Note that when Baker read barriers are enabled, the type
2902 // checks are performed without read barriers. This is fine,
2903 // even in the case where a class object is in the from-space
2904 // after the flip, as a comparison involving such a type would
2905 // not produce a false positive; it may of course produce a
2906 // false negative, in which case we would take the ArraySet
2907 // slow path.
2908
2909 // /* HeapReference<Class> */ temp = array->klass_
2910 {
2911 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2912 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2913 __ Ldr(temp, HeapOperand(array, class_offset));
2914 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames97833a02015-04-16 15:07:12 +01002915 }
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002916 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Alexandre Rames97833a02015-04-16 15:07:12 +01002917
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002918 // /* HeapReference<Class> */ temp = temp->component_type_
2919 __ Ldr(temp, HeapOperand(temp, component_offset));
2920 // /* HeapReference<Class> */ temp2 = value->klass_
2921 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2922 // If heap poisoning is enabled, no need to unpoison `temp`
2923 // nor `temp2`, as we are comparing two poisoned references.
2924 __ Cmp(temp, temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002925
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002926 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2927 vixl::aarch64::Label do_put;
2928 __ B(eq, &do_put);
2929 // If heap poisoning is enabled, the `temp` reference has
2930 // not been unpoisoned yet; unpoison it now.
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002931 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002932
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002933 // /* HeapReference<Class> */ temp = temp->super_class_
2934 __ Ldr(temp, HeapOperand(temp, super_offset));
2935 // If heap poisoning is enabled, no need to unpoison
2936 // `temp`, as we are comparing against null below.
2937 __ Cbnz(temp, slow_path->GetEntryLabel());
2938 __ Bind(&do_put);
Vladimir Markod1ef8732017-04-18 13:55:13 +01002939 } else {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002940 __ B(ne, slow_path->GetEntryLabel());
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002941 }
2942 }
2943
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00002944 if (instruction->GetWriteBarrierKind() != WriteBarrierKind::kDontEmit) {
2945 DCHECK_EQ(instruction->GetWriteBarrierKind(), WriteBarrierKind::kEmitNoNullCheck)
2946 << " Already null checked so we shouldn't do it again.";
2947 codegen_->MarkGCCard(array, value.W(), /* emit_null_check= */ false);
2948 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002949
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002950 if (can_value_be_null) {
2951 DCHECK(do_store.IsLinked());
2952 __ Bind(&do_store);
2953 }
2954
2955 UseScratchRegisterScope temps(masm);
2956 if (kPoisonHeapReferences) {
2957 Register temp_source = temps.AcquireSameSizeAs(array);
2958 DCHECK(value.IsW());
2959 __ Mov(temp_source, value.W());
2960 GetAssembler()->PoisonHeapReference(temp_source);
2961 source = temp_source;
2962 }
2963
2964 if (index.IsConstant()) {
2965 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
2966 destination = HeapOperand(array, offset);
2967 } else {
2968 Register temp_base = temps.AcquireSameSizeAs(array);
2969 __ Add(temp_base, array, offset);
2970 destination = HeapOperand(temp_base,
2971 XRegisterFrom(index),
2972 LSL,
2973 DataType::SizeShift(value_type));
2974 }
2975
2976 {
2977 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2978 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2979 __ Str(source, destination);
2980
2981 if (can_value_be_null || !needs_type_check) {
2982 codegen_->MaybeRecordImplicitNullCheck(instruction);
2983 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002984 }
2985
2986 if (slow_path != nullptr) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002987 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002988 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002989 }
2990}
2991
Alexandre Rames67555f72014-11-18 10:55:16 +00002992void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002993 RegisterSet caller_saves = RegisterSet::Empty();
2994 InvokeRuntimeCallingConvention calling_convention;
2995 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2996 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2997 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002998
2999 // If both index and length are constant, we can check the bounds statically and
3000 // generate code accordingly. We want to make sure we generate constant locations
3001 // in that case, regardless of whether they are encodable in the comparison or not.
3002 HInstruction* index = instruction->InputAt(0);
3003 HInstruction* length = instruction->InputAt(1);
3004 bool both_const = index->IsConstant() && length->IsConstant();
3005 locations->SetInAt(0, both_const
3006 ? Location::ConstantLocation(index->AsConstant())
3007 : ARM64EncodableConstantOrRegister(index, instruction));
3008 locations->SetInAt(1, both_const
3009 ? Location::ConstantLocation(length->AsConstant())
3010 : ARM64EncodableConstantOrRegister(length, instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003011}
3012
3013void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Georgia Kouvelibe530852019-01-17 10:46:41 +00003014 LocationSummary* locations = instruction->GetLocations();
3015 Location index_loc = locations->InAt(0);
3016 Location length_loc = locations->InAt(1);
3017
3018 int cmp_first_input = 0;
3019 int cmp_second_input = 1;
3020 Condition cond = hs;
3021
3022 if (index_loc.IsConstant()) {
3023 int64_t index = Int64FromLocation(index_loc);
3024 if (length_loc.IsConstant()) {
3025 int64_t length = Int64FromLocation(length_loc);
3026 if (index < 0 || index >= length) {
3027 BoundsCheckSlowPathARM64* slow_path =
3028 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
3029 codegen_->AddSlowPath(slow_path);
3030 __ B(slow_path->GetEntryLabel());
3031 } else {
3032 // BCE will remove the bounds check if we are guaranteed to pass.
3033 // However, some optimization after BCE may have generated this, and we should not
3034 // generate a bounds check if it is a valid range.
3035 }
3036 return;
3037 }
3038 // Only the index is constant: change the order of the operands and commute the condition
3039 // so we can use an immediate constant for the index (only the second input to a cmp
3040 // instruction can be an immediate).
3041 cmp_first_input = 1;
3042 cmp_second_input = 0;
3043 cond = ls;
3044 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003045 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003046 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Georgia Kouvelibe530852019-01-17 10:46:41 +00003047 __ Cmp(InputRegisterAt(instruction, cmp_first_input),
3048 InputOperandAt(instruction, cmp_second_input));
Alexandre Rames67555f72014-11-18 10:55:16 +00003049 codegen_->AddSlowPath(slow_path);
Georgia Kouvelibe530852019-01-17 10:46:41 +00003050 __ B(slow_path->GetEntryLabel(), cond);
Alexandre Rames67555f72014-11-18 10:55:16 +00003051}
3052
Alexandre Rames67555f72014-11-18 10:55:16 +00003053void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3054 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003055 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00003056 locations->SetInAt(0, Location::RequiresRegister());
3057 if (check->HasUses()) {
3058 locations->SetOut(Location::SameAsFirstInput());
3059 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01003060 // Rely on the type initialization to save everything we need.
3061 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Alexandre Rames67555f72014-11-18 10:55:16 +00003062}
3063
3064void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3065 // We assume the class is not null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01003066 SlowPathCodeARM64* slow_path =
3067 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(check->GetLoadClass(), check);
Alexandre Rames67555f72014-11-18 10:55:16 +00003068 codegen_->AddSlowPath(slow_path);
3069 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3070}
3071
Roland Levillain1a653882016-03-18 18:05:57 +00003072static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3073 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3074 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3075}
3076
3077void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01003078 VRegister lhs_reg = InputFPRegisterAt(instruction, 0);
Roland Levillain1a653882016-03-18 18:05:57 +00003079 Location rhs_loc = instruction->GetLocations()->InAt(1);
3080 if (rhs_loc.IsConstant()) {
3081 // 0.0 is the only immediate that can be encoded directly in
3082 // an FCMP instruction.
3083 //
3084 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3085 // specify that in a floating-point comparison, positive zero
3086 // and negative zero are considered equal, so we can use the
3087 // literal 0.0 for both cases here.
3088 //
3089 // Note however that some methods (Float.equal, Float.compare,
3090 // Float.compareTo, Double.equal, Double.compare,
3091 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3092 // StrictMath.min) consider 0.0 to be (strictly) greater than
3093 // -0.0. So if we ever translate calls to these methods into a
3094 // HCompare instruction, we must handle the -0.0 case with
3095 // care here.
3096 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3097 __ Fcmp(lhs_reg, 0.0);
3098 } else {
3099 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3100 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003101}
3102
Serban Constantinescu02164b32014-11-13 14:05:07 +00003103void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003104 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003105 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003106 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003107 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003108 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003109 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003110 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003111 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003112 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003113 case DataType::Type::kInt32:
3114 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003115 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003116 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003117 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3118 break;
3119 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003120 case DataType::Type::kFloat32:
3121 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003122 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003123 locations->SetInAt(1,
3124 IsFloatingPointZeroConstant(compare->InputAt(1))
3125 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3126 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003127 locations->SetOut(Location::RequiresRegister());
3128 break;
3129 }
3130 default:
3131 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3132 }
3133}
3134
3135void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003136 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00003137
3138 // 0 if: left == right
3139 // 1 if: left > right
3140 // -1 if: left < right
3141 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003142 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003143 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003144 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003145 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003146 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003147 case DataType::Type::kInt32:
3148 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003149 Register result = OutputRegister(compare);
3150 Register left = InputRegisterAt(compare, 0);
3151 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003152 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003153 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3154 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003155 break;
3156 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003157 case DataType::Type::kFloat32:
3158 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003159 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003160 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003161 __ Cset(result, ne);
3162 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003163 break;
3164 }
3165 default:
3166 LOG(FATAL) << "Unimplemented compare type " << in_type;
3167 }
3168}
3169
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003170void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003171 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003172
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003173 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003174 locations->SetInAt(0, Location::RequiresFpuRegister());
3175 locations->SetInAt(1,
3176 IsFloatingPointZeroConstant(instruction->InputAt(1))
3177 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3178 : Location::RequiresFpuRegister());
3179 } else {
3180 // Integer cases.
3181 locations->SetInAt(0, Location::RequiresRegister());
3182 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3183 }
3184
David Brazdilb3e773e2016-01-26 11:28:37 +00003185 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003186 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003187 }
3188}
3189
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003190void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003191 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003192 return;
3193 }
3194
3195 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003196 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003197 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003198
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003199 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003200 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003201 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003202 } else {
3203 // Integer cases.
3204 Register lhs = InputRegisterAt(instruction, 0);
3205 Operand rhs = InputOperandAt(instruction, 1);
3206 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003207 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003208 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003209}
3210
3211#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3212 M(Equal) \
3213 M(NotEqual) \
3214 M(LessThan) \
3215 M(LessThanOrEqual) \
3216 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003217 M(GreaterThanOrEqual) \
3218 M(Below) \
3219 M(BelowOrEqual) \
3220 M(Above) \
3221 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003222#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003223void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3224void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003225FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003226#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003227#undef FOR_EACH_CONDITION_INSTRUCTION
3228
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003229void InstructionCodeGeneratorARM64::GenerateIntDivForPower2Denom(HDiv* instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003230 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003231 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003232 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
3233
3234 Register out = OutputRegister(instruction);
3235 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003236
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003237 Register final_dividend;
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003238 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003239 // No need to adjust the result for non-negative dividends or the INT32_MIN/INT64_MIN dividends.
3240 // NOTE: The generated code for HDiv correctly works for the INT32_MIN/INT64_MIN dividends:
3241 // imm == 2
3242 // add out, dividend(0x80000000), dividend(0x80000000), lsr #31 => out = 0x80000001
3243 // asr out, out(0x80000001), #1 => out = 0xc0000000
3244 // This is the same as 'asr out, 0x80000000, #1'
3245 //
3246 // imm > 2
3247 // add temp, dividend(0x80000000), imm - 1 => temp = 0b10..01..1, where the number
3248 // of the rightmost 1s is ctz_imm.
3249 // cmp dividend(0x80000000), 0 => N = 1, V = 0 (lt is true)
3250 // csel out, temp(0b10..01..1), dividend(0x80000000), lt => out = 0b10..01..1
3251 // asr out, out(0b10..01..1), #ctz_imm => out = 0b1..10..0, where the number of the
3252 // leftmost 1s is ctz_imm + 1.
3253 // This is the same as 'asr out, dividend(0x80000000), #ctz_imm'.
3254 //
3255 // imm == INT32_MIN
3256 // add tmp, dividend(0x80000000), #0x7fffffff => tmp = -1
3257 // cmp dividend(0x80000000), 0 => N = 1, V = 0 (lt is true)
3258 // csel out, temp(-1), dividend(0x80000000), lt => out = -1
3259 // neg out, out(-1), asr #31 => out = 1
3260 // This is the same as 'neg out, dividend(0x80000000), asr #31'.
3261 final_dividend = dividend;
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003262 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003263 if (abs_imm == 2) {
3264 int bits = DataType::Size(instruction->GetResultType()) * kBitsPerByte;
3265 __ Add(out, dividend, Operand(dividend, LSR, bits - 1));
3266 } else {
3267 UseScratchRegisterScope temps(GetVIXLAssembler());
3268 Register temp = temps.AcquireSameSizeAs(out);
3269 __ Add(temp, dividend, abs_imm - 1);
3270 __ Cmp(dividend, 0);
3271 __ Csel(out, temp, dividend, lt);
3272 }
3273 final_dividend = out;
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003274 }
3275
Zheng Xuc6667102015-05-15 16:08:45 +08003276 int ctz_imm = CTZ(abs_imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003277 if (imm > 0) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003278 __ Asr(out, final_dividend, ctz_imm);
Zheng Xuc6667102015-05-15 16:08:45 +08003279 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003280 __ Neg(out, Operand(final_dividend, ASR, ctz_imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003281 }
3282}
3283
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003284// Return true if the magic number was modified by subtracting 2^32(Int32 div) or 2^64(Int64 div).
3285// So dividend needs to be added.
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003286static inline bool NeedToAddDividend(int64_t magic_number, int64_t divisor) {
3287 return divisor > 0 && magic_number < 0;
3288}
3289
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003290// Return true if the magic number was modified by adding 2^32(Int32 div) or 2^64(Int64 div).
3291// So dividend needs to be subtracted.
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003292static inline bool NeedToSubDividend(int64_t magic_number, int64_t divisor) {
3293 return divisor < 0 && magic_number > 0;
3294}
3295
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003296// Generate code which increments the value in register 'in' by 1 if the value is negative.
3297// It is done with 'add out, in, in, lsr #31 or #63'.
3298// If the value is a result of an operation setting the N flag, CINC MI can be used
3299// instead of ADD. 'use_cond_inc' controls this.
3300void InstructionCodeGeneratorARM64::GenerateIncrementNegativeByOne(
3301 Register out,
3302 Register in,
3303 bool use_cond_inc) {
3304 if (use_cond_inc) {
3305 __ Cinc(out, in, mi);
3306 } else {
3307 __ Add(out, in, Operand(in, LSR, in.GetSizeInBits() - 1));
3308 }
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003309}
3310
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003311// Helper to generate code producing the result of HRem with a constant divisor.
3312void InstructionCodeGeneratorARM64::GenerateResultRemWithAnyConstant(
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003313 Register out,
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003314 Register dividend,
3315 Register quotient,
3316 int64_t divisor,
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003317 UseScratchRegisterScope* temps_scope) {
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003318 Register temp_imm = temps_scope->AcquireSameSizeAs(out);
3319 __ Mov(temp_imm, divisor);
3320 __ Msub(out, quotient, temp_imm, dividend);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003321}
3322
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003323// Helper to generate code for HDiv/HRem instructions when a dividend is non-negative and
3324// a divisor is a positive constant, not power of 2.
3325void InstructionCodeGeneratorARM64::GenerateInt64UnsignedDivRemWithAnyPositiveConstant(
3326 HBinaryOperation* instruction) {
3327 DCHECK(instruction->IsDiv() || instruction->IsRem());
3328 DCHECK(instruction->GetResultType() == DataType::Type::kInt64);
3329
3330 LocationSummary* locations = instruction->GetLocations();
3331 Location second = locations->InAt(1);
3332 DCHECK(second.IsConstant());
3333
3334 Register out = OutputRegister(instruction);
3335 Register dividend = InputRegisterAt(instruction, 0);
3336 int64_t imm = Int64FromConstant(second.GetConstant());
3337 DCHECK_GT(imm, 0);
3338
3339 int64_t magic;
3340 int shift;
3341 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ true, &magic, &shift);
3342
3343 UseScratchRegisterScope temps(GetVIXLAssembler());
3344 Register temp = temps.AcquireSameSizeAs(out);
3345
3346 auto generate_unsigned_div_code = [this, magic, shift](Register out,
3347 Register dividend,
3348 Register temp) {
3349 // temp = get_high(dividend * magic)
3350 __ Mov(temp, magic);
3351 if (magic > 0 && shift == 0) {
3352 __ Smulh(out, dividend, temp);
3353 } else {
3354 __ Smulh(temp, dividend, temp);
3355 if (magic < 0) {
3356 // The negative magic means that the multiplier m is greater than INT64_MAX.
3357 // In such a case shift is never 0. See the proof in
3358 // InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant.
3359 __ Add(temp, temp, dividend);
3360 }
3361 DCHECK_NE(shift, 0);
3362 __ Lsr(out, temp, shift);
3363 }
3364 };
3365
3366 if (instruction->IsDiv()) {
3367 generate_unsigned_div_code(out, dividend, temp);
3368 } else {
3369 generate_unsigned_div_code(temp, dividend, temp);
3370 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3371 }
3372}
3373
3374// Helper to generate code for HDiv/HRem instructions for any dividend and a constant divisor
3375// (not power of 2).
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003376void InstructionCodeGeneratorARM64::GenerateInt64DivRemWithAnyConstant(
3377 HBinaryOperation* instruction) {
Zheng Xuc6667102015-05-15 16:08:45 +08003378 DCHECK(instruction->IsDiv() || instruction->IsRem());
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003379 DCHECK(instruction->GetResultType() == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003380
3381 LocationSummary* locations = instruction->GetLocations();
3382 Location second = locations->InAt(1);
3383 DCHECK(second.IsConstant());
3384
3385 Register out = OutputRegister(instruction);
3386 Register dividend = InputRegisterAt(instruction, 0);
3387 int64_t imm = Int64FromConstant(second.GetConstant());
3388
Zheng Xuc6667102015-05-15 16:08:45 +08003389 int64_t magic;
3390 int shift;
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003391 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ true, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08003392
3393 UseScratchRegisterScope temps(GetVIXLAssembler());
3394 Register temp = temps.AcquireSameSizeAs(out);
3395
3396 // temp = get_high(dividend * magic)
3397 __ Mov(temp, magic);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003398 __ Smulh(temp, dividend, temp);
3399
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003400 // The multiplication result might need some corrections to be finalized.
3401 // The last correction is to increment by 1, if the result is negative.
3402 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3403 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3404 // However if one of the corrections is ADD or SUB, the sign can be detected
3405 // with ADDS/SUBS. They set the N flag if the result is negative.
3406 // This allows to use CINC MI which has latency 1.
3407 bool use_cond_inc = false;
3408
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003409 // Some combinations of magic_number and the divisor require to correct the result.
3410 // Check whether the correction is needed.
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003411 if (NeedToAddDividend(magic, imm)) {
3412 __ Adds(temp, temp, dividend);
3413 use_cond_inc = true;
3414 } else if (NeedToSubDividend(magic, imm)) {
3415 __ Subs(temp, temp, dividend);
3416 use_cond_inc = true;
3417 }
3418
3419 if (shift != 0) {
3420 __ Asr(temp, temp, shift);
3421 }
3422
3423 if (instruction->IsRem()) {
3424 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3425 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3426 } else {
3427 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3428 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003429}
3430
3431void InstructionCodeGeneratorARM64::GenerateInt32DivRemWithAnyConstant(
3432 HBinaryOperation* instruction) {
3433 DCHECK(instruction->IsDiv() || instruction->IsRem());
3434 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
3435
3436 LocationSummary* locations = instruction->GetLocations();
3437 Location second = locations->InAt(1);
3438 DCHECK(second.IsConstant());
3439
3440 Register out = OutputRegister(instruction);
3441 Register dividend = InputRegisterAt(instruction, 0);
3442 int64_t imm = Int64FromConstant(second.GetConstant());
3443
3444 int64_t magic;
3445 int shift;
3446 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
3447 UseScratchRegisterScope temps(GetVIXLAssembler());
3448 Register temp = temps.AcquireSameSizeAs(out);
3449
3450 // temp = get_high(dividend * magic)
3451 __ Mov(temp, magic);
3452 __ Smull(temp.X(), dividend, temp);
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003453
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003454 // The multiplication result might need some corrections to be finalized.
3455 // The last correction is to increment by 1, if the result is negative.
3456 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3457 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3458 // However if one of the corrections is ADD or SUB, the sign can be detected
3459 // with ADDS/SUBS. They set the N flag if the result is negative.
3460 // This allows to use CINC MI which has latency 1.
3461 bool use_cond_inc = false;
3462
3463 // ADD/SUB correction is performed in the high 32 bits
3464 // as high 32 bits are ignored because type are kInt32.
3465 if (NeedToAddDividend(magic, imm)) {
3466 __ Adds(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3467 use_cond_inc = true;
3468 } else if (NeedToSubDividend(magic, imm)) {
3469 __ Subs(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3470 use_cond_inc = true;
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003471 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003472
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003473 // Extract the result from the high 32 bits and apply the final right shift.
3474 DCHECK_LT(shift, 32);
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003475 if (imm > 0 && HasNonNegativeInputAt(instruction, 0)) {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01003476 // No need to adjust the result for a non-negative dividend and a positive divisor.
3477 if (instruction->IsDiv()) {
3478 __ Lsr(out.X(), temp.X(), 32 + shift);
3479 } else {
3480 __ Lsr(temp.X(), temp.X(), 32 + shift);
3481 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3482 }
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003483 } else {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01003484 __ Asr(temp.X(), temp.X(), 32 + shift);
3485
3486 if (instruction->IsRem()) {
3487 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3488 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3489 } else {
3490 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3491 }
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003492 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003493}
3494
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003495void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction,
3496 int64_t divisor) {
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003497 DCHECK(instruction->IsDiv() || instruction->IsRem());
3498 if (instruction->GetResultType() == DataType::Type::kInt64) {
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003499 if (divisor > 0 && HasNonNegativeInputAt(instruction, 0)) {
3500 GenerateInt64UnsignedDivRemWithAnyPositiveConstant(instruction);
3501 } else {
3502 GenerateInt64DivRemWithAnyConstant(instruction);
3503 }
Zheng Xuc6667102015-05-15 16:08:45 +08003504 } else {
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003505 GenerateInt32DivRemWithAnyConstant(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003506 }
3507}
3508
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003509void InstructionCodeGeneratorARM64::GenerateIntDivForConstDenom(HDiv *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003510 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Zheng Xuc6667102015-05-15 16:08:45 +08003511
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003512 if (imm == 0) {
3513 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3514 return;
3515 }
Zheng Xuc6667102015-05-15 16:08:45 +08003516
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003517 if (IsPowerOfTwo(AbsOrMin(imm))) {
3518 GenerateIntDivForPower2Denom(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003519 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003520 // Cases imm == -1 or imm == 1 are handled by InstructionSimplifier.
3521 DCHECK(imm < -2 || imm > 2) << imm;
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003522 GenerateDivRemWithAnyConstant(instruction, imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003523 }
3524}
3525
3526void InstructionCodeGeneratorARM64::GenerateIntDiv(HDiv *instruction) {
3527 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
3528 << instruction->GetResultType();
3529
3530 if (instruction->GetLocations()->InAt(1).IsConstant()) {
3531 GenerateIntDivForConstDenom(instruction);
3532 } else {
3533 Register out = OutputRegister(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003534 Register dividend = InputRegisterAt(instruction, 0);
3535 Register divisor = InputRegisterAt(instruction, 1);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003536 __ Sdiv(out, dividend, divisor);
Zheng Xuc6667102015-05-15 16:08:45 +08003537 }
3538}
3539
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003540void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3541 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003542 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003543 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003544 case DataType::Type::kInt32:
3545 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003546 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003547 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003548 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3549 break;
3550
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003551 case DataType::Type::kFloat32:
3552 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003553 locations->SetInAt(0, Location::RequiresFpuRegister());
3554 locations->SetInAt(1, Location::RequiresFpuRegister());
3555 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3556 break;
3557
3558 default:
3559 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3560 }
3561}
3562
3563void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003564 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003565 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003566 case DataType::Type::kInt32:
3567 case DataType::Type::kInt64:
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003568 GenerateIntDiv(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003569 break;
3570
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003571 case DataType::Type::kFloat32:
3572 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003573 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3574 break;
3575
3576 default:
3577 LOG(FATAL) << "Unexpected div type " << type;
3578 }
3579}
3580
Alexandre Rames67555f72014-11-18 10:55:16 +00003581void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003582 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003583 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003584}
3585
3586void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3587 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003588 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003589 codegen_->AddSlowPath(slow_path);
3590 Location value = instruction->GetLocations()->InAt(0);
3591
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003592 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003593
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003594 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003595 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Elliott Hughesc1896c92018-11-29 11:33:18 -08003596 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003597 }
3598
Alexandre Rames67555f72014-11-18 10:55:16 +00003599 if (value.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003600 int64_t divisor = Int64FromLocation(value);
Alexandre Rames67555f72014-11-18 10:55:16 +00003601 if (divisor == 0) {
3602 __ B(slow_path->GetEntryLabel());
3603 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003604 // A division by a non-null constant is valid. We don't need to perform
3605 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003606 }
3607 } else {
3608 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3609 }
3610}
3611
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003612void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3613 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003614 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003615 locations->SetOut(Location::ConstantLocation(constant));
3616}
3617
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003618void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3619 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003620 // Will be generated at use site.
3621}
3622
Alexandre Rames5319def2014-10-23 10:03:10 +01003623void LocationsBuilderARM64::VisitExit(HExit* exit) {
3624 exit->SetLocations(nullptr);
3625}
3626
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003627void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003628}
3629
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003630void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3631 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003632 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003633 locations->SetOut(Location::ConstantLocation(constant));
3634}
3635
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003636void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003637 // Will be generated at use site.
3638}
3639
David Brazdilfc6a86a2015-06-26 10:33:45 +00003640void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003641 if (successor->IsExitBlock()) {
3642 DCHECK(got->GetPrevious()->AlwaysThrows());
3643 return; // no code needed
3644 }
3645
Serban Constantinescu02164b32014-11-13 14:05:07 +00003646 HBasicBlock* block = got->GetBlock();
3647 HInstruction* previous = got->GetPrevious();
3648 HLoopInformation* info = block->GetLoopInformation();
3649
David Brazdil46e2a392015-03-16 17:31:52 +00003650 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00003651 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003652 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
Vladimir Markoe45883e2022-01-11 12:38:35 +00003653 return; // `GenerateSuspendCheck()` emitted the jump.
Serban Constantinescu02164b32014-11-13 14:05:07 +00003654 }
3655 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3656 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08003657 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003658 }
3659 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003660 __ B(codegen_->GetLabelOf(successor));
3661 }
3662}
3663
David Brazdilfc6a86a2015-06-26 10:33:45 +00003664void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3665 got->SetLocations(nullptr);
3666}
3667
3668void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3669 HandleGoto(got, got->GetSuccessor());
3670}
3671
3672void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3673 try_boundary->SetLocations(nullptr);
3674}
3675
3676void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3677 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3678 if (!successor->IsExitBlock()) {
3679 HandleGoto(try_boundary, successor);
3680 }
3681}
3682
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003683void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003684 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003685 vixl::aarch64::Label* true_target,
3686 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003687 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003688
David Brazdil0debae72015-11-12 18:37:00 +00003689 if (true_target == nullptr && false_target == nullptr) {
3690 // Nothing to do. The code always falls through.
3691 return;
3692 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003693 // Constant condition, statically compared against "true" (integer value 1).
3694 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003695 if (true_target != nullptr) {
3696 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003697 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003698 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003699 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003700 if (false_target != nullptr) {
3701 __ B(false_target);
3702 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003703 }
David Brazdil0debae72015-11-12 18:37:00 +00003704 return;
3705 }
3706
3707 // The following code generates these patterns:
3708 // (1) true_target == nullptr && false_target != nullptr
3709 // - opposite condition true => branch to false_target
3710 // (2) true_target != nullptr && false_target == nullptr
3711 // - condition true => branch to true_target
3712 // (3) true_target != nullptr && false_target != nullptr
3713 // - condition true => branch to true_target
3714 // - branch to false_target
3715 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003716 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003717 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003718 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003719 if (true_target == nullptr) {
3720 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3721 } else {
3722 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3723 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003724 } else {
3725 // The condition instruction has not been materialized, use its inputs as
3726 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003727 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003728
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003729 DataType::Type type = condition->InputAt(0)->GetType();
3730 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003731 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003732 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003733 IfCondition opposite_condition = condition->GetOppositeCondition();
3734 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003735 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003736 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003737 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003738 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003739 // Integer cases.
3740 Register lhs = InputRegisterAt(condition, 0);
3741 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003742
3743 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003744 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003745 if (true_target == nullptr) {
3746 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3747 non_fallthrough_target = false_target;
3748 } else {
3749 arm64_cond = ARM64Condition(condition->GetCondition());
3750 non_fallthrough_target = true_target;
3751 }
3752
Aart Bik086d27e2016-01-20 17:02:00 -08003753 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003754 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003755 switch (arm64_cond) {
3756 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003757 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003758 break;
3759 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003760 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003761 break;
3762 case lt:
3763 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003764 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003765 break;
3766 case ge:
3767 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003768 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003769 break;
3770 default:
3771 // Without the `static_cast` the compiler throws an error for
3772 // `-Werror=sign-promo`.
3773 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3774 }
3775 } else {
3776 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003777 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003778 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003779 }
3780 }
David Brazdil0debae72015-11-12 18:37:00 +00003781
3782 // If neither branch falls through (case 3), the conditional branch to `true_target`
3783 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3784 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003785 __ B(false_target);
3786 }
3787}
3788
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003789void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003790 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003791 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003792 locations->SetInAt(0, Location::RequiresRegister());
3793 }
3794}
3795
3796void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003797 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3798 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003799 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3800 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3801 true_target = nullptr;
3802 }
3803 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3804 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3805 false_target = nullptr;
3806 }
Andreas Gampe3db70682018-12-26 15:12:03 -08003807 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003808}
3809
3810void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003811 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003812 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003813 InvokeRuntimeCallingConvention calling_convention;
3814 RegisterSet caller_saves = RegisterSet::Empty();
3815 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3816 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003817 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003818 locations->SetInAt(0, Location::RequiresRegister());
3819 }
3820}
3821
3822void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003823 SlowPathCodeARM64* slow_path =
3824 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003825 GenerateTestAndBranch(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08003826 /* condition_input_index= */ 0,
David Brazdil0debae72015-11-12 18:37:00 +00003827 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08003828 /* false_target= */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003829}
3830
Mingyao Yang063fc772016-08-02 11:02:54 -07003831void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003832 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003833 LocationSummary(flag, LocationSummary::kNoCall);
3834 locations->SetOut(Location::RequiresRegister());
3835}
3836
3837void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3838 __ Ldr(OutputRegister(flag),
3839 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3840}
3841
David Brazdilc0b601b2016-02-08 14:20:45 +00003842static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3843 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003844 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003845}
3846
Alexandre Rames880f1192016-06-13 16:04:50 +01003847static inline Condition GetConditionForSelect(HCondition* condition) {
3848 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003849 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3850 : ARM64Condition(cond);
3851}
3852
David Brazdil74eb1b22015-12-14 11:44:01 +00003853void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003854 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003855 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003856 locations->SetInAt(0, Location::RequiresFpuRegister());
3857 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003858 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003859 } else {
3860 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3861 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3862 bool is_true_value_constant = cst_true_value != nullptr;
3863 bool is_false_value_constant = cst_false_value != nullptr;
3864 // Ask VIXL whether we should synthesize constants in registers.
3865 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3866 Operand true_op = is_true_value_constant ?
3867 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3868 Operand false_op = is_false_value_constant ?
3869 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3870 bool true_value_in_register = false;
3871 bool false_value_in_register = false;
3872 MacroAssembler::GetCselSynthesisInformation(
3873 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3874 true_value_in_register |= !is_true_value_constant;
3875 false_value_in_register |= !is_false_value_constant;
3876
3877 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3878 : Location::ConstantLocation(cst_true_value));
3879 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3880 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003881 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003882 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003883
David Brazdil74eb1b22015-12-14 11:44:01 +00003884 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3885 locations->SetInAt(2, Location::RequiresRegister());
3886 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003887}
3888
3889void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003890 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003891 Condition csel_cond;
3892
3893 if (IsBooleanValueOrMaterializedCondition(cond)) {
3894 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003895 // Use the condition flags set by the previous instruction.
3896 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003897 } else {
3898 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003899 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003900 }
3901 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003902 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003903 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003904 } else {
3905 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003906 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003907 }
3908
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003909 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003910 __ Fcsel(OutputFPRegister(select),
3911 InputFPRegisterAt(select, 1),
3912 InputFPRegisterAt(select, 0),
3913 csel_cond);
3914 } else {
3915 __ Csel(OutputRegister(select),
3916 InputOperandAt(select, 1),
3917 InputOperandAt(select, 0),
3918 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003919 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003920}
3921
Santiago Aboy Solanescaf9b652022-06-24 10:03:30 +01003922void LocationsBuilderARM64::VisitNop(HNop* nop) {
3923 new (GetGraph()->GetAllocator()) LocationSummary(nop);
David Srbecky0cf44932015-12-09 14:09:59 +00003924}
3925
Santiago Aboy Solanescaf9b652022-06-24 10:03:30 +01003926void InstructionCodeGeneratorARM64::VisitNop(HNop*) {
3927 // The environment recording already happened in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003928}
3929
Vladimir Markodec78172020-06-19 15:31:23 +01003930void CodeGeneratorARM64::IncreaseFrame(size_t adjustment) {
3931 __ Claim(adjustment);
3932 GetAssembler()->cfi().AdjustCFAOffset(adjustment);
3933}
3934
3935void CodeGeneratorARM64::DecreaseFrame(size_t adjustment) {
3936 __ Drop(adjustment);
3937 GetAssembler()->cfi().AdjustCFAOffset(-adjustment);
3938}
3939
David Srbeckyc7098ff2016-02-09 14:30:11 +00003940void CodeGeneratorARM64::GenerateNop() {
3941 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003942}
3943
Alex Light3a73ffb2021-01-25 14:11:05 +00003944void LocationsBuilderARM64::VisitPredicatedInstanceFieldGet(
3945 HPredicatedInstanceFieldGet* instruction) {
3946 HandleFieldGet(instruction, instruction->GetFieldInfo());
3947}
3948
Alexandre Rames5319def2014-10-23 10:03:10 +01003949void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003950 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003951}
3952
Alex Light3a73ffb2021-01-25 14:11:05 +00003953void InstructionCodeGeneratorARM64::VisitPredicatedInstanceFieldGet(
3954 HPredicatedInstanceFieldGet* instruction) {
3955 vixl::aarch64::Label finish;
3956 __ Cbz(InputRegisterAt(instruction, 1), &finish);
3957 HandleFieldGet(instruction, instruction->GetFieldInfo());
3958 __ Bind(&finish);
3959}
3960
Alexandre Rames5319def2014-10-23 10:03:10 +01003961void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003962 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003963}
3964
3965void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003966 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003967}
3968
3969void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00003970 HandleFieldSet(instruction,
3971 instruction->GetFieldInfo(),
3972 instruction->GetValueCanBeNull(),
3973 instruction->GetWriteBarrierKind());
Alexandre Rames5319def2014-10-23 10:03:10 +01003974}
3975
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003976// Temp is used for read barrier.
3977static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00003978 if (gUseReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003979 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003980 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3981 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3982 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3983 return 1;
3984 }
3985 return 0;
3986}
3987
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003988// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003989// interface pointer, one for loading the current interface.
3990// The other checks have one temp for loading the object's class.
3991static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3992 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3993 return 3;
3994 }
3995 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003996}
3997
Alexandre Rames67555f72014-11-18 10:55:16 +00003998void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003999 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004000 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01004001 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004002 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004003 case TypeCheckKind::kExactCheck:
4004 case TypeCheckKind::kAbstractClassCheck:
4005 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00004006 case TypeCheckKind::kArrayObjectCheck: {
4007 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
4008 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
4009 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004010 break;
Vladimir Marko87584542017-12-12 17:47:52 +00004011 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004012 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004013 case TypeCheckKind::kUnresolvedCheck:
4014 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004015 call_kind = LocationSummary::kCallOnSlowPath;
4016 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00004017 case TypeCheckKind::kBitstringCheck:
4018 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004019 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004020
Vladimir Markoca6fff82017-10-03 14:49:14 +01004021 LocationSummary* locations =
4022 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01004023 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004024 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004025 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004026 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00004027 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
4028 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
4029 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
4030 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
4031 } else {
4032 locations->SetInAt(1, Location::RequiresRegister());
4033 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004034 // The "out" register is used as a temporary, so it overlaps with the inputs.
4035 // Note that TypeCheckSlowPathARM64 uses this register too.
4036 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004037 // Add temps if necessary for read barriers.
4038 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00004039}
4040
4041void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004042 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00004043 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004044 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004045 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00004046 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
4047 ? Register()
4048 : InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004049 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00004050 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004051 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
4052 DCHECK_LE(num_temps, 1u);
4053 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004054 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4055 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4056 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4057 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00004058
Scott Wakeling97c72b72016-06-24 16:19:36 +01004059 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004060 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004061
4062 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004063 // Avoid null check if we know `obj` is not null.
4064 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004065 __ Cbz(obj, &zero);
4066 }
4067
Roland Levillain44015862016-01-22 11:47:17 +00004068 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004069 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004070 ReadBarrierOption read_barrier_option =
4071 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004072 // /* HeapReference<Class> */ out = obj->klass_
4073 GenerateReferenceLoadTwoRegisters(instruction,
4074 out_loc,
4075 obj_loc,
4076 class_offset,
4077 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004078 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004079 __ Cmp(out, cls);
4080 __ Cset(out, eq);
4081 if (zero.IsLinked()) {
4082 __ B(&done);
4083 }
4084 break;
4085 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004086
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004087 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004088 ReadBarrierOption read_barrier_option =
4089 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004090 // /* HeapReference<Class> */ out = obj->klass_
4091 GenerateReferenceLoadTwoRegisters(instruction,
4092 out_loc,
4093 obj_loc,
4094 class_offset,
4095 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004096 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004097 // If the class is abstract, we eagerly fetch the super class of the
4098 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004099 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004100 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004101 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004102 GenerateReferenceLoadOneRegister(instruction,
4103 out_loc,
4104 super_offset,
4105 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004106 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004107 // If `out` is null, we use it for the result, and jump to `done`.
4108 __ Cbz(out, &done);
4109 __ Cmp(out, cls);
4110 __ B(ne, &loop);
4111 __ Mov(out, 1);
4112 if (zero.IsLinked()) {
4113 __ B(&done);
4114 }
4115 break;
4116 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004117
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004118 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004119 ReadBarrierOption read_barrier_option =
4120 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004121 // /* HeapReference<Class> */ out = obj->klass_
4122 GenerateReferenceLoadTwoRegisters(instruction,
4123 out_loc,
4124 obj_loc,
4125 class_offset,
4126 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004127 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004128 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004129 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004130 __ Bind(&loop);
4131 __ Cmp(out, cls);
4132 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004133 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004134 GenerateReferenceLoadOneRegister(instruction,
4135 out_loc,
4136 super_offset,
4137 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004138 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004139 __ Cbnz(out, &loop);
4140 // If `out` is null, we use it for the result, and jump to `done`.
4141 __ B(&done);
4142 __ Bind(&success);
4143 __ Mov(out, 1);
4144 if (zero.IsLinked()) {
4145 __ B(&done);
4146 }
4147 break;
4148 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004149
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004150 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004151 ReadBarrierOption read_barrier_option =
4152 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004153 // /* HeapReference<Class> */ out = obj->klass_
4154 GenerateReferenceLoadTwoRegisters(instruction,
4155 out_loc,
4156 obj_loc,
4157 class_offset,
4158 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004159 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004160 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004161 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004162 __ Cmp(out, cls);
4163 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004164 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004165 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004166 GenerateReferenceLoadOneRegister(instruction,
4167 out_loc,
4168 component_offset,
4169 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004170 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004171 // If `out` is null, we use it for the result, and jump to `done`.
4172 __ Cbz(out, &done);
4173 __ Ldrh(out, HeapOperand(out, primitive_offset));
4174 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4175 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004176 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004177 __ Mov(out, 1);
4178 __ B(&done);
4179 break;
4180 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004181
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004182 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004183 // No read barrier since the slow path will retry upon failure.
4184 // /* HeapReference<Class> */ out = obj->klass_
4185 GenerateReferenceLoadTwoRegisters(instruction,
4186 out_loc,
4187 obj_loc,
4188 class_offset,
4189 maybe_temp_loc,
4190 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004191 __ Cmp(out, cls);
4192 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004193 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08004194 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004195 codegen_->AddSlowPath(slow_path);
4196 __ B(ne, slow_path->GetEntryLabel());
4197 __ Mov(out, 1);
4198 if (zero.IsLinked()) {
4199 __ B(&done);
4200 }
4201 break;
4202 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004203
Calin Juravle98893e12015-10-02 21:05:03 +01004204 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004205 case TypeCheckKind::kInterfaceCheck: {
4206 // Note that we indeed only call on slow path, but we always go
4207 // into the slow path for the unresolved and interface check
4208 // cases.
4209 //
4210 // We cannot directly call the InstanceofNonTrivial runtime
4211 // entry point without resorting to a type checking slow path
4212 // here (i.e. by calling InvokeRuntime directly), as it would
4213 // require to assign fixed registers for the inputs of this
4214 // HInstanceOf instruction (following the runtime calling
4215 // convention), which might be cluttered by the potential first
4216 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004217 //
4218 // TODO: Introduce a new runtime entry point taking the object
4219 // to test (instead of its class) as argument, and let it deal
4220 // with the read barrier issues. This will let us refactor this
4221 // case of the `switch` code as it was previously (with a direct
4222 // call to the runtime not using a type checking slow path).
4223 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004224 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004225 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08004226 instruction, /* is_fatal= */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004227 codegen_->AddSlowPath(slow_path);
4228 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004229 if (zero.IsLinked()) {
4230 __ B(&done);
4231 }
4232 break;
4233 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004234
4235 case TypeCheckKind::kBitstringCheck: {
4236 // /* HeapReference<Class> */ temp = obj->klass_
4237 GenerateReferenceLoadTwoRegisters(instruction,
4238 out_loc,
4239 obj_loc,
4240 class_offset,
4241 maybe_temp_loc,
4242 kWithoutReadBarrier);
4243
4244 GenerateBitstringTypeCheckCompare(instruction, out);
4245 __ Cset(out, eq);
4246 if (zero.IsLinked()) {
4247 __ B(&done);
4248 }
4249 break;
4250 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004251 }
4252
4253 if (zero.IsLinked()) {
4254 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004255 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004256 }
4257
4258 if (done.IsLinked()) {
4259 __ Bind(&done);
4260 }
4261
4262 if (slow_path != nullptr) {
4263 __ Bind(slow_path->GetExitLabel());
4264 }
4265}
4266
4267void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004268 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00004269 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004270 LocationSummary* locations =
4271 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004272 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00004273 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
4274 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
4275 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
4276 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
4277 } else {
4278 locations->SetInAt(1, Location::RequiresRegister());
4279 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004280 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4281 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004282}
4283
4284void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004285 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004286 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004287 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004288 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00004289 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
4290 ? Register()
4291 : InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004292 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4293 DCHECK_GE(num_temps, 1u);
4294 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004295 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004296 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4297 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004298 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004299 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4300 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4301 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4302 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4303 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4304 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4305 const uint32_t object_array_data_offset =
4306 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004307
Vladimir Marko87584542017-12-12 17:47:52 +00004308 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004309 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004310 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4311 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004312 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004313
Scott Wakeling97c72b72016-06-24 16:19:36 +01004314 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004315 // Avoid null check if we know obj is not null.
4316 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004317 __ Cbz(obj, &done);
4318 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004319
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004320 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004321 case TypeCheckKind::kExactCheck:
4322 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004323 // /* HeapReference<Class> */ temp = obj->klass_
4324 GenerateReferenceLoadTwoRegisters(instruction,
4325 temp_loc,
4326 obj_loc,
4327 class_offset,
4328 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004329 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004330
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004331 __ Cmp(temp, cls);
4332 // Jump to slow path for throwing the exception or doing a
4333 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004334 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004335 break;
4336 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004337
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004338 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004339 // /* HeapReference<Class> */ temp = obj->klass_
4340 GenerateReferenceLoadTwoRegisters(instruction,
4341 temp_loc,
4342 obj_loc,
4343 class_offset,
4344 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004345 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004346
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004347 // If the class is abstract, we eagerly fetch the super class of the
4348 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004349 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004350 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004351 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004352 GenerateReferenceLoadOneRegister(instruction,
4353 temp_loc,
4354 super_offset,
4355 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004356 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004357
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004358 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4359 // exception.
4360 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4361 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004362 __ Cmp(temp, cls);
4363 __ B(ne, &loop);
4364 break;
4365 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004366
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004367 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004368 // /* HeapReference<Class> */ temp = obj->klass_
4369 GenerateReferenceLoadTwoRegisters(instruction,
4370 temp_loc,
4371 obj_loc,
4372 class_offset,
4373 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004374 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004375
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004376 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004377 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004378 __ Bind(&loop);
4379 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004380 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004381
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004382 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004383 GenerateReferenceLoadOneRegister(instruction,
4384 temp_loc,
4385 super_offset,
4386 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004387 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004388
4389 // If the class reference currently in `temp` is not null, jump
4390 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004391 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004392 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004393 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004394 break;
4395 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004396
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004397 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004398 // /* HeapReference<Class> */ temp = obj->klass_
4399 GenerateReferenceLoadTwoRegisters(instruction,
4400 temp_loc,
4401 obj_loc,
4402 class_offset,
4403 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004404 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004405
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004406 // Do an exact check.
4407 __ Cmp(temp, cls);
4408 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004409
4410 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004411 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004412 GenerateReferenceLoadOneRegister(instruction,
4413 temp_loc,
4414 component_offset,
4415 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004416 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004417
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004418 // If the component type is null, jump to the slow path to throw the exception.
4419 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4420 // Otherwise, the object is indeed an array. Further check that this component type is not a
4421 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004422 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4423 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004424 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004425 break;
4426 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004427
Calin Juravle98893e12015-10-02 21:05:03 +01004428 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004429 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004430 //
4431 // We cannot directly call the CheckCast runtime entry point
4432 // without resorting to a type checking slow path here (i.e. by
4433 // calling InvokeRuntime directly), as it would require to
4434 // assign fixed registers for the inputs of this HInstanceOf
4435 // instruction (following the runtime calling convention), which
4436 // might be cluttered by the potential first read barrier
4437 // emission at the beginning of this method.
4438 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004439 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004440 case TypeCheckKind::kInterfaceCheck: {
4441 // /* HeapReference<Class> */ temp = obj->klass_
4442 GenerateReferenceLoadTwoRegisters(instruction,
4443 temp_loc,
4444 obj_loc,
4445 class_offset,
4446 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004447 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004448
4449 // /* HeapReference<Class> */ temp = temp->iftable_
4450 GenerateReferenceLoadTwoRegisters(instruction,
4451 temp_loc,
4452 temp_loc,
4453 iftable_offset,
4454 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004455 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004456 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004457 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004458 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004459 vixl::aarch64::Label start_loop;
4460 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004461 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004462 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4463 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004464 // Go to next interface.
4465 __ Add(temp, temp, 2 * kHeapReferenceSize);
4466 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004467 // Compare the classes and continue the loop if they do not match.
4468 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4469 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004470 break;
4471 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004472
4473 case TypeCheckKind::kBitstringCheck: {
4474 // /* HeapReference<Class> */ temp = obj->klass_
4475 GenerateReferenceLoadTwoRegisters(instruction,
4476 temp_loc,
4477 obj_loc,
4478 class_offset,
4479 maybe_temp2_loc,
4480 kWithoutReadBarrier);
4481
4482 GenerateBitstringTypeCheckCompare(instruction, temp);
4483 __ B(ne, type_check_slow_path->GetEntryLabel());
4484 break;
4485 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004486 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004487 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004488
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004489 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004490}
4491
Alexandre Rames5319def2014-10-23 10:03:10 +01004492void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004493 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004494 locations->SetOut(Location::ConstantLocation(constant));
4495}
4496
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004497void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004498 // Will be generated at use site.
4499}
4500
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004501void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004502 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004503 locations->SetOut(Location::ConstantLocation(constant));
4504}
4505
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004506void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004507 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004508}
4509
Calin Juravle175dc732015-08-25 15:42:32 +01004510void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4511 // The trampoline uses the same calling convention as dex calling conventions,
4512 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4513 // the method_idx.
4514 HandleInvoke(invoke);
4515}
4516
4517void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4518 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004519 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004520}
4521
Alexandre Rames5319def2014-10-23 10:03:10 +01004522void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004523 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004524 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004525}
4526
Alexandre Rames67555f72014-11-18 10:55:16 +00004527void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4528 HandleInvoke(invoke);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004529 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
4530 // We cannot request ip1 as it's blocked by the register allocator.
4531 invoke->GetLocations()->SetInAt(invoke->GetNumberOfArguments() - 1, Location::Any());
4532 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004533}
4534
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004535void CodeGeneratorARM64::MaybeGenerateInlineCacheCheck(HInstruction* instruction,
4536 Register klass) {
4537 DCHECK_EQ(klass.GetCode(), 0u);
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004538 // We know the destination of an intrinsic, so no need to record inline
4539 // caches.
4540 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00004541 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004542 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004543 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
Nicolas Geoffray9e598902021-11-19 14:53:07 +00004544 ProfilingInfo* info = GetGraph()->GetProfilingInfo();
4545 DCHECK(info != nullptr);
4546 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
4547 uint64_t address = reinterpret_cast64<uint64_t>(cache);
4548 vixl::aarch64::Label done;
4549 __ Mov(x8, address);
4550 __ Ldr(x9, MemOperand(x8, InlineCache::ClassesOffset().Int32Value()));
4551 // Fast path for a monomorphic cache.
4552 __ Cmp(klass, x9);
4553 __ B(eq, &done);
4554 InvokeRuntime(kQuickUpdateInlineCache, instruction, instruction->GetDexPc());
4555 __ Bind(&done);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004556 }
4557}
4558
Alexandre Rames67555f72014-11-18 10:55:16 +00004559void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4560 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004561 LocationSummary* locations = invoke->GetLocations();
4562 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004563 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004564 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004565 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004566
Artem Serov914d7a82017-02-07 14:33:49 +00004567 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004568 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004569 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004570 {
4571 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4572 // /* HeapReference<Class> */ temp = temp->klass_
4573 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4574 codegen_->MaybeRecordImplicitNullCheck(invoke);
4575 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004576 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004577 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004578 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004579 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004580 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004581 }
Artem Serov914d7a82017-02-07 14:33:49 +00004582
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004583 // Instead of simply (possibly) unpoisoning `temp` here, we should
4584 // emit a read barrier for the previous class reference load.
4585 // However this is not required in practice, as this is an
4586 // intermediate/temporary reference and because the current
4587 // concurrent copying collector keeps the from-space memory
4588 // intact/accessible until the end of the marking phase (the
4589 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004590 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004591
4592 // If we're compiling baseline, update the inline cache.
4593 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
4594
4595 // The register ip1 is required to be used for the hidden argument in
4596 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
4597 MacroAssembler* masm = GetVIXLAssembler();
4598 UseScratchRegisterScope scratch_scope(masm);
4599 scratch_scope.Exclude(ip1);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004600 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
4601 Location interface_method = locations->InAt(invoke->GetNumberOfArguments() - 1);
4602 if (interface_method.IsStackSlot()) {
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00004603 __ Ldr(ip1, StackOperandFrom(interface_method));
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004604 } else {
4605 __ Mov(ip1, XRegisterFrom(interface_method));
4606 }
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00004607 // If the load kind is through a runtime call, we will pass the method we
4608 // fetch the IMT, which will either be a no-op if we don't hit the conflict
4609 // stub, or will make us always go through the trampoline when there is a
4610 // conflict.
4611 } else if (invoke->GetHiddenArgumentLoadKind() != MethodLoadKind::kRuntimeCall) {
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004612 codegen_->LoadMethod(
4613 invoke->GetHiddenArgumentLoadKind(), Location::RegisterLocation(ip1.GetCode()), invoke);
4614 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004615
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004616 __ Ldr(temp,
4617 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4618 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004619 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004620 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004621 __ Ldr(temp, MemOperand(temp, method_offset));
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00004622 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRuntimeCall) {
4623 // We pass the method from the IMT in case of a conflict. This will ensure
4624 // we go into the runtime to resolve the actual method.
4625 __ Mov(ip1, temp);
4626 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004627 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004628 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004629
4630 {
4631 // Ensure the pc position is recorded immediately after the `blr` instruction.
4632 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4633
4634 // lr();
4635 __ blr(lr);
4636 DCHECK(!codegen_->IsLeafMethod());
4637 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4638 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004639
Andreas Gampe3db70682018-12-26 15:12:03 -08004640 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004641}
4642
4643void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004644 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004645 if (intrinsic.TryDispatch(invoke)) {
4646 return;
4647 }
4648
Alexandre Rames67555f72014-11-18 10:55:16 +00004649 HandleInvoke(invoke);
4650}
4651
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004652void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004653 // Explicit clinit checks triggered by static invokes must have been pruned by
4654 // art::PrepareForRegisterAllocation.
4655 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004656
Vladimir Markoca6fff82017-10-03 14:49:14 +01004657 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004658 if (intrinsic.TryDispatch(invoke)) {
4659 return;
4660 }
4661
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004662 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
Vladimir Marko86c87522020-05-11 16:55:55 +01004663 CriticalNativeCallingConventionVisitorARM64 calling_convention_visitor(
4664 /*for_register_allocation=*/ true);
4665 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4666 } else {
4667 HandleInvoke(invoke);
4668 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004669}
4670
Andreas Gampe878d58c2015-01-15 23:24:00 -08004671static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4672 if (invoke->GetLocations()->Intrinsified()) {
4673 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4674 intrinsic.Dispatch(invoke);
4675 return true;
4676 }
4677 return false;
4678}
4679
Vladimir Markodc151b22015-10-15 18:02:30 +01004680HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4681 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004682 ArtMethod* method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004683 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004684 return desired_dispatch_info;
4685}
4686
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004687void CodeGeneratorARM64::LoadMethod(MethodLoadKind load_kind, Location temp, HInvoke* invoke) {
4688 switch (load_kind) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004689 case MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01004690 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01004691 // Add ADRP with its PC-relative method patch.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004692 vixl::aarch64::Label* adrp_label =
4693 NewBootImageMethodPatch(invoke->GetResolvedMethodReference());
Vladimir Marko65979462017-05-19 17:25:12 +01004694 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4695 // Add ADD with its PC-relative method patch.
4696 vixl::aarch64::Label* add_label =
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004697 NewBootImageMethodPatch(invoke->GetResolvedMethodReference(), adrp_label);
Vladimir Marko65979462017-05-19 17:25:12 +01004698 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4699 break;
4700 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004701 case MethodLoadKind::kBootImageRelRo: {
Vladimir Markob066d432018-01-03 13:14:37 +00004702 // 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 +01004703 uint32_t boot_image_offset = GetBootImageOffset(invoke);
4704 LoadBootImageRelRoEntry(WRegisterFrom(temp), boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00004705 break;
4706 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004707 case MethodLoadKind::kBssEntry: {
Vladimir Markob066d432018-01-03 13:14:37 +00004708 // Add ADRP with its PC-relative .bss entry patch.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004709 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(invoke->GetMethodReference());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004710 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Markob066d432018-01-03 13:14:37 +00004711 // Add LDR with its PC-relative .bss entry patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004712 vixl::aarch64::Label* ldr_label =
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004713 NewMethodBssEntryPatch(invoke->GetMethodReference(), adrp_label);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01004714 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004715 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004716 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004717 }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004718 case MethodLoadKind::kJitDirectAddress: {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004719 // Load method address from literal pool.
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004720 __ Ldr(XRegisterFrom(temp),
4721 DeduplicateUint64Literal(reinterpret_cast<uint64_t>(invoke->GetResolvedMethod())));
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004722 break;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004723 }
4724 case MethodLoadKind::kRuntimeCall: {
4725 // Test situation, don't do anything.
4726 break;
4727 }
4728 default: {
4729 LOG(FATAL) << "Load kind should have already been handled " << load_kind;
4730 UNREACHABLE();
4731 }
4732 }
4733}
4734
4735void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4736 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
4737 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
4738 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4739 switch (invoke->GetMethodLoadKind()) {
4740 case MethodLoadKind::kStringInit: {
4741 uint32_t offset =
4742 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4743 // temp = thread->string_init_entrypoint
4744 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
4745 break;
4746 }
4747 case MethodLoadKind::kRecursive: {
4748 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
4749 break;
4750 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004751 case MethodLoadKind::kRuntimeCall: {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004752 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4753 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004754 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004755 case MethodLoadKind::kBootImageLinkTimePcRelative:
4756 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
4757 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
4758 // Do not materialize the method pointer, load directly the entrypoint.
4759 // Add ADRP with its PC-relative JNI entrypoint patch.
4760 vixl::aarch64::Label* adrp_label =
4761 NewBootImageJniEntrypointPatch(invoke->GetResolvedMethodReference());
4762 EmitAdrpPlaceholder(adrp_label, lr);
4763 // Add the LDR with its PC-relative method patch.
4764 vixl::aarch64::Label* add_label =
4765 NewBootImageJniEntrypointPatch(invoke->GetResolvedMethodReference(), adrp_label);
4766 EmitLdrOffsetPlaceholder(add_label, lr, lr);
4767 break;
4768 }
4769 FALLTHROUGH_INTENDED;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004770 default: {
4771 LoadMethod(invoke->GetMethodLoadKind(), temp, invoke);
4772 break;
4773 }
Vladimir Marko58155012015-08-19 12:49:41 +00004774 }
4775
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004776 auto call_lr = [&]() {
4777 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4778 ExactAssemblyScope eas(GetVIXLAssembler(),
4779 kInstructionSize,
4780 CodeBufferCheckScope::kExactSize);
4781 // lr()
4782 __ blr(lr);
4783 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko86c87522020-05-11 16:55:55 +01004784 };
Vladimir Marko58155012015-08-19 12:49:41 +00004785 switch (invoke->GetCodePtrLocation()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004786 case CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004787 {
Nicolas Geoffray282795c2021-09-24 18:16:41 +01004788 DCHECK(!GetGraph()->HasShouldDeoptimizeFlag());
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004789 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4790 ExactAssemblyScope eas(GetVIXLAssembler(),
4791 kInstructionSize,
4792 CodeBufferCheckScope::kExactSize);
4793 __ bl(&frame_entry_label_);
4794 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4795 }
Vladimir Marko58155012015-08-19 12:49:41 +00004796 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004797 case CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01004798 size_t out_frame_size =
4799 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorARM64,
4800 kAapcs64StackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01004801 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004802 if (invoke->GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative) {
4803 call_lr();
4804 } else {
4805 // LR = callee_method->ptr_sized_fields_.data_; // EntryPointFromJni
4806 MemberOffset offset = ArtMethod::EntryPointFromJniOffset(kArm64PointerSize);
4807 __ Ldr(lr, MemOperand(XRegisterFrom(callee_method), offset.Int32Value()));
4808 // lr()
4809 call_lr();
4810 }
Vladimir Marko86c87522020-05-11 16:55:55 +01004811 // Zero-/sign-extend the result when needed due to native and managed ABI mismatch.
4812 switch (invoke->GetType()) {
4813 case DataType::Type::kBool:
4814 __ Ubfx(w0, w0, 0, 8);
4815 break;
4816 case DataType::Type::kInt8:
4817 __ Sbfx(w0, w0, 0, 8);
4818 break;
4819 case DataType::Type::kUint16:
4820 __ Ubfx(w0, w0, 0, 16);
4821 break;
4822 case DataType::Type::kInt16:
4823 __ Sbfx(w0, w0, 0, 16);
4824 break;
4825 case DataType::Type::kInt32:
4826 case DataType::Type::kInt64:
4827 case DataType::Type::kFloat32:
4828 case DataType::Type::kFloat64:
4829 case DataType::Type::kVoid:
4830 break;
4831 default:
4832 DCHECK(false) << invoke->GetType();
4833 break;
4834 }
4835 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01004836 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01004837 }
4838 break;
4839 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004840 case CodePtrLocation::kCallArtMethod: {
4841 // LR = callee_method->ptr_sized_fields_.entry_point_from_quick_compiled_code_;
4842 MemberOffset offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
4843 __ Ldr(lr, MemOperand(XRegisterFrom(callee_method), offset.Int32Value()));
4844 // lr()
4845 call_lr();
Vladimir Marko58155012015-08-19 12:49:41 +00004846 break;
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004847 }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004848 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004849
Andreas Gampe878d58c2015-01-15 23:24:00 -08004850 DCHECK(!IsLeafMethod());
4851}
4852
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004853void CodeGeneratorARM64::GenerateVirtualCall(
4854 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004855 // Use the calling convention instead of the location of the receiver, as
4856 // intrinsics may have put the receiver in a different register. In the intrinsics
4857 // slow path, the arguments have been moved to the right place, so here we are
4858 // guaranteed that the receiver is the first register of the calling convention.
4859 InvokeDexCallingConvention calling_convention;
4860 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004861 Register temp = XRegisterFrom(temp_in);
4862 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4863 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4864 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004865 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004866
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004867 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004868
4869 {
4870 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4871 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4872 // /* HeapReference<Class> */ temp = receiver->klass_
4873 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4874 MaybeRecordImplicitNullCheck(invoke);
4875 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004876 // Instead of simply (possibly) unpoisoning `temp` here, we should
4877 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004878 // intermediate/temporary reference and because the current
4879 // concurrent copying collector keeps the from-space memory
4880 // intact/accessible until the end of the marking phase (the
4881 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004882 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004883
4884 // If we're compiling baseline, update the inline cache.
4885 MaybeGenerateInlineCacheCheck(invoke, temp);
4886
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004887 // temp = temp->GetMethodAt(method_offset);
4888 __ Ldr(temp, MemOperand(temp, method_offset));
4889 // lr = temp->GetEntryPoint();
4890 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004891 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004892 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004893 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4894 // lr();
4895 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004896 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004897 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004898}
4899
Vladimir Marko9922f002020-06-08 15:05:15 +01004900void CodeGeneratorARM64::MoveFromReturnRegister(Location trg, DataType::Type type) {
4901 if (!trg.IsValid()) {
4902 DCHECK(type == DataType::Type::kVoid);
4903 return;
4904 }
4905
4906 DCHECK_NE(type, DataType::Type::kVoid);
4907
4908 if (DataType::IsIntegralType(type) || type == DataType::Type::kReference) {
4909 Register trg_reg = RegisterFrom(trg, type);
4910 Register res_reg = RegisterFrom(ARM64ReturnLocation(type), type);
4911 __ Mov(trg_reg, res_reg, kDiscardForSameWReg);
4912 } else {
4913 VRegister trg_reg = FPRegisterFrom(trg, type);
4914 VRegister res_reg = FPRegisterFrom(ARM64ReturnLocation(type), type);
4915 __ Fmov(trg_reg, res_reg);
4916 }
4917}
4918
Orion Hodsonac141392017-01-13 11:53:47 +00004919void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00004920 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
4921 if (intrinsic.TryDispatch(invoke)) {
4922 return;
4923 }
Orion Hodsonac141392017-01-13 11:53:47 +00004924 HandleInvoke(invoke);
4925}
4926
4927void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00004928 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4929 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
4930 return;
4931 }
Orion Hodsonac141392017-01-13 11:53:47 +00004932 codegen_->GenerateInvokePolymorphicCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004933 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004934}
4935
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004936void LocationsBuilderARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4937 HandleInvoke(invoke);
4938}
4939
4940void InstructionCodeGeneratorARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4941 codegen_->GenerateInvokeCustomCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004942 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004943}
4944
Vladimir Marko6fd16062018-06-26 11:02:04 +01004945vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageIntrinsicPatch(
4946 uint32_t intrinsic_data,
4947 vixl::aarch64::Label* adrp_label) {
4948 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004949 /* dex_file= */ nullptr, intrinsic_data, adrp_label, &boot_image_other_patches_);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004950}
4951
Vladimir Markob066d432018-01-03 13:14:37 +00004952vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageRelRoPatch(
4953 uint32_t boot_image_offset,
4954 vixl::aarch64::Label* adrp_label) {
4955 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004956 /* dex_file= */ nullptr, boot_image_offset, adrp_label, &boot_image_other_patches_);
Vladimir Markob066d432018-01-03 13:14:37 +00004957}
4958
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004959vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004960 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004961 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004962 return NewPcRelativePatch(
4963 target_method.dex_file, target_method.index, adrp_label, &boot_image_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004964}
4965
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004966vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4967 MethodReference target_method,
4968 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004969 return NewPcRelativePatch(
4970 target_method.dex_file, target_method.index, adrp_label, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004971}
4972
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004973vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageTypePatch(
Scott Wakeling97c72b72016-06-24 16:19:36 +01004974 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004975 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004976 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004977 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &boot_image_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004978}
4979
Vladimir Marko1998cd02017-01-13 13:02:58 +00004980vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
Vladimir Marko8f63f102020-09-28 12:10:28 +01004981 HLoadClass* load_class,
Vladimir Marko1998cd02017-01-13 13:02:58 +00004982 vixl::aarch64::Label* adrp_label) {
Vladimir Marko8f63f102020-09-28 12:10:28 +01004983 const DexFile& dex_file = load_class->GetDexFile();
4984 dex::TypeIndex type_index = load_class->GetTypeIndex();
4985 ArenaDeque<PcRelativePatchInfo>* patches = nullptr;
4986 switch (load_class->GetLoadKind()) {
4987 case HLoadClass::LoadKind::kBssEntry:
4988 patches = &type_bss_entry_patches_;
4989 break;
4990 case HLoadClass::LoadKind::kBssEntryPublic:
4991 patches = &public_type_bss_entry_patches_;
4992 break;
4993 case HLoadClass::LoadKind::kBssEntryPackage:
4994 patches = &package_type_bss_entry_patches_;
4995 break;
4996 default:
4997 LOG(FATAL) << "Unexpected load kind: " << load_class->GetLoadKind();
4998 UNREACHABLE();
4999 }
5000 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005001}
5002
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005003vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01005004 const DexFile& dex_file,
5005 dex::StringIndex string_index,
5006 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005007 return NewPcRelativePatch(
5008 &dex_file, string_index.index_, adrp_label, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01005009}
5010
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005011vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
5012 const DexFile& dex_file,
5013 dex::StringIndex string_index,
5014 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005015 return NewPcRelativePatch(&dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005016}
5017
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005018vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageJniEntrypointPatch(
5019 MethodReference target_method,
5020 vixl::aarch64::Label* adrp_label) {
5021 return NewPcRelativePatch(
5022 target_method.dex_file, target_method.index, adrp_label, &boot_image_jni_entrypoint_patches_);
5023}
5024
Vladimir Markof6675082019-05-17 12:05:28 +01005025void CodeGeneratorARM64::EmitEntrypointThunkCall(ThreadOffset64 entrypoint_offset) {
5026 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01005027 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markof6675082019-05-17 12:05:28 +01005028 call_entrypoint_patches_.emplace_back(/*dex_file*/ nullptr, entrypoint_offset.Uint32Value());
5029 vixl::aarch64::Label* bl_label = &call_entrypoint_patches_.back().label;
5030 __ bind(bl_label);
5031 __ bl(static_cast<int64_t>(0)); // Placeholder, patched at link-time.
5032}
5033
Vladimir Marko966b46f2018-08-03 10:20:19 +00005034void CodeGeneratorARM64::EmitBakerReadBarrierCbnz(uint32_t custom_data) {
Vladimir Marko94796f82018-08-08 15:15:33 +01005035 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01005036 if (GetCompilerOptions().IsJitCompiler()) {
Vladimir Marko966b46f2018-08-03 10:20:19 +00005037 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
5038 vixl::aarch64::Label* slow_path_entry = &it->second.label;
5039 __ cbnz(mr, slow_path_entry);
5040 } else {
5041 baker_read_barrier_patches_.emplace_back(custom_data);
5042 vixl::aarch64::Label* cbnz_label = &baker_read_barrier_patches_.back().label;
5043 __ bind(cbnz_label);
5044 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
5045 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005046}
5047
Scott Wakeling97c72b72016-06-24 16:19:36 +01005048vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005049 const DexFile* dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005050 uint32_t offset_or_index,
5051 vixl::aarch64::Label* adrp_label,
5052 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005053 // Add a patch entry and return the label.
5054 patches->emplace_back(dex_file, offset_or_index);
5055 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01005056 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005057 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
5058 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
5059 return label;
5060}
5061
Scott Wakeling97c72b72016-06-24 16:19:36 +01005062vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
5063 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005064 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005065}
5066
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005067vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005068 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005069 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005070 return jit_string_patches_.GetOrCreate(
5071 StringReference(&dex_file, string_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08005072 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005073}
5074
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005075vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005076 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005077 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005078 return jit_class_patches_.GetOrCreate(
5079 TypeReference(&dex_file, type_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08005080 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005081}
5082
Vladimir Markoaad75c62016-10-03 08:46:48 +00005083void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
5084 vixl::aarch64::Register reg) {
5085 DCHECK(reg.IsX());
5086 SingleEmissionCheckScope guard(GetVIXLAssembler());
5087 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005088 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005089}
5090
5091void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
5092 vixl::aarch64::Register out,
5093 vixl::aarch64::Register base) {
5094 DCHECK(out.IsX());
5095 DCHECK(base.IsX());
5096 SingleEmissionCheckScope guard(GetVIXLAssembler());
5097 __ Bind(fixup_label);
5098 __ add(out, base, Operand(/* offset placeholder */ 0));
5099}
5100
5101void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
5102 vixl::aarch64::Register out,
5103 vixl::aarch64::Register base) {
5104 DCHECK(base.IsX());
5105 SingleEmissionCheckScope guard(GetVIXLAssembler());
5106 __ Bind(fixup_label);
5107 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
5108}
5109
Vladimir Marko9d31daa2022-04-14 10:48:44 +01005110void CodeGeneratorARM64::LoadBootImageRelRoEntry(vixl::aarch64::Register reg,
5111 uint32_t boot_image_offset) {
5112 DCHECK(reg.IsW());
5113 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5114 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_offset);
5115 EmitAdrpPlaceholder(adrp_label, reg.X());
5116 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
5117 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_offset, adrp_label);
5118 EmitLdrOffsetPlaceholder(ldr_label, reg.W(), reg.X());
5119}
5120
Vladimir Markoeebb8212018-06-05 14:57:24 +01005121void CodeGeneratorARM64::LoadBootImageAddress(vixl::aarch64::Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01005122 uint32_t boot_image_reference) {
5123 if (GetCompilerOptions().IsBootImage()) {
5124 // Add ADRP with its PC-relative type patch.
5125 vixl::aarch64::Label* adrp_label = NewBootImageIntrinsicPatch(boot_image_reference);
5126 EmitAdrpPlaceholder(adrp_label, reg.X());
5127 // Add ADD with its PC-relative type patch.
5128 vixl::aarch64::Label* add_label = NewBootImageIntrinsicPatch(boot_image_reference, adrp_label);
5129 EmitAddPlaceholder(add_label, reg.X(), reg.X());
Vladimir Markoa2da9b92018-10-10 14:21:55 +01005130 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Marko9d31daa2022-04-14 10:48:44 +01005131 LoadBootImageRelRoEntry(reg, boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01005132 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01005133 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01005134 gc::Heap* heap = Runtime::Current()->GetHeap();
5135 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005136 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01005137 __ Ldr(reg.W(), DeduplicateBootImageAddressLiteral(reinterpret_cast<uintptr_t>(address)));
5138 }
5139}
5140
Vladimir Marko98873af2020-12-16 12:10:03 +00005141void CodeGeneratorARM64::LoadTypeForBootImageIntrinsic(vixl::aarch64::Register reg,
5142 TypeReference target_type) {
5143 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
Vladimir Marko9d31daa2022-04-14 10:48:44 +01005144 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko98873af2020-12-16 12:10:03 +00005145 // Add ADRP with its PC-relative type patch.
5146 vixl::aarch64::Label* adrp_label =
5147 NewBootImageTypePatch(*target_type.dex_file, target_type.TypeIndex());
5148 EmitAdrpPlaceholder(adrp_label, reg.X());
5149 // Add ADD with its PC-relative type patch.
5150 vixl::aarch64::Label* add_label =
5151 NewBootImageTypePatch(*target_type.dex_file, target_type.TypeIndex(), adrp_label);
5152 EmitAddPlaceholder(add_label, reg.X(), reg.X());
5153}
5154
Vladimir Markode91ca92020-10-27 13:41:40 +00005155void CodeGeneratorARM64::LoadIntrinsicDeclaringClass(vixl::aarch64::Register reg, HInvoke* invoke) {
5156 DCHECK_NE(invoke->GetIntrinsic(), Intrinsics::kNone);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005157 if (GetCompilerOptions().IsBootImage()) {
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005158 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01005159 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
Vladimir Marko98873af2020-12-16 12:10:03 +00005160 LoadTypeForBootImageIntrinsic(reg, TypeReference(target_method.dex_file, type_idx));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005161 } else {
Vladimir Markode91ca92020-10-27 13:41:40 +00005162 uint32_t boot_image_offset = GetBootImageOffsetOfIntrinsicDeclaringClass(invoke);
5163 LoadBootImageAddress(reg, boot_image_offset);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005164 }
Vladimir Marko6fd16062018-06-26 11:02:04 +01005165}
5166
Vladimir Marko98873af2020-12-16 12:10:03 +00005167void CodeGeneratorARM64::LoadClassRootForIntrinsic(vixl::aarch64::Register reg,
5168 ClassRoot class_root) {
5169 if (GetCompilerOptions().IsBootImage()) {
5170 ScopedObjectAccess soa(Thread::Current());
5171 ObjPtr<mirror::Class> klass = GetClassRoot(class_root);
5172 TypeReference target_type(&klass->GetDexFile(), klass->GetDexTypeIndex());
5173 LoadTypeForBootImageIntrinsic(reg, target_type);
5174 } else {
5175 uint32_t boot_image_offset = GetBootImageOffset(class_root);
5176 LoadBootImageAddress(reg, boot_image_offset);
5177 }
5178}
5179
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005180template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005181inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
5182 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005183 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005184 for (const PcRelativePatchInfo& info : infos) {
5185 linker_patches->push_back(Factory(info.label.GetLocation(),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005186 info.target_dex_file,
Vladimir Markoaad75c62016-10-03 08:46:48 +00005187 info.pc_insn_label->GetLocation(),
5188 info.offset_or_index));
5189 }
5190}
5191
Vladimir Marko6fd16062018-06-26 11:02:04 +01005192template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5193linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5194 const DexFile* target_dex_file,
5195 uint32_t pc_insn_offset,
5196 uint32_t boot_image_offset) {
5197 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5198 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005199}
5200
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005201void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005202 DCHECK(linker_patches->empty());
5203 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005204 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005205 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005206 boot_image_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005207 type_bss_entry_patches_.size() +
Vladimir Marko8f63f102020-09-28 12:10:28 +01005208 public_type_bss_entry_patches_.size() +
5209 package_type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005210 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005211 string_bss_entry_patches_.size() +
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005212 boot_image_jni_entrypoint_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005213 boot_image_other_patches_.size() +
Vladimir Markof6675082019-05-17 12:05:28 +01005214 call_entrypoint_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005215 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00005216 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005217 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005218 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005219 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005220 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005221 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005222 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005223 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01005224 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005225 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005226 DCHECK(boot_image_type_patches_.empty());
5227 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005228 }
5229 if (GetCompilerOptions().IsBootImage()) {
5230 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5231 boot_image_other_patches_, linker_patches);
5232 } else {
5233 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5234 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005235 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005236 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5237 method_bss_entry_patches_, linker_patches);
5238 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5239 type_bss_entry_patches_, linker_patches);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005240 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PublicTypeBssEntryPatch>(
5241 public_type_bss_entry_patches_, linker_patches);
5242 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PackageTypeBssEntryPatch>(
5243 package_type_bss_entry_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005244 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5245 string_bss_entry_patches_, linker_patches);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005246 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeJniEntrypointPatch>(
5247 boot_image_jni_entrypoint_patches_, linker_patches);
Vladimir Markof6675082019-05-17 12:05:28 +01005248 for (const PatchInfo<vixl::aarch64::Label>& info : call_entrypoint_patches_) {
5249 DCHECK(info.target_dex_file == nullptr);
5250 linker_patches->push_back(linker::LinkerPatch::CallEntrypointPatch(
5251 info.label.GetLocation(), info.offset_or_index));
5252 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005253 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005254 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
5255 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005256 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00005257 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005258}
5259
Vladimir Markoca1e0382018-04-11 09:58:41 +00005260bool CodeGeneratorARM64::NeedsThunkCode(const linker::LinkerPatch& patch) const {
Vladimir Markof6675082019-05-17 12:05:28 +01005261 return patch.GetType() == linker::LinkerPatch::Type::kCallEntrypoint ||
5262 patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
Vladimir Markoca1e0382018-04-11 09:58:41 +00005263 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
5264}
5265
5266void CodeGeneratorARM64::EmitThunkCode(const linker::LinkerPatch& patch,
5267 /*out*/ ArenaVector<uint8_t>* code,
5268 /*out*/ std::string* debug_name) {
5269 Arm64Assembler assembler(GetGraph()->GetAllocator());
5270 switch (patch.GetType()) {
5271 case linker::LinkerPatch::Type::kCallRelative: {
5272 // The thunk just uses the entry point in the ArtMethod. This works even for calls
5273 // to the generic JNI and interpreter trampolines.
5274 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
5275 kArm64PointerSize).Int32Value());
5276 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
David Srbecky889da942021-04-30 13:03:14 +01005277 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00005278 *debug_name = "MethodCallThunk";
5279 }
5280 break;
5281 }
Vladimir Markof6675082019-05-17 12:05:28 +01005282 case linker::LinkerPatch::Type::kCallEntrypoint: {
5283 Offset offset(patch.EntrypointOffset());
5284 assembler.JumpTo(ManagedRegister(arm64::TR), offset, ManagedRegister(arm64::IP0));
David Srbecky889da942021-04-30 13:03:14 +01005285 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markof6675082019-05-17 12:05:28 +01005286 *debug_name = "EntrypointCallThunk_" + std::to_string(offset.Uint32Value());
5287 }
5288 break;
5289 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00005290 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
5291 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
5292 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
5293 break;
5294 }
5295 default:
5296 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
5297 UNREACHABLE();
5298 }
5299
5300 // Ensure we emit the literal pool if any.
5301 assembler.FinalizeCode();
5302 code->resize(assembler.CodeSize());
5303 MemoryRegion code_region(code->data(), code->size());
5304 assembler.FinalizeInstructions(code_region);
5305}
5306
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005307vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
5308 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005309 value,
5310 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
5311}
5312
Scott Wakeling97c72b72016-06-24 16:19:36 +01005313vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005314 return uint64_literals_.GetOrCreate(
5315 value,
5316 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00005317}
5318
Andreas Gampe878d58c2015-01-15 23:24:00 -08005319void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005320 // Explicit clinit checks triggered by static invokes must have been pruned by
5321 // art::PrepareForRegisterAllocation.
5322 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01005323
Andreas Gampe878d58c2015-01-15 23:24:00 -08005324 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08005325 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08005326 return;
5327 }
5328
Vladimir Marko86c87522020-05-11 16:55:55 +01005329 LocationSummary* locations = invoke->GetLocations();
5330 codegen_->GenerateStaticOrDirectCall(
5331 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005332
Andreas Gampe3db70682018-12-26 15:12:03 -08005333 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005334}
5335
5336void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08005337 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08005338 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08005339 return;
5340 }
5341
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005342 {
5343 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
5344 // are no pools emitted.
5345 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
5346 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
5347 DCHECK(!codegen_->IsLeafMethod());
5348 }
5349
Andreas Gampe3db70682018-12-26 15:12:03 -08005350 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005351}
5352
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005353HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
5354 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005355 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005356 case HLoadClass::LoadKind::kInvalid:
5357 LOG(FATAL) << "UNREACHABLE";
5358 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005359 case HLoadClass::LoadKind::kReferrersClass:
5360 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005361 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005362 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005363 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8f63f102020-09-28 12:10:28 +01005364 case HLoadClass::LoadKind::kBssEntryPublic:
5365 case HLoadClass::LoadKind::kBssEntryPackage:
Vladimir Marko695348f2020-05-19 14:42:02 +01005366 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005367 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005368 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005369 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01005370 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005371 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005372 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005373 break;
5374 }
5375 return desired_class_load_kind;
5376}
5377
Alexandre Rames67555f72014-11-18 10:55:16 +00005378void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005379 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005380 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005381 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005382 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005383 cls,
5384 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005385 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00005386 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005387 return;
5388 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005389 DCHECK_EQ(cls->NeedsAccessCheck(),
5390 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
5391 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005392
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00005393 const bool requires_read_barrier = gUseReadBarrier && !cls->IsInBootImage();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005394 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005395 ? LocationSummary::kCallOnSlowPath
5396 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005397 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005398 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005399 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005400 }
5401
Vladimir Marko41559982017-01-06 14:04:23 +00005402 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005403 locations->SetInAt(0, Location::RequiresRegister());
5404 }
5405 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005406 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00005407 if (!gUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005408 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005409 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005410 } else {
5411 // For non-Baker read barrier we have a temp-clobbering call.
5412 }
5413 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005414}
5415
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005416// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5417// move.
5418void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005419 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005420 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005421 codegen_->GenerateLoadClassRuntimeCall(cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08005422 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01005423 return;
5424 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005425 DCHECK_EQ(cls->NeedsAccessCheck(),
5426 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
5427 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Calin Juravle580b6092015-10-06 17:35:58 +01005428
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005429 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01005430 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00005431
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005432 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5433 ? kWithoutReadBarrier
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00005434 : gCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005435 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005436 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005437 case HLoadClass::LoadKind::kReferrersClass: {
5438 DCHECK(!cls->CanCallRuntime());
5439 DCHECK(!cls->MustGenerateClinitCheck());
5440 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5441 Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00005442 codegen_->GenerateGcRootFieldLoad(cls,
5443 out_loc,
5444 current_method,
5445 ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005446 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005447 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005448 break;
5449 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005450 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005451 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5452 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005453 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005454 // Add ADRP with its PC-relative type patch.
5455 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08005456 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005457 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005458 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005459 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005460 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005461 codegen_->NewBootImageTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005462 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005463 break;
5464 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005465 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005466 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markode91ca92020-10-27 13:41:40 +00005467 uint32_t boot_image_offset = CodeGenerator::GetBootImageOffset(cls);
Vladimir Marko9d31daa2022-04-14 10:48:44 +01005468 codegen_->LoadBootImageRelRoEntry(out.W(), boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005469 break;
5470 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005471 case HLoadClass::LoadKind::kBssEntry:
5472 case HLoadClass::LoadKind::kBssEntryPublic:
5473 case HLoadClass::LoadKind::kBssEntryPackage: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005474 // Add ADRP with its PC-relative Class .bss entry patch.
Vladimir Markof3c52b42017-11-17 17:32:12 +00005475 vixl::aarch64::Register temp = XRegisterFrom(out_loc);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005476 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(cls);
Vladimir Markof3c52b42017-11-17 17:32:12 +00005477 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005478 // Add LDR with its PC-relative Class .bss entry patch.
Vladimir Marko8f63f102020-09-28 12:10:28 +01005479 vixl::aarch64::Label* ldr_label = codegen_->NewBssEntryTypePatch(cls, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005480 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005481 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005482 codegen_->GenerateGcRootFieldLoad(cls,
5483 out_loc,
5484 temp,
5485 /* offset placeholder */ 0u,
5486 ldr_label,
5487 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005488 generate_null_check = true;
5489 break;
5490 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005491 case HLoadClass::LoadKind::kJitBootImageAddress: {
5492 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5493 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
5494 DCHECK_NE(address, 0u);
5495 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5496 break;
5497 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005498 case HLoadClass::LoadKind::kJitTableAddress: {
5499 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5500 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005501 cls->GetClass()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005502 codegen_->GenerateGcRootFieldLoad(cls,
5503 out_loc,
5504 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005505 /* offset= */ 0,
5506 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005507 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005508 break;
5509 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005510 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005511 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005512 LOG(FATAL) << "UNREACHABLE";
5513 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005514 }
5515
Vladimir Markoea4c1262017-02-06 19:59:33 +00005516 bool do_clinit = cls->MustGenerateClinitCheck();
5517 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005518 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01005519 SlowPathCodeARM64* slow_path =
5520 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005521 codegen_->AddSlowPath(slow_path);
5522 if (generate_null_check) {
5523 __ Cbz(out, slow_path->GetEntryLabel());
5524 }
5525 if (cls->MustGenerateClinitCheck()) {
5526 GenerateClassInitializationCheck(slow_path, out);
5527 } else {
5528 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005529 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005530 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005531 }
5532}
5533
Orion Hodsondbaa5c72018-05-10 08:22:46 +01005534void LocationsBuilderARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5535 InvokeRuntimeCallingConvention calling_convention;
5536 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5537 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
5538}
5539
5540void InstructionCodeGeneratorARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5541 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
5542}
5543
Orion Hodson18259d72018-04-12 11:18:23 +01005544void LocationsBuilderARM64::VisitLoadMethodType(HLoadMethodType* load) {
5545 InvokeRuntimeCallingConvention calling_convention;
5546 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5547 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
5548}
5549
5550void InstructionCodeGeneratorARM64::VisitLoadMethodType(HLoadMethodType* load) {
5551 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
5552}
5553
David Brazdilcb1c0552015-08-04 16:22:25 +01005554static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005555 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005556}
5557
Alexandre Rames67555f72014-11-18 10:55:16 +00005558void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5559 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005560 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00005561 locations->SetOut(Location::RequiresRegister());
5562}
5563
5564void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005565 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5566}
5567
5568void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005569 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005570}
5571
5572void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5573 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005574}
5575
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005576HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5577 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005578 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005579 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005580 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005581 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01005582 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005583 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005584 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005585 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01005586 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005587 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005588 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005589 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005590 }
5591 return desired_string_load_kind;
5592}
5593
Alexandre Rames67555f72014-11-18 10:55:16 +00005594void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005595 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005596 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005597 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005598 InvokeRuntimeCallingConvention calling_convention;
5599 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5600 } else {
5601 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005602 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00005603 if (!gUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005604 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005605 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005606 } else {
5607 // For non-Baker read barrier we have a temp-clobbering call.
5608 }
5609 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005610 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005611}
5612
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005613// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5614// move.
5615void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005616 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005617 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005618
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005619 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005620 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005621 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5622 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005623 // Add ADRP with its PC-relative String patch.
5624 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005625 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005626 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005627 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005628 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005629 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005630 codegen_->NewBootImageStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005631 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005632 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005633 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005634 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005635 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markode91ca92020-10-27 13:41:40 +00005636 uint32_t boot_image_offset = CodeGenerator::GetBootImageOffset(load);
Vladimir Marko9d31daa2022-04-14 10:48:44 +01005637 codegen_->LoadBootImageRelRoEntry(out.W(), boot_image_offset);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005638 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005639 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005640 case HLoadString::LoadKind::kBssEntry: {
5641 // Add ADRP with its PC-relative String .bss entry patch.
5642 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005643 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00005644 Register temp = XRegisterFrom(out_loc);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005645 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005646 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005647 // Add LDR with its PC-relative String .bss entry patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005648 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005649 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005650 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005651 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005652 codegen_->GenerateGcRootFieldLoad(load,
5653 out_loc,
5654 temp,
5655 /* offset placeholder */ 0u,
5656 ldr_label,
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00005657 gCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005658 SlowPathCodeARM64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005659 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005660 codegen_->AddSlowPath(slow_path);
5661 __ Cbz(out.X(), slow_path->GetEntryLabel());
5662 __ Bind(slow_path->GetExitLabel());
Andreas Gampe3db70682018-12-26 15:12:03 -08005663 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005664 return;
5665 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005666 case HLoadString::LoadKind::kJitBootImageAddress: {
5667 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
5668 DCHECK_NE(address, 0u);
5669 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5670 return;
5671 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005672 case HLoadString::LoadKind::kJitTableAddress: {
5673 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005674 load->GetStringIndex(),
5675 load->GetString()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005676 codegen_->GenerateGcRootFieldLoad(load,
5677 out_loc,
5678 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005679 /* offset= */ 0,
5680 /* fixup_label= */ nullptr,
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00005681 gCompilerReadBarrierOption);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005682 return;
5683 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005684 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005685 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005686 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005687
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005688 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005689 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005690 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005691 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005692 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5693 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005694 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005695}
5696
Alexandre Rames5319def2014-10-23 10:03:10 +01005697void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005698 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01005699 locations->SetOut(Location::ConstantLocation(constant));
5700}
5701
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005702void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005703 // Will be generated at use site.
5704}
5705
Alexandre Rames67555f72014-11-18 10:55:16 +00005706void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005707 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5708 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005709 InvokeRuntimeCallingConvention calling_convention;
5710 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5711}
5712
5713void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005714 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005715 instruction,
5716 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005717 if (instruction->IsEnter()) {
5718 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5719 } else {
5720 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5721 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005722 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005723}
5724
Alexandre Rames42d641b2014-10-27 14:00:51 +00005725void LocationsBuilderARM64::VisitMul(HMul* mul) {
5726 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005727 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005728 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005729 case DataType::Type::kInt32:
5730 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005731 locations->SetInAt(0, Location::RequiresRegister());
5732 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005733 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005734 break;
5735
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005736 case DataType::Type::kFloat32:
5737 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005738 locations->SetInAt(0, Location::RequiresFpuRegister());
5739 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005740 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005741 break;
5742
5743 default:
5744 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5745 }
5746}
5747
5748void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5749 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005750 case DataType::Type::kInt32:
5751 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005752 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5753 break;
5754
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005755 case DataType::Type::kFloat32:
5756 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005757 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005758 break;
5759
5760 default:
5761 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5762 }
5763}
5764
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005765void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5766 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005767 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005768 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005769 case DataType::Type::kInt32:
5770 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005771 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005772 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005773 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005774
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005775 case DataType::Type::kFloat32:
5776 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005777 locations->SetInAt(0, Location::RequiresFpuRegister());
5778 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005779 break;
5780
5781 default:
5782 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5783 }
5784}
5785
5786void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5787 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005788 case DataType::Type::kInt32:
5789 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005790 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5791 break;
5792
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005793 case DataType::Type::kFloat32:
5794 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005795 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005796 break;
5797
5798 default:
5799 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5800 }
5801}
5802
5803void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005804 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5805 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005806 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005807 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005808 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5809 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005810}
5811
5812void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005813 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5814 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005815 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005816 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005817 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005818}
5819
Alexandre Rames5319def2014-10-23 10:03:10 +01005820void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005821 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5822 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005823 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07005824 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005825 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005826}
5827
5828void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005829 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5830 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005831 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005832}
5833
5834void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005835 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005836 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005837 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005838}
5839
5840void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005841 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005842 case DataType::Type::kInt32:
5843 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005844 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005845 break;
5846
5847 default:
5848 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5849 }
5850}
5851
David Brazdil66d126e2015-04-03 16:02:44 +01005852void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005853 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005854 locations->SetInAt(0, Location::RequiresRegister());
5855 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5856}
5857
5858void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005859 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005860}
5861
Alexandre Rames5319def2014-10-23 10:03:10 +01005862void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005863 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5864 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005865}
5866
Calin Juravle2ae48182016-03-16 14:05:09 +00005867void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5868 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005869 return;
5870 }
Artem Serov914d7a82017-02-07 14:33:49 +00005871 {
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01005872 // Ensure that between load and RecordPcInfo there are no pools emitted.
Artem Serov914d7a82017-02-07 14:33:49 +00005873 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5874 Location obj = instruction->GetLocations()->InAt(0);
5875 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5876 RecordPcInfo(instruction, instruction->GetDexPc());
5877 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005878}
5879
Calin Juravle2ae48182016-03-16 14:05:09 +00005880void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005881 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005882 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005883
5884 LocationSummary* locations = instruction->GetLocations();
5885 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005886
5887 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005888}
5889
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005890void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005891 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005892}
5893
Alexandre Rames67555f72014-11-18 10:55:16 +00005894void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5895 HandleBinaryOp(instruction);
5896}
5897
5898void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5899 HandleBinaryOp(instruction);
5900}
5901
Alexandre Rames3e69f162014-12-10 10:36:50 +00005902void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5903 LOG(FATAL) << "Unreachable";
5904}
5905
5906void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005907 if (instruction->GetNext()->IsSuspendCheck() &&
5908 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5909 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5910 // The back edge will generate the suspend check.
5911 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5912 }
5913
Alexandre Rames3e69f162014-12-10 10:36:50 +00005914 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5915}
5916
Alexandre Rames5319def2014-10-23 10:03:10 +01005917void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005918 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005919 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5920 if (location.IsStackSlot()) {
5921 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5922 } else if (location.IsDoubleStackSlot()) {
5923 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5924 }
5925 locations->SetOut(location);
5926}
5927
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005928void InstructionCodeGeneratorARM64::VisitParameterValue(
5929 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005930 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005931}
5932
5933void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5934 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005935 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005936 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005937}
5938
5939void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5940 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5941 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005942}
5943
5944void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005945 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005946 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005947 locations->SetInAt(i, Location::Any());
5948 }
5949 locations->SetOut(Location::Any());
5950}
5951
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005952void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005953 LOG(FATAL) << "Unreachable";
5954}
5955
Serban Constantinescu02164b32014-11-13 14:05:07 +00005956void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005957 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005958 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005959 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005960 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005961 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005962
5963 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005964 case DataType::Type::kInt32:
5965 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005966 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005967 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005968 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5969 break;
5970
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005971 case DataType::Type::kFloat32:
5972 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005973 InvokeRuntimeCallingConvention calling_convention;
5974 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5975 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5976 locations->SetOut(calling_convention.GetReturnLocation(type));
5977
5978 break;
5979 }
5980
Serban Constantinescu02164b32014-11-13 14:05:07 +00005981 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005982 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005983 }
5984}
5985
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005986void InstructionCodeGeneratorARM64::GenerateIntRemForPower2Denom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005987 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005988 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
5989 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
5990
5991 Register out = OutputRegister(instruction);
5992 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005993
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01005994 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01005995 // No need to adjust the result for non-negative dividends or the INT32_MIN/INT64_MIN dividends.
5996 // NOTE: The generated code for HRem correctly works for the INT32_MIN/INT64_MIN dividends.
5997 // INT*_MIN % imm must be 0 for any imm of power 2. 'and' works only with bits
5998 // 0..30 (Int32 case)/0..62 (Int64 case) of a dividend. For INT32_MIN/INT64_MIN they are zeros.
5999 // So 'and' always produces zero.
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01006000 __ And(out, dividend, abs_imm - 1);
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01006001 } else {
6002 if (abs_imm == 2) {
6003 __ Cmp(dividend, 0);
6004 __ And(out, dividend, 1);
6005 __ Csneg(out, out, out, ge);
6006 } else {
6007 UseScratchRegisterScope temps(GetVIXLAssembler());
6008 Register temp = temps.AcquireSameSizeAs(out);
6009
6010 __ Negs(temp, dividend);
6011 __ And(out, dividend, abs_imm - 1);
6012 __ And(temp, temp, abs_imm - 1);
6013 __ Csneg(out, out, temp, mi);
6014 }
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01006015 }
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01006016}
6017
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01006018void InstructionCodeGeneratorARM64::GenerateIntRemForConstDenom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01006019 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01006020
6021 if (imm == 0) {
6022 // Do not generate anything.
6023 // DivZeroCheck would prevent any code to be executed.
6024 return;
6025 }
6026
Evgeny Astigeevichf58dc652018-06-25 17:54:07 +01006027 if (IsPowerOfTwo(AbsOrMin(imm))) {
6028 // Cases imm == -1 or imm == 1 are handled in constant folding by
6029 // InstructionWithAbsorbingInputSimplifier.
6030 // If the cases have survided till code generation they are handled in
6031 // GenerateIntRemForPower2Denom becauses -1 and 1 are the power of 2 (2^0).
6032 // The correct code is generated for them, just more instructions.
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01006033 GenerateIntRemForPower2Denom(instruction);
6034 } else {
6035 DCHECK(imm < -2 || imm > 2) << imm;
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01006036 GenerateDivRemWithAnyConstant(instruction, imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01006037 }
6038}
6039
6040void InstructionCodeGeneratorARM64::GenerateIntRem(HRem* instruction) {
6041 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
6042 << instruction->GetResultType();
6043
6044 if (instruction->GetLocations()->InAt(1).IsConstant()) {
6045 GenerateIntRemForConstDenom(instruction);
6046 } else {
6047 Register out = OutputRegister(instruction);
6048 Register dividend = InputRegisterAt(instruction, 0);
6049 Register divisor = InputRegisterAt(instruction, 1);
6050 UseScratchRegisterScope temps(GetVIXLAssembler());
6051 Register temp = temps.AcquireSameSizeAs(out);
6052 __ Sdiv(temp, dividend, divisor);
6053 __ Msub(out, temp, divisor, dividend);
6054 }
6055}
6056
Serban Constantinescu02164b32014-11-13 14:05:07 +00006057void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006058 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00006059
Serban Constantinescu02164b32014-11-13 14:05:07 +00006060 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006061 case DataType::Type::kInt32:
6062 case DataType::Type::kInt64: {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01006063 GenerateIntRem(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006064 break;
6065 }
6066
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006067 case DataType::Type::kFloat32:
6068 case DataType::Type::kFloat64: {
6069 QuickEntrypointEnum entrypoint =
6070 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00006071 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006072 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00006073 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6074 } else {
6075 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6076 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00006077 break;
6078 }
6079
Serban Constantinescu02164b32014-11-13 14:05:07 +00006080 default:
6081 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00006082 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00006083 }
6084}
6085
Aart Bik1f8d51b2018-02-15 10:42:37 -08006086void LocationsBuilderARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01006087 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08006088}
6089
Aart Bik1f8d51b2018-02-15 10:42:37 -08006090void InstructionCodeGeneratorARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01006091 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08006092}
6093
6094void LocationsBuilderARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01006095 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08006096}
6097
6098void InstructionCodeGeneratorARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01006099 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08006100}
6101
Aart Bik3dad3412018-02-28 12:01:46 -08006102void LocationsBuilderARM64::VisitAbs(HAbs* abs) {
6103 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
6104 switch (abs->GetResultType()) {
6105 case DataType::Type::kInt32:
6106 case DataType::Type::kInt64:
6107 locations->SetInAt(0, Location::RequiresRegister());
6108 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6109 break;
6110 case DataType::Type::kFloat32:
6111 case DataType::Type::kFloat64:
6112 locations->SetInAt(0, Location::RequiresFpuRegister());
6113 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6114 break;
6115 default:
6116 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
6117 }
6118}
6119
6120void InstructionCodeGeneratorARM64::VisitAbs(HAbs* abs) {
6121 switch (abs->GetResultType()) {
6122 case DataType::Type::kInt32:
6123 case DataType::Type::kInt64: {
6124 Register in_reg = InputRegisterAt(abs, 0);
6125 Register out_reg = OutputRegister(abs);
6126 __ Cmp(in_reg, Operand(0));
6127 __ Cneg(out_reg, in_reg, lt);
6128 break;
6129 }
6130 case DataType::Type::kFloat32:
6131 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01006132 VRegister in_reg = InputFPRegisterAt(abs, 0);
6133 VRegister out_reg = OutputFPRegister(abs);
Aart Bik3dad3412018-02-28 12:01:46 -08006134 __ Fabs(out_reg, in_reg);
6135 break;
6136 }
6137 default:
6138 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
6139 }
6140}
6141
Igor Murashkind01745e2017-04-05 16:40:31 -07006142void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
6143 constructor_fence->SetLocations(nullptr);
6144}
6145
6146void InstructionCodeGeneratorARM64::VisitConstructorFence(
6147 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
6148 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
6149}
6150
Calin Juravle27df7582015-04-17 19:12:31 +01006151void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6152 memory_barrier->SetLocations(nullptr);
6153}
6154
6155void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006156 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01006157}
6158
Alexandre Rames5319def2014-10-23 10:03:10 +01006159void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006160 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006161 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00006162 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01006163}
6164
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00006165void InstructionCodeGeneratorARM64::VisitReturn(HReturn* ret) {
6166 if (GetGraph()->IsCompilingOsr()) {
6167 // To simplify callers of an OSR method, we put the return value in both
6168 // floating point and core register.
6169 switch (ret->InputAt(0)->GetType()) {
6170 case DataType::Type::kFloat32:
6171 __ Fmov(w0, s0);
6172 break;
6173 case DataType::Type::kFloat64:
6174 __ Fmov(x0, d0);
6175 break;
6176 default:
6177 break;
6178 }
6179 }
Alexandre Rames5319def2014-10-23 10:03:10 +01006180 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01006181}
6182
6183void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
6184 instruction->SetLocations(nullptr);
6185}
6186
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006187void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01006188 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01006189}
6190
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006191void LocationsBuilderARM64::VisitRor(HRor* ror) {
6192 HandleBinaryOp(ror);
6193}
6194
6195void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
6196 HandleBinaryOp(ror);
6197}
6198
Serban Constantinescu02164b32014-11-13 14:05:07 +00006199void LocationsBuilderARM64::VisitShl(HShl* shl) {
6200 HandleShift(shl);
6201}
6202
6203void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
6204 HandleShift(shl);
6205}
6206
6207void LocationsBuilderARM64::VisitShr(HShr* shr) {
6208 HandleShift(shr);
6209}
6210
6211void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
6212 HandleShift(shr);
6213}
6214
Alexandre Rames5319def2014-10-23 10:03:10 +01006215void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006216 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006217}
6218
6219void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006220 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006221}
6222
Alexandre Rames67555f72014-11-18 10:55:16 +00006223void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006224 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00006225}
6226
6227void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01006228 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00006229}
6230
6231void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01006232 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006233}
6234
Alexandre Rames67555f72014-11-18 10:55:16 +00006235void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00006236 HandleFieldSet(instruction,
6237 instruction->GetFieldInfo(),
6238 instruction->GetValueCanBeNull(),
6239 instruction->GetWriteBarrierKind());
Alexandre Rames5319def2014-10-23 10:03:10 +01006240}
6241
Vladimir Marko552a1342017-10-31 10:56:47 +00006242void LocationsBuilderARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6243 codegen_->CreateStringBuilderAppendLocations(instruction, LocationFrom(x0));
6244}
6245
6246void InstructionCodeGeneratorARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6247 __ Mov(w0, instruction->GetFormat()->GetValue());
6248 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
6249}
6250
Calin Juravlee460d1d2015-09-29 04:52:17 +01006251void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
6252 HUnresolvedInstanceFieldGet* instruction) {
6253 FieldAccessCallingConventionARM64 calling_convention;
6254 codegen_->CreateUnresolvedFieldLocationSummary(
6255 instruction, instruction->GetFieldType(), calling_convention);
6256}
6257
6258void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
6259 HUnresolvedInstanceFieldGet* instruction) {
6260 FieldAccessCallingConventionARM64 calling_convention;
6261 codegen_->GenerateUnresolvedFieldAccess(instruction,
6262 instruction->GetFieldType(),
6263 instruction->GetFieldIndex(),
6264 instruction->GetDexPc(),
6265 calling_convention);
6266}
6267
6268void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
6269 HUnresolvedInstanceFieldSet* instruction) {
6270 FieldAccessCallingConventionARM64 calling_convention;
6271 codegen_->CreateUnresolvedFieldLocationSummary(
6272 instruction, instruction->GetFieldType(), calling_convention);
6273}
6274
6275void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
6276 HUnresolvedInstanceFieldSet* instruction) {
6277 FieldAccessCallingConventionARM64 calling_convention;
6278 codegen_->GenerateUnresolvedFieldAccess(instruction,
6279 instruction->GetFieldType(),
6280 instruction->GetFieldIndex(),
6281 instruction->GetDexPc(),
6282 calling_convention);
6283}
6284
6285void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
6286 HUnresolvedStaticFieldGet* instruction) {
6287 FieldAccessCallingConventionARM64 calling_convention;
6288 codegen_->CreateUnresolvedFieldLocationSummary(
6289 instruction, instruction->GetFieldType(), calling_convention);
6290}
6291
6292void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
6293 HUnresolvedStaticFieldGet* instruction) {
6294 FieldAccessCallingConventionARM64 calling_convention;
6295 codegen_->GenerateUnresolvedFieldAccess(instruction,
6296 instruction->GetFieldType(),
6297 instruction->GetFieldIndex(),
6298 instruction->GetDexPc(),
6299 calling_convention);
6300}
6301
6302void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
6303 HUnresolvedStaticFieldSet* instruction) {
6304 FieldAccessCallingConventionARM64 calling_convention;
6305 codegen_->CreateUnresolvedFieldLocationSummary(
6306 instruction, instruction->GetFieldType(), calling_convention);
6307}
6308
6309void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
6310 HUnresolvedStaticFieldSet* instruction) {
6311 FieldAccessCallingConventionARM64 calling_convention;
6312 codegen_->GenerateUnresolvedFieldAccess(instruction,
6313 instruction->GetFieldType(),
6314 instruction->GetFieldIndex(),
6315 instruction->GetDexPc(),
6316 calling_convention);
6317}
6318
Alexandre Rames5319def2014-10-23 10:03:10 +01006319void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006320 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6321 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01006322 // In suspend check slow path, usually there are no caller-save registers at all.
6323 // If SIMD instructions are present, however, we force spilling all live SIMD
6324 // registers in full width (since the runtime only saves/restores lower part).
6325 locations->SetCustomSlowPathCallerSaves(
6326 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01006327}
6328
6329void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006330 HBasicBlock* block = instruction->GetBlock();
6331 if (block->GetLoopInformation() != nullptr) {
6332 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6333 // The back edge will generate the suspend check.
6334 return;
6335 }
6336 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6337 // The goto will generate the suspend check.
6338 return;
6339 }
6340 GenerateSuspendCheck(instruction, nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08006341 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01006342}
6343
Alexandre Rames67555f72014-11-18 10:55:16 +00006344void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006345 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6346 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00006347 InvokeRuntimeCallingConvention calling_convention;
6348 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
6349}
6350
6351void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00006352 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08006353 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00006354}
6355
6356void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
6357 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006358 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006359 DataType::Type input_type = conversion->GetInputType();
6360 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006361 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6362 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006363 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
6364 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006365 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6366 }
6367
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006368 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006369 locations->SetInAt(0, Location::RequiresFpuRegister());
6370 } else {
6371 locations->SetInAt(0, Location::RequiresRegister());
6372 }
6373
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006374 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006375 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6376 } else {
6377 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6378 }
6379}
6380
6381void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006382 DataType::Type result_type = conversion->GetResultType();
6383 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00006384
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006385 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6386 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006387
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006388 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
6389 int result_size = DataType::Size(result_type);
6390 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00006391 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006392 Register output = OutputRegister(conversion);
6393 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006394 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01006395 // 'int' values are used directly as W registers, discarding the top
6396 // bits, so we don't need to sign-extend and can just perform a move.
6397 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
6398 // top 32 bits of the target register. We theoretically could leave those
6399 // bits unchanged, but we would have to make sure that no code uses a
6400 // 32bit input value as a 64bit value assuming that the top 32 bits are
6401 // zero.
6402 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006403 } else if (DataType::IsUnsignedType(result_type) ||
6404 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
6405 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006406 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00006407 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006408 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006409 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006410 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006411 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6412 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006413 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006414 } else if (DataType::IsFloatingPointType(result_type) &&
6415 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006416 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
6417 } else {
6418 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6419 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006420 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00006421}
Alexandre Rames67555f72014-11-18 10:55:16 +00006422
Serban Constantinescu02164b32014-11-13 14:05:07 +00006423void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
6424 HandleShift(ushr);
6425}
6426
6427void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
6428 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00006429}
6430
6431void LocationsBuilderARM64::VisitXor(HXor* instruction) {
6432 HandleBinaryOp(instruction);
6433}
6434
6435void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
6436 HandleBinaryOp(instruction);
6437}
6438
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006439void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006440 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006441 LOG(FATAL) << "Unreachable";
6442}
6443
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006444void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006445 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006446 LOG(FATAL) << "Unreachable";
6447}
6448
Mark Mendellfe57faa2015-09-18 09:26:15 -04006449// Simple implementation of packed switch - generate cascaded compare/jumps.
6450void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6451 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006452 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006453 locations->SetInAt(0, Location::RequiresRegister());
6454}
6455
6456void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6457 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08006458 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006459 Register value_reg = InputRegisterAt(switch_instr, 0);
6460 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6461
Zheng Xu3927c8b2015-11-18 17:46:25 +08006462 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01006463 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08006464 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
6465 // make sure we don't emit it if the target may run out of range.
6466 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
6467 // ranges and emit the tables only as required.
6468 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04006469
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006470 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08006471 // Current instruction id is an upper bound of the number of HIRs in the graph.
6472 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
6473 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006474 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6475 Register temp = temps.AcquireW();
6476 __ Subs(temp, value_reg, Operand(lower_bound));
6477
Zheng Xu3927c8b2015-11-18 17:46:25 +08006478 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006479 // Jump to successors[0] if value == lower_bound.
6480 __ B(eq, codegen_->GetLabelOf(successors[0]));
6481 int32_t last_index = 0;
6482 for (; num_entries - last_index > 2; last_index += 2) {
6483 __ Subs(temp, temp, Operand(2));
6484 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6485 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
6486 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6487 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
6488 }
6489 if (num_entries - last_index == 2) {
6490 // The last missing case_value.
6491 __ Cmp(temp, Operand(1));
6492 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08006493 }
6494
6495 // And the default for any other value.
6496 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6497 __ B(codegen_->GetLabelOf(default_block));
6498 }
6499 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01006500 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08006501
6502 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6503
6504 // Below instructions should use at most one blocked register. Since there are two blocked
6505 // registers, we are free to block one.
6506 Register temp_w = temps.AcquireW();
6507 Register index;
6508 // Remove the bias.
6509 if (lower_bound != 0) {
6510 index = temp_w;
6511 __ Sub(index, value_reg, Operand(lower_bound));
6512 } else {
6513 index = value_reg;
6514 }
6515
6516 // Jump to default block if index is out of the range.
6517 __ Cmp(index, Operand(num_entries));
6518 __ B(hs, codegen_->GetLabelOf(default_block));
6519
6520 // In current VIXL implementation, it won't require any blocked registers to encode the
6521 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
6522 // register pressure.
6523 Register table_base = temps.AcquireX();
6524 // Load jump offset from the table.
6525 __ Adr(table_base, jump_table->GetTableStartLabel());
6526 Register jump_offset = temp_w;
6527 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
6528
6529 // Jump to target block by branching to table_base(pc related) + offset.
6530 Register target_address = table_base;
6531 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
6532 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006533 }
6534}
6535
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006536void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
6537 HInstruction* instruction,
6538 Location out,
6539 uint32_t offset,
6540 Location maybe_temp,
6541 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006542 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006543 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006544 if (read_barrier_option == kWithReadBarrier) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006545 CHECK(gUseReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006546 if (kUseBakerReadBarrier) {
6547 // Load with fast path based Baker's read barrier.
6548 // /* HeapReference<Object> */ out = *(out + offset)
6549 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6550 out,
6551 out_reg,
6552 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006553 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006554 /* needs_null_check= */ false,
6555 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006556 } else {
6557 // Load with slow path based read barrier.
6558 // Save the value of `out` into `maybe_temp` before overwriting it
6559 // in the following move operation, as we will need it for the
6560 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006561 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00006562 __ Mov(temp_reg, out_reg);
6563 // /* HeapReference<Object> */ out = *(out + offset)
6564 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6565 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6566 }
6567 } else {
6568 // Plain load with no read barrier.
6569 // /* HeapReference<Object> */ out = *(out + offset)
6570 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6571 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6572 }
6573}
6574
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006575void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
6576 HInstruction* instruction,
6577 Location out,
6578 Location obj,
6579 uint32_t offset,
6580 Location maybe_temp,
6581 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006582 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006583 Register out_reg = RegisterFrom(out, type);
6584 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006585 if (read_barrier_option == kWithReadBarrier) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006586 CHECK(gUseReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006587 if (kUseBakerReadBarrier) {
6588 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00006589 // /* HeapReference<Object> */ out = *(obj + offset)
6590 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6591 out,
6592 obj_reg,
6593 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006594 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006595 /* needs_null_check= */ false,
6596 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006597 } else {
6598 // Load with slow path based read barrier.
6599 // /* HeapReference<Object> */ out = *(obj + offset)
6600 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6601 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6602 }
6603 } else {
6604 // Plain load with no read barrier.
6605 // /* HeapReference<Object> */ out = *(obj + offset)
6606 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6607 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6608 }
6609}
6610
Vladimir Markoca1e0382018-04-11 09:58:41 +00006611void CodeGeneratorARM64::GenerateGcRootFieldLoad(
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006612 HInstruction* instruction,
6613 Location root,
6614 Register obj,
6615 uint32_t offset,
6616 vixl::aarch64::Label* fixup_label,
6617 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006618 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006619 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006620 if (read_barrier_option == kWithReadBarrier) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006621 DCHECK(gUseReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006622 if (kUseBakerReadBarrier) {
6623 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00006624 // Baker's read barrier are used.
Roland Levillain44015862016-01-22 11:47:17 +00006625
Vladimir Marko008e09f32018-08-06 15:42:43 +01006626 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6627 // the Marking Register) to decide whether we need to enter
6628 // the slow path to mark the GC root.
6629 //
6630 // We use shared thunks for the slow path; shared within the method
6631 // for JIT, across methods for AOT. That thunk checks the reference
6632 // and jumps to the entrypoint if needed.
6633 //
6634 // lr = &return_address;
6635 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6636 // if (mr) { // Thread::Current()->GetIsGcMarking()
6637 // goto gc_root_thunk<root_reg>(lr)
6638 // }
6639 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00006640
Vladimir Marko008e09f32018-08-06 15:42:43 +01006641 UseScratchRegisterScope temps(GetVIXLAssembler());
6642 DCHECK(temps.IsAvailable(ip0));
6643 DCHECK(temps.IsAvailable(ip1));
6644 temps.Exclude(ip0, ip1);
6645 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
Roland Levillain44015862016-01-22 11:47:17 +00006646
Vladimir Marko008e09f32018-08-06 15:42:43 +01006647 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6648 vixl::aarch64::Label return_address;
6649 __ adr(lr, &return_address);
6650 if (fixup_label != nullptr) {
6651 __ bind(fixup_label);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006652 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006653 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
Vladimir Marko94796f82018-08-08 15:15:33 +01006654 "GC root LDR must be 2 instructions (8B) before the return address label.");
Vladimir Marko008e09f32018-08-06 15:42:43 +01006655 __ ldr(root_reg, MemOperand(obj.X(), offset));
6656 EmitBakerReadBarrierCbnz(custom_data);
6657 __ bind(&return_address);
Roland Levillain44015862016-01-22 11:47:17 +00006658 } else {
6659 // GC root loaded through a slow path for read barriers other
6660 // than Baker's.
6661 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006662 if (fixup_label == nullptr) {
6663 __ Add(root_reg.X(), obj.X(), offset);
6664 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006665 EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006666 }
Roland Levillain44015862016-01-22 11:47:17 +00006667 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00006668 GenerateReadBarrierForRootSlow(instruction, root, root);
Roland Levillain44015862016-01-22 11:47:17 +00006669 }
6670 } else {
6671 // Plain GC root load with no read barrier.
6672 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006673 if (fixup_label == nullptr) {
6674 __ Ldr(root_reg, MemOperand(obj, offset));
6675 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006676 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006677 }
Roland Levillain44015862016-01-22 11:47:17 +00006678 // Note that GC roots are not affected by heap poisoning, thus we
6679 // do not have to unpoison `root_reg` here.
6680 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006681 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00006682}
6683
Vladimir Markoc8178f52020-11-24 10:38:16 +00006684void CodeGeneratorARM64::GenerateIntrinsicCasMoveWithBakerReadBarrier(
6685 vixl::aarch64::Register marked_old_value,
Vladimir Marko94796f82018-08-08 15:15:33 +01006686 vixl::aarch64::Register old_value) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006687 DCHECK(gUseReadBarrier);
Vladimir Marko94796f82018-08-08 15:15:33 +01006688 DCHECK(kUseBakerReadBarrier);
6689
6690 // Similar to the Baker RB path in GenerateGcRootFieldLoad(), with a MOV instead of LDR.
Vladimir Markoc8178f52020-11-24 10:38:16 +00006691 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(marked_old_value.GetCode());
Vladimir Marko94796f82018-08-08 15:15:33 +01006692
6693 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6694 vixl::aarch64::Label return_address;
6695 __ adr(lr, &return_address);
6696 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6697 "GC root LDR must be 2 instructions (8B) before the return address label.");
Vladimir Markoc8178f52020-11-24 10:38:16 +00006698 __ mov(marked_old_value, old_value);
Vladimir Marko94796f82018-08-08 15:15:33 +01006699 EmitBakerReadBarrierCbnz(custom_data);
6700 __ bind(&return_address);
6701}
6702
Roland Levillain44015862016-01-22 11:47:17 +00006703void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6704 Location ref,
Vladimir Marko248141f2018-08-10 10:40:07 +01006705 vixl::aarch64::Register obj,
6706 const vixl::aarch64::MemOperand& src,
Roland Levillain44015862016-01-22 11:47:17 +00006707 bool needs_null_check,
6708 bool use_load_acquire) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006709 DCHECK(gUseReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006710 DCHECK(kUseBakerReadBarrier);
6711
Vladimir Marko0ecac682018-08-07 10:40:38 +01006712 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6713 // Marking Register) to decide whether we need to enter the slow
6714 // path to mark the reference. Then, in the slow path, check the
6715 // gray bit in the lock word of the reference's holder (`obj`) to
6716 // decide whether to mark `ref` or not.
6717 //
6718 // We use shared thunks for the slow path; shared within the method
6719 // for JIT, across methods for AOT. That thunk checks the holder
6720 // and jumps to the entrypoint if needed. If the holder is not gray,
6721 // it creates a fake dependency and returns to the LDR instruction.
6722 //
6723 // lr = &gray_return_address;
6724 // if (mr) { // Thread::Current()->GetIsGcMarking()
6725 // goto field_thunk<holder_reg, base_reg, use_load_acquire>(lr)
6726 // }
6727 // not_gray_return_address:
6728 // // Original reference load. If the offset is too large to fit
6729 // // into LDR, we use an adjusted base register here.
6730 // HeapReference<mirror::Object> reference = *(obj+offset);
6731 // gray_return_address:
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006732
Vladimir Marko248141f2018-08-10 10:40:07 +01006733 DCHECK(src.GetAddrMode() == vixl::aarch64::Offset);
6734 DCHECK_ALIGNED(src.GetOffset(), sizeof(mirror::HeapReference<mirror::Object>));
6735
6736 UseScratchRegisterScope temps(GetVIXLAssembler());
6737 DCHECK(temps.IsAvailable(ip0));
6738 DCHECK(temps.IsAvailable(ip1));
6739 temps.Exclude(ip0, ip1);
6740 uint32_t custom_data = use_load_acquire
6741 ? EncodeBakerReadBarrierAcquireData(src.GetBaseRegister().GetCode(), obj.GetCode())
6742 : EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode());
6743
6744 {
6745 ExactAssemblyScope guard(GetVIXLAssembler(),
6746 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6747 vixl::aarch64::Label return_address;
6748 __ adr(lr, &return_address);
6749 EmitBakerReadBarrierCbnz(custom_data);
6750 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6751 "Field LDR must be 1 instruction (4B) before the return address label; "
6752 " 2 instructions (8B) for heap poisoning.");
6753 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
6754 if (use_load_acquire) {
6755 DCHECK_EQ(src.GetOffset(), 0);
6756 __ ldar(ref_reg, src);
6757 } else {
6758 __ ldr(ref_reg, src);
6759 }
6760 if (needs_null_check) {
6761 MaybeRecordImplicitNullCheck(instruction);
6762 }
6763 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6764 // macro instructions disallowed in ExactAssemblyScope.
6765 if (kPoisonHeapReferences) {
6766 __ neg(ref_reg, Operand(ref_reg));
6767 }
6768 __ bind(&return_address);
6769 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006770 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Vladimir Marko248141f2018-08-10 10:40:07 +01006771}
6772
6773void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6774 Location ref,
6775 Register obj,
6776 uint32_t offset,
6777 Location maybe_temp,
6778 bool needs_null_check,
6779 bool use_load_acquire) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006780 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6781 Register base = obj;
6782 if (use_load_acquire) {
6783 DCHECK(maybe_temp.IsRegister());
6784 base = WRegisterFrom(maybe_temp);
6785 __ Add(base, obj, offset);
6786 offset = 0u;
6787 } else if (offset >= kReferenceLoadMinFarOffset) {
6788 DCHECK(maybe_temp.IsRegister());
6789 base = WRegisterFrom(maybe_temp);
6790 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6791 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6792 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006793 }
Vladimir Marko248141f2018-08-10 10:40:07 +01006794 MemOperand src(base.X(), offset);
6795 GenerateFieldLoadWithBakerReadBarrier(
6796 instruction, ref, obj, src, needs_null_check, use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006797}
6798
Artem Serov0806f582018-10-11 20:14:20 +01006799void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HArrayGet* instruction,
6800 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006801 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006802 uint32_t data_offset,
6803 Location index,
Roland Levillain44015862016-01-22 11:47:17 +00006804 bool needs_null_check) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006805 DCHECK(gUseReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006806 DCHECK(kUseBakerReadBarrier);
6807
Vladimir Marko66d691d2017-04-07 17:53:39 +01006808 static_assert(
6809 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6810 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006811 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006812
Vladimir Marko008e09f32018-08-06 15:42:43 +01006813 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6814 // Marking Register) to decide whether we need to enter the slow
6815 // path to mark the reference. Then, in the slow path, check the
6816 // gray bit in the lock word of the reference's holder (`obj`) to
6817 // decide whether to mark `ref` or not.
6818 //
6819 // We use shared thunks for the slow path; shared within the method
6820 // for JIT, across methods for AOT. That thunk checks the holder
6821 // and jumps to the entrypoint if needed. If the holder is not gray,
6822 // it creates a fake dependency and returns to the LDR instruction.
6823 //
6824 // lr = &gray_return_address;
6825 // if (mr) { // Thread::Current()->GetIsGcMarking()
6826 // goto array_thunk<base_reg>(lr)
6827 // }
6828 // not_gray_return_address:
6829 // // Original reference load. If the offset is too large to fit
6830 // // into LDR, we use an adjusted base register here.
6831 // HeapReference<mirror::Object> reference = data[index];
6832 // gray_return_address:
Vladimir Marko66d691d2017-04-07 17:53:39 +01006833
Vladimir Marko008e09f32018-08-06 15:42:43 +01006834 DCHECK(index.IsValid());
6835 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
6836 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006837
Vladimir Marko008e09f32018-08-06 15:42:43 +01006838 UseScratchRegisterScope temps(GetVIXLAssembler());
6839 DCHECK(temps.IsAvailable(ip0));
6840 DCHECK(temps.IsAvailable(ip1));
6841 temps.Exclude(ip0, ip1);
Artem Serov0806f582018-10-11 20:14:20 +01006842
6843 Register temp;
6844 if (instruction->GetArray()->IsIntermediateAddress()) {
6845 // We do not need to compute the intermediate address from the array: the
6846 // input instruction has done it already. See the comment in
6847 // `TryExtractArrayAccessAddress()`.
6848 if (kIsDebugBuild) {
6849 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
6850 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
6851 }
6852 temp = obj;
6853 } else {
6854 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
6855 __ Add(temp.X(), obj.X(), Operand(data_offset));
6856 }
6857
Vladimir Marko008e09f32018-08-06 15:42:43 +01006858 uint32_t custom_data = EncodeBakerReadBarrierArrayData(temp.GetCode());
Vladimir Marko66d691d2017-04-07 17:53:39 +01006859
Vladimir Marko008e09f32018-08-06 15:42:43 +01006860 {
6861 ExactAssemblyScope guard(GetVIXLAssembler(),
6862 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6863 vixl::aarch64::Label return_address;
6864 __ adr(lr, &return_address);
6865 EmitBakerReadBarrierCbnz(custom_data);
6866 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6867 "Array LDR must be 1 instruction (4B) before the return address label; "
6868 " 2 instructions (8B) for heap poisoning.");
6869 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6870 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6871 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6872 // macro instructions disallowed in ExactAssemblyScope.
6873 if (kPoisonHeapReferences) {
6874 __ neg(ref_reg, Operand(ref_reg));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006875 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006876 __ bind(&return_address);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006877 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006878 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Roland Levillain44015862016-01-22 11:47:17 +00006879}
6880
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006881void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6882 // The following condition is a compile-time one, so it does not have a run-time cost.
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006883 if (kIsDebugBuild && gUseReadBarrier && kUseBakerReadBarrier) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006884 // The following condition is a run-time one; it is executed after the
6885 // previous compile-time test, to avoid penalizing non-debug builds.
6886 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6887 UseScratchRegisterScope temps(GetVIXLAssembler());
6888 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6889 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6890 }
6891 }
6892}
6893
Vladimir Marko1bff99f2020-11-02 15:07:33 +00006894SlowPathCodeARM64* CodeGeneratorARM64::AddReadBarrierSlowPath(HInstruction* instruction,
6895 Location out,
6896 Location ref,
6897 Location obj,
6898 uint32_t offset,
6899 Location index) {
6900 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
6901 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6902 AddSlowPath(slow_path);
6903 return slow_path;
6904}
6905
Roland Levillain44015862016-01-22 11:47:17 +00006906void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6907 Location out,
6908 Location ref,
6909 Location obj,
6910 uint32_t offset,
6911 Location index) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006912 DCHECK(gUseReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006913
Roland Levillain44015862016-01-22 11:47:17 +00006914 // Insert a slow path based read barrier *after* the reference load.
6915 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006916 // If heap poisoning is enabled, the unpoisoning of the loaded
6917 // reference will be carried out by the runtime within the slow
6918 // path.
6919 //
6920 // Note that `ref` currently does not get unpoisoned (when heap
6921 // poisoning is enabled), which is alright as the `ref` argument is
6922 // not used by the artReadBarrierSlow entry point.
6923 //
6924 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko1bff99f2020-11-02 15:07:33 +00006925 SlowPathCodeARM64* slow_path = AddReadBarrierSlowPath(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006926
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006927 __ B(slow_path->GetEntryLabel());
6928 __ Bind(slow_path->GetExitLabel());
6929}
6930
Roland Levillain44015862016-01-22 11:47:17 +00006931void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6932 Location out,
6933 Location ref,
6934 Location obj,
6935 uint32_t offset,
6936 Location index) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006937 if (gUseReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006938 // Baker's read barriers shall be handled by the fast path
6939 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6940 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006941 // If heap poisoning is enabled, unpoisoning will be taken care of
6942 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006943 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006944 } else if (kPoisonHeapReferences) {
6945 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6946 }
6947}
6948
Roland Levillain44015862016-01-22 11:47:17 +00006949void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6950 Location out,
6951 Location root) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006952 DCHECK(gUseReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006953
Roland Levillain44015862016-01-22 11:47:17 +00006954 // Insert a slow path based read barrier *after* the GC root load.
6955 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006956 // Note that GC roots are not affected by heap poisoning, so we do
6957 // not need to do anything special for this here.
6958 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006959 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006960 AddSlowPath(slow_path);
6961
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006962 __ B(slow_path->GetEntryLabel());
6963 __ Bind(slow_path->GetExitLabel());
6964}
6965
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006966void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6967 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006968 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006969 locations->SetInAt(0, Location::RequiresRegister());
6970 locations->SetOut(Location::RequiresRegister());
6971}
6972
6973void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6974 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006975 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006976 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006977 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006978 __ Ldr(XRegisterFrom(locations->Out()),
6979 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006980 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006981 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006982 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006983 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6984 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006985 __ Ldr(XRegisterFrom(locations->Out()),
6986 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006987 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006988}
6989
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006990static void PatchJitRootUse(uint8_t* code,
6991 const uint8_t* roots_data,
6992 vixl::aarch64::Literal<uint32_t>* literal,
6993 uint64_t index_in_table) {
6994 uint32_t literal_offset = literal->GetOffset();
6995 uintptr_t address =
6996 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6997 uint8_t* data = code + literal_offset;
6998 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6999}
7000
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007001void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7002 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007003 const StringReference& string_reference = entry.first;
7004 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01007005 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007006 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007007 }
7008 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007009 const TypeReference& type_reference = entry.first;
7010 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01007011 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007012 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007013 }
7014}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007015
Artem Serov8ba4de12019-12-04 21:10:23 +00007016MemOperand InstructionCodeGeneratorARM64::VecNEONAddress(
Artem Serov1a719e42019-07-18 14:24:55 +01007017 HVecMemoryOperation* instruction,
7018 UseScratchRegisterScope* temps_scope,
7019 size_t size,
7020 bool is_string_char_at,
7021 /*out*/ Register* scratch) {
7022 LocationSummary* locations = instruction->GetLocations();
7023 Register base = InputRegisterAt(instruction, 0);
7024
7025 if (instruction->InputAt(1)->IsIntermediateAddressIndex()) {
7026 DCHECK(!is_string_char_at);
7027 return MemOperand(base.X(), InputRegisterAt(instruction, 1).X());
7028 }
7029
7030 Location index = locations->InAt(1);
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
7036 // HIntermediateAddress optimization is only applied for scalar ArrayGet and ArraySet.
7037 DCHECK(!instruction->InputAt(0)->IsIntermediateAddress());
7038
7039 if (index.IsConstant()) {
7040 offset += Int64FromLocation(index) << shift;
7041 return HeapOperand(base, offset);
7042 } else {
7043 *scratch = temps_scope->AcquireSameSizeAs(base);
7044 __ Add(*scratch, base, Operand(WRegisterFrom(index), LSL, shift));
7045 return HeapOperand(*scratch, offset);
7046 }
7047}
7048
Artem Serov8ba4de12019-12-04 21:10:23 +00007049SVEMemOperand InstructionCodeGeneratorARM64::VecSVEAddress(
7050 HVecMemoryOperation* instruction,
7051 UseScratchRegisterScope* temps_scope,
7052 size_t size,
7053 bool is_string_char_at,
7054 /*out*/ Register* scratch) {
7055 LocationSummary* locations = instruction->GetLocations();
7056 Register base = InputRegisterAt(instruction, 0);
7057 Location index = locations->InAt(1);
7058
Artem Serov8ba4de12019-12-04 21:10:23 +00007059 DCHECK(!instruction->InputAt(1)->IsIntermediateAddressIndex());
Artem Serov8ba4de12019-12-04 21:10:23 +00007060 DCHECK(!index.IsConstant());
7061
7062 uint32_t offset = is_string_char_at
7063 ? mirror::String::ValueOffset().Uint32Value()
7064 : mirror::Array::DataOffset(size).Uint32Value();
7065 size_t shift = ComponentSizeShiftWidth(size);
7066
Raphael Gault0700b692020-09-30 08:33:10 +00007067 if (instruction->InputAt(0)->IsIntermediateAddress()) {
7068 return SVEMemOperand(base.X(), XRegisterFrom(index), LSL, shift);
7069 }
7070
Artem Serov8ba4de12019-12-04 21:10:23 +00007071 *scratch = temps_scope->AcquireSameSizeAs(base);
7072 __ Add(*scratch, base, offset);
7073 return SVEMemOperand(scratch->X(), XRegisterFrom(index), LSL, shift);
7074}
7075
Alexandre Rames67555f72014-11-18 10:55:16 +00007076#undef __
7077#undef QUICK_ENTRY_POINT
7078
Vladimir Markoca1e0382018-04-11 09:58:41 +00007079#define __ assembler.GetVIXLAssembler()->
7080
7081static void EmitGrayCheckAndFastPath(arm64::Arm64Assembler& assembler,
7082 vixl::aarch64::Register base_reg,
7083 vixl::aarch64::MemOperand& lock_word,
Vladimir Marko7a695052018-04-12 10:26:50 +01007084 vixl::aarch64::Label* slow_path,
7085 vixl::aarch64::Label* throw_npe = nullptr) {
Tamas Kamanf4655e52023-02-10 11:28:40 +01007086 vixl::aarch64::Label throw_npe_cont;
Vladimir Markoca1e0382018-04-11 09:58:41 +00007087 // Load the lock word containing the rb_state.
7088 __ Ldr(ip0.W(), lock_word);
7089 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01007090 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Vladimir Markoca1e0382018-04-11 09:58:41 +00007091 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7092 __ Tbnz(ip0.W(), LockWord::kReadBarrierStateShift, slow_path);
7093 static_assert(
7094 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET,
7095 "Field and array LDR offsets must be the same to reuse the same code.");
Vladimir Marko7a695052018-04-12 10:26:50 +01007096 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
7097 if (throw_npe != nullptr) {
Tamas Kamanf4655e52023-02-10 11:28:40 +01007098 __ Bind(&throw_npe_cont);
Vladimir Marko7a695052018-04-12 10:26:50 +01007099 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00007100 // Adjust the return address back to the LDR (1 instruction; 2 for heap poisoning).
7101 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
7102 "Field LDR must be 1 instruction (4B) before the return address label; "
7103 " 2 instructions (8B) for heap poisoning.");
7104 __ Add(lr, lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
7105 // Introduce a dependency on the lock_word including rb_state,
7106 // to prevent load-load reordering, and without using
7107 // a memory barrier (which would be more expensive).
7108 __ Add(base_reg, base_reg, Operand(ip0, LSR, 32));
7109 __ Br(lr); // And return back to the function.
Tamas Kamanf4655e52023-02-10 11:28:40 +01007110 if (throw_npe != nullptr) {
7111 // Clear IP0 before returning to the fast path.
7112 __ Bind(throw_npe);
7113 __ Mov(ip0.X(), xzr);
7114 __ B(&throw_npe_cont);
7115 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00007116 // Note: The fake dependency is unnecessary for the slow path.
7117}
7118
7119// Load the read barrier introspection entrypoint in register `entrypoint`.
7120static void LoadReadBarrierMarkIntrospectionEntrypoint(arm64::Arm64Assembler& assembler,
7121 vixl::aarch64::Register entrypoint) {
7122 // entrypoint = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
7123 DCHECK_EQ(ip0.GetCode(), 16u);
7124 const int32_t entry_point_offset =
7125 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
7126 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
7127}
7128
7129void CodeGeneratorARM64::CompileBakerReadBarrierThunk(Arm64Assembler& assembler,
7130 uint32_t encoded_data,
7131 /*out*/ std::string* debug_name) {
7132 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
7133 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01007134 case BakerReadBarrierKind::kField:
7135 case BakerReadBarrierKind::kAcquire: {
Artem Serova07de552020-11-01 22:42:43 +00007136 Register base_reg =
7137 vixl::aarch64::XRegister(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00007138 CheckValidReg(base_reg.GetCode());
Artem Serova07de552020-11-01 22:42:43 +00007139 Register holder_reg =
7140 vixl::aarch64::XRegister(BakerReadBarrierSecondRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00007141 CheckValidReg(holder_reg.GetCode());
7142 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
7143 temps.Exclude(ip0, ip1);
Roland Levillain988c3912019-09-25 19:33:35 +01007144 // In the case of a field load (with relaxed semantic), if `base_reg` differs from
7145 // `holder_reg`, the offset was too large and we must have emitted (during the construction
7146 // of the HIR graph, see `art::HInstructionBuilder::BuildInstanceFieldAccess`) and preserved
7147 // (see `art::PrepareForRegisterAllocation::VisitNullCheck`) an explicit null check before
7148 // the load. Otherwise, for implicit null checks, we need to null-check the holder as we do
7149 // not necessarily do that check before going to the thunk.
7150 //
7151 // In the case of a field load with load-acquire semantics (where `base_reg` always differs
7152 // from `holder_reg`), we also need an explicit null check when implicit null checks are
7153 // allowed, as we do not emit one before going to the thunk.
Vladimir Marko7a695052018-04-12 10:26:50 +01007154 vixl::aarch64::Label throw_npe_label;
7155 vixl::aarch64::Label* throw_npe = nullptr;
Roland Levillain988c3912019-09-25 19:33:35 +01007156 if (GetCompilerOptions().GetImplicitNullChecks() &&
7157 (holder_reg.Is(base_reg) || (kind == BakerReadBarrierKind::kAcquire))) {
Vladimir Marko7a695052018-04-12 10:26:50 +01007158 throw_npe = &throw_npe_label;
7159 __ Cbz(holder_reg.W(), throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00007160 }
Vladimir Marko7a695052018-04-12 10:26:50 +01007161 // Check if the holder is gray and, if not, add fake dependency to the base register
7162 // and return to the LDR instruction to load the reference. Otherwise, use introspection
7163 // to load the reference and call the entrypoint that performs further checks on the
7164 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00007165 vixl::aarch64::Label slow_path;
7166 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
Vladimir Marko7a695052018-04-12 10:26:50 +01007167 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00007168 __ Bind(&slow_path);
Vladimir Marko0ecac682018-08-07 10:40:38 +01007169 if (kind == BakerReadBarrierKind::kField) {
7170 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
7171 __ Ldr(ip0.W(), ldr_address); // Load the LDR (immediate) unsigned offset.
7172 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
7173 __ Ubfx(ip0.W(), ip0.W(), 10, 12); // Extract the offset.
7174 __ Ldr(ip0.W(), MemOperand(base_reg, ip0, LSL, 2)); // Load the reference.
7175 } else {
7176 DCHECK(kind == BakerReadBarrierKind::kAcquire);
7177 DCHECK(!base_reg.Is(holder_reg));
7178 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
7179 __ Ldar(ip0.W(), MemOperand(base_reg));
7180 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00007181 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
7182 __ Br(ip1); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00007183 break;
7184 }
7185 case BakerReadBarrierKind::kArray: {
Artem Serova07de552020-11-01 22:42:43 +00007186 Register base_reg =
7187 vixl::aarch64::XRegister(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00007188 CheckValidReg(base_reg.GetCode());
7189 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7190 BakerReadBarrierSecondRegField::Decode(encoded_data));
7191 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
7192 temps.Exclude(ip0, ip1);
7193 vixl::aarch64::Label slow_path;
7194 int32_t data_offset =
7195 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
7196 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
7197 DCHECK_LT(lock_word.GetOffset(), 0);
7198 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path);
7199 __ Bind(&slow_path);
7200 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
7201 __ Ldr(ip0.W(), ldr_address); // Load the LDR (register) unsigned offset.
7202 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
7203 __ Ubfx(ip0, ip0, 16, 6); // Extract the index register, plus 32 (bit 21 is set).
7204 __ Bfi(ip1, ip0, 3, 6); // Insert ip0 to the entrypoint address to create
7205 // a switch case target based on the index register.
7206 __ Mov(ip0, base_reg); // Move the base register to ip0.
7207 __ Br(ip1); // Jump to the entrypoint's array switch case.
7208 break;
7209 }
7210 case BakerReadBarrierKind::kGcRoot: {
7211 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
7212 // and it does not have a forwarding address), call the correct introspection entrypoint;
7213 // otherwise return the reference (or the extracted forwarding address).
7214 // There is no gray bit check for GC roots.
Artem Serova07de552020-11-01 22:42:43 +00007215 Register root_reg =
7216 vixl::aarch64::WRegister(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00007217 CheckValidReg(root_reg.GetCode());
7218 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7219 BakerReadBarrierSecondRegField::Decode(encoded_data));
7220 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
7221 temps.Exclude(ip0, ip1);
7222 vixl::aarch64::Label return_label, not_marked, forwarding_address;
7223 __ Cbz(root_reg, &return_label);
7224 MemOperand lock_word(root_reg.X(), mirror::Object::MonitorOffset().Int32Value());
7225 __ Ldr(ip0.W(), lock_word);
7226 __ Tbz(ip0.W(), LockWord::kMarkBitStateShift, &not_marked);
7227 __ Bind(&return_label);
7228 __ Br(lr);
7229 __ Bind(&not_marked);
7230 __ Tst(ip0.W(), Operand(ip0.W(), LSL, 1));
7231 __ B(&forwarding_address, mi);
7232 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
7233 // Adjust the art_quick_read_barrier_mark_introspection address in IP1 to
7234 // art_quick_read_barrier_mark_introspection_gc_roots.
7235 __ Add(ip1, ip1, Operand(BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRYPOINT_OFFSET));
7236 __ Mov(ip0.W(), root_reg);
7237 __ Br(ip1);
7238 __ Bind(&forwarding_address);
7239 __ Lsl(root_reg, ip0.W(), LockWord::kForwardingAddressShift);
7240 __ Br(lr);
7241 break;
7242 }
7243 default:
7244 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
7245 UNREACHABLE();
7246 }
7247
Vladimir Marko966b46f2018-08-03 10:20:19 +00007248 // For JIT, the slow path is considered part of the compiled method,
Vladimir Markof91fc122020-05-13 09:21:00 +01007249 // so JIT should pass null as `debug_name`.
Santiago Aboy Solanes872ec722022-02-18 14:10:25 +00007250 DCHECK_IMPLIES(GetCompilerOptions().IsJitCompiler(), debug_name == nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00007251 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00007252 std::ostringstream oss;
7253 oss << "BakerReadBarrierThunk";
7254 switch (kind) {
7255 case BakerReadBarrierKind::kField:
7256 oss << "Field_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
7257 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
7258 break;
Vladimir Marko0ecac682018-08-07 10:40:38 +01007259 case BakerReadBarrierKind::kAcquire:
7260 oss << "Acquire_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
7261 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
7262 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +00007263 case BakerReadBarrierKind::kArray:
7264 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
7265 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7266 BakerReadBarrierSecondRegField::Decode(encoded_data));
7267 break;
7268 case BakerReadBarrierKind::kGcRoot:
7269 oss << "GcRoot_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
7270 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7271 BakerReadBarrierSecondRegField::Decode(encoded_data));
7272 break;
7273 }
7274 *debug_name = oss.str();
7275 }
7276}
7277
7278#undef __
7279
Alexandre Rames5319def2014-10-23 10:03:10 +01007280} // namespace arm64
7281} // namespace art