blob: d455614cfd248fd4fddc8a43cc8276588a6fb539 [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"
Vladimir Marko58155012015-08-19 12:49:41 +000030#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010031#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080032#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010033#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010034#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070035#include "heap_poisoning.h"
Nicolas Geoffray4313ccb2020-08-26 17:01:15 +010036#include "interpreter/mterp/nterp.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080037#include "intrinsics.h"
38#include "intrinsics_arm64.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010039#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070040#include "lock_word.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010041#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070042#include "mirror/class-inl.h"
Vladimir Marko2d98dc22020-10-01 11:21:37 +000043#include "mirror/var_handle.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000044#include "offsets.h"
Alex Light3a73ffb2021-01-25 14:11:05 +000045#include "optimizing/common_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010046#include "thread.h"
47#include "utils/arm64/assembler_arm64.h"
48#include "utils/assembler.h"
49#include "utils/stack_checks.h"
50
Scott Wakeling97c72b72016-06-24 16:19:36 +010051using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000052using vixl::ExactAssemblyScope;
53using vixl::CodeBufferCheckScope;
54using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010055
56#ifdef __
57#error "ARM64 Codegen VIXL macro-assembler macro already defined."
58#endif
59
Vladimir Marko0a516052019-10-14 13:00:44 +000060namespace art {
Alexandre Rames5319def2014-10-23 10:03:10 +010061
Roland Levillain22ccc3a2015-11-24 13:10:05 +000062template<class MirrorType>
63class GcRoot;
64
Alexandre Rames5319def2014-10-23 10:03:10 +010065namespace arm64 {
66
Alexandre Ramesbe919d92016-08-23 18:33:36 +010067using helpers::ARM64EncodableConstantOrRegister;
68using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069using helpers::CPURegisterFrom;
70using helpers::DRegisterFrom;
71using helpers::FPRegisterFrom;
72using helpers::HeapOperand;
73using helpers::HeapOperandFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010074using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080075using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080076using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010077using helpers::InputRegisterAt;
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +010078using helpers::Int64FromLocation;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010079using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080080using helpers::LocationFrom;
81using helpers::OperandFromMemOperand;
82using helpers::OutputCPURegister;
83using helpers::OutputFPRegister;
84using helpers::OutputRegister;
85using helpers::RegisterFrom;
86using helpers::StackOperandFrom;
87using helpers::VIXLRegCodeFromART;
88using helpers::WRegisterFrom;
89using helpers::XRegisterFrom;
90
Vladimir Markof3e0ee22015-12-17 15:23:13 +000091// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080092// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
93// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000094static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010095
Vladimir Markof4f2daa2017-03-20 18:26:59 +000096// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
97// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
Vladimir Marko008e09f32018-08-06 15:42:43 +010098// For the Baker read barrier implementation using link-time generated thunks we need to split
Vladimir Markof4f2daa2017-03-20 18:26:59 +000099// the offset explicitly.
100constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
101
Alexandre Rames5319def2014-10-23 10:03:10 +0100102inline Condition ARM64Condition(IfCondition cond) {
103 switch (cond) {
104 case kCondEQ: return eq;
105 case kCondNE: return ne;
106 case kCondLT: return lt;
107 case kCondLE: return le;
108 case kCondGT: return gt;
109 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700110 case kCondB: return lo;
111 case kCondBE: return ls;
112 case kCondA: return hi;
113 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100114 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000115 LOG(FATAL) << "Unreachable";
116 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100117}
118
Vladimir Markod6e069b2016-01-18 11:11:01 +0000119inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
120 // The ARM64 condition codes can express all the necessary branches, see the
121 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
122 // There is no dex instruction or HIR that would need the missing conditions
123 // "equal or unordered" or "not equal".
124 switch (cond) {
125 case kCondEQ: return eq;
126 case kCondNE: return ne /* unordered */;
127 case kCondLT: return gt_bias ? cc : lt /* unordered */;
128 case kCondLE: return gt_bias ? ls : le /* unordered */;
129 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
130 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
131 default:
132 LOG(FATAL) << "UNREACHABLE";
133 UNREACHABLE();
134 }
135}
136
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100137Location ARM64ReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000138 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
139 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
140 // but we use the exact registers for clarity.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100141 if (return_type == DataType::Type::kFloat32) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000142 return LocationFrom(s0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100143 } else if (return_type == DataType::Type::kFloat64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000144 return LocationFrom(d0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100145 } else if (return_type == DataType::Type::kInt64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000146 return LocationFrom(x0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100147 } else if (return_type == DataType::Type::kVoid) {
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100148 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000149 } else {
150 return LocationFrom(w0);
151 }
152}
153
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100154Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000155 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100156}
157
Vladimir Marko3232dbb2018-07-25 15:42:46 +0100158static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
159 InvokeRuntimeCallingConvention calling_convention;
160 RegisterSet caller_saves = RegisterSet::Empty();
161 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
162 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
163 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
164 DataType::Type::kReference).GetCode());
165 return caller_saves;
166}
167
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100168// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
169#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700170#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100171
Zheng Xuda403092015-04-24 17:35:39 +0800172void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800173 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Andreas Gampe3db70682018-12-26 15:12:03 -0800174 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100175 for (uint32_t i : LowToHighBits(core_spills)) {
176 // If the register holds an object, update the stack mask.
177 if (locations->RegisterContainsObject(i)) {
178 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800179 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100180 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
181 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
182 saved_core_stack_offsets_[i] = stack_offset;
183 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800184 }
185
Artem Serovc8150b52019-07-31 18:28:00 +0100186 const size_t fp_reg_size = codegen->GetSlowPathFPWidth();
Andreas Gampe3db70682018-12-26 15:12:03 -0800187 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100188 for (uint32_t i : LowToHighBits(fp_spills)) {
189 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
190 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
191 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serov9df37b92019-07-23 16:41:54 +0100192 stack_offset += fp_reg_size;
Zheng Xuda403092015-04-24 17:35:39 +0800193 }
194
Artem Serov55ab7e82020-04-27 21:02:28 +0100195 InstructionCodeGeneratorARM64* visitor =
196 down_cast<CodeGeneratorARM64*>(codegen)->GetInstructionCodeGeneratorArm64();
197 visitor->SaveLiveRegistersHelper(locations, codegen->GetFirstRegisterSlotInSlowPath());
Zheng Xuda403092015-04-24 17:35:39 +0800198}
199
200void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Artem Serov55ab7e82020-04-27 21:02:28 +0100201 InstructionCodeGeneratorARM64* visitor =
202 down_cast<CodeGeneratorARM64*>(codegen)->GetInstructionCodeGeneratorArm64();
203 visitor->RestoreLiveRegistersHelper(locations, codegen->GetFirstRegisterSlotInSlowPath());
Zheng Xuda403092015-04-24 17:35:39 +0800204}
205
Alexandre Rames5319def2014-10-23 10:03:10 +0100206class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
207 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000208 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100209
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100210 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100211 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000212 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100213
Alexandre Rames5319def2014-10-23 10:03:10 +0100214 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000215 if (instruction_->CanThrowIntoCatchBlock()) {
216 // Live registers will be restored in the catch block if caught.
217 SaveLiveRegisters(codegen, instruction_->GetLocations());
218 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000219 // We're moving two locations to locations that could overlap, so we need a parallel
220 // move resolver.
221 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100222 codegen->EmitParallelMoves(locations->InAt(0),
223 LocationFrom(calling_convention.GetRegisterAt(0)),
224 DataType::Type::kInt32,
225 locations->InAt(1),
226 LocationFrom(calling_convention.GetRegisterAt(1)),
227 DataType::Type::kInt32);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000228 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
229 ? kQuickThrowStringBounds
230 : kQuickThrowArrayBounds;
231 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100232 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800233 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100234 }
235
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100236 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100237
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100238 const char* GetDescription() const override { return "BoundsCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100239
Alexandre Rames5319def2014-10-23 10:03:10 +0100240 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100241 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
242};
243
Alexandre Rames67555f72014-11-18 10:55:16 +0000244class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
245 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000246 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000247
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100248 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000249 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
250 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000251 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800252 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000253 }
254
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100255 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100256
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100257 const char* GetDescription() const override { return "DivZeroCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100258
Alexandre Rames67555f72014-11-18 10:55:16 +0000259 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000260 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
261};
262
263class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
264 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100265 LoadClassSlowPathARM64(HLoadClass* cls, HInstruction* at)
266 : SlowPathCodeARM64(at), cls_(cls) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000267 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100268 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Alexandre Rames67555f72014-11-18 10:55:16 +0000269 }
270
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100271 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000272 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000273 Location out = locations->Out();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100274 const uint32_t dex_pc = instruction_->GetDexPc();
275 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
276 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
Alexandre Rames67555f72014-11-18 10:55:16 +0000277
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100278 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames67555f72014-11-18 10:55:16 +0000279 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000280 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000281
Vladimir Markof3c52b42017-11-17 17:32:12 +0000282 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100283 if (must_resolve_type) {
284 DCHECK(IsSameDexFile(cls_->GetDexFile(), arm64_codegen->GetGraph()->GetDexFile()));
285 dex::TypeIndex type_index = cls_->GetTypeIndex();
286 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Vladimir Marko8f63f102020-09-28 12:10:28 +0100287 if (cls_->NeedsAccessCheck()) {
288 CheckEntrypointTypes<kQuickResolveTypeAndVerifyAccess, void*, uint32_t>();
289 arm64_codegen->InvokeRuntime(kQuickResolveTypeAndVerifyAccess, instruction_, dex_pc, this);
290 } else {
291 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
292 arm64_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
293 }
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100294 // If we also must_do_clinit, the resolved type is now in the correct register.
295 } else {
296 DCHECK(must_do_clinit);
297 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
298 arm64_codegen->MoveLocation(LocationFrom(calling_convention.GetRegisterAt(0)),
299 source,
300 cls_->GetType());
301 }
302 if (must_do_clinit) {
303 arm64_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
304 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800305 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000306
307 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000308 if (out.IsValid()) {
309 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100310 DataType::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000311 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000312 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000313 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000314 __ B(GetExitLabel());
315 }
316
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100317 const char* GetDescription() const override { return "LoadClassSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100318
Alexandre Rames67555f72014-11-18 10:55:16 +0000319 private:
320 // The class this slow path will load.
321 HLoadClass* const cls_;
322
Alexandre Rames67555f72014-11-18 10:55:16 +0000323 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
324};
325
Vladimir Markoaad75c62016-10-03 08:46:48 +0000326class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
327 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000328 explicit LoadStringSlowPathARM64(HLoadString* instruction)
329 : SlowPathCodeARM64(instruction) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000330
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100331 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000332 LocationSummary* locations = instruction_->GetLocations();
333 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
334 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
335
336 __ Bind(GetEntryLabel());
337 SaveLiveRegisters(codegen, locations);
338
Vladimir Markof3c52b42017-11-17 17:32:12 +0000339 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000340 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
341 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000342 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
343 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100344 DataType::Type type = instruction_->GetType();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000345 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
346
347 RestoreLiveRegisters(codegen, locations);
348
Vladimir Markoaad75c62016-10-03 08:46:48 +0000349 __ B(GetExitLabel());
350 }
351
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100352 const char* GetDescription() const override { return "LoadStringSlowPathARM64"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000353
354 private:
355 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
356};
357
Alexandre Rames5319def2014-10-23 10:03:10 +0100358class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
359 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000360 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100361
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100362 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000363 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100364 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000365 if (instruction_->CanThrowIntoCatchBlock()) {
366 // Live registers will be restored in the catch block if caught.
367 SaveLiveRegisters(codegen, instruction_->GetLocations());
368 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000369 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
370 instruction_,
371 instruction_->GetDexPc(),
372 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800373 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100374 }
375
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100376 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100377
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100378 const char* GetDescription() const override { return "NullCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100379
Alexandre Rames5319def2014-10-23 10:03:10 +0100380 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100381 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
382};
383
384class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
385 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100386 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000387 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100388
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100389 void EmitNativeCode(CodeGenerator* codegen) override {
Artem Serov7957d952017-04-04 15:44:09 +0100390 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000391 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100392 __ Bind(GetEntryLabel());
Artem Serov1a719e42019-07-18 14:24:55 +0100393 SaveLiveRegisters(codegen, locations); // Only saves live vector regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000394 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800395 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov1a719e42019-07-18 14:24:55 +0100396 RestoreLiveRegisters(codegen, locations); // Only restores live vector regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000397 if (successor_ == nullptr) {
398 __ B(GetReturnLabel());
399 } else {
400 __ B(arm64_codegen->GetLabelOf(successor_));
401 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100402 }
403
Scott Wakeling97c72b72016-06-24 16:19:36 +0100404 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100405 DCHECK(successor_ == nullptr);
406 return &return_label_;
407 }
408
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100409 HBasicBlock* GetSuccessor() const {
410 return successor_;
411 }
412
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100413 const char* GetDescription() const override { return "SuspendCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100414
Alexandre Rames5319def2014-10-23 10:03:10 +0100415 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100416 // If not null, the block to branch to after the suspend check.
417 HBasicBlock* const successor_;
418
419 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100420 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100421
422 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
423};
424
Alexandre Rames67555f72014-11-18 10:55:16 +0000425class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
426 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000427 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000428 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000429
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100430 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000431 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800432
Alexandre Rames3e69f162014-12-10 10:36:50 +0000433 DCHECK(instruction_->IsCheckCast()
434 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
435 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100436 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000437
Alexandre Rames67555f72014-11-18 10:55:16 +0000438 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000439
Vladimir Marko87584542017-12-12 17:47:52 +0000440 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000441 SaveLiveRegisters(codegen, locations);
442 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000443
444 // We're moving two locations to locations that could overlap, so we need a parallel
445 // move resolver.
446 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800447 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800448 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100449 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800450 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800451 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100452 DataType::Type::kReference);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000453 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000454 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800455 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100456 DataType::Type ret_type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000457 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
458 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
459 } else {
460 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800461 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
462 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000463 }
464
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000465 if (!is_fatal_) {
466 RestoreLiveRegisters(codegen, locations);
467 __ B(GetExitLabel());
468 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000469 }
470
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100471 const char* GetDescription() const override { return "TypeCheckSlowPathARM64"; }
472 bool IsFatal() const override { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100473
Alexandre Rames67555f72014-11-18 10:55:16 +0000474 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000475 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000476
Alexandre Rames67555f72014-11-18 10:55:16 +0000477 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
478};
479
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700480class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
481 public:
Aart Bik42249c32016-01-07 15:33:50 -0800482 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000483 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700484
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100485 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bik42249c32016-01-07 15:33:50 -0800486 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700487 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100488 LocationSummary* locations = instruction_->GetLocations();
489 SaveLiveRegisters(codegen, locations);
490 InvokeRuntimeCallingConvention calling_convention;
491 __ Mov(calling_convention.GetRegisterAt(0),
492 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000493 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100494 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700495 }
496
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100497 const char* GetDescription() const override { return "DeoptimizationSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100498
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700499 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700500 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
501};
502
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100503class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
504 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000505 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100506
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100507 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100508 LocationSummary* locations = instruction_->GetLocations();
509 __ Bind(GetEntryLabel());
510 SaveLiveRegisters(codegen, locations);
511
512 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100513 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100514 parallel_move.AddMove(
515 locations->InAt(0),
516 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100517 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100518 nullptr);
519 parallel_move.AddMove(
520 locations->InAt(1),
521 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100522 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100523 nullptr);
524 parallel_move.AddMove(
525 locations->InAt(2),
526 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100527 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100528 nullptr);
529 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
530
531 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000532 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100533 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
534 RestoreLiveRegisters(codegen, locations);
535 __ B(GetExitLabel());
536 }
537
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100538 const char* GetDescription() const override { return "ArraySetSlowPathARM64"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100539
540 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100541 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
542};
543
Zheng Xu3927c8b2015-11-18 17:46:25 +0800544void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
545 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000546 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800547
548 // We are about to use the assembler to place literals directly. Make sure we have enough
549 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000550 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
551 num_entries * sizeof(int32_t),
552 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800553
554 __ Bind(&table_start_);
555 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
556 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100557 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800558 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100559 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800560 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
561 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
562 Literal<int32_t> literal(jump_offset);
563 __ place(&literal);
564 }
565}
566
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000567// Slow path generating a read barrier for a heap reference.
568class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
569 public:
570 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
571 Location out,
572 Location ref,
573 Location obj,
574 uint32_t offset,
575 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000576 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000577 out_(out),
578 ref_(ref),
579 obj_(obj),
580 offset_(offset),
581 index_(index) {
582 DCHECK(kEmitCompilerReadBarrier);
583 // If `obj` is equal to `out` or `ref`, it means the initial object
584 // has been overwritten by (or after) the heap object reference load
585 // to be instrumented, e.g.:
586 //
587 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000588 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000589 //
590 // In that case, we have lost the information about the original
591 // object, and the emitted read barrier cannot work properly.
592 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
593 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
594 }
595
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100596 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000597 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
598 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100599 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000600 DCHECK(locations->CanCall());
601 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100602 DCHECK(instruction_->IsInstanceFieldGet() ||
Alex Light3a73ffb2021-01-25 14:11:05 +0000603 instruction_->IsPredicatedInstanceFieldGet() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100604 instruction_->IsStaticFieldGet() ||
605 instruction_->IsArrayGet() ||
606 instruction_->IsInstanceOf() ||
607 instruction_->IsCheckCast() ||
Vladimir Markoa41ea272020-09-07 15:24:36 +0000608 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000609 << "Unexpected instruction in read barrier for heap reference slow path: "
610 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000611 // The read barrier instrumentation of object ArrayGet
612 // instructions does not support the HIntermediateAddress
613 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000614 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100615 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000616
617 __ Bind(GetEntryLabel());
618
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000619 SaveLiveRegisters(codegen, locations);
620
621 // We may have to change the index's value, but as `index_` is a
622 // constant member (like other "inputs" of this slow path),
623 // introduce a copy of it, `index`.
624 Location index = index_;
625 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100626 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000627 if (instruction_->IsArrayGet()) {
628 // Compute the actual memory offset and store it in `index`.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100629 Register index_reg = RegisterFrom(index_, DataType::Type::kInt32);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000630 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
631 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
632 // We are about to change the value of `index_reg` (see the
633 // calls to vixl::MacroAssembler::Lsl and
634 // vixl::MacroAssembler::Mov below), but it has
635 // not been saved by the previous call to
636 // art::SlowPathCode::SaveLiveRegisters, as it is a
637 // callee-save register --
638 // art::SlowPathCode::SaveLiveRegisters does not consider
639 // callee-save registers, as it has been designed with the
640 // assumption that callee-save registers are supposed to be
641 // handled by the called function. So, as a callee-save
642 // register, `index_reg` _would_ eventually be saved onto
643 // the stack, but it would be too late: we would have
644 // changed its value earlier. Therefore, we manually save
645 // it here into another freely available register,
646 // `free_reg`, chosen of course among the caller-save
647 // registers (as a callee-save `free_reg` register would
648 // exhibit the same problem).
649 //
650 // Note we could have requested a temporary register from
651 // the register allocator instead; but we prefer not to, as
652 // this is a slow path, and we know we can find a
653 // caller-save register that is available.
654 Register free_reg = FindAvailableCallerSaveRegister(codegen);
655 __ Mov(free_reg.W(), index_reg);
656 index_reg = free_reg;
657 index = LocationFrom(index_reg);
658 } else {
659 // The initial register stored in `index_` has already been
660 // saved in the call to art::SlowPathCode::SaveLiveRegisters
661 // (as it is not a callee-save register), so we can freely
662 // use it.
663 }
664 // Shifting the index value contained in `index_reg` by the scale
665 // factor (2) cannot overflow in practice, as the runtime is
666 // unable to allocate object arrays with a size larger than
667 // 2^26 - 1 (that is, 2^28 - 4 bytes).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100668 __ Lsl(index_reg, index_reg, DataType::SizeShift(type));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000669 static_assert(
670 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
671 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
672 __ Add(index_reg, index_reg, Operand(offset_));
673 } else {
Vladimir Markoa41ea272020-09-07 15:24:36 +0000674 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile/VarHandleGet
Roland Levillain3d312422016-06-23 13:53:42 +0100675 // intrinsics, `index_` is not shifted by a scale factor of 2
676 // (as in the case of ArrayGet), as it is actually an offset
677 // to an object field within an object.
678 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000679 DCHECK(instruction_->GetLocations()->Intrinsified());
Vladimir Marko2d98dc22020-10-01 11:21:37 +0000680 Intrinsics intrinsic = instruction_->AsInvoke()->GetIntrinsic();
681 DCHECK(intrinsic == Intrinsics::kUnsafeGetObject ||
682 intrinsic == Intrinsics::kUnsafeGetObjectVolatile ||
Vladimir Markoe17530a2020-11-11 17:02:26 +0000683 intrinsic == Intrinsics::kUnsafeCASObject ||
Sorin Basca2f01e8e2021-06-18 06:44:07 +0000684 intrinsic == Intrinsics::kJdkUnsafeGetObject ||
685 intrinsic == Intrinsics::kJdkUnsafeGetObjectVolatile ||
Sorin Basca4a4696a2021-10-09 07:14:40 +0000686 intrinsic == Intrinsics::kJdkUnsafeGetObjectAcquire ||
Sorin Basca2f01e8e2021-06-18 06:44:07 +0000687 intrinsic == Intrinsics::kJdkUnsafeCASObject ||
Vladimir Marko2d98dc22020-10-01 11:21:37 +0000688 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
Vladimir Marko1bff99f2020-11-02 15:07:33 +0000689 mirror::VarHandle::AccessModeTemplate::kGet ||
690 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
691 mirror::VarHandle::AccessModeTemplate::kCompareAndSet ||
692 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
Vladimir Marko32c2eb82020-11-10 16:58:47 +0000693 mirror::VarHandle::AccessModeTemplate::kCompareAndExchange ||
694 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
695 mirror::VarHandle::AccessModeTemplate::kGetAndUpdate)
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000696 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100697 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100698 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000699 }
700 }
701
702 // We're moving two or three locations to locations that could
703 // overlap, so we need a parallel move resolver.
704 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100705 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000706 parallel_move.AddMove(ref_,
707 LocationFrom(calling_convention.GetRegisterAt(0)),
708 type,
709 nullptr);
710 parallel_move.AddMove(obj_,
711 LocationFrom(calling_convention.GetRegisterAt(1)),
712 type,
713 nullptr);
714 if (index.IsValid()) {
715 parallel_move.AddMove(index,
716 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100717 DataType::Type::kInt32,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000718 nullptr);
719 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
720 } else {
721 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
722 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
723 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000724 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000725 instruction_,
726 instruction_->GetDexPc(),
727 this);
728 CheckEntrypointTypes<
729 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
730 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
731
732 RestoreLiveRegisters(codegen, locations);
733
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000734 __ B(GetExitLabel());
735 }
736
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100737 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000738
739 private:
740 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100741 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
742 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000743 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
744 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
745 return Register(VIXLRegCodeFromART(i), kXRegSize);
746 }
747 }
748 // We shall never fail to find a free caller-save register, as
749 // there are more than two core caller-save registers on ARM64
750 // (meaning it is possible to find one which is different from
751 // `ref` and `obj`).
752 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
753 LOG(FATAL) << "Could not find a free register";
754 UNREACHABLE();
755 }
756
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000757 const Location out_;
758 const Location ref_;
759 const Location obj_;
760 const uint32_t offset_;
761 // An additional location containing an index to an array.
762 // Only used for HArrayGet and the UnsafeGetObject &
763 // UnsafeGetObjectVolatile intrinsics.
764 const Location index_;
765
766 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
767};
768
769// Slow path generating a read barrier for a GC root.
770class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
771 public:
772 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000773 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000774 DCHECK(kEmitCompilerReadBarrier);
775 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000776
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100777 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000778 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100779 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000780 DCHECK(locations->CanCall());
781 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Vladimir Markoa41ea272020-09-07 15:24:36 +0000782 DCHECK(instruction_->IsLoadClass() ||
783 instruction_->IsLoadString() ||
784 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000785 << "Unexpected instruction in read barrier for GC root slow path: "
786 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000787
788 __ Bind(GetEntryLabel());
789 SaveLiveRegisters(codegen, locations);
790
791 InvokeRuntimeCallingConvention calling_convention;
792 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
793 // The argument of the ReadBarrierForRootSlow is not a managed
794 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
795 // thus we need a 64-bit move here, and we cannot use
796 //
797 // arm64_codegen->MoveLocation(
798 // LocationFrom(calling_convention.GetRegisterAt(0)),
799 // root_,
800 // type);
801 //
802 // which would emit a 32-bit move, as `type` is a (32-bit wide)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100803 // reference type (`DataType::Type::kReference`).
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000804 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000805 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000806 instruction_,
807 instruction_->GetDexPc(),
808 this);
809 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
810 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
811
812 RestoreLiveRegisters(codegen, locations);
813 __ B(GetExitLabel());
814 }
815
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100816 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000817
818 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000819 const Location out_;
820 const Location root_;
821
822 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
823};
824
Mythri Alle5097f832021-11-02 14:52:30 +0000825class MethodEntryExitHooksSlowPathARM64 : public SlowPathCodeARM64 {
826 public:
827 explicit MethodEntryExitHooksSlowPathARM64(HInstruction* instruction)
828 : SlowPathCodeARM64(instruction) {}
829
830 void EmitNativeCode(CodeGenerator* codegen) override {
831 LocationSummary* locations = instruction_->GetLocations();
832 QuickEntrypointEnum entry_point =
833 (instruction_->IsMethodEntryHook()) ? kQuickMethodEntryHook : kQuickMethodExitHook;
834 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
835 __ Bind(GetEntryLabel());
836 SaveLiveRegisters(codegen, locations);
837 arm64_codegen->InvokeRuntime(entry_point, instruction_, instruction_->GetDexPc(), this);
838 RestoreLiveRegisters(codegen, locations);
839 __ B(GetExitLabel());
840 }
841
842 const char* GetDescription() const override {
843 return "MethodEntryExitHooksSlowPath";
844 }
845
846 private:
847 DISALLOW_COPY_AND_ASSIGN(MethodEntryExitHooksSlowPathARM64);
848};
849
Alexandre Rames5319def2014-10-23 10:03:10 +0100850#undef __
851
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100852Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100853 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100854 if (type == DataType::Type::kVoid) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100855 LOG(FATAL) << "Unreachable type " << type;
856 }
857
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100858 if (DataType::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100859 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
860 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100861 } else if (!DataType::IsFloatingPointType(type) &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000862 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000863 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
864 } else {
865 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100866 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
867 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100868 }
869
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000870 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100871 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100872 return next_location;
873}
874
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100875Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100876 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100877}
878
Vladimir Marko86c87522020-05-11 16:55:55 +0100879Location CriticalNativeCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
880 DCHECK_NE(type, DataType::Type::kReference);
881
882 Location location = Location::NoLocation();
883 if (DataType::IsFloatingPointType(type)) {
884 if (fpr_index_ < kParameterFPRegistersLength) {
885 location = LocationFrom(kParameterFPRegisters[fpr_index_]);
886 ++fpr_index_;
887 }
888 } else {
889 // Native ABI uses the same registers as managed, except that the method register x0
890 // is a normal argument.
891 if (gpr_index_ < 1u + kParameterCoreRegistersLength) {
892 location = LocationFrom(gpr_index_ == 0u ? x0 : kParameterCoreRegisters[gpr_index_ - 1u]);
893 ++gpr_index_;
894 }
895 }
896 if (location.IsInvalid()) {
897 if (DataType::Is64BitType(type)) {
898 location = Location::DoubleStackSlot(stack_offset_);
899 } else {
900 location = Location::StackSlot(stack_offset_);
901 }
902 stack_offset_ += kFramePointerSize;
903
904 if (for_register_allocation_) {
905 location = Location::Any();
906 }
907 }
908 return location;
909}
910
911Location CriticalNativeCallingConventionVisitorARM64::GetReturnLocation(DataType::Type type) const {
912 // We perform conversion to the managed ABI return register after the call if needed.
913 InvokeDexCallingConventionVisitorARM64 dex_calling_convention;
914 return dex_calling_convention.GetReturnLocation(type);
915}
916
917Location CriticalNativeCallingConventionVisitorARM64::GetMethodLocation() const {
918 // Pass the method in the hidden argument x15.
919 return Location::RegisterLocation(x15.GetCode());
920}
921
Serban Constantinescu579885a2015-02-22 20:51:33 +0000922CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100923 const CompilerOptions& compiler_options,
924 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100925 : CodeGenerator(graph,
926 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000927 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000928 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100929 callee_saved_core_registers.GetList(),
930 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100931 compiler_options,
932 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100933 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
934 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov1a719e42019-07-18 14:24:55 +0100935 location_builder_neon_(graph, this),
936 instruction_visitor_neon_(graph, this),
937 location_builder_sve_(graph, this),
938 instruction_visitor_sve_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100939 move_resolver_(graph->GetAllocator(), this),
Artem Serovaa6f4832018-11-21 18:57:54 +0000940 assembler_(graph->GetAllocator(),
941 compiler_options.GetInstructionSetFeatures()->AsArm64InstructionSetFeatures()),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000942 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100943 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000944 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100945 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko8f63f102020-09-28 12:10:28 +0100946 public_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
947 package_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000948 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100949 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeb9eb002020-10-02 13:54:19 +0100950 boot_image_jni_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +0100951 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100952 call_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100953 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100954 uint32_literals_(std::less<uint32_t>(),
955 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
956 uint64_literals_(std::less<uint64_t>(),
957 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000958 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100959 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000960 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +0000961 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
962 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
963 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000964 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000965 AddAllocatedRegister(LocationFrom(lr));
Artem Serov1a719e42019-07-18 14:24:55 +0100966
967 bool use_sve = ShouldUseSVE();
968 if (use_sve) {
969 location_builder_ = &location_builder_sve_;
970 instruction_visitor_ = &instruction_visitor_sve_;
971 } else {
972 location_builder_ = &location_builder_neon_;
973 instruction_visitor_ = &instruction_visitor_neon_;
974 }
975}
976
977bool CodeGeneratorARM64::ShouldUseSVE() const {
Artem Serov8ba4de12019-12-04 21:10:23 +0000978 return GetInstructionSetFeatures().HasSVE();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000979}
Alexandre Rames5319def2014-10-23 10:03:10 +0100980
Artem Serov55ab7e82020-04-27 21:02:28 +0100981size_t CodeGeneratorARM64::GetSIMDRegisterWidth() const {
982 return SupportsPredicatedSIMD()
983 ? GetInstructionSetFeatures().GetSVEVectorLength() / kBitsPerByte
984 : vixl::aarch64::kQRegSizeInBytes;
985}
986
Alexandre Rames67555f72014-11-18 10:55:16 +0000987#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100988
Zheng Xu3927c8b2015-11-18 17:46:25 +0800989void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100990 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800991 jump_table->EmitTable(this);
992 }
993}
994
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000995void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800996 EmitJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +0000997
998 // Emit JIT baker read barrier slow paths.
Vladimir Marko695348f2020-05-19 14:42:02 +0100999 DCHECK(GetCompilerOptions().IsJitCompiler() || jit_baker_read_barrier_slow_paths_.empty());
Vladimir Marko966b46f2018-08-03 10:20:19 +00001000 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
1001 uint32_t encoded_data = entry.first;
1002 vixl::aarch64::Label* slow_path_entry = &entry.second.label;
1003 __ Bind(slow_path_entry);
Andreas Gampe3db70682018-12-26 15:12:03 -08001004 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name= */ nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00001005 }
1006
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001007 // Ensure we emit the literal pool.
1008 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001009
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001010 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001011
1012 // Verify Baker read barrier linker patches.
1013 if (kIsDebugBuild) {
1014 ArrayRef<const uint8_t> code = allocator->GetMemory();
1015 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
1016 DCHECK(info.label.IsBound());
1017 uint32_t literal_offset = info.label.GetLocation();
1018 DCHECK_ALIGNED(literal_offset, 4u);
1019
1020 auto GetInsn = [&code](uint32_t offset) {
1021 DCHECK_ALIGNED(offset, 4u);
1022 return
1023 (static_cast<uint32_t>(code[offset + 0]) << 0) +
1024 (static_cast<uint32_t>(code[offset + 1]) << 8) +
1025 (static_cast<uint32_t>(code[offset + 2]) << 16)+
1026 (static_cast<uint32_t>(code[offset + 3]) << 24);
1027 };
1028
1029 const uint32_t encoded_data = info.custom_data;
1030 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
1031 // Check that the next instruction matches the expected LDR.
1032 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01001033 case BakerReadBarrierKind::kField:
1034 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00001035 DCHECK_GE(code.size() - literal_offset, 8u);
1036 uint32_t next_insn = GetInsn(literal_offset + 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001037 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1038 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko0ecac682018-08-07 10:40:38 +01001039 if (kind == BakerReadBarrierKind::kField) {
1040 // LDR (immediate) with correct base_reg.
1041 CHECK_EQ(next_insn & 0xffc003e0u, 0xb9400000u | (base_reg << 5));
1042 } else {
1043 DCHECK(kind == BakerReadBarrierKind::kAcquire);
1044 // LDAR with correct base_reg.
1045 CHECK_EQ(next_insn & 0xffffffe0u, 0x88dffc00u | (base_reg << 5));
1046 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001047 break;
1048 }
1049 case BakerReadBarrierKind::kArray: {
1050 DCHECK_GE(code.size() - literal_offset, 8u);
1051 uint32_t next_insn = GetInsn(literal_offset + 4u);
1052 // LDR (register) with the correct base_reg, size=10 (32-bit), option=011 (extend = LSL),
1053 // and S=1 (shift amount = 2 for 32-bit version), i.e. LDR Wt, [Xn, Xm, LSL #2].
1054 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1055 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1056 CHECK_EQ(next_insn & 0xffe0ffe0u, 0xb8607800u | (base_reg << 5));
1057 CheckValidReg((next_insn >> 16) & 0x1f); // Check index register
1058 break;
1059 }
1060 case BakerReadBarrierKind::kGcRoot: {
1061 DCHECK_GE(literal_offset, 4u);
1062 uint32_t prev_insn = GetInsn(literal_offset - 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001063 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko94796f82018-08-08 15:15:33 +01001064 // Usually LDR (immediate) with correct root_reg but
Vladimir Markoc8178f52020-11-24 10:38:16 +00001065 // we may have a "MOV marked, old_value" for intrinsic CAS.
Vladimir Marko94796f82018-08-08 15:15:33 +01001066 if ((prev_insn & 0xffe0ffff) != (0x2a0003e0 | root_reg)) { // MOV?
1067 CHECK_EQ(prev_insn & 0xffc0001fu, 0xb9400000u | root_reg); // LDR?
1068 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001069 break;
1070 }
1071 default:
1072 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
1073 UNREACHABLE();
1074 }
1075 }
1076 }
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001077}
1078
Zheng Xuad4450e2015-04-17 18:48:56 +08001079void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1080 // Note: There are 6 kinds of moves:
1081 // 1. constant -> GPR/FPR (non-cycle)
1082 // 2. constant -> stack (non-cycle)
1083 // 3. GPR/FPR -> GPR/FPR
1084 // 4. GPR/FPR -> stack
1085 // 5. stack -> GPR/FPR
1086 // 6. stack -> stack (non-cycle)
1087 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1088 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1089 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1090 // dependency.
1091 vixl_temps_.Open(GetVIXLAssembler());
1092}
1093
1094void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1095 vixl_temps_.Close();
1096}
1097
1098Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001099 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1100 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1101 || kind == Location::kSIMDStackSlot);
1102 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1103 ? Location::kFpuRegister
1104 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001105 Location scratch = GetScratchLocation(kind);
1106 if (!scratch.Equals(Location::NoLocation())) {
1107 return scratch;
1108 }
1109 // Allocate from VIXL temp registers.
1110 if (kind == Location::kRegister) {
1111 scratch = LocationFrom(vixl_temps_.AcquireX());
1112 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001113 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serov1a719e42019-07-18 14:24:55 +01001114 scratch = codegen_->GetGraph()->HasSIMD()
1115 ? codegen_->GetInstructionCodeGeneratorArm64()->AllocateSIMDScratchLocation(&vixl_temps_)
1116 : LocationFrom(vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001117 }
1118 AddScratchLocation(scratch);
1119 return scratch;
1120}
1121
1122void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1123 if (loc.IsRegister()) {
1124 vixl_temps_.Release(XRegisterFrom(loc));
1125 } else {
1126 DCHECK(loc.IsFpuRegister());
Artem Serov1a719e42019-07-18 14:24:55 +01001127 if (codegen_->GetGraph()->HasSIMD()) {
1128 codegen_->GetInstructionCodeGeneratorArm64()->FreeSIMDScratchLocation(loc, &vixl_temps_);
1129 } else {
1130 vixl_temps_.Release(DRegisterFrom(loc));
1131 }
Zheng Xuad4450e2015-04-17 18:48:56 +08001132 }
1133 RemoveScratchLocation(loc);
1134}
1135
Alexandre Rames3e69f162014-12-10 10:36:50 +00001136void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001137 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001138 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001139}
1140
Mythri Alle5097f832021-11-02 14:52:30 +00001141void LocationsBuilderARM64::VisitMethodExitHook(HMethodExitHook* method_hook) {
1142 LocationSummary* locations = new (GetGraph()->GetAllocator())
1143 LocationSummary(method_hook, LocationSummary::kCallOnSlowPath);
1144 DataType::Type return_type = method_hook->InputAt(0)->GetType();
1145 locations->SetInAt(0, ARM64ReturnLocation(return_type));
1146}
1147
1148void InstructionCodeGeneratorARM64::GenerateMethodEntryExitHook(HInstruction* instruction) {
1149 MacroAssembler* masm = GetVIXLAssembler();
1150 UseScratchRegisterScope temps(masm);
1151 Register temp = temps.AcquireX();
1152 Register value = temps.AcquireW();
1153
1154 SlowPathCodeARM64* slow_path =
1155 new (codegen_->GetScopedAllocator()) MethodEntryExitHooksSlowPathARM64(instruction);
1156 codegen_->AddSlowPath(slow_path);
1157
1158 uint64_t address = reinterpret_cast64<uint64_t>(Runtime::Current()->GetInstrumentation());
1159 int offset = instrumentation::Instrumentation::NeedsEntryExitHooksOffset().Int32Value();
1160 __ Mov(temp, address + offset);
1161 __ Ldrh(value, MemOperand(temp, 0));
1162 __ Cbnz(value, slow_path->GetEntryLabel());
1163 __ Bind(slow_path->GetExitLabel());
1164}
1165
1166void InstructionCodeGeneratorARM64::VisitMethodExitHook(HMethodExitHook* instruction) {
1167 DCHECK(codegen_->GetCompilerOptions().IsJitCompiler() && GetGraph()->IsDebuggable());
1168 DCHECK(codegen_->RequiresCurrentMethod());
1169 GenerateMethodEntryExitHook(instruction);
1170}
1171
1172void LocationsBuilderARM64::VisitMethodEntryHook(HMethodEntryHook* method_hook) {
1173 new (GetGraph()->GetAllocator()) LocationSummary(method_hook, LocationSummary::kCallOnSlowPath);
1174}
1175
1176void InstructionCodeGeneratorARM64::VisitMethodEntryHook(HMethodEntryHook* instruction) {
1177 DCHECK(codegen_->GetCompilerOptions().IsJitCompiler() && GetGraph()->IsDebuggable());
1178 DCHECK(codegen_->RequiresCurrentMethod());
1179 GenerateMethodEntryExitHook(instruction);
1180}
1181
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001182void CodeGeneratorARM64::MaybeIncrementHotness(bool is_frame_entry) {
1183 MacroAssembler* masm = GetVIXLAssembler();
1184 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1185 UseScratchRegisterScope temps(masm);
1186 Register counter = temps.AcquireX();
1187 Register method = is_frame_entry ? kArtMethodRegister : temps.AcquireX();
1188 if (!is_frame_entry) {
1189 __ Ldr(method, MemOperand(sp, 0));
1190 }
1191 __ Ldrh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
Vladimir Markoce131fe2021-10-26 20:03:35 +00001192 __ Add(counter, counter, 1);
1193 // Subtract one if the counter would overflow.
1194 __ Sub(counter, counter, Operand(counter, LSR, 16));
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001195 __ Strh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
1196 }
1197
1198 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffray095dc462020-08-17 16:40:28 +01001199 ScopedProfilingInfoUse spiu(
1200 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
1201 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001202 if (info != nullptr) {
Nicolas Geoffrayc1cd1332020-01-25 13:08:24 +00001203 uint64_t address = reinterpret_cast64<uint64_t>(info);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001204 vixl::aarch64::Label done;
1205 UseScratchRegisterScope temps(masm);
1206 Register temp = temps.AcquireX();
1207 Register counter = temps.AcquireW();
1208 __ Mov(temp, address);
1209 __ Ldrh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
1210 __ Add(counter, counter, 1);
Nicolas Geoffray4313ccb2020-08-26 17:01:15 +01001211 __ And(counter, counter, interpreter::kTieredHotnessMask);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001212 __ Strh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
Nicolas Geoffray4313ccb2020-08-26 17:01:15 +01001213 __ Cbnz(counter, &done);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001214 if (is_frame_entry) {
1215 if (HasEmptyFrame()) {
Vladimir Markodec78172020-06-19 15:31:23 +01001216 // The entrypoint expects the method at the bottom of the stack. We
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001217 // claim stack space necessary for alignment.
Vladimir Markodec78172020-06-19 15:31:23 +01001218 IncreaseFrame(kStackAlignment);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001219 __ Stp(kArtMethodRegister, lr, MemOperand(sp, 0));
1220 } else if (!RequiresCurrentMethod()) {
1221 __ Str(kArtMethodRegister, MemOperand(sp, 0));
1222 }
1223 } else {
1224 CHECK(RequiresCurrentMethod());
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001225 }
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001226 uint32_t entrypoint_offset =
1227 GetThreadOffset<kArm64PointerSize>(kQuickCompileOptimized).Int32Value();
1228 __ Ldr(lr, MemOperand(tr, entrypoint_offset));
1229 // Note: we don't record the call here (and therefore don't generate a stack
1230 // map), as the entrypoint should never be suspended.
1231 __ Blr(lr);
1232 if (HasEmptyFrame()) {
1233 CHECK(is_frame_entry);
1234 __ Ldr(lr, MemOperand(sp, 8));
Vladimir Markodec78172020-06-19 15:31:23 +01001235 DecreaseFrame(kStackAlignment);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001236 }
1237 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001238 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001239 }
1240}
1241
Alexandre Rames5319def2014-10-23 10:03:10 +01001242void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001243 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001244 __ Bind(&frame_entry_label_);
1245
Vladimir Marko33bff252017-11-01 14:35:42 +00001246 bool do_overflow_check =
1247 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001248 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001249 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001250 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001251 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001252 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001253 {
1254 // Ensure that between load and RecordPcInfo there are no pools emitted.
1255 ExactAssemblyScope eas(GetVIXLAssembler(),
1256 kInstructionSize,
1257 CodeBufferCheckScope::kExactSize);
1258 __ ldr(wzr, MemOperand(temp, 0));
1259 RecordPcInfo(nullptr, 0);
1260 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001261 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001262
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001263 if (!HasEmptyFrame()) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001264 // Stack layout:
1265 // sp[frame_size - 8] : lr.
1266 // ... : other preserved core registers.
1267 // ... : other preserved fp registers.
1268 // ... : reserved frame space.
1269 // sp[0] : current method.
Vladimir Marko1a225a72019-07-05 13:37:42 +01001270 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1271 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1272 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1273 DCHECK(!preserved_core_registers.IsEmpty());
1274 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1275 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001276
Vladimir Marko1a225a72019-07-05 13:37:42 +01001277 // Save the current method if we need it, or if using STP reduces code
1278 // size. Note that we do not do this in HCurrentMethod, as the
1279 // instruction might have been removed in the SSA graph.
1280 CPURegister lowest_spill;
1281 if (core_spills_offset == kXRegSizeInBytes) {
1282 // If there is no gap between the method and the lowest core spill, use
1283 // aligned STP pre-index to store both. Max difference is 512. We do
1284 // that to reduce code size even if we do not have to save the method.
1285 DCHECK_LE(frame_size, 512); // 32 core registers are only 256 bytes.
1286 lowest_spill = preserved_core_registers.PopLowestIndex();
1287 __ Stp(kArtMethodRegister, lowest_spill, MemOperand(sp, -frame_size, PreIndex));
1288 } else if (RequiresCurrentMethod()) {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001289 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001290 } else {
1291 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001292 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001293 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Vladimir Marko1a225a72019-07-05 13:37:42 +01001294 if (lowest_spill.IsValid()) {
1295 GetAssembler()->cfi().RelOffset(DWARFReg(lowest_spill), core_spills_offset);
1296 core_spills_offset += kXRegSizeInBytes;
1297 }
1298 GetAssembler()->SpillRegisters(preserved_core_registers, core_spills_offset);
1299 GetAssembler()->SpillRegisters(preserved_fp_registers, fp_spills_offset);
Mingyao Yang063fc772016-08-02 11:02:54 -07001300
1301 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1302 // Initialize should_deoptimize flag to 0.
1303 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1304 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1305 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001306 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001307 MaybeIncrementHotness(/* is_frame_entry= */ true);
Andreas Gampe3db70682018-12-26 15:12:03 -08001308 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001309}
1310
1311void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001312 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001313 if (!HasEmptyFrame()) {
Vladimir Marko1a225a72019-07-05 13:37:42 +01001314 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1315 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1316 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1317 DCHECK(!preserved_core_registers.IsEmpty());
1318 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1319 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
1320
1321 CPURegister lowest_spill;
1322 if (core_spills_offset == kXRegSizeInBytes) {
1323 // If there is no gap between the method and the lowest core spill, use
1324 // aligned LDP pre-index to pop both. Max difference is 504. We do
1325 // that to reduce code size even though the loaded method is unused.
1326 DCHECK_LE(frame_size, 504); // 32 core registers are only 256 bytes.
1327 lowest_spill = preserved_core_registers.PopLowestIndex();
1328 core_spills_offset += kXRegSizeInBytes;
1329 }
1330 GetAssembler()->UnspillRegisters(preserved_fp_registers, fp_spills_offset);
1331 GetAssembler()->UnspillRegisters(preserved_core_registers, core_spills_offset);
1332 if (lowest_spill.IsValid()) {
1333 __ Ldp(xzr, lowest_spill, MemOperand(sp, frame_size, PostIndex));
1334 GetAssembler()->cfi().Restore(DWARFReg(lowest_spill));
1335 } else {
1336 __ Drop(frame_size);
1337 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001338 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001339 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001340 __ Ret();
1341 GetAssembler()->cfi().RestoreState();
1342 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001343}
1344
Scott Wakeling97c72b72016-06-24 16:19:36 +01001345CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001346 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001347 return CPURegList(CPURegister::kRegister, kXRegSize,
1348 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001349}
1350
Scott Wakeling97c72b72016-06-24 16:19:36 +01001351CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001352 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1353 GetNumberOfFloatingPointRegisters()));
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001354 return CPURegList(CPURegister::kVRegister, kDRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001355 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001356}
1357
Alexandre Rames5319def2014-10-23 10:03:10 +01001358void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1359 __ Bind(GetLabelOf(block));
1360}
1361
Calin Juravle175dc732015-08-25 15:42:32 +01001362void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1363 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001364 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001365}
1366
Calin Juravlee460d1d2015-09-29 04:52:17 +01001367void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1368 if (location.IsRegister()) {
1369 locations->AddTemp(location);
1370 } else {
1371 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1372 }
1373}
1374
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001375void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001376 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001377 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001378 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001379 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001380 if (value_can_be_null) {
1381 __ Cbz(value, &done);
1382 }
Roland Levillainc73f0522018-08-14 15:16:50 +01001383 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07001384 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01001385 // Calculate the offset (in the card table) of the card corresponding to
1386 // `object`.
Alexandre Rames5319def2014-10-23 10:03:10 +01001387 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Roland Levillainc73f0522018-08-14 15:16:50 +01001388 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
1389 // `object`'s card.
1390 //
1391 // Register `card` contains the address of the card table. Note that the card
1392 // table's base is biased during its creation so that it always starts at an
1393 // address whose least-significant byte is equal to `kCardDirty` (see
1394 // art::gc::accounting::CardTable::Create). Therefore the STRB instruction
1395 // below writes the `kCardDirty` (byte) value into the `object`'s card
1396 // (located at `card + object >> kCardShift`).
1397 //
1398 // This dual use of the value in register `card` (1. to calculate the location
1399 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
1400 // (no need to explicitly load `kCardDirty` as an immediate value).
Serban Constantinescu02164b32014-11-13 14:05:07 +00001401 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001402 if (value_can_be_null) {
1403 __ Bind(&done);
1404 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001405}
1406
David Brazdil58282f42016-01-14 12:45:10 +00001407void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001408 // Blocked core registers:
1409 // lr : Runtime reserved.
1410 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001411 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001412 // ip1 : VIXL core temp.
1413 // ip0 : VIXL core temp.
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001414 // x18 : Platform register.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001415 //
1416 // Blocked fp registers:
1417 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001418 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1419 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001420 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001421 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001422 }
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001423 blocked_core_registers_[X18] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001424
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001425 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001426 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001427 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001428 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001429
David Brazdil58282f42016-01-14 12:45:10 +00001430 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001431 // Stubs do not save callee-save floating point registers. If the graph
1432 // is debuggable, we need to deal with these registers differently. For
1433 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001434 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1435 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001436 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001437 }
1438 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001439}
1440
Alexandre Rames3e69f162014-12-10 10:36:50 +00001441size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1442 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1443 __ Str(reg, MemOperand(sp, stack_index));
1444 return kArm64WordSize;
1445}
1446
1447size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1448 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1449 __ Ldr(reg, MemOperand(sp, stack_index));
1450 return kArm64WordSize;
1451}
1452
Artem Serov9df37b92019-07-23 16:41:54 +01001453size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1454 uint32_t reg_id ATTRIBUTE_UNUSED) {
1455 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1456 << "use SaveRestoreLiveRegistersHelper";
1457 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001458}
1459
Artem Serov9df37b92019-07-23 16:41:54 +01001460size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1461 uint32_t reg_id ATTRIBUTE_UNUSED) {
1462 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1463 << "use SaveRestoreLiveRegistersHelper";
1464 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001465}
1466
Alexandre Rames5319def2014-10-23 10:03:10 +01001467void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001468 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001469}
1470
1471void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001472 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001473}
1474
Vladimir Markoa0431112018-06-25 09:32:54 +01001475const Arm64InstructionSetFeatures& CodeGeneratorARM64::GetInstructionSetFeatures() const {
1476 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArm64InstructionSetFeatures();
1477}
1478
Alexandre Rames67555f72014-11-18 10:55:16 +00001479void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001480 if (constant->IsIntConstant()) {
1481 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1482 } else if (constant->IsLongConstant()) {
1483 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1484 } else if (constant->IsNullConstant()) {
1485 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001486 } else if (constant->IsFloatConstant()) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001487 __ Fmov(VRegister(destination), constant->AsFloatConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001488 } else {
1489 DCHECK(constant->IsDoubleConstant());
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001490 __ Fmov(VRegister(destination), constant->AsDoubleConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001491 }
1492}
1493
Alexandre Rames3e69f162014-12-10 10:36:50 +00001494
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001495static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001496 DCHECK(constant.IsConstant());
1497 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001498 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001499 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001500 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1501 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1502 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1503 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001504}
1505
Roland Levillain952b2352017-05-03 19:49:14 +01001506// Allocate a scratch register from the VIXL pool, querying first
1507// the floating-point register pool, and then the core register
1508// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001509// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1510// using a different allocation strategy.
1511static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1512 vixl::aarch64::UseScratchRegisterScope* temps,
1513 int size_in_bits) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001514 return masm->GetScratchVRegisterList()->IsEmpty()
Roland Levillain558dea12017-01-27 19:40:44 +00001515 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1516 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1517}
1518
Calin Juravlee460d1d2015-09-29 04:52:17 +01001519void CodeGeneratorARM64::MoveLocation(Location destination,
1520 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001521 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001522 if (source.Equals(destination)) {
1523 return;
1524 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001525
1526 // A valid move can always be inferred from the destination and source
1527 // locations. When moving from and to a register, the argument type can be
1528 // used to generate 32bit instead of 64bit moves. In debug mode we also
1529 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001530 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001531
1532 if (destination.IsRegister() || destination.IsFpuRegister()) {
1533 if (unspecified_type) {
1534 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1535 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001536 (src_cst != nullptr && (src_cst->IsIntConstant()
1537 || src_cst->IsFloatConstant()
1538 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001539 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001540 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001541 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001542 // If the source is a double stack slot or a 64bit constant, a 64bit
1543 // type is appropriate. Else the source is a register, and since the
1544 // type has not been specified, we chose a 64bit type to force a 64bit
1545 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001546 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001547 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001548 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001549 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1550 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001551 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001552 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1553 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1554 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001555 } else if (source.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001556 GetInstructionCodeGeneratorArm64()->LoadSIMDRegFromStack(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001557 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001558 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001559 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001560 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001561 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001562 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001563 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001564 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001565 DataType::Type source_type = DataType::Is64BitType(dst_type)
1566 ? DataType::Type::kInt64
1567 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001568 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1569 }
1570 } else {
1571 DCHECK(source.IsFpuRegister());
1572 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001573 DataType::Type source_type = DataType::Is64BitType(dst_type)
1574 ? DataType::Type::kFloat64
1575 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001576 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1577 } else {
1578 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001579 if (GetGraph()->HasSIMD()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001580 GetInstructionCodeGeneratorArm64()->MoveSIMDRegToSIMDReg(destination, source);
Artem Serovd4bccf12017-04-03 18:47:32 +01001581 } else {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001582 __ Fmov(VRegister(dst), FPRegisterFrom(source, dst_type));
Artem Serovd4bccf12017-04-03 18:47:32 +01001583 }
1584 }
1585 }
1586 } else if (destination.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001587 GetInstructionCodeGeneratorArm64()->MoveToSIMDStackSlot(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001588 } else { // The destination is not a register. It must be a stack slot.
1589 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1590 if (source.IsRegister() || source.IsFpuRegister()) {
1591 if (unspecified_type) {
1592 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001593 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001594 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001595 dst_type =
1596 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001597 }
1598 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001599 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1600 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001601 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001602 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001603 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1604 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001605 UseScratchRegisterScope temps(GetVIXLAssembler());
1606 HConstant* src_cst = source.GetConstant();
1607 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001608 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001609 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1610 ? Register(xzr)
1611 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001612 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001613 if (src_cst->IsIntConstant()) {
1614 temp = temps.AcquireW();
1615 } else if (src_cst->IsLongConstant()) {
1616 temp = temps.AcquireX();
1617 } else if (src_cst->IsFloatConstant()) {
1618 temp = temps.AcquireS();
1619 } else {
1620 DCHECK(src_cst->IsDoubleConstant());
1621 temp = temps.AcquireD();
1622 }
1623 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001624 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001625 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001626 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001627 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001628 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001629 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001630 // Use any scratch register (a core or a floating-point one)
1631 // from VIXL scratch register pools as a temporary.
1632 //
1633 // We used to only use the FP scratch register pool, but in some
1634 // rare cases the only register from this pool (D31) would
1635 // already be used (e.g. within a ParallelMove instruction, when
1636 // a move is blocked by a another move requiring a scratch FP
1637 // register, which would reserve D31). To prevent this issue, we
1638 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001639 //
1640 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001641 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001642 // use AcquireFPOrCoreCPURegisterOfSize instead of
1643 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1644 // allocates core scratch registers first.
1645 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1646 GetVIXLAssembler(),
1647 &temps,
1648 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001649 __ Ldr(temp, StackOperandFrom(source));
1650 __ Str(temp, StackOperandFrom(destination));
1651 }
1652 }
1653}
1654
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001655void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001656 CPURegister dst,
1657 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001658 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001659 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001660 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001661 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001662 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001663 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001664 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001665 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001666 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001667 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001668 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001669 case DataType::Type::kInt16:
1670 __ Ldrsh(Register(dst), src);
1671 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001672 case DataType::Type::kInt32:
1673 case DataType::Type::kReference:
1674 case DataType::Type::kInt64:
1675 case DataType::Type::kFloat32:
1676 case DataType::Type::kFloat64:
1677 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001678 __ Ldr(dst, src);
1679 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001680 case DataType::Type::kUint32:
1681 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001682 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001683 LOG(FATAL) << "Unreachable type " << type;
1684 }
1685}
1686
Calin Juravle77520bc2015-01-12 18:45:46 +00001687void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Vladimir Marko98873af2020-12-16 12:10:03 +00001688 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001689 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001690 const MemOperand& src,
1691 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001692 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001693 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001694 Register temp_base = temps.AcquireX();
1695
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001696 DCHECK(!src.IsPreIndex());
1697 DCHECK(!src.IsPostIndex());
1698
1699 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001700 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001701 {
1702 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1703 MemOperand base = MemOperand(temp_base);
1704 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001705 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001706 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001707 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001708 {
1709 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1710 __ ldarb(Register(dst), base);
1711 if (needs_null_check) {
1712 MaybeRecordImplicitNullCheck(instruction);
1713 }
1714 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001715 if (type == DataType::Type::kInt8) {
1716 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001717 }
1718 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001719 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001720 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001721 {
1722 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1723 __ ldarh(Register(dst), base);
1724 if (needs_null_check) {
1725 MaybeRecordImplicitNullCheck(instruction);
1726 }
1727 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001728 if (type == DataType::Type::kInt16) {
1729 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
1730 }
Artem Serov914d7a82017-02-07 14:33:49 +00001731 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001732 case DataType::Type::kInt32:
1733 case DataType::Type::kReference:
1734 case DataType::Type::kInt64:
1735 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001736 {
1737 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1738 __ ldar(Register(dst), base);
1739 if (needs_null_check) {
1740 MaybeRecordImplicitNullCheck(instruction);
1741 }
1742 }
1743 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001744 case DataType::Type::kFloat32:
1745 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00001746 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001747 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001748
Artem Serov914d7a82017-02-07 14:33:49 +00001749 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1750 {
1751 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1752 __ ldar(temp, base);
1753 if (needs_null_check) {
1754 MaybeRecordImplicitNullCheck(instruction);
1755 }
1756 }
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001757 __ Fmov(VRegister(dst), temp);
Artem Serov914d7a82017-02-07 14:33:49 +00001758 break;
Roland Levillain44015862016-01-22 11:47:17 +00001759 }
Aart Bik66c158e2018-01-31 12:55:04 -08001760 case DataType::Type::kUint32:
1761 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001762 case DataType::Type::kVoid:
Artem Serov914d7a82017-02-07 14:33:49 +00001763 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001764 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001765 }
1766}
1767
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001768void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001769 CPURegister src,
1770 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001771 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001772 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001773 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001774 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001775 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001776 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001777 case DataType::Type::kUint16:
1778 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001779 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001780 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001781 case DataType::Type::kInt32:
1782 case DataType::Type::kReference:
1783 case DataType::Type::kInt64:
1784 case DataType::Type::kFloat32:
1785 case DataType::Type::kFloat64:
1786 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001787 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001788 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001789 case DataType::Type::kUint32:
1790 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001791 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001792 LOG(FATAL) << "Unreachable type " << type;
1793 }
1794}
1795
Artem Serov914d7a82017-02-07 14:33:49 +00001796void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001797 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001798 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00001799 const MemOperand& dst,
1800 bool needs_null_check) {
1801 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001802 UseScratchRegisterScope temps(GetVIXLAssembler());
1803 Register temp_base = temps.AcquireX();
1804
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001805 DCHECK(!dst.IsPreIndex());
1806 DCHECK(!dst.IsPostIndex());
1807
1808 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001809 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001810 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001811 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00001812 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001813 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001814 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001815 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001816 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001817 {
1818 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1819 __ stlrb(Register(src), base);
1820 if (needs_null_check) {
1821 MaybeRecordImplicitNullCheck(instruction);
1822 }
1823 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001824 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001825 case DataType::Type::kUint16:
1826 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001827 {
1828 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1829 __ stlrh(Register(src), base);
1830 if (needs_null_check) {
1831 MaybeRecordImplicitNullCheck(instruction);
1832 }
1833 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001834 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001835 case DataType::Type::kInt32:
1836 case DataType::Type::kReference:
1837 case DataType::Type::kInt64:
1838 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001839 {
1840 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1841 __ stlr(Register(src), base);
1842 if (needs_null_check) {
1843 MaybeRecordImplicitNullCheck(instruction);
1844 }
1845 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001846 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001847 case DataType::Type::kFloat32:
1848 case DataType::Type::kFloat64: {
1849 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001850 Register temp_src;
1851 if (src.IsZero()) {
1852 // The zero register is used to avoid synthesizing zero constants.
1853 temp_src = Register(src);
1854 } else {
1855 DCHECK(src.IsFPRegister());
1856 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001857 __ Fmov(temp_src, VRegister(src));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001858 }
Artem Serov914d7a82017-02-07 14:33:49 +00001859 {
1860 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1861 __ stlr(temp_src, base);
1862 if (needs_null_check) {
1863 MaybeRecordImplicitNullCheck(instruction);
1864 }
1865 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001866 break;
1867 }
Aart Bik66c158e2018-01-31 12:55:04 -08001868 case DataType::Type::kUint32:
1869 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001870 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001871 LOG(FATAL) << "Unreachable type " << type;
1872 }
1873}
1874
Calin Juravle175dc732015-08-25 15:42:32 +01001875void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1876 HInstruction* instruction,
1877 uint32_t dex_pc,
1878 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001879 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00001880
Vladimir Markof6675082019-05-17 12:05:28 +01001881 ThreadOffset64 entrypoint_offset = GetThreadOffset<kArm64PointerSize>(entrypoint);
1882 // Reduce code size for AOT by using shared trampolines for slow path runtime calls across the
1883 // entire oat file. This adds an extra branch and we do not want to slow down the main path.
1884 // For JIT, thunk sharing is per-method, so the gains would be smaller or even negative.
Vladimir Marko695348f2020-05-19 14:42:02 +01001885 if (slow_path == nullptr || GetCompilerOptions().IsJitCompiler()) {
Vladimir Markof6675082019-05-17 12:05:28 +01001886 __ Ldr(lr, MemOperand(tr, entrypoint_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00001887 // Ensure the pc position is recorded immediately after the `blr` instruction.
1888 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1889 __ blr(lr);
1890 if (EntrypointRequiresStackMap(entrypoint)) {
1891 RecordPcInfo(instruction, dex_pc, slow_path);
1892 }
Vladimir Markof6675082019-05-17 12:05:28 +01001893 } else {
1894 // Ensure the pc position is recorded immediately after the `bl` instruction.
1895 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1896 EmitEntrypointThunkCall(entrypoint_offset);
1897 if (EntrypointRequiresStackMap(entrypoint)) {
1898 RecordPcInfo(instruction, dex_pc, slow_path);
1899 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001900 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001901}
1902
Roland Levillaindec8f632016-07-22 17:10:06 +01001903void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1904 HInstruction* instruction,
1905 SlowPathCode* slow_path) {
1906 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01001907 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1908 __ Blr(lr);
1909}
1910
Alexandre Rames67555f72014-11-18 10:55:16 +00001911void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001912 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001913 UseScratchRegisterScope temps(GetVIXLAssembler());
1914 Register temp = temps.AcquireW();
Vladimir Markodc682aa2018-01-04 18:42:57 +00001915 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001916 const size_t status_byte_offset =
1917 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1918 constexpr uint32_t shifted_visibly_initialized_value =
1919 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001920
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001921 // CMP (immediate) is limited to imm12 or imm12<<12, so we would need to materialize
1922 // the constant 0xf0000000 for comparison with the full 32-bit field. To reduce the code
1923 // size, load only the high byte of the field and compare with 0xf0.
1924 // Note: The same code size could be achieved with LDR+MNV(asr #24)+CBNZ but benchmarks
1925 // show that this pattern is slower (tested on little cores).
1926 __ Ldrb(temp, HeapOperand(class_reg, status_byte_offset));
1927 __ Cmp(temp, shifted_visibly_initialized_value);
1928 __ B(lo, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001929 __ Bind(slow_path->GetExitLabel());
1930}
Alexandre Rames5319def2014-10-23 10:03:10 +01001931
Vladimir Marko175e7862018-03-27 09:03:13 +00001932void InstructionCodeGeneratorARM64::GenerateBitstringTypeCheckCompare(
1933 HTypeCheckInstruction* check, vixl::aarch64::Register temp) {
1934 uint32_t path_to_root = check->GetBitstringPathToRoot();
1935 uint32_t mask = check->GetBitstringMask();
1936 DCHECK(IsPowerOfTwo(mask + 1));
1937 size_t mask_bits = WhichPowerOf2(mask + 1);
1938
1939 if (mask_bits == 16u) {
1940 // Load only the bitstring part of the status word.
1941 __ Ldrh(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1942 } else {
1943 // /* uint32_t */ temp = temp->status_
1944 __ Ldr(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1945 // Extract the bitstring bits.
1946 __ Ubfx(temp, temp, 0, mask_bits);
1947 }
1948 // Compare the bitstring bits to `path_to_root`.
1949 __ Cmp(temp, path_to_root);
1950}
1951
Roland Levillain44015862016-01-22 11:47:17 +00001952void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001953 BarrierType type = BarrierAll;
1954
1955 switch (kind) {
1956 case MemBarrierKind::kAnyAny:
1957 case MemBarrierKind::kAnyStore: {
1958 type = BarrierAll;
1959 break;
1960 }
1961 case MemBarrierKind::kLoadAny: {
1962 type = BarrierReads;
1963 break;
1964 }
1965 case MemBarrierKind::kStoreStore: {
1966 type = BarrierWrites;
1967 break;
1968 }
1969 default:
1970 LOG(FATAL) << "Unexpected memory barrier " << kind;
1971 }
1972 __ Dmb(InnerShareable, type);
1973}
1974
Serban Constantinescu02164b32014-11-13 14:05:07 +00001975void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1976 HBasicBlock* successor) {
1977 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001978 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1979 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001980 slow_path =
1981 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001982 instruction->SetSlowPath(slow_path);
1983 codegen_->AddSlowPath(slow_path);
1984 if (successor != nullptr) {
1985 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001986 }
1987 } else {
1988 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1989 }
1990
Serban Constantinescu02164b32014-11-13 14:05:07 +00001991 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1992 Register temp = temps.AcquireW();
1993
Andreas Gampe542451c2016-07-26 09:02:02 -07001994 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001995 if (successor == nullptr) {
1996 __ Cbnz(temp, slow_path->GetEntryLabel());
1997 __ Bind(slow_path->GetReturnLabel());
1998 } else {
1999 __ Cbz(temp, codegen_->GetLabelOf(successor));
2000 __ B(slow_path->GetEntryLabel());
2001 // slow_path will return to GetLabelOf(successor).
2002 }
2003}
2004
Alexandre Rames5319def2014-10-23 10:03:10 +01002005InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2006 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002007 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002008 assembler_(codegen->GetAssembler()),
2009 codegen_(codegen) {}
2010
Alexandre Rames67555f72014-11-18 10:55:16 +00002011void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002012 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002013 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002014 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002015 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002016 case DataType::Type::kInt32:
2017 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01002018 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002019 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002020 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002021 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002022
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002023 case DataType::Type::kFloat32:
2024 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002025 locations->SetInAt(0, Location::RequiresFpuRegister());
2026 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002027 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002028 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002029
Alexandre Rames5319def2014-10-23 10:03:10 +01002030 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002031 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002032 }
2033}
2034
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002035void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2036 const FieldInfo& field_info) {
Alex Light3a73ffb2021-01-25 14:11:05 +00002037 DCHECK(instruction->IsInstanceFieldGet() ||
2038 instruction->IsStaticFieldGet() ||
2039 instruction->IsPredicatedInstanceFieldGet());
2040
2041 bool is_predicated = instruction->IsPredicatedInstanceFieldGet();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002042
2043 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002044 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01002045 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002046 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2047 object_field_get_with_read_barrier
2048 ? LocationSummary::kCallOnSlowPath
2049 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002050 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002051 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko0ecac682018-08-07 10:40:38 +01002052 // We need a temporary register for the read barrier load in
2053 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2054 // only if the field is volatile or the offset is too big.
2055 if (field_info.IsVolatile() ||
2056 field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2057 locations->AddTemp(FixedTempLocation());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002058 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002059 }
Alex Light3a73ffb2021-01-25 14:11:05 +00002060 // Input for object receiver.
2061 locations->SetInAt(is_predicated ? 1 : 0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002062 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alex Light3a73ffb2021-01-25 14:11:05 +00002063 if (is_predicated) {
2064 locations->SetInAt(0, Location::RequiresFpuRegister());
2065 locations->SetOut(Location::SameAsFirstInput());
2066 } else {
2067 locations->SetOut(Location::RequiresFpuRegister());
2068 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002069 } else {
Alex Light3a73ffb2021-01-25 14:11:05 +00002070 if (is_predicated) {
2071 locations->SetInAt(0, Location::RequiresRegister());
2072 locations->SetOut(Location::SameAsFirstInput());
2073 } else {
2074 // The output overlaps for an object field get when read barriers
2075 // are enabled: we do not want the load to overwrite the object's
2076 // location, as we need it to emit the read barrier.
2077 locations->SetOut(Location::RequiresRegister(),
2078 object_field_get_with_read_barrier ? Location::kOutputOverlap
2079 : Location::kNoOutputOverlap);
2080 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002081 }
2082}
2083
2084void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2085 const FieldInfo& field_info) {
Alex Light3a73ffb2021-01-25 14:11:05 +00002086 DCHECK(instruction->IsInstanceFieldGet() ||
2087 instruction->IsStaticFieldGet() ||
2088 instruction->IsPredicatedInstanceFieldGet());
2089 bool is_predicated = instruction->IsPredicatedInstanceFieldGet();
Roland Levillain44015862016-01-22 11:47:17 +00002090 LocationSummary* locations = instruction->GetLocations();
Alex Light3a73ffb2021-01-25 14:11:05 +00002091 uint32_t receiver_input = is_predicated ? 1 : 0;
2092 Location base_loc = locations->InAt(receiver_input);
Roland Levillain44015862016-01-22 11:47:17 +00002093 Location out = locations->Out();
2094 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01002095 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
2096 DataType::Type load_type = instruction->GetType();
Alex Light3a73ffb2021-01-25 14:11:05 +00002097 MemOperand field =
2098 HeapOperand(InputRegisterAt(instruction, receiver_input), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002099
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002100 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01002101 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002102 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002103 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002104 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002105 Location maybe_temp =
2106 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002107 // Note that potential implicit null checks are handled in this
2108 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2109 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2110 instruction,
2111 out,
2112 base,
2113 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002114 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002115 /* needs_null_check= */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002116 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002117 } else {
2118 // General case.
2119 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002120 // Note that a potential implicit null check is handled in this
2121 // CodeGeneratorARM64::LoadAcquire call.
2122 // NB: LoadAcquire will record the pc info if needed.
Vladimir Marko98873af2020-12-16 12:10:03 +00002123 codegen_->LoadAcquire(instruction,
2124 load_type,
2125 OutputCPURegister(instruction),
2126 field,
2127 /* needs_null_check= */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002128 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002129 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2130 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01002131 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002132 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002133 }
Vladimir Marko61b92282017-10-11 13:23:17 +01002134 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002135 // If read barriers are enabled, emit read barriers other than
2136 // Baker's using a slow path (and also unpoison the loaded
2137 // reference, if heap poisoning is enabled).
2138 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2139 }
Roland Levillain4d027112015-07-01 15:41:14 +01002140 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002141}
2142
2143void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2144 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002145 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01002146 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002147 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2148 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002149 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002150 locations->SetInAt(1, Location::RequiresFpuRegister());
2151 } else {
2152 locations->SetInAt(1, Location::RequiresRegister());
2153 }
2154}
2155
2156void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002157 const FieldInfo& field_info,
2158 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002159 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alex Light3a73ffb2021-01-25 14:11:05 +00002160 bool is_predicated =
2161 instruction->IsInstanceFieldSet() && instruction->AsInstanceFieldSet()->GetIsPredicatedSet();
Alexandre Rames09a99962015-04-15 11:47:56 +01002162
2163 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002164 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002165 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002166 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002167 DataType::Type field_type = field_info.GetFieldType();
Alex Light3a73ffb2021-01-25 14:11:05 +00002168 std::optional<vixl::aarch64::Label> pred_is_null;
2169 if (is_predicated) {
2170 pred_is_null.emplace();
2171 __ Cbz(obj, &*pred_is_null);
2172 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002173
Roland Levillain4d027112015-07-01 15:41:14 +01002174 {
2175 // We use a block to end the scratch scope before the write barrier, thus
2176 // freeing the temporary registers so they can be used in `MarkGCCard`.
2177 UseScratchRegisterScope temps(GetVIXLAssembler());
2178
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002179 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002180 DCHECK(value.IsW());
2181 Register temp = temps.AcquireW();
2182 __ Mov(temp, value.W());
2183 GetAssembler()->PoisonHeapReference(temp.W());
2184 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002185 }
Roland Levillain4d027112015-07-01 15:41:14 +01002186
2187 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002188 codegen_->StoreRelease(
Andreas Gampe3db70682018-12-26 15:12:03 -08002189 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check= */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002190 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002191 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2192 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002193 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2194 codegen_->MaybeRecordImplicitNullCheck(instruction);
2195 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002196 }
2197
2198 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002199 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002200 }
Alex Light3a73ffb2021-01-25 14:11:05 +00002201
2202 if (is_predicated) {
2203 __ Bind(&*pred_is_null);
2204 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002205}
2206
Alexandre Rames67555f72014-11-18 10:55:16 +00002207void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002208 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002209
2210 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002211 case DataType::Type::kInt32:
2212 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002213 Register dst = OutputRegister(instr);
2214 Register lhs = InputRegisterAt(instr, 0);
2215 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002216 if (instr->IsAdd()) {
2217 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002218 } else if (instr->IsAnd()) {
2219 __ And(dst, lhs, rhs);
2220 } else if (instr->IsOr()) {
2221 __ Orr(dst, lhs, rhs);
2222 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002223 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002224 } else if (instr->IsRor()) {
2225 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002226 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002227 __ Ror(dst, lhs, shift);
2228 } else {
2229 // Ensure shift distance is in the same size register as the result. If
2230 // we are rotating a long and the shift comes in a w register originally,
2231 // we don't need to sxtw for use as an x since the shift distances are
2232 // all & reg_bits - 1.
2233 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2234 }
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002235 } else if (instr->IsMin() || instr->IsMax()) {
2236 __ Cmp(lhs, rhs);
2237 __ Csel(dst, lhs, rhs, instr->IsMin() ? lt : gt);
Alexandre Rames67555f72014-11-18 10:55:16 +00002238 } else {
2239 DCHECK(instr->IsXor());
2240 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002241 }
2242 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002243 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002244 case DataType::Type::kFloat32:
2245 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002246 VRegister dst = OutputFPRegister(instr);
2247 VRegister lhs = InputFPRegisterAt(instr, 0);
2248 VRegister rhs = InputFPRegisterAt(instr, 1);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002249 if (instr->IsAdd()) {
2250 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002251 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002252 __ Fsub(dst, lhs, rhs);
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002253 } else if (instr->IsMin()) {
2254 __ Fmin(dst, lhs, rhs);
2255 } else if (instr->IsMax()) {
2256 __ Fmax(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002257 } else {
2258 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002259 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002260 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002261 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002262 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002263 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002264 }
2265}
2266
Serban Constantinescu02164b32014-11-13 14:05:07 +00002267void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2268 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2269
Vladimir Markoca6fff82017-10-03 14:49:14 +01002270 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002271 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002272 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002273 case DataType::Type::kInt32:
2274 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002275 locations->SetInAt(0, Location::RequiresRegister());
2276 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002277 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002278 break;
2279 }
2280 default:
2281 LOG(FATAL) << "Unexpected shift type " << type;
2282 }
2283}
2284
2285void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2286 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2287
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002288 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002289 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002290 case DataType::Type::kInt32:
2291 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002292 Register dst = OutputRegister(instr);
2293 Register lhs = InputRegisterAt(instr, 0);
2294 Operand rhs = InputOperandAt(instr, 1);
2295 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002296 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002297 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002298 if (instr->IsShl()) {
2299 __ Lsl(dst, lhs, shift_value);
2300 } else if (instr->IsShr()) {
2301 __ Asr(dst, lhs, shift_value);
2302 } else {
2303 __ Lsr(dst, lhs, shift_value);
2304 }
2305 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002306 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002307
2308 if (instr->IsShl()) {
2309 __ Lsl(dst, lhs, rhs_reg);
2310 } else if (instr->IsShr()) {
2311 __ Asr(dst, lhs, rhs_reg);
2312 } else {
2313 __ Lsr(dst, lhs, rhs_reg);
2314 }
2315 }
2316 break;
2317 }
2318 default:
2319 LOG(FATAL) << "Unexpected shift operation type " << type;
2320 }
2321}
2322
Alexandre Rames5319def2014-10-23 10:03:10 +01002323void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002324 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002325}
2326
2327void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002328 HandleBinaryOp(instruction);
2329}
2330
2331void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2332 HandleBinaryOp(instruction);
2333}
2334
2335void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2336 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002337}
2338
Artem Serov7fc63502016-02-09 17:15:29 +00002339void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002340 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002341 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002342 locations->SetInAt(0, Location::RequiresRegister());
2343 // There is no immediate variant of negated bitwise instructions in AArch64.
2344 locations->SetInAt(1, Location::RequiresRegister());
2345 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2346}
2347
Artem Serov7fc63502016-02-09 17:15:29 +00002348void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002349 Register dst = OutputRegister(instr);
2350 Register lhs = InputRegisterAt(instr, 0);
2351 Register rhs = InputRegisterAt(instr, 1);
2352
2353 switch (instr->GetOpKind()) {
2354 case HInstruction::kAnd:
2355 __ Bic(dst, lhs, rhs);
2356 break;
2357 case HInstruction::kOr:
2358 __ Orn(dst, lhs, rhs);
2359 break;
2360 case HInstruction::kXor:
2361 __ Eon(dst, lhs, rhs);
2362 break;
2363 default:
2364 LOG(FATAL) << "Unreachable";
2365 }
2366}
2367
Anton Kirilov74234da2017-01-13 14:42:47 +00002368void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2369 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002370 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2371 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002372 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002373 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002374 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2375 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2376 } else {
2377 locations->SetInAt(0, Location::RequiresRegister());
2378 }
2379 locations->SetInAt(1, Location::RequiresRegister());
2380 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2381}
2382
Anton Kirilov74234da2017-01-13 14:42:47 +00002383void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2384 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002385 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002386 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002387 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002388 Register out = OutputRegister(instruction);
2389 Register left;
2390 if (kind != HInstruction::kNeg) {
2391 left = InputRegisterAt(instruction, 0);
2392 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002393 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002394 // shifter operand operation, the IR generating `right_reg` (input to the type
2395 // conversion) can have a different type from the current instruction's type,
2396 // so we manually indicate the type.
2397 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002398 Operand right_operand(0);
2399
Anton Kirilov74234da2017-01-13 14:42:47 +00002400 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2401 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002402 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2403 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002404 right_operand = Operand(right_reg,
2405 helpers::ShiftFromOpKind(op_kind),
2406 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002407 }
2408
2409 // Logical binary operations do not support extension operations in the
2410 // operand. Note that VIXL would still manage if it was passed by generating
2411 // the extension as a separate instruction.
2412 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2413 DCHECK(!right_operand.IsExtendedRegister() ||
2414 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2415 kind != HInstruction::kNeg));
2416 switch (kind) {
2417 case HInstruction::kAdd:
2418 __ Add(out, left, right_operand);
2419 break;
2420 case HInstruction::kAnd:
2421 __ And(out, left, right_operand);
2422 break;
2423 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002424 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002425 __ Neg(out, right_operand);
2426 break;
2427 case HInstruction::kOr:
2428 __ Orr(out, left, right_operand);
2429 break;
2430 case HInstruction::kSub:
2431 __ Sub(out, left, right_operand);
2432 break;
2433 case HInstruction::kXor:
2434 __ Eor(out, left, right_operand);
2435 break;
2436 default:
2437 LOG(FATAL) << "Unexpected operation kind: " << kind;
2438 UNREACHABLE();
2439 }
2440}
2441
Artem Serov328429f2016-07-06 16:23:04 +01002442void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002443 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002444 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002445 locations->SetInAt(0, Location::RequiresRegister());
2446 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002447 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002448}
2449
Roland Levillain19c54192016-11-04 13:44:09 +00002450void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002451 __ Add(OutputRegister(instruction),
2452 InputRegisterAt(instruction, 0),
2453 Operand(InputOperandAt(instruction, 1)));
2454}
2455
Artem Serove1811ed2017-04-27 16:50:47 +01002456void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2457 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002458 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002459
2460 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2461
2462 locations->SetInAt(0, Location::RequiresRegister());
2463 // For byte case we don't need to shift the index variable so we can encode the data offset into
2464 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2465 // data offset constant generation out of the loop and reduce the critical path length in the
2466 // loop.
2467 locations->SetInAt(1, shift->GetValue() == 0
2468 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2469 : Location::RequiresRegister());
2470 locations->SetInAt(2, Location::ConstantLocation(shift));
2471 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2472}
2473
2474void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2475 HIntermediateAddressIndex* instruction) {
2476 Register index_reg = InputRegisterAt(instruction, 0);
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002477 uint32_t shift = Int64FromLocation(instruction->GetLocations()->InAt(2));
Artem Serove1811ed2017-04-27 16:50:47 +01002478 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2479
2480 if (shift == 0) {
2481 __ Add(OutputRegister(instruction), index_reg, offset);
2482 } else {
2483 Register offset_reg = InputRegisterAt(instruction, 1);
2484 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2485 }
2486}
2487
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002488void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002489 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002490 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002491 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2492 if (instr->GetOpKind() == HInstruction::kSub &&
2493 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002494 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002495 // Don't allocate register for Mneg instruction.
2496 } else {
2497 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2498 Location::RequiresRegister());
2499 }
2500 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2501 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002502 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2503}
2504
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002505void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002506 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002507 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2508 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002509
2510 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2511 // This fixup should be carried out for all multiply-accumulate instructions:
2512 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002513 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002514 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2515 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002516 vixl::aarch64::Instruction* prev =
2517 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002518 if (prev->IsLoadOrStore()) {
2519 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002520 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002521 __ nop();
2522 }
2523 }
2524
2525 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002526 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002527 __ Madd(res, mul_left, mul_right, accumulator);
2528 } else {
2529 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002530 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002531 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002532 __ Mneg(res, mul_left, mul_right);
2533 } else {
2534 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2535 __ Msub(res, mul_left, mul_right, accumulator);
2536 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002537 }
2538}
2539
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002540void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002541 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002542 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002543 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002544 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2545 object_array_get_with_read_barrier
2546 ? LocationSummary::kCallOnSlowPath
2547 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002548 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002549 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002550 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002551 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002552 // We need a temporary register for the read barrier load in
2553 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2554 // only if the offset is too big.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002555 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2556 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002557 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002558 if (offset >= kReferenceLoadMinFarOffset) {
2559 locations->AddTemp(FixedTempLocation());
2560 }
Artem Serov0806f582018-10-11 20:14:20 +01002561 } else if (!instruction->GetArray()->IsIntermediateAddress()) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01002562 // We need a non-scratch temporary for the array data pointer in
Artem Serov0806f582018-10-11 20:14:20 +01002563 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier() for the case with no
2564 // intermediate address.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002565 locations->AddTemp(Location::RequiresRegister());
2566 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002567 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002568 locations->SetInAt(0, Location::RequiresRegister());
2569 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002570 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002571 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2572 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002573 // The output overlaps in the case of an object array get with
2574 // read barriers enabled: we do not want the move to overwrite the
2575 // array's location, as we need it to emit the read barrier.
2576 locations->SetOut(
2577 Location::RequiresRegister(),
2578 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002579 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002580}
2581
2582void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002583 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002584 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002585 LocationSummary* locations = instruction->GetLocations();
2586 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002587 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002588 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002589 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2590 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002591 MacroAssembler* masm = GetVIXLAssembler();
2592 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002593
Artem Serov0806f582018-10-11 20:14:20 +01002594 // The non-Baker read barrier instrumentation of object ArrayGet instructions
Roland Levillain19c54192016-11-04 13:44:09 +00002595 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002596 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002597 instruction->GetArray()->IsIntermediateAddress() &&
Artem Serov0806f582018-10-11 20:14:20 +01002598 kEmitCompilerReadBarrier &&
2599 !kUseBakerReadBarrier));
Roland Levillain19c54192016-11-04 13:44:09 +00002600
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002601 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002602 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002603 // Note that a potential implicit null check is handled in the
2604 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002605 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002606 if (index.IsConstant()) {
Artem Serov0806f582018-10-11 20:14:20 +01002607 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002608 // Array load with a constant index can be treated as a field load.
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002609 offset += Int64FromLocation(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002610 Location maybe_temp =
2611 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2612 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2613 out,
2614 obj.W(),
2615 offset,
2616 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002617 /* needs_null_check= */ false,
2618 /* use_load_acquire= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002619 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002620 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08002621 instruction, out, obj.W(), offset, index, /* needs_null_check= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002622 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002623 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002624 // General case.
2625 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002626 Register length;
2627 if (maybe_compressed_char_at) {
2628 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2629 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002630 {
2631 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2632 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2633
2634 if (instruction->GetArray()->IsIntermediateAddress()) {
2635 DCHECK_LT(count_offset, offset);
2636 int64_t adjusted_offset =
2637 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2638 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2639 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2640 } else {
2641 __ Ldr(length, HeapOperand(obj, count_offset));
2642 }
2643 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002644 }
jessicahandojo05765752016-09-09 19:01:32 -07002645 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002646 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002647 if (maybe_compressed_char_at) {
2648 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002649 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2650 "Expecting 0=compressed, 1=uncompressed");
2651 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002652 __ Ldrb(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002653 HeapOperand(obj, offset + Int64FromLocation(index)));
jessicahandojo05765752016-09-09 19:01:32 -07002654 __ B(&done);
2655 __ Bind(&uncompressed_load);
2656 __ Ldrh(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002657 HeapOperand(obj, offset + (Int64FromLocation(index) << 1)));
jessicahandojo05765752016-09-09 19:01:32 -07002658 __ Bind(&done);
2659 } else {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002660 offset += Int64FromLocation(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002661 source = HeapOperand(obj, offset);
2662 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002663 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002664 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002665 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002666 // We do not need to compute the intermediate address from the array: the
2667 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002668 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002669 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002670 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2671 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
Roland Levillain44015862016-01-22 11:47:17 +00002672 }
2673 temp = obj;
2674 } else {
2675 __ Add(temp, obj, offset);
2676 }
jessicahandojo05765752016-09-09 19:01:32 -07002677 if (maybe_compressed_char_at) {
2678 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002679 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2680 "Expecting 0=compressed, 1=uncompressed");
2681 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002682 __ Ldrb(Register(OutputCPURegister(instruction)),
2683 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2684 __ B(&done);
2685 __ Bind(&uncompressed_load);
2686 __ Ldrh(Register(OutputCPURegister(instruction)),
2687 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2688 __ Bind(&done);
2689 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002690 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002691 }
Roland Levillain44015862016-01-22 11:47:17 +00002692 }
jessicahandojo05765752016-09-09 19:01:32 -07002693 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002694 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2695 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002696 codegen_->Load(type, OutputCPURegister(instruction), source);
2697 codegen_->MaybeRecordImplicitNullCheck(instruction);
2698 }
Roland Levillain44015862016-01-22 11:47:17 +00002699
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002700 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002701 static_assert(
2702 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2703 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2704 Location obj_loc = locations->InAt(0);
2705 if (index.IsConstant()) {
2706 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2707 } else {
2708 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2709 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002710 }
Roland Levillain4d027112015-07-01 15:41:14 +01002711 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002712}
2713
Alexandre Rames5319def2014-10-23 10:03:10 +01002714void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002715 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002716 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002717 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002718}
2719
2720void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002721 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002722 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002723 {
2724 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2725 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2726 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2727 codegen_->MaybeRecordImplicitNullCheck(instruction);
2728 }
jessicahandojo05765752016-09-09 19:01:32 -07002729 // Mask out compression flag from String's array length.
2730 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002731 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002732 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002733}
2734
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002735void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002736 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002737
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002738 bool needs_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002739 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002740 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002741 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002742 locations->SetInAt(0, Location::RequiresRegister());
2743 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002744 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2745 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002746 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002747 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002748 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002749 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002750 }
2751}
2752
2753void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002754 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002755 LocationSummary* locations = instruction->GetLocations();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002756 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002757 bool needs_write_barrier =
2758 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002759
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002760 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002761 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002762 CPURegister source = value;
2763 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002764 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002765 MemOperand destination = HeapOperand(array);
2766 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002767
2768 if (!needs_write_barrier) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002769 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002770 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002771 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002772 destination = HeapOperand(array, offset);
2773 } else {
2774 UseScratchRegisterScope temps(masm);
2775 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002776 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002777 // We do not need to compute the intermediate address from the array: the
2778 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002779 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002780 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002781 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2782 DCHECK(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002783 }
2784 temp = array;
2785 } else {
2786 __ Add(temp, array, offset);
2787 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002788 destination = HeapOperand(temp,
2789 XRegisterFrom(index),
2790 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002791 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002792 }
Artem Serov914d7a82017-02-07 14:33:49 +00002793 {
2794 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2795 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2796 codegen_->Store(value_type, value, destination);
2797 codegen_->MaybeRecordImplicitNullCheck(instruction);
2798 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002799 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002800 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002801
2802 bool can_value_be_null = instruction->GetValueCanBeNull();
2803 vixl::aarch64::Label do_store;
2804 if (can_value_be_null) {
2805 __ Cbz(Register(value), &do_store);
2806 }
2807
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002808 SlowPathCodeARM64* slow_path = nullptr;
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002809 if (needs_type_check) {
2810 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
2811 codegen_->AddSlowPath(slow_path);
2812
2813 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2814 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2815 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2816
Alexandre Rames97833a02015-04-16 15:07:12 +01002817 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002818 Register temp = temps.AcquireSameSizeAs(array);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002819 Register temp2 = temps.AcquireSameSizeAs(array);
2820
2821 // Note that when Baker read barriers are enabled, the type
2822 // checks are performed without read barriers. This is fine,
2823 // even in the case where a class object is in the from-space
2824 // after the flip, as a comparison involving such a type would
2825 // not produce a false positive; it may of course produce a
2826 // false negative, in which case we would take the ArraySet
2827 // slow path.
2828
2829 // /* HeapReference<Class> */ temp = array->klass_
2830 {
2831 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2832 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2833 __ Ldr(temp, HeapOperand(array, class_offset));
2834 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames97833a02015-04-16 15:07:12 +01002835 }
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002836 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Alexandre Rames97833a02015-04-16 15:07:12 +01002837
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002838 // /* HeapReference<Class> */ temp = temp->component_type_
2839 __ Ldr(temp, HeapOperand(temp, component_offset));
2840 // /* HeapReference<Class> */ temp2 = value->klass_
2841 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2842 // If heap poisoning is enabled, no need to unpoison `temp`
2843 // nor `temp2`, as we are comparing two poisoned references.
2844 __ Cmp(temp, temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002845
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002846 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2847 vixl::aarch64::Label do_put;
2848 __ B(eq, &do_put);
2849 // If heap poisoning is enabled, the `temp` reference has
2850 // not been unpoisoned yet; unpoison it now.
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002851 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002852
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002853 // /* HeapReference<Class> */ temp = temp->super_class_
2854 __ Ldr(temp, HeapOperand(temp, super_offset));
2855 // If heap poisoning is enabled, no need to unpoison
2856 // `temp`, as we are comparing against null below.
2857 __ Cbnz(temp, slow_path->GetEntryLabel());
2858 __ Bind(&do_put);
Vladimir Markod1ef8732017-04-18 13:55:13 +01002859 } else {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002860 __ B(ne, slow_path->GetEntryLabel());
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002861 }
2862 }
2863
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002864 codegen_->MarkGCCard(array, value.W(), /* value_can_be_null= */ false);
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002865
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002866 if (can_value_be_null) {
2867 DCHECK(do_store.IsLinked());
2868 __ Bind(&do_store);
2869 }
2870
2871 UseScratchRegisterScope temps(masm);
2872 if (kPoisonHeapReferences) {
2873 Register temp_source = temps.AcquireSameSizeAs(array);
2874 DCHECK(value.IsW());
2875 __ Mov(temp_source, value.W());
2876 GetAssembler()->PoisonHeapReference(temp_source);
2877 source = temp_source;
2878 }
2879
2880 if (index.IsConstant()) {
2881 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
2882 destination = HeapOperand(array, offset);
2883 } else {
2884 Register temp_base = temps.AcquireSameSizeAs(array);
2885 __ Add(temp_base, array, offset);
2886 destination = HeapOperand(temp_base,
2887 XRegisterFrom(index),
2888 LSL,
2889 DataType::SizeShift(value_type));
2890 }
2891
2892 {
2893 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2894 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2895 __ Str(source, destination);
2896
2897 if (can_value_be_null || !needs_type_check) {
2898 codegen_->MaybeRecordImplicitNullCheck(instruction);
2899 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002900 }
2901
2902 if (slow_path != nullptr) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002903 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002904 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002905 }
2906}
2907
Alexandre Rames67555f72014-11-18 10:55:16 +00002908void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002909 RegisterSet caller_saves = RegisterSet::Empty();
2910 InvokeRuntimeCallingConvention calling_convention;
2911 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2912 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2913 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002914
2915 // If both index and length are constant, we can check the bounds statically and
2916 // generate code accordingly. We want to make sure we generate constant locations
2917 // in that case, regardless of whether they are encodable in the comparison or not.
2918 HInstruction* index = instruction->InputAt(0);
2919 HInstruction* length = instruction->InputAt(1);
2920 bool both_const = index->IsConstant() && length->IsConstant();
2921 locations->SetInAt(0, both_const
2922 ? Location::ConstantLocation(index->AsConstant())
2923 : ARM64EncodableConstantOrRegister(index, instruction));
2924 locations->SetInAt(1, both_const
2925 ? Location::ConstantLocation(length->AsConstant())
2926 : ARM64EncodableConstantOrRegister(length, instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002927}
2928
2929void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Georgia Kouvelibe530852019-01-17 10:46:41 +00002930 LocationSummary* locations = instruction->GetLocations();
2931 Location index_loc = locations->InAt(0);
2932 Location length_loc = locations->InAt(1);
2933
2934 int cmp_first_input = 0;
2935 int cmp_second_input = 1;
2936 Condition cond = hs;
2937
2938 if (index_loc.IsConstant()) {
2939 int64_t index = Int64FromLocation(index_loc);
2940 if (length_loc.IsConstant()) {
2941 int64_t length = Int64FromLocation(length_loc);
2942 if (index < 0 || index >= length) {
2943 BoundsCheckSlowPathARM64* slow_path =
2944 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
2945 codegen_->AddSlowPath(slow_path);
2946 __ B(slow_path->GetEntryLabel());
2947 } else {
2948 // BCE will remove the bounds check if we are guaranteed to pass.
2949 // However, some optimization after BCE may have generated this, and we should not
2950 // generate a bounds check if it is a valid range.
2951 }
2952 return;
2953 }
2954 // Only the index is constant: change the order of the operands and commute the condition
2955 // so we can use an immediate constant for the index (only the second input to a cmp
2956 // instruction can be an immediate).
2957 cmp_first_input = 1;
2958 cmp_second_input = 0;
2959 cond = ls;
2960 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002961 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002962 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002963 __ Cmp(InputRegisterAt(instruction, cmp_first_input),
2964 InputOperandAt(instruction, cmp_second_input));
Alexandre Rames67555f72014-11-18 10:55:16 +00002965 codegen_->AddSlowPath(slow_path);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002966 __ B(slow_path->GetEntryLabel(), cond);
Alexandre Rames67555f72014-11-18 10:55:16 +00002967}
2968
Alexandre Rames67555f72014-11-18 10:55:16 +00002969void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2970 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002971 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00002972 locations->SetInAt(0, Location::RequiresRegister());
2973 if (check->HasUses()) {
2974 locations->SetOut(Location::SameAsFirstInput());
2975 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01002976 // Rely on the type initialization to save everything we need.
2977 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Alexandre Rames67555f72014-11-18 10:55:16 +00002978}
2979
2980void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2981 // We assume the class is not null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01002982 SlowPathCodeARM64* slow_path =
2983 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(check->GetLoadClass(), check);
Alexandre Rames67555f72014-11-18 10:55:16 +00002984 codegen_->AddSlowPath(slow_path);
2985 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2986}
2987
Roland Levillain1a653882016-03-18 18:05:57 +00002988static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2989 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2990 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2991}
2992
2993void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002994 VRegister lhs_reg = InputFPRegisterAt(instruction, 0);
Roland Levillain1a653882016-03-18 18:05:57 +00002995 Location rhs_loc = instruction->GetLocations()->InAt(1);
2996 if (rhs_loc.IsConstant()) {
2997 // 0.0 is the only immediate that can be encoded directly in
2998 // an FCMP instruction.
2999 //
3000 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3001 // specify that in a floating-point comparison, positive zero
3002 // and negative zero are considered equal, so we can use the
3003 // literal 0.0 for both cases here.
3004 //
3005 // Note however that some methods (Float.equal, Float.compare,
3006 // Float.compareTo, Double.equal, Double.compare,
3007 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3008 // StrictMath.min) consider 0.0 to be (strictly) greater than
3009 // -0.0. So if we ever translate calls to these methods into a
3010 // HCompare instruction, we must handle the -0.0 case with
3011 // care here.
3012 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3013 __ Fcmp(lhs_reg, 0.0);
3014 } else {
3015 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3016 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003017}
3018
Serban Constantinescu02164b32014-11-13 14:05:07 +00003019void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003020 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003021 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003022 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003023 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003024 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003025 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003026 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003027 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003028 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003029 case DataType::Type::kInt32:
3030 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003031 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003032 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003033 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3034 break;
3035 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003036 case DataType::Type::kFloat32:
3037 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003038 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003039 locations->SetInAt(1,
3040 IsFloatingPointZeroConstant(compare->InputAt(1))
3041 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3042 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003043 locations->SetOut(Location::RequiresRegister());
3044 break;
3045 }
3046 default:
3047 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3048 }
3049}
3050
3051void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003052 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00003053
3054 // 0 if: left == right
3055 // 1 if: left > right
3056 // -1 if: left < right
3057 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003058 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003059 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003060 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003061 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003062 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003063 case DataType::Type::kInt32:
3064 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003065 Register result = OutputRegister(compare);
3066 Register left = InputRegisterAt(compare, 0);
3067 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003068 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003069 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3070 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003071 break;
3072 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003073 case DataType::Type::kFloat32:
3074 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003075 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003076 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003077 __ Cset(result, ne);
3078 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003079 break;
3080 }
3081 default:
3082 LOG(FATAL) << "Unimplemented compare type " << in_type;
3083 }
3084}
3085
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003086void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003087 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003088
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003089 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003090 locations->SetInAt(0, Location::RequiresFpuRegister());
3091 locations->SetInAt(1,
3092 IsFloatingPointZeroConstant(instruction->InputAt(1))
3093 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3094 : Location::RequiresFpuRegister());
3095 } else {
3096 // Integer cases.
3097 locations->SetInAt(0, Location::RequiresRegister());
3098 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3099 }
3100
David Brazdilb3e773e2016-01-26 11:28:37 +00003101 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003102 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003103 }
3104}
3105
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003106void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003107 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003108 return;
3109 }
3110
3111 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003112 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003113 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003114
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003115 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003116 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003117 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003118 } else {
3119 // Integer cases.
3120 Register lhs = InputRegisterAt(instruction, 0);
3121 Operand rhs = InputOperandAt(instruction, 1);
3122 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003123 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003124 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003125}
3126
3127#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3128 M(Equal) \
3129 M(NotEqual) \
3130 M(LessThan) \
3131 M(LessThanOrEqual) \
3132 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003133 M(GreaterThanOrEqual) \
3134 M(Below) \
3135 M(BelowOrEqual) \
3136 M(Above) \
3137 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003138#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003139void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3140void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003141FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003142#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003143#undef FOR_EACH_CONDITION_INSTRUCTION
3144
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003145void InstructionCodeGeneratorARM64::GenerateIntDivForPower2Denom(HDiv* instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003146 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003147 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003148 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
3149
3150 Register out = OutputRegister(instruction);
3151 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003152
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003153 Register final_dividend;
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003154 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003155 // No need to adjust the result for non-negative dividends or the INT32_MIN/INT64_MIN dividends.
3156 // NOTE: The generated code for HDiv correctly works for the INT32_MIN/INT64_MIN dividends:
3157 // imm == 2
3158 // add out, dividend(0x80000000), dividend(0x80000000), lsr #31 => out = 0x80000001
3159 // asr out, out(0x80000001), #1 => out = 0xc0000000
3160 // This is the same as 'asr out, 0x80000000, #1'
3161 //
3162 // imm > 2
3163 // add temp, dividend(0x80000000), imm - 1 => temp = 0b10..01..1, where the number
3164 // of the rightmost 1s is ctz_imm.
3165 // cmp dividend(0x80000000), 0 => N = 1, V = 0 (lt is true)
3166 // csel out, temp(0b10..01..1), dividend(0x80000000), lt => out = 0b10..01..1
3167 // asr out, out(0b10..01..1), #ctz_imm => out = 0b1..10..0, where the number of the
3168 // leftmost 1s is ctz_imm + 1.
3169 // This is the same as 'asr out, dividend(0x80000000), #ctz_imm'.
3170 //
3171 // imm == INT32_MIN
3172 // add tmp, dividend(0x80000000), #0x7fffffff => tmp = -1
3173 // cmp dividend(0x80000000), 0 => N = 1, V = 0 (lt is true)
3174 // csel out, temp(-1), dividend(0x80000000), lt => out = -1
3175 // neg out, out(-1), asr #31 => out = 1
3176 // This is the same as 'neg out, dividend(0x80000000), asr #31'.
3177 final_dividend = dividend;
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003178 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003179 if (abs_imm == 2) {
3180 int bits = DataType::Size(instruction->GetResultType()) * kBitsPerByte;
3181 __ Add(out, dividend, Operand(dividend, LSR, bits - 1));
3182 } else {
3183 UseScratchRegisterScope temps(GetVIXLAssembler());
3184 Register temp = temps.AcquireSameSizeAs(out);
3185 __ Add(temp, dividend, abs_imm - 1);
3186 __ Cmp(dividend, 0);
3187 __ Csel(out, temp, dividend, lt);
3188 }
3189 final_dividend = out;
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003190 }
3191
Zheng Xuc6667102015-05-15 16:08:45 +08003192 int ctz_imm = CTZ(abs_imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003193 if (imm > 0) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003194 __ Asr(out, final_dividend, ctz_imm);
Zheng Xuc6667102015-05-15 16:08:45 +08003195 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003196 __ Neg(out, Operand(final_dividend, ASR, ctz_imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003197 }
3198}
3199
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003200// Return true if the magic number was modified by subtracting 2^32(Int32 div) or 2^64(Int64 div).
3201// So dividend needs to be added.
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003202static inline bool NeedToAddDividend(int64_t magic_number, int64_t divisor) {
3203 return divisor > 0 && magic_number < 0;
3204}
3205
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003206// Return true if the magic number was modified by adding 2^32(Int32 div) or 2^64(Int64 div).
3207// So dividend needs to be subtracted.
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003208static inline bool NeedToSubDividend(int64_t magic_number, int64_t divisor) {
3209 return divisor < 0 && magic_number > 0;
3210}
3211
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003212// Generate code which increments the value in register 'in' by 1 if the value is negative.
3213// It is done with 'add out, in, in, lsr #31 or #63'.
3214// If the value is a result of an operation setting the N flag, CINC MI can be used
3215// instead of ADD. 'use_cond_inc' controls this.
3216void InstructionCodeGeneratorARM64::GenerateIncrementNegativeByOne(
3217 Register out,
3218 Register in,
3219 bool use_cond_inc) {
3220 if (use_cond_inc) {
3221 __ Cinc(out, in, mi);
3222 } else {
3223 __ Add(out, in, Operand(in, LSR, in.GetSizeInBits() - 1));
3224 }
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003225}
3226
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003227// Helper to generate code producing the result of HRem with a constant divisor.
3228void InstructionCodeGeneratorARM64::GenerateResultRemWithAnyConstant(
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003229 Register out,
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003230 Register dividend,
3231 Register quotient,
3232 int64_t divisor,
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003233 UseScratchRegisterScope* temps_scope) {
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003234 Register temp_imm = temps_scope->AcquireSameSizeAs(out);
3235 __ Mov(temp_imm, divisor);
3236 __ Msub(out, quotient, temp_imm, dividend);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003237}
3238
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003239// Helper to generate code for HDiv/HRem instructions when a dividend is non-negative and
3240// a divisor is a positive constant, not power of 2.
3241void InstructionCodeGeneratorARM64::GenerateInt64UnsignedDivRemWithAnyPositiveConstant(
3242 HBinaryOperation* instruction) {
3243 DCHECK(instruction->IsDiv() || instruction->IsRem());
3244 DCHECK(instruction->GetResultType() == DataType::Type::kInt64);
3245
3246 LocationSummary* locations = instruction->GetLocations();
3247 Location second = locations->InAt(1);
3248 DCHECK(second.IsConstant());
3249
3250 Register out = OutputRegister(instruction);
3251 Register dividend = InputRegisterAt(instruction, 0);
3252 int64_t imm = Int64FromConstant(second.GetConstant());
3253 DCHECK_GT(imm, 0);
3254
3255 int64_t magic;
3256 int shift;
3257 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ true, &magic, &shift);
3258
3259 UseScratchRegisterScope temps(GetVIXLAssembler());
3260 Register temp = temps.AcquireSameSizeAs(out);
3261
3262 auto generate_unsigned_div_code = [this, magic, shift](Register out,
3263 Register dividend,
3264 Register temp) {
3265 // temp = get_high(dividend * magic)
3266 __ Mov(temp, magic);
3267 if (magic > 0 && shift == 0) {
3268 __ Smulh(out, dividend, temp);
3269 } else {
3270 __ Smulh(temp, dividend, temp);
3271 if (magic < 0) {
3272 // The negative magic means that the multiplier m is greater than INT64_MAX.
3273 // In such a case shift is never 0. See the proof in
3274 // InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant.
3275 __ Add(temp, temp, dividend);
3276 }
3277 DCHECK_NE(shift, 0);
3278 __ Lsr(out, temp, shift);
3279 }
3280 };
3281
3282 if (instruction->IsDiv()) {
3283 generate_unsigned_div_code(out, dividend, temp);
3284 } else {
3285 generate_unsigned_div_code(temp, dividend, temp);
3286 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3287 }
3288}
3289
3290// Helper to generate code for HDiv/HRem instructions for any dividend and a constant divisor
3291// (not power of 2).
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003292void InstructionCodeGeneratorARM64::GenerateInt64DivRemWithAnyConstant(
3293 HBinaryOperation* instruction) {
Zheng Xuc6667102015-05-15 16:08:45 +08003294 DCHECK(instruction->IsDiv() || instruction->IsRem());
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003295 DCHECK(instruction->GetResultType() == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003296
3297 LocationSummary* locations = instruction->GetLocations();
3298 Location second = locations->InAt(1);
3299 DCHECK(second.IsConstant());
3300
3301 Register out = OutputRegister(instruction);
3302 Register dividend = InputRegisterAt(instruction, 0);
3303 int64_t imm = Int64FromConstant(second.GetConstant());
3304
Zheng Xuc6667102015-05-15 16:08:45 +08003305 int64_t magic;
3306 int shift;
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003307 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ true, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08003308
3309 UseScratchRegisterScope temps(GetVIXLAssembler());
3310 Register temp = temps.AcquireSameSizeAs(out);
3311
3312 // temp = get_high(dividend * magic)
3313 __ Mov(temp, magic);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003314 __ Smulh(temp, dividend, temp);
3315
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003316 // The multiplication result might need some corrections to be finalized.
3317 // The last correction is to increment by 1, if the result is negative.
3318 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3319 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3320 // However if one of the corrections is ADD or SUB, the sign can be detected
3321 // with ADDS/SUBS. They set the N flag if the result is negative.
3322 // This allows to use CINC MI which has latency 1.
3323 bool use_cond_inc = false;
3324
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003325 // Some combinations of magic_number and the divisor require to correct the result.
3326 // Check whether the correction is needed.
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003327 if (NeedToAddDividend(magic, imm)) {
3328 __ Adds(temp, temp, dividend);
3329 use_cond_inc = true;
3330 } else if (NeedToSubDividend(magic, imm)) {
3331 __ Subs(temp, temp, dividend);
3332 use_cond_inc = true;
3333 }
3334
3335 if (shift != 0) {
3336 __ Asr(temp, temp, shift);
3337 }
3338
3339 if (instruction->IsRem()) {
3340 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3341 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3342 } else {
3343 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3344 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003345}
3346
3347void InstructionCodeGeneratorARM64::GenerateInt32DivRemWithAnyConstant(
3348 HBinaryOperation* instruction) {
3349 DCHECK(instruction->IsDiv() || instruction->IsRem());
3350 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
3351
3352 LocationSummary* locations = instruction->GetLocations();
3353 Location second = locations->InAt(1);
3354 DCHECK(second.IsConstant());
3355
3356 Register out = OutputRegister(instruction);
3357 Register dividend = InputRegisterAt(instruction, 0);
3358 int64_t imm = Int64FromConstant(second.GetConstant());
3359
3360 int64_t magic;
3361 int shift;
3362 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
3363 UseScratchRegisterScope temps(GetVIXLAssembler());
3364 Register temp = temps.AcquireSameSizeAs(out);
3365
3366 // temp = get_high(dividend * magic)
3367 __ Mov(temp, magic);
3368 __ Smull(temp.X(), dividend, temp);
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003369
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003370 // The multiplication result might need some corrections to be finalized.
3371 // The last correction is to increment by 1, if the result is negative.
3372 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3373 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3374 // However if one of the corrections is ADD or SUB, the sign can be detected
3375 // with ADDS/SUBS. They set the N flag if the result is negative.
3376 // This allows to use CINC MI which has latency 1.
3377 bool use_cond_inc = false;
3378
3379 // ADD/SUB correction is performed in the high 32 bits
3380 // as high 32 bits are ignored because type are kInt32.
3381 if (NeedToAddDividend(magic, imm)) {
3382 __ Adds(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3383 use_cond_inc = true;
3384 } else if (NeedToSubDividend(magic, imm)) {
3385 __ Subs(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3386 use_cond_inc = true;
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003387 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003388
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003389 // Extract the result from the high 32 bits and apply the final right shift.
3390 DCHECK_LT(shift, 32);
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003391 if (imm > 0 && HasNonNegativeInputAt(instruction, 0)) {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01003392 // No need to adjust the result for a non-negative dividend and a positive divisor.
3393 if (instruction->IsDiv()) {
3394 __ Lsr(out.X(), temp.X(), 32 + shift);
3395 } else {
3396 __ Lsr(temp.X(), temp.X(), 32 + shift);
3397 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3398 }
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003399 } else {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01003400 __ Asr(temp.X(), temp.X(), 32 + shift);
3401
3402 if (instruction->IsRem()) {
3403 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3404 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3405 } else {
3406 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3407 }
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003408 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003409}
3410
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003411void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction,
3412 int64_t divisor) {
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003413 DCHECK(instruction->IsDiv() || instruction->IsRem());
3414 if (instruction->GetResultType() == DataType::Type::kInt64) {
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003415 if (divisor > 0 && HasNonNegativeInputAt(instruction, 0)) {
3416 GenerateInt64UnsignedDivRemWithAnyPositiveConstant(instruction);
3417 } else {
3418 GenerateInt64DivRemWithAnyConstant(instruction);
3419 }
Zheng Xuc6667102015-05-15 16:08:45 +08003420 } else {
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003421 GenerateInt32DivRemWithAnyConstant(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003422 }
3423}
3424
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003425void InstructionCodeGeneratorARM64::GenerateIntDivForConstDenom(HDiv *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003426 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Zheng Xuc6667102015-05-15 16:08:45 +08003427
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003428 if (imm == 0) {
3429 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3430 return;
3431 }
Zheng Xuc6667102015-05-15 16:08:45 +08003432
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003433 if (IsPowerOfTwo(AbsOrMin(imm))) {
3434 GenerateIntDivForPower2Denom(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003435 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003436 // Cases imm == -1 or imm == 1 are handled by InstructionSimplifier.
3437 DCHECK(imm < -2 || imm > 2) << imm;
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003438 GenerateDivRemWithAnyConstant(instruction, imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003439 }
3440}
3441
3442void InstructionCodeGeneratorARM64::GenerateIntDiv(HDiv *instruction) {
3443 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
3444 << instruction->GetResultType();
3445
3446 if (instruction->GetLocations()->InAt(1).IsConstant()) {
3447 GenerateIntDivForConstDenom(instruction);
3448 } else {
3449 Register out = OutputRegister(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003450 Register dividend = InputRegisterAt(instruction, 0);
3451 Register divisor = InputRegisterAt(instruction, 1);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003452 __ Sdiv(out, dividend, divisor);
Zheng Xuc6667102015-05-15 16:08:45 +08003453 }
3454}
3455
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003456void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3457 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003458 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003459 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003460 case DataType::Type::kInt32:
3461 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003462 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003463 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003464 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3465 break;
3466
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003467 case DataType::Type::kFloat32:
3468 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003469 locations->SetInAt(0, Location::RequiresFpuRegister());
3470 locations->SetInAt(1, Location::RequiresFpuRegister());
3471 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3472 break;
3473
3474 default:
3475 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3476 }
3477}
3478
3479void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003480 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003481 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003482 case DataType::Type::kInt32:
3483 case DataType::Type::kInt64:
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003484 GenerateIntDiv(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003485 break;
3486
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003487 case DataType::Type::kFloat32:
3488 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003489 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3490 break;
3491
3492 default:
3493 LOG(FATAL) << "Unexpected div type " << type;
3494 }
3495}
3496
Alexandre Rames67555f72014-11-18 10:55:16 +00003497void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003498 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003499 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003500}
3501
3502void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3503 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003504 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003505 codegen_->AddSlowPath(slow_path);
3506 Location value = instruction->GetLocations()->InAt(0);
3507
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003508 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003509
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003510 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003511 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Elliott Hughesc1896c92018-11-29 11:33:18 -08003512 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003513 }
3514
Alexandre Rames67555f72014-11-18 10:55:16 +00003515 if (value.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003516 int64_t divisor = Int64FromLocation(value);
Alexandre Rames67555f72014-11-18 10:55:16 +00003517 if (divisor == 0) {
3518 __ B(slow_path->GetEntryLabel());
3519 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003520 // A division by a non-null constant is valid. We don't need to perform
3521 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003522 }
3523 } else {
3524 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3525 }
3526}
3527
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003528void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3529 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003530 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003531 locations->SetOut(Location::ConstantLocation(constant));
3532}
3533
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003534void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3535 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003536 // Will be generated at use site.
3537}
3538
Alexandre Rames5319def2014-10-23 10:03:10 +01003539void LocationsBuilderARM64::VisitExit(HExit* exit) {
3540 exit->SetLocations(nullptr);
3541}
3542
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003543void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003544}
3545
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003546void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3547 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003548 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003549 locations->SetOut(Location::ConstantLocation(constant));
3550}
3551
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003552void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003553 // Will be generated at use site.
3554}
3555
David Brazdilfc6a86a2015-06-26 10:33:45 +00003556void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003557 if (successor->IsExitBlock()) {
3558 DCHECK(got->GetPrevious()->AlwaysThrows());
3559 return; // no code needed
3560 }
3561
Serban Constantinescu02164b32014-11-13 14:05:07 +00003562 HBasicBlock* block = got->GetBlock();
3563 HInstruction* previous = got->GetPrevious();
3564 HLoopInformation* info = block->GetLoopInformation();
3565
David Brazdil46e2a392015-03-16 17:31:52 +00003566 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00003567 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003568 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3569 return;
3570 }
3571 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3572 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08003573 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003574 }
3575 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003576 __ B(codegen_->GetLabelOf(successor));
3577 }
3578}
3579
David Brazdilfc6a86a2015-06-26 10:33:45 +00003580void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3581 got->SetLocations(nullptr);
3582}
3583
3584void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3585 HandleGoto(got, got->GetSuccessor());
3586}
3587
3588void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3589 try_boundary->SetLocations(nullptr);
3590}
3591
3592void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3593 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3594 if (!successor->IsExitBlock()) {
3595 HandleGoto(try_boundary, successor);
3596 }
3597}
3598
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003599void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003600 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003601 vixl::aarch64::Label* true_target,
3602 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003603 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003604
David Brazdil0debae72015-11-12 18:37:00 +00003605 if (true_target == nullptr && false_target == nullptr) {
3606 // Nothing to do. The code always falls through.
3607 return;
3608 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003609 // Constant condition, statically compared against "true" (integer value 1).
3610 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003611 if (true_target != nullptr) {
3612 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003613 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003614 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003615 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003616 if (false_target != nullptr) {
3617 __ B(false_target);
3618 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003619 }
David Brazdil0debae72015-11-12 18:37:00 +00003620 return;
3621 }
3622
3623 // The following code generates these patterns:
3624 // (1) true_target == nullptr && false_target != nullptr
3625 // - opposite condition true => branch to false_target
3626 // (2) true_target != nullptr && false_target == nullptr
3627 // - condition true => branch to true_target
3628 // (3) true_target != nullptr && false_target != nullptr
3629 // - condition true => branch to true_target
3630 // - branch to false_target
3631 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003632 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003633 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003634 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003635 if (true_target == nullptr) {
3636 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3637 } else {
3638 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3639 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003640 } else {
3641 // The condition instruction has not been materialized, use its inputs as
3642 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003643 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003644
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003645 DataType::Type type = condition->InputAt(0)->GetType();
3646 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003647 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003648 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003649 IfCondition opposite_condition = condition->GetOppositeCondition();
3650 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003651 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003652 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003653 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003654 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003655 // Integer cases.
3656 Register lhs = InputRegisterAt(condition, 0);
3657 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003658
3659 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003660 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003661 if (true_target == nullptr) {
3662 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3663 non_fallthrough_target = false_target;
3664 } else {
3665 arm64_cond = ARM64Condition(condition->GetCondition());
3666 non_fallthrough_target = true_target;
3667 }
3668
Aart Bik086d27e2016-01-20 17:02:00 -08003669 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003670 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003671 switch (arm64_cond) {
3672 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003673 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003674 break;
3675 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003676 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003677 break;
3678 case lt:
3679 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003680 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003681 break;
3682 case ge:
3683 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003684 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003685 break;
3686 default:
3687 // Without the `static_cast` the compiler throws an error for
3688 // `-Werror=sign-promo`.
3689 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3690 }
3691 } else {
3692 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003693 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003694 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003695 }
3696 }
David Brazdil0debae72015-11-12 18:37:00 +00003697
3698 // If neither branch falls through (case 3), the conditional branch to `true_target`
3699 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3700 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003701 __ B(false_target);
3702 }
3703}
3704
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003705void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003706 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003707 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003708 locations->SetInAt(0, Location::RequiresRegister());
3709 }
3710}
3711
3712void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003713 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3714 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003715 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3716 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3717 true_target = nullptr;
3718 }
3719 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3720 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3721 false_target = nullptr;
3722 }
Andreas Gampe3db70682018-12-26 15:12:03 -08003723 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003724}
3725
3726void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003727 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003728 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003729 InvokeRuntimeCallingConvention calling_convention;
3730 RegisterSet caller_saves = RegisterSet::Empty();
3731 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3732 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003733 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003734 locations->SetInAt(0, Location::RequiresRegister());
3735 }
3736}
3737
3738void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003739 SlowPathCodeARM64* slow_path =
3740 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003741 GenerateTestAndBranch(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08003742 /* condition_input_index= */ 0,
David Brazdil0debae72015-11-12 18:37:00 +00003743 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08003744 /* false_target= */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003745}
3746
Mingyao Yang063fc772016-08-02 11:02:54 -07003747void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003748 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003749 LocationSummary(flag, LocationSummary::kNoCall);
3750 locations->SetOut(Location::RequiresRegister());
3751}
3752
3753void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3754 __ Ldr(OutputRegister(flag),
3755 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3756}
3757
David Brazdilc0b601b2016-02-08 14:20:45 +00003758static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3759 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003760 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003761}
3762
Alexandre Rames880f1192016-06-13 16:04:50 +01003763static inline Condition GetConditionForSelect(HCondition* condition) {
3764 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003765 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3766 : ARM64Condition(cond);
3767}
3768
David Brazdil74eb1b22015-12-14 11:44:01 +00003769void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003770 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003771 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003772 locations->SetInAt(0, Location::RequiresFpuRegister());
3773 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003774 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003775 } else {
3776 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3777 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3778 bool is_true_value_constant = cst_true_value != nullptr;
3779 bool is_false_value_constant = cst_false_value != nullptr;
3780 // Ask VIXL whether we should synthesize constants in registers.
3781 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3782 Operand true_op = is_true_value_constant ?
3783 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3784 Operand false_op = is_false_value_constant ?
3785 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3786 bool true_value_in_register = false;
3787 bool false_value_in_register = false;
3788 MacroAssembler::GetCselSynthesisInformation(
3789 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3790 true_value_in_register |= !is_true_value_constant;
3791 false_value_in_register |= !is_false_value_constant;
3792
3793 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3794 : Location::ConstantLocation(cst_true_value));
3795 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3796 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003797 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003798 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003799
David Brazdil74eb1b22015-12-14 11:44:01 +00003800 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3801 locations->SetInAt(2, Location::RequiresRegister());
3802 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003803}
3804
3805void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003806 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003807 Condition csel_cond;
3808
3809 if (IsBooleanValueOrMaterializedCondition(cond)) {
3810 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003811 // Use the condition flags set by the previous instruction.
3812 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003813 } else {
3814 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003815 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003816 }
3817 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003818 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003819 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003820 } else {
3821 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003822 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003823 }
3824
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003825 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003826 __ Fcsel(OutputFPRegister(select),
3827 InputFPRegisterAt(select, 1),
3828 InputFPRegisterAt(select, 0),
3829 csel_cond);
3830 } else {
3831 __ Csel(OutputRegister(select),
3832 InputOperandAt(select, 1),
3833 InputOperandAt(select, 0),
3834 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003835 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003836}
3837
David Srbecky0cf44932015-12-09 14:09:59 +00003838void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003839 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00003840}
3841
David Srbeckyd28f4a02016-03-14 17:14:24 +00003842void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3843 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003844}
3845
Vladimir Markodec78172020-06-19 15:31:23 +01003846void CodeGeneratorARM64::IncreaseFrame(size_t adjustment) {
3847 __ Claim(adjustment);
3848 GetAssembler()->cfi().AdjustCFAOffset(adjustment);
3849}
3850
3851void CodeGeneratorARM64::DecreaseFrame(size_t adjustment) {
3852 __ Drop(adjustment);
3853 GetAssembler()->cfi().AdjustCFAOffset(-adjustment);
3854}
3855
David Srbeckyc7098ff2016-02-09 14:30:11 +00003856void CodeGeneratorARM64::GenerateNop() {
3857 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003858}
3859
Alex Light3a73ffb2021-01-25 14:11:05 +00003860void LocationsBuilderARM64::VisitPredicatedInstanceFieldGet(
3861 HPredicatedInstanceFieldGet* instruction) {
3862 HandleFieldGet(instruction, instruction->GetFieldInfo());
3863}
3864
Alexandre Rames5319def2014-10-23 10:03:10 +01003865void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003866 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003867}
3868
Alex Light3a73ffb2021-01-25 14:11:05 +00003869void InstructionCodeGeneratorARM64::VisitPredicatedInstanceFieldGet(
3870 HPredicatedInstanceFieldGet* instruction) {
3871 vixl::aarch64::Label finish;
3872 __ Cbz(InputRegisterAt(instruction, 1), &finish);
3873 HandleFieldGet(instruction, instruction->GetFieldInfo());
3874 __ Bind(&finish);
3875}
3876
Alexandre Rames5319def2014-10-23 10:03:10 +01003877void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003878 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003879}
3880
3881void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003882 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003883}
3884
3885void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003886 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003887}
3888
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003889// Temp is used for read barrier.
3890static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3891 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003892 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003893 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3894 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3895 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3896 return 1;
3897 }
3898 return 0;
3899}
3900
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003901// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003902// interface pointer, one for loading the current interface.
3903// The other checks have one temp for loading the object's class.
3904static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3905 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3906 return 3;
3907 }
3908 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003909}
3910
Alexandre Rames67555f72014-11-18 10:55:16 +00003911void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003912 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003913 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003914 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003915 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003916 case TypeCheckKind::kExactCheck:
3917 case TypeCheckKind::kAbstractClassCheck:
3918 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00003919 case TypeCheckKind::kArrayObjectCheck: {
3920 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
3921 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
3922 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003923 break;
Vladimir Marko87584542017-12-12 17:47:52 +00003924 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003925 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003926 case TypeCheckKind::kUnresolvedCheck:
3927 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003928 call_kind = LocationSummary::kCallOnSlowPath;
3929 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00003930 case TypeCheckKind::kBitstringCheck:
3931 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003932 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003933
Vladimir Markoca6fff82017-10-03 14:49:14 +01003934 LocationSummary* locations =
3935 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003936 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003937 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003938 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003939 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003940 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3941 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3942 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3943 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3944 } else {
3945 locations->SetInAt(1, Location::RequiresRegister());
3946 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003947 // The "out" register is used as a temporary, so it overlaps with the inputs.
3948 // Note that TypeCheckSlowPathARM64 uses this register too.
3949 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003950 // Add temps if necessary for read barriers.
3951 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003952}
3953
3954void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003955 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003956 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003957 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003958 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003959 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3960 ? Register()
3961 : InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003962 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003963 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003964 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3965 DCHECK_LE(num_temps, 1u);
3966 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003967 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3968 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3969 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3970 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003971
Scott Wakeling97c72b72016-06-24 16:19:36 +01003972 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003973 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003974
3975 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003976 // Avoid null check if we know `obj` is not null.
3977 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003978 __ Cbz(obj, &zero);
3979 }
3980
Roland Levillain44015862016-01-22 11:47:17 +00003981 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003982 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003983 ReadBarrierOption read_barrier_option =
3984 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003985 // /* HeapReference<Class> */ out = obj->klass_
3986 GenerateReferenceLoadTwoRegisters(instruction,
3987 out_loc,
3988 obj_loc,
3989 class_offset,
3990 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003991 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003992 __ Cmp(out, cls);
3993 __ Cset(out, eq);
3994 if (zero.IsLinked()) {
3995 __ B(&done);
3996 }
3997 break;
3998 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003999
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004000 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004001 ReadBarrierOption read_barrier_option =
4002 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004003 // /* HeapReference<Class> */ out = obj->klass_
4004 GenerateReferenceLoadTwoRegisters(instruction,
4005 out_loc,
4006 obj_loc,
4007 class_offset,
4008 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004009 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004010 // If the class is abstract, we eagerly fetch the super class of the
4011 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004012 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004013 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004014 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004015 GenerateReferenceLoadOneRegister(instruction,
4016 out_loc,
4017 super_offset,
4018 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004019 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004020 // If `out` is null, we use it for the result, and jump to `done`.
4021 __ Cbz(out, &done);
4022 __ Cmp(out, cls);
4023 __ B(ne, &loop);
4024 __ Mov(out, 1);
4025 if (zero.IsLinked()) {
4026 __ B(&done);
4027 }
4028 break;
4029 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004030
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004031 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004032 ReadBarrierOption read_barrier_option =
4033 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004034 // /* HeapReference<Class> */ out = obj->klass_
4035 GenerateReferenceLoadTwoRegisters(instruction,
4036 out_loc,
4037 obj_loc,
4038 class_offset,
4039 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004040 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004041 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004042 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004043 __ Bind(&loop);
4044 __ Cmp(out, cls);
4045 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004046 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004047 GenerateReferenceLoadOneRegister(instruction,
4048 out_loc,
4049 super_offset,
4050 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004051 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004052 __ Cbnz(out, &loop);
4053 // If `out` is null, we use it for the result, and jump to `done`.
4054 __ B(&done);
4055 __ Bind(&success);
4056 __ Mov(out, 1);
4057 if (zero.IsLinked()) {
4058 __ B(&done);
4059 }
4060 break;
4061 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004062
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004063 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004064 ReadBarrierOption read_barrier_option =
4065 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004066 // /* HeapReference<Class> */ out = obj->klass_
4067 GenerateReferenceLoadTwoRegisters(instruction,
4068 out_loc,
4069 obj_loc,
4070 class_offset,
4071 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004072 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004073 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004074 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004075 __ Cmp(out, cls);
4076 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004077 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004078 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004079 GenerateReferenceLoadOneRegister(instruction,
4080 out_loc,
4081 component_offset,
4082 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004083 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004084 // If `out` is null, we use it for the result, and jump to `done`.
4085 __ Cbz(out, &done);
4086 __ Ldrh(out, HeapOperand(out, primitive_offset));
4087 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4088 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004089 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004090 __ Mov(out, 1);
4091 __ B(&done);
4092 break;
4093 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004094
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004095 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004096 // No read barrier since the slow path will retry upon failure.
4097 // /* HeapReference<Class> */ out = obj->klass_
4098 GenerateReferenceLoadTwoRegisters(instruction,
4099 out_loc,
4100 obj_loc,
4101 class_offset,
4102 maybe_temp_loc,
4103 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004104 __ Cmp(out, cls);
4105 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004106 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08004107 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004108 codegen_->AddSlowPath(slow_path);
4109 __ B(ne, slow_path->GetEntryLabel());
4110 __ Mov(out, 1);
4111 if (zero.IsLinked()) {
4112 __ B(&done);
4113 }
4114 break;
4115 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004116
Calin Juravle98893e12015-10-02 21:05:03 +01004117 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004118 case TypeCheckKind::kInterfaceCheck: {
4119 // Note that we indeed only call on slow path, but we always go
4120 // into the slow path for the unresolved and interface check
4121 // cases.
4122 //
4123 // We cannot directly call the InstanceofNonTrivial runtime
4124 // entry point without resorting to a type checking slow path
4125 // here (i.e. by calling InvokeRuntime directly), as it would
4126 // require to assign fixed registers for the inputs of this
4127 // HInstanceOf instruction (following the runtime calling
4128 // convention), which might be cluttered by the potential first
4129 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004130 //
4131 // TODO: Introduce a new runtime entry point taking the object
4132 // to test (instead of its class) as argument, and let it deal
4133 // with the read barrier issues. This will let us refactor this
4134 // case of the `switch` code as it was previously (with a direct
4135 // call to the runtime not using a type checking slow path).
4136 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004137 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004138 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08004139 instruction, /* is_fatal= */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004140 codegen_->AddSlowPath(slow_path);
4141 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004142 if (zero.IsLinked()) {
4143 __ B(&done);
4144 }
4145 break;
4146 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004147
4148 case TypeCheckKind::kBitstringCheck: {
4149 // /* HeapReference<Class> */ temp = obj->klass_
4150 GenerateReferenceLoadTwoRegisters(instruction,
4151 out_loc,
4152 obj_loc,
4153 class_offset,
4154 maybe_temp_loc,
4155 kWithoutReadBarrier);
4156
4157 GenerateBitstringTypeCheckCompare(instruction, out);
4158 __ Cset(out, eq);
4159 if (zero.IsLinked()) {
4160 __ B(&done);
4161 }
4162 break;
4163 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004164 }
4165
4166 if (zero.IsLinked()) {
4167 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004168 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004169 }
4170
4171 if (done.IsLinked()) {
4172 __ Bind(&done);
4173 }
4174
4175 if (slow_path != nullptr) {
4176 __ Bind(slow_path->GetExitLabel());
4177 }
4178}
4179
4180void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004181 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00004182 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004183 LocationSummary* locations =
4184 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004185 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00004186 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
4187 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
4188 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
4189 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
4190 } else {
4191 locations->SetInAt(1, Location::RequiresRegister());
4192 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004193 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4194 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004195}
4196
4197void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004198 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004199 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004200 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004201 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00004202 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
4203 ? Register()
4204 : InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004205 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4206 DCHECK_GE(num_temps, 1u);
4207 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004208 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004209 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4210 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004211 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004212 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4213 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4214 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4215 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4216 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4217 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4218 const uint32_t object_array_data_offset =
4219 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004220
Vladimir Marko87584542017-12-12 17:47:52 +00004221 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004222 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004223 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4224 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004225 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004226
Scott Wakeling97c72b72016-06-24 16:19:36 +01004227 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004228 // Avoid null check if we know obj is not null.
4229 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004230 __ Cbz(obj, &done);
4231 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004232
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004233 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004234 case TypeCheckKind::kExactCheck:
4235 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004236 // /* HeapReference<Class> */ temp = obj->klass_
4237 GenerateReferenceLoadTwoRegisters(instruction,
4238 temp_loc,
4239 obj_loc,
4240 class_offset,
4241 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004242 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004243
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004244 __ Cmp(temp, cls);
4245 // Jump to slow path for throwing the exception or doing a
4246 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004247 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004248 break;
4249 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004250
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004251 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004252 // /* HeapReference<Class> */ temp = obj->klass_
4253 GenerateReferenceLoadTwoRegisters(instruction,
4254 temp_loc,
4255 obj_loc,
4256 class_offset,
4257 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004258 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004259
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004260 // If the class is abstract, we eagerly fetch the super class of the
4261 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004262 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004263 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004264 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004265 GenerateReferenceLoadOneRegister(instruction,
4266 temp_loc,
4267 super_offset,
4268 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004269 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004270
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004271 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4272 // exception.
4273 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4274 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004275 __ Cmp(temp, cls);
4276 __ B(ne, &loop);
4277 break;
4278 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004279
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004280 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004281 // /* HeapReference<Class> */ temp = obj->klass_
4282 GenerateReferenceLoadTwoRegisters(instruction,
4283 temp_loc,
4284 obj_loc,
4285 class_offset,
4286 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004287 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004288
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004289 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004290 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004291 __ Bind(&loop);
4292 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004293 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004294
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004295 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004296 GenerateReferenceLoadOneRegister(instruction,
4297 temp_loc,
4298 super_offset,
4299 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004300 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004301
4302 // If the class reference currently in `temp` is not null, jump
4303 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004304 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004305 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004306 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004307 break;
4308 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004309
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004310 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004311 // /* HeapReference<Class> */ temp = obj->klass_
4312 GenerateReferenceLoadTwoRegisters(instruction,
4313 temp_loc,
4314 obj_loc,
4315 class_offset,
4316 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004317 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004318
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004319 // Do an exact check.
4320 __ Cmp(temp, cls);
4321 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004322
4323 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004324 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004325 GenerateReferenceLoadOneRegister(instruction,
4326 temp_loc,
4327 component_offset,
4328 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004329 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004330
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004331 // If the component type is null, jump to the slow path to throw the exception.
4332 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4333 // Otherwise, the object is indeed an array. Further check that this component type is not a
4334 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004335 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4336 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004337 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004338 break;
4339 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004340
Calin Juravle98893e12015-10-02 21:05:03 +01004341 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004342 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004343 //
4344 // We cannot directly call the CheckCast runtime entry point
4345 // without resorting to a type checking slow path here (i.e. by
4346 // calling InvokeRuntime directly), as it would require to
4347 // assign fixed registers for the inputs of this HInstanceOf
4348 // instruction (following the runtime calling convention), which
4349 // might be cluttered by the potential first read barrier
4350 // emission at the beginning of this method.
4351 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004352 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004353 case TypeCheckKind::kInterfaceCheck: {
4354 // /* HeapReference<Class> */ temp = obj->klass_
4355 GenerateReferenceLoadTwoRegisters(instruction,
4356 temp_loc,
4357 obj_loc,
4358 class_offset,
4359 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004360 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004361
4362 // /* HeapReference<Class> */ temp = temp->iftable_
4363 GenerateReferenceLoadTwoRegisters(instruction,
4364 temp_loc,
4365 temp_loc,
4366 iftable_offset,
4367 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004368 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004369 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004370 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004371 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004372 vixl::aarch64::Label start_loop;
4373 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004374 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004375 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4376 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004377 // Go to next interface.
4378 __ Add(temp, temp, 2 * kHeapReferenceSize);
4379 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004380 // Compare the classes and continue the loop if they do not match.
4381 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4382 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004383 break;
4384 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004385
4386 case TypeCheckKind::kBitstringCheck: {
4387 // /* HeapReference<Class> */ temp = obj->klass_
4388 GenerateReferenceLoadTwoRegisters(instruction,
4389 temp_loc,
4390 obj_loc,
4391 class_offset,
4392 maybe_temp2_loc,
4393 kWithoutReadBarrier);
4394
4395 GenerateBitstringTypeCheckCompare(instruction, temp);
4396 __ B(ne, type_check_slow_path->GetEntryLabel());
4397 break;
4398 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004399 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004400 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004401
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004402 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004403}
4404
Alexandre Rames5319def2014-10-23 10:03:10 +01004405void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004406 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004407 locations->SetOut(Location::ConstantLocation(constant));
4408}
4409
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004410void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004411 // Will be generated at use site.
4412}
4413
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004414void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004415 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004416 locations->SetOut(Location::ConstantLocation(constant));
4417}
4418
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004419void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004420 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004421}
4422
Calin Juravle175dc732015-08-25 15:42:32 +01004423void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4424 // The trampoline uses the same calling convention as dex calling conventions,
4425 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4426 // the method_idx.
4427 HandleInvoke(invoke);
4428}
4429
4430void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4431 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004432 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004433}
4434
Alexandre Rames5319def2014-10-23 10:03:10 +01004435void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004436 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004437 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004438}
4439
Alexandre Rames67555f72014-11-18 10:55:16 +00004440void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4441 HandleInvoke(invoke);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004442 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
4443 // We cannot request ip1 as it's blocked by the register allocator.
4444 invoke->GetLocations()->SetInAt(invoke->GetNumberOfArguments() - 1, Location::Any());
4445 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004446}
4447
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004448void CodeGeneratorARM64::MaybeGenerateInlineCacheCheck(HInstruction* instruction,
4449 Register klass) {
4450 DCHECK_EQ(klass.GetCode(), 0u);
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004451 // We know the destination of an intrinsic, so no need to record inline
4452 // caches.
4453 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00004454 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004455 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004456 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
Nicolas Geoffray095dc462020-08-17 16:40:28 +01004457 ScopedProfilingInfoUse spiu(
4458 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
4459 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00004460 if (info != nullptr) {
4461 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
4462 uint64_t address = reinterpret_cast64<uint64_t>(cache);
4463 vixl::aarch64::Label done;
4464 __ Mov(x8, address);
4465 __ Ldr(x9, MemOperand(x8, InlineCache::ClassesOffset().Int32Value()));
4466 // Fast path for a monomorphic cache.
4467 __ Cmp(klass, x9);
4468 __ B(eq, &done);
4469 InvokeRuntime(kQuickUpdateInlineCache, instruction, instruction->GetDexPc());
4470 __ Bind(&done);
4471 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004472 }
4473}
4474
Alexandre Rames67555f72014-11-18 10:55:16 +00004475void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4476 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004477 LocationSummary* locations = invoke->GetLocations();
4478 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004479 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004480 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004481 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004482
Artem Serov914d7a82017-02-07 14:33:49 +00004483 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004484 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004485 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004486 {
4487 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4488 // /* HeapReference<Class> */ temp = temp->klass_
4489 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4490 codegen_->MaybeRecordImplicitNullCheck(invoke);
4491 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004492 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004493 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004494 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004495 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004496 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004497 }
Artem Serov914d7a82017-02-07 14:33:49 +00004498
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004499 // Instead of simply (possibly) unpoisoning `temp` here, we should
4500 // emit a read barrier for the previous class reference load.
4501 // However this is not required in practice, as this is an
4502 // intermediate/temporary reference and because the current
4503 // concurrent copying collector keeps the from-space memory
4504 // intact/accessible until the end of the marking phase (the
4505 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004506 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004507
4508 // If we're compiling baseline, update the inline cache.
4509 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
4510
4511 // The register ip1 is required to be used for the hidden argument in
4512 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
4513 MacroAssembler* masm = GetVIXLAssembler();
4514 UseScratchRegisterScope scratch_scope(masm);
4515 scratch_scope.Exclude(ip1);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004516 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
4517 Location interface_method = locations->InAt(invoke->GetNumberOfArguments() - 1);
4518 if (interface_method.IsStackSlot()) {
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00004519 __ Ldr(ip1, StackOperandFrom(interface_method));
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004520 } else {
4521 __ Mov(ip1, XRegisterFrom(interface_method));
4522 }
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00004523 // If the load kind is through a runtime call, we will pass the method we
4524 // fetch the IMT, which will either be a no-op if we don't hit the conflict
4525 // stub, or will make us always go through the trampoline when there is a
4526 // conflict.
4527 } else if (invoke->GetHiddenArgumentLoadKind() != MethodLoadKind::kRuntimeCall) {
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004528 codegen_->LoadMethod(
4529 invoke->GetHiddenArgumentLoadKind(), Location::RegisterLocation(ip1.GetCode()), invoke);
4530 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004531
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004532 __ Ldr(temp,
4533 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4534 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004535 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004536 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004537 __ Ldr(temp, MemOperand(temp, method_offset));
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00004538 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRuntimeCall) {
4539 // We pass the method from the IMT in case of a conflict. This will ensure
4540 // we go into the runtime to resolve the actual method.
4541 __ Mov(ip1, temp);
4542 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004543 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004544 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004545
4546 {
4547 // Ensure the pc position is recorded immediately after the `blr` instruction.
4548 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4549
4550 // lr();
4551 __ blr(lr);
4552 DCHECK(!codegen_->IsLeafMethod());
4553 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4554 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004555
Andreas Gampe3db70682018-12-26 15:12:03 -08004556 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004557}
4558
4559void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004560 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004561 if (intrinsic.TryDispatch(invoke)) {
4562 return;
4563 }
4564
Alexandre Rames67555f72014-11-18 10:55:16 +00004565 HandleInvoke(invoke);
4566}
4567
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004568void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004569 // Explicit clinit checks triggered by static invokes must have been pruned by
4570 // art::PrepareForRegisterAllocation.
4571 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004572
Vladimir Markoca6fff82017-10-03 14:49:14 +01004573 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004574 if (intrinsic.TryDispatch(invoke)) {
4575 return;
4576 }
4577
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004578 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
Vladimir Marko86c87522020-05-11 16:55:55 +01004579 CriticalNativeCallingConventionVisitorARM64 calling_convention_visitor(
4580 /*for_register_allocation=*/ true);
4581 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4582 } else {
4583 HandleInvoke(invoke);
4584 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004585}
4586
Andreas Gampe878d58c2015-01-15 23:24:00 -08004587static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4588 if (invoke->GetLocations()->Intrinsified()) {
4589 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4590 intrinsic.Dispatch(invoke);
4591 return true;
4592 }
4593 return false;
4594}
4595
Vladimir Markodc151b22015-10-15 18:02:30 +01004596HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4597 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004598 ArtMethod* method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004599 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004600 return desired_dispatch_info;
4601}
4602
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004603void CodeGeneratorARM64::LoadMethod(MethodLoadKind load_kind, Location temp, HInvoke* invoke) {
4604 switch (load_kind) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004605 case MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01004606 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01004607 // Add ADRP with its PC-relative method patch.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004608 vixl::aarch64::Label* adrp_label =
4609 NewBootImageMethodPatch(invoke->GetResolvedMethodReference());
Vladimir Marko65979462017-05-19 17:25:12 +01004610 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4611 // Add ADD with its PC-relative method patch.
4612 vixl::aarch64::Label* add_label =
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004613 NewBootImageMethodPatch(invoke->GetResolvedMethodReference(), adrp_label);
Vladimir Marko65979462017-05-19 17:25:12 +01004614 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4615 break;
4616 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004617 case MethodLoadKind::kBootImageRelRo: {
Vladimir Markob066d432018-01-03 13:14:37 +00004618 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004619 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00004620 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_offset);
4621 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4622 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
4623 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_offset, adrp_label);
4624 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
4625 EmitLdrOffsetPlaceholder(ldr_label, WRegisterFrom(temp), XRegisterFrom(temp));
4626 break;
4627 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004628 case MethodLoadKind::kBssEntry: {
Vladimir Markob066d432018-01-03 13:14:37 +00004629 // Add ADRP with its PC-relative .bss entry patch.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004630 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(invoke->GetMethodReference());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004631 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Markob066d432018-01-03 13:14:37 +00004632 // Add LDR with its PC-relative .bss entry patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004633 vixl::aarch64::Label* ldr_label =
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004634 NewMethodBssEntryPatch(invoke->GetMethodReference(), adrp_label);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01004635 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004636 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004637 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004638 }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004639 case MethodLoadKind::kJitDirectAddress: {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004640 // Load method address from literal pool.
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004641 __ Ldr(XRegisterFrom(temp),
4642 DeduplicateUint64Literal(reinterpret_cast<uint64_t>(invoke->GetResolvedMethod())));
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004643 break;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004644 }
4645 case MethodLoadKind::kRuntimeCall: {
4646 // Test situation, don't do anything.
4647 break;
4648 }
4649 default: {
4650 LOG(FATAL) << "Load kind should have already been handled " << load_kind;
4651 UNREACHABLE();
4652 }
4653 }
4654}
4655
4656void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4657 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
4658 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
4659 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4660 switch (invoke->GetMethodLoadKind()) {
4661 case MethodLoadKind::kStringInit: {
4662 uint32_t offset =
4663 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4664 // temp = thread->string_init_entrypoint
4665 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
4666 break;
4667 }
4668 case MethodLoadKind::kRecursive: {
4669 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
4670 break;
4671 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004672 case MethodLoadKind::kRuntimeCall: {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004673 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4674 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004675 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004676 case MethodLoadKind::kBootImageLinkTimePcRelative:
4677 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
4678 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
4679 // Do not materialize the method pointer, load directly the entrypoint.
4680 // Add ADRP with its PC-relative JNI entrypoint patch.
4681 vixl::aarch64::Label* adrp_label =
4682 NewBootImageJniEntrypointPatch(invoke->GetResolvedMethodReference());
4683 EmitAdrpPlaceholder(adrp_label, lr);
4684 // Add the LDR with its PC-relative method patch.
4685 vixl::aarch64::Label* add_label =
4686 NewBootImageJniEntrypointPatch(invoke->GetResolvedMethodReference(), adrp_label);
4687 EmitLdrOffsetPlaceholder(add_label, lr, lr);
4688 break;
4689 }
4690 FALLTHROUGH_INTENDED;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004691 default: {
4692 LoadMethod(invoke->GetMethodLoadKind(), temp, invoke);
4693 break;
4694 }
Vladimir Marko58155012015-08-19 12:49:41 +00004695 }
4696
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004697 auto call_lr = [&]() {
4698 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4699 ExactAssemblyScope eas(GetVIXLAssembler(),
4700 kInstructionSize,
4701 CodeBufferCheckScope::kExactSize);
4702 // lr()
4703 __ blr(lr);
4704 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko86c87522020-05-11 16:55:55 +01004705 };
Vladimir Marko58155012015-08-19 12:49:41 +00004706 switch (invoke->GetCodePtrLocation()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004707 case CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004708 {
Nicolas Geoffray282795c2021-09-24 18:16:41 +01004709 DCHECK(!GetGraph()->HasShouldDeoptimizeFlag());
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004710 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4711 ExactAssemblyScope eas(GetVIXLAssembler(),
4712 kInstructionSize,
4713 CodeBufferCheckScope::kExactSize);
4714 __ bl(&frame_entry_label_);
4715 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4716 }
Vladimir Marko58155012015-08-19 12:49:41 +00004717 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004718 case CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01004719 size_t out_frame_size =
4720 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorARM64,
4721 kAapcs64StackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01004722 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004723 if (invoke->GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative) {
4724 call_lr();
4725 } else {
4726 // LR = callee_method->ptr_sized_fields_.data_; // EntryPointFromJni
4727 MemberOffset offset = ArtMethod::EntryPointFromJniOffset(kArm64PointerSize);
4728 __ Ldr(lr, MemOperand(XRegisterFrom(callee_method), offset.Int32Value()));
4729 // lr()
4730 call_lr();
4731 }
Vladimir Marko86c87522020-05-11 16:55:55 +01004732 // Zero-/sign-extend the result when needed due to native and managed ABI mismatch.
4733 switch (invoke->GetType()) {
4734 case DataType::Type::kBool:
4735 __ Ubfx(w0, w0, 0, 8);
4736 break;
4737 case DataType::Type::kInt8:
4738 __ Sbfx(w0, w0, 0, 8);
4739 break;
4740 case DataType::Type::kUint16:
4741 __ Ubfx(w0, w0, 0, 16);
4742 break;
4743 case DataType::Type::kInt16:
4744 __ Sbfx(w0, w0, 0, 16);
4745 break;
4746 case DataType::Type::kInt32:
4747 case DataType::Type::kInt64:
4748 case DataType::Type::kFloat32:
4749 case DataType::Type::kFloat64:
4750 case DataType::Type::kVoid:
4751 break;
4752 default:
4753 DCHECK(false) << invoke->GetType();
4754 break;
4755 }
4756 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01004757 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01004758 }
4759 break;
4760 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004761 case CodePtrLocation::kCallArtMethod: {
4762 // LR = callee_method->ptr_sized_fields_.entry_point_from_quick_compiled_code_;
4763 MemberOffset offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
4764 __ Ldr(lr, MemOperand(XRegisterFrom(callee_method), offset.Int32Value()));
4765 // lr()
4766 call_lr();
Vladimir Marko58155012015-08-19 12:49:41 +00004767 break;
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004768 }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004769 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004770
Andreas Gampe878d58c2015-01-15 23:24:00 -08004771 DCHECK(!IsLeafMethod());
4772}
4773
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004774void CodeGeneratorARM64::GenerateVirtualCall(
4775 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004776 // Use the calling convention instead of the location of the receiver, as
4777 // intrinsics may have put the receiver in a different register. In the intrinsics
4778 // slow path, the arguments have been moved to the right place, so here we are
4779 // guaranteed that the receiver is the first register of the calling convention.
4780 InvokeDexCallingConvention calling_convention;
4781 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004782 Register temp = XRegisterFrom(temp_in);
4783 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4784 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4785 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004786 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004787
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004788 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004789
4790 {
4791 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4792 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4793 // /* HeapReference<Class> */ temp = receiver->klass_
4794 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4795 MaybeRecordImplicitNullCheck(invoke);
4796 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004797 // Instead of simply (possibly) unpoisoning `temp` here, we should
4798 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004799 // intermediate/temporary reference and because the current
4800 // concurrent copying collector keeps the from-space memory
4801 // intact/accessible until the end of the marking phase (the
4802 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004803 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004804
4805 // If we're compiling baseline, update the inline cache.
4806 MaybeGenerateInlineCacheCheck(invoke, temp);
4807
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004808 // temp = temp->GetMethodAt(method_offset);
4809 __ Ldr(temp, MemOperand(temp, method_offset));
4810 // lr = temp->GetEntryPoint();
4811 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004812 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004813 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004814 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4815 // lr();
4816 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004817 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004818 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004819}
4820
Vladimir Marko9922f002020-06-08 15:05:15 +01004821void CodeGeneratorARM64::MoveFromReturnRegister(Location trg, DataType::Type type) {
4822 if (!trg.IsValid()) {
4823 DCHECK(type == DataType::Type::kVoid);
4824 return;
4825 }
4826
4827 DCHECK_NE(type, DataType::Type::kVoid);
4828
4829 if (DataType::IsIntegralType(type) || type == DataType::Type::kReference) {
4830 Register trg_reg = RegisterFrom(trg, type);
4831 Register res_reg = RegisterFrom(ARM64ReturnLocation(type), type);
4832 __ Mov(trg_reg, res_reg, kDiscardForSameWReg);
4833 } else {
4834 VRegister trg_reg = FPRegisterFrom(trg, type);
4835 VRegister res_reg = FPRegisterFrom(ARM64ReturnLocation(type), type);
4836 __ Fmov(trg_reg, res_reg);
4837 }
4838}
4839
Orion Hodsonac141392017-01-13 11:53:47 +00004840void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00004841 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
4842 if (intrinsic.TryDispatch(invoke)) {
4843 return;
4844 }
Orion Hodsonac141392017-01-13 11:53:47 +00004845 HandleInvoke(invoke);
4846}
4847
4848void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00004849 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4850 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
4851 return;
4852 }
Orion Hodsonac141392017-01-13 11:53:47 +00004853 codegen_->GenerateInvokePolymorphicCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004854 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004855}
4856
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004857void LocationsBuilderARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4858 HandleInvoke(invoke);
4859}
4860
4861void InstructionCodeGeneratorARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4862 codegen_->GenerateInvokeCustomCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004863 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004864}
4865
Vladimir Marko6fd16062018-06-26 11:02:04 +01004866vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageIntrinsicPatch(
4867 uint32_t intrinsic_data,
4868 vixl::aarch64::Label* adrp_label) {
4869 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004870 /* dex_file= */ nullptr, intrinsic_data, adrp_label, &boot_image_other_patches_);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004871}
4872
Vladimir Markob066d432018-01-03 13:14:37 +00004873vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageRelRoPatch(
4874 uint32_t boot_image_offset,
4875 vixl::aarch64::Label* adrp_label) {
4876 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004877 /* dex_file= */ nullptr, boot_image_offset, adrp_label, &boot_image_other_patches_);
Vladimir Markob066d432018-01-03 13:14:37 +00004878}
4879
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004880vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004881 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004882 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004883 return NewPcRelativePatch(
4884 target_method.dex_file, target_method.index, adrp_label, &boot_image_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004885}
4886
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004887vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4888 MethodReference target_method,
4889 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004890 return NewPcRelativePatch(
4891 target_method.dex_file, target_method.index, adrp_label, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004892}
4893
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004894vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageTypePatch(
Scott Wakeling97c72b72016-06-24 16:19:36 +01004895 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004896 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004897 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004898 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &boot_image_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004899}
4900
Vladimir Marko1998cd02017-01-13 13:02:58 +00004901vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
Vladimir Marko8f63f102020-09-28 12:10:28 +01004902 HLoadClass* load_class,
Vladimir Marko1998cd02017-01-13 13:02:58 +00004903 vixl::aarch64::Label* adrp_label) {
Vladimir Marko8f63f102020-09-28 12:10:28 +01004904 const DexFile& dex_file = load_class->GetDexFile();
4905 dex::TypeIndex type_index = load_class->GetTypeIndex();
4906 ArenaDeque<PcRelativePatchInfo>* patches = nullptr;
4907 switch (load_class->GetLoadKind()) {
4908 case HLoadClass::LoadKind::kBssEntry:
4909 patches = &type_bss_entry_patches_;
4910 break;
4911 case HLoadClass::LoadKind::kBssEntryPublic:
4912 patches = &public_type_bss_entry_patches_;
4913 break;
4914 case HLoadClass::LoadKind::kBssEntryPackage:
4915 patches = &package_type_bss_entry_patches_;
4916 break;
4917 default:
4918 LOG(FATAL) << "Unexpected load kind: " << load_class->GetLoadKind();
4919 UNREACHABLE();
4920 }
4921 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004922}
4923
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004924vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004925 const DexFile& dex_file,
4926 dex::StringIndex string_index,
4927 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004928 return NewPcRelativePatch(
4929 &dex_file, string_index.index_, adrp_label, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01004930}
4931
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004932vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4933 const DexFile& dex_file,
4934 dex::StringIndex string_index,
4935 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004936 return NewPcRelativePatch(&dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004937}
4938
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004939vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageJniEntrypointPatch(
4940 MethodReference target_method,
4941 vixl::aarch64::Label* adrp_label) {
4942 return NewPcRelativePatch(
4943 target_method.dex_file, target_method.index, adrp_label, &boot_image_jni_entrypoint_patches_);
4944}
4945
Vladimir Markof6675082019-05-17 12:05:28 +01004946void CodeGeneratorARM64::EmitEntrypointThunkCall(ThreadOffset64 entrypoint_offset) {
4947 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01004948 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markof6675082019-05-17 12:05:28 +01004949 call_entrypoint_patches_.emplace_back(/*dex_file*/ nullptr, entrypoint_offset.Uint32Value());
4950 vixl::aarch64::Label* bl_label = &call_entrypoint_patches_.back().label;
4951 __ bind(bl_label);
4952 __ bl(static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4953}
4954
Vladimir Marko966b46f2018-08-03 10:20:19 +00004955void CodeGeneratorARM64::EmitBakerReadBarrierCbnz(uint32_t custom_data) {
Vladimir Marko94796f82018-08-08 15:15:33 +01004956 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01004957 if (GetCompilerOptions().IsJitCompiler()) {
Vladimir Marko966b46f2018-08-03 10:20:19 +00004958 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
4959 vixl::aarch64::Label* slow_path_entry = &it->second.label;
4960 __ cbnz(mr, slow_path_entry);
4961 } else {
4962 baker_read_barrier_patches_.emplace_back(custom_data);
4963 vixl::aarch64::Label* cbnz_label = &baker_read_barrier_patches_.back().label;
4964 __ bind(cbnz_label);
4965 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4966 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004967}
4968
Scott Wakeling97c72b72016-06-24 16:19:36 +01004969vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004970 const DexFile* dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004971 uint32_t offset_or_index,
4972 vixl::aarch64::Label* adrp_label,
4973 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004974 // Add a patch entry and return the label.
4975 patches->emplace_back(dex_file, offset_or_index);
4976 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004977 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004978 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4979 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4980 return label;
4981}
4982
Scott Wakeling97c72b72016-06-24 16:19:36 +01004983vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4984 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004985 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004986}
4987
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004988vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004989 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004990 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004991 return jit_string_patches_.GetOrCreate(
4992 StringReference(&dex_file, string_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004993 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004994}
4995
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004996vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004997 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004998 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004999 return jit_class_patches_.GetOrCreate(
5000 TypeReference(&dex_file, type_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08005001 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005002}
5003
Vladimir Markoaad75c62016-10-03 08:46:48 +00005004void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
5005 vixl::aarch64::Register reg) {
5006 DCHECK(reg.IsX());
5007 SingleEmissionCheckScope guard(GetVIXLAssembler());
5008 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005009 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005010}
5011
5012void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
5013 vixl::aarch64::Register out,
5014 vixl::aarch64::Register base) {
5015 DCHECK(out.IsX());
5016 DCHECK(base.IsX());
5017 SingleEmissionCheckScope guard(GetVIXLAssembler());
5018 __ Bind(fixup_label);
5019 __ add(out, base, Operand(/* offset placeholder */ 0));
5020}
5021
5022void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
5023 vixl::aarch64::Register out,
5024 vixl::aarch64::Register base) {
5025 DCHECK(base.IsX());
5026 SingleEmissionCheckScope guard(GetVIXLAssembler());
5027 __ Bind(fixup_label);
5028 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
5029}
5030
Vladimir Markoeebb8212018-06-05 14:57:24 +01005031void CodeGeneratorARM64::LoadBootImageAddress(vixl::aarch64::Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01005032 uint32_t boot_image_reference) {
5033 if (GetCompilerOptions().IsBootImage()) {
5034 // Add ADRP with its PC-relative type patch.
5035 vixl::aarch64::Label* adrp_label = NewBootImageIntrinsicPatch(boot_image_reference);
5036 EmitAdrpPlaceholder(adrp_label, reg.X());
5037 // Add ADD with its PC-relative type patch.
5038 vixl::aarch64::Label* add_label = NewBootImageIntrinsicPatch(boot_image_reference, adrp_label);
5039 EmitAddPlaceholder(add_label, reg.X(), reg.X());
Vladimir Markoa2da9b92018-10-10 14:21:55 +01005040 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01005041 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01005042 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01005043 EmitAdrpPlaceholder(adrp_label, reg.X());
5044 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01005045 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_reference, adrp_label);
Vladimir Markoeebb8212018-06-05 14:57:24 +01005046 EmitLdrOffsetPlaceholder(ldr_label, reg.W(), reg.X());
5047 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01005048 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01005049 gc::Heap* heap = Runtime::Current()->GetHeap();
5050 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005051 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01005052 __ Ldr(reg.W(), DeduplicateBootImageAddressLiteral(reinterpret_cast<uintptr_t>(address)));
5053 }
5054}
5055
Vladimir Marko98873af2020-12-16 12:10:03 +00005056void CodeGeneratorARM64::LoadTypeForBootImageIntrinsic(vixl::aarch64::Register reg,
5057 TypeReference target_type) {
5058 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
5059 DCHECK(GetCompilerOptions().IsBootImage());
5060 // Add ADRP with its PC-relative type patch.
5061 vixl::aarch64::Label* adrp_label =
5062 NewBootImageTypePatch(*target_type.dex_file, target_type.TypeIndex());
5063 EmitAdrpPlaceholder(adrp_label, reg.X());
5064 // Add ADD with its PC-relative type patch.
5065 vixl::aarch64::Label* add_label =
5066 NewBootImageTypePatch(*target_type.dex_file, target_type.TypeIndex(), adrp_label);
5067 EmitAddPlaceholder(add_label, reg.X(), reg.X());
5068}
5069
Vladimir Markode91ca92020-10-27 13:41:40 +00005070void CodeGeneratorARM64::LoadIntrinsicDeclaringClass(vixl::aarch64::Register reg, HInvoke* invoke) {
5071 DCHECK_NE(invoke->GetIntrinsic(), Intrinsics::kNone);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005072 if (GetCompilerOptions().IsBootImage()) {
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005073 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01005074 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
Vladimir Marko98873af2020-12-16 12:10:03 +00005075 LoadTypeForBootImageIntrinsic(reg, TypeReference(target_method.dex_file, type_idx));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005076 } else {
Vladimir Markode91ca92020-10-27 13:41:40 +00005077 uint32_t boot_image_offset = GetBootImageOffsetOfIntrinsicDeclaringClass(invoke);
5078 LoadBootImageAddress(reg, boot_image_offset);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005079 }
Vladimir Marko6fd16062018-06-26 11:02:04 +01005080}
5081
Vladimir Marko98873af2020-12-16 12:10:03 +00005082void CodeGeneratorARM64::LoadClassRootForIntrinsic(vixl::aarch64::Register reg,
5083 ClassRoot class_root) {
5084 if (GetCompilerOptions().IsBootImage()) {
5085 ScopedObjectAccess soa(Thread::Current());
5086 ObjPtr<mirror::Class> klass = GetClassRoot(class_root);
5087 TypeReference target_type(&klass->GetDexFile(), klass->GetDexTypeIndex());
5088 LoadTypeForBootImageIntrinsic(reg, target_type);
5089 } else {
5090 uint32_t boot_image_offset = GetBootImageOffset(class_root);
5091 LoadBootImageAddress(reg, boot_image_offset);
5092 }
5093}
5094
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005095template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005096inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
5097 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005098 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005099 for (const PcRelativePatchInfo& info : infos) {
5100 linker_patches->push_back(Factory(info.label.GetLocation(),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005101 info.target_dex_file,
Vladimir Markoaad75c62016-10-03 08:46:48 +00005102 info.pc_insn_label->GetLocation(),
5103 info.offset_or_index));
5104 }
5105}
5106
Vladimir Marko6fd16062018-06-26 11:02:04 +01005107template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5108linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5109 const DexFile* target_dex_file,
5110 uint32_t pc_insn_offset,
5111 uint32_t boot_image_offset) {
5112 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5113 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005114}
5115
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005116void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005117 DCHECK(linker_patches->empty());
5118 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005119 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005120 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005121 boot_image_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005122 type_bss_entry_patches_.size() +
Vladimir Marko8f63f102020-09-28 12:10:28 +01005123 public_type_bss_entry_patches_.size() +
5124 package_type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005125 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005126 string_bss_entry_patches_.size() +
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005127 boot_image_jni_entrypoint_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005128 boot_image_other_patches_.size() +
Vladimir Markof6675082019-05-17 12:05:28 +01005129 call_entrypoint_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005130 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00005131 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005132 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005133 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005134 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005135 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005136 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005137 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005138 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01005139 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005140 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005141 DCHECK(boot_image_type_patches_.empty());
5142 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005143 }
5144 if (GetCompilerOptions().IsBootImage()) {
5145 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5146 boot_image_other_patches_, linker_patches);
5147 } else {
5148 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5149 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005150 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005151 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5152 method_bss_entry_patches_, linker_patches);
5153 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5154 type_bss_entry_patches_, linker_patches);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005155 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PublicTypeBssEntryPatch>(
5156 public_type_bss_entry_patches_, linker_patches);
5157 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PackageTypeBssEntryPatch>(
5158 package_type_bss_entry_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005159 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5160 string_bss_entry_patches_, linker_patches);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005161 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeJniEntrypointPatch>(
5162 boot_image_jni_entrypoint_patches_, linker_patches);
Vladimir Markof6675082019-05-17 12:05:28 +01005163 for (const PatchInfo<vixl::aarch64::Label>& info : call_entrypoint_patches_) {
5164 DCHECK(info.target_dex_file == nullptr);
5165 linker_patches->push_back(linker::LinkerPatch::CallEntrypointPatch(
5166 info.label.GetLocation(), info.offset_or_index));
5167 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005168 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005169 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
5170 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005171 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00005172 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005173}
5174
Vladimir Markoca1e0382018-04-11 09:58:41 +00005175bool CodeGeneratorARM64::NeedsThunkCode(const linker::LinkerPatch& patch) const {
Vladimir Markof6675082019-05-17 12:05:28 +01005176 return patch.GetType() == linker::LinkerPatch::Type::kCallEntrypoint ||
5177 patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
Vladimir Markoca1e0382018-04-11 09:58:41 +00005178 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
5179}
5180
5181void CodeGeneratorARM64::EmitThunkCode(const linker::LinkerPatch& patch,
5182 /*out*/ ArenaVector<uint8_t>* code,
5183 /*out*/ std::string* debug_name) {
5184 Arm64Assembler assembler(GetGraph()->GetAllocator());
5185 switch (patch.GetType()) {
5186 case linker::LinkerPatch::Type::kCallRelative: {
5187 // The thunk just uses the entry point in the ArtMethod. This works even for calls
5188 // to the generic JNI and interpreter trampolines.
5189 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
5190 kArm64PointerSize).Int32Value());
5191 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
David Srbecky889da942021-04-30 13:03:14 +01005192 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00005193 *debug_name = "MethodCallThunk";
5194 }
5195 break;
5196 }
Vladimir Markof6675082019-05-17 12:05:28 +01005197 case linker::LinkerPatch::Type::kCallEntrypoint: {
5198 Offset offset(patch.EntrypointOffset());
5199 assembler.JumpTo(ManagedRegister(arm64::TR), offset, ManagedRegister(arm64::IP0));
David Srbecky889da942021-04-30 13:03:14 +01005200 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markof6675082019-05-17 12:05:28 +01005201 *debug_name = "EntrypointCallThunk_" + std::to_string(offset.Uint32Value());
5202 }
5203 break;
5204 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00005205 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
5206 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
5207 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
5208 break;
5209 }
5210 default:
5211 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
5212 UNREACHABLE();
5213 }
5214
5215 // Ensure we emit the literal pool if any.
5216 assembler.FinalizeCode();
5217 code->resize(assembler.CodeSize());
5218 MemoryRegion code_region(code->data(), code->size());
5219 assembler.FinalizeInstructions(code_region);
5220}
5221
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005222vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
5223 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005224 value,
5225 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
5226}
5227
Scott Wakeling97c72b72016-06-24 16:19:36 +01005228vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005229 return uint64_literals_.GetOrCreate(
5230 value,
5231 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00005232}
5233
Andreas Gampe878d58c2015-01-15 23:24:00 -08005234void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005235 // Explicit clinit checks triggered by static invokes must have been pruned by
5236 // art::PrepareForRegisterAllocation.
5237 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01005238
Andreas Gampe878d58c2015-01-15 23:24:00 -08005239 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08005240 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08005241 return;
5242 }
5243
Vladimir Marko86c87522020-05-11 16:55:55 +01005244 LocationSummary* locations = invoke->GetLocations();
5245 codegen_->GenerateStaticOrDirectCall(
5246 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005247
Andreas Gampe3db70682018-12-26 15:12:03 -08005248 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005249}
5250
5251void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08005252 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08005253 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08005254 return;
5255 }
5256
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005257 {
5258 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
5259 // are no pools emitted.
5260 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
5261 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
5262 DCHECK(!codegen_->IsLeafMethod());
5263 }
5264
Andreas Gampe3db70682018-12-26 15:12:03 -08005265 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005266}
5267
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005268HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
5269 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005270 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005271 case HLoadClass::LoadKind::kInvalid:
5272 LOG(FATAL) << "UNREACHABLE";
5273 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005274 case HLoadClass::LoadKind::kReferrersClass:
5275 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005276 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005277 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005278 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8f63f102020-09-28 12:10:28 +01005279 case HLoadClass::LoadKind::kBssEntryPublic:
5280 case HLoadClass::LoadKind::kBssEntryPackage:
Vladimir Marko695348f2020-05-19 14:42:02 +01005281 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005282 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005283 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005284 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01005285 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005286 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005287 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005288 break;
5289 }
5290 return desired_class_load_kind;
5291}
5292
Alexandre Rames67555f72014-11-18 10:55:16 +00005293void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005294 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005295 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005296 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005297 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005298 cls,
5299 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005300 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00005301 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005302 return;
5303 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005304 DCHECK_EQ(cls->NeedsAccessCheck(),
5305 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
5306 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005307
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005308 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5309 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005310 ? LocationSummary::kCallOnSlowPath
5311 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005312 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005313 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005314 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005315 }
5316
Vladimir Marko41559982017-01-06 14:04:23 +00005317 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005318 locations->SetInAt(0, Location::RequiresRegister());
5319 }
5320 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005321 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
5322 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5323 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005324 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005325 } else {
5326 // For non-Baker read barrier we have a temp-clobbering call.
5327 }
5328 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005329}
5330
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005331// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5332// move.
5333void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005334 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005335 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005336 codegen_->GenerateLoadClassRuntimeCall(cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08005337 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01005338 return;
5339 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005340 DCHECK_EQ(cls->NeedsAccessCheck(),
5341 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
5342 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Calin Juravle580b6092015-10-06 17:35:58 +01005343
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005344 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01005345 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00005346
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005347 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5348 ? kWithoutReadBarrier
5349 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005350 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005351 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005352 case HLoadClass::LoadKind::kReferrersClass: {
5353 DCHECK(!cls->CanCallRuntime());
5354 DCHECK(!cls->MustGenerateClinitCheck());
5355 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5356 Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00005357 codegen_->GenerateGcRootFieldLoad(cls,
5358 out_loc,
5359 current_method,
5360 ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005361 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005362 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005363 break;
5364 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005365 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005366 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5367 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005368 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005369 // Add ADRP with its PC-relative type patch.
5370 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08005371 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005372 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005373 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005374 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005375 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005376 codegen_->NewBootImageTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005377 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005378 break;
5379 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005380 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005381 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markode91ca92020-10-27 13:41:40 +00005382 uint32_t boot_image_offset = CodeGenerator::GetBootImageOffset(cls);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005383 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5384 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005385 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005386 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005387 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005388 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005389 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005390 break;
5391 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005392 case HLoadClass::LoadKind::kBssEntry:
5393 case HLoadClass::LoadKind::kBssEntryPublic:
5394 case HLoadClass::LoadKind::kBssEntryPackage: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005395 // Add ADRP with its PC-relative Class .bss entry patch.
Vladimir Markof3c52b42017-11-17 17:32:12 +00005396 vixl::aarch64::Register temp = XRegisterFrom(out_loc);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005397 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(cls);
Vladimir Markof3c52b42017-11-17 17:32:12 +00005398 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005399 // Add LDR with its PC-relative Class .bss entry patch.
Vladimir Marko8f63f102020-09-28 12:10:28 +01005400 vixl::aarch64::Label* ldr_label = codegen_->NewBssEntryTypePatch(cls, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005401 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005402 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005403 codegen_->GenerateGcRootFieldLoad(cls,
5404 out_loc,
5405 temp,
5406 /* offset placeholder */ 0u,
5407 ldr_label,
5408 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005409 generate_null_check = true;
5410 break;
5411 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005412 case HLoadClass::LoadKind::kJitBootImageAddress: {
5413 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5414 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
5415 DCHECK_NE(address, 0u);
5416 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5417 break;
5418 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005419 case HLoadClass::LoadKind::kJitTableAddress: {
5420 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5421 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005422 cls->GetClass()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005423 codegen_->GenerateGcRootFieldLoad(cls,
5424 out_loc,
5425 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005426 /* offset= */ 0,
5427 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005428 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005429 break;
5430 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005431 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005432 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005433 LOG(FATAL) << "UNREACHABLE";
5434 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005435 }
5436
Vladimir Markoea4c1262017-02-06 19:59:33 +00005437 bool do_clinit = cls->MustGenerateClinitCheck();
5438 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005439 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01005440 SlowPathCodeARM64* slow_path =
5441 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005442 codegen_->AddSlowPath(slow_path);
5443 if (generate_null_check) {
5444 __ Cbz(out, slow_path->GetEntryLabel());
5445 }
5446 if (cls->MustGenerateClinitCheck()) {
5447 GenerateClassInitializationCheck(slow_path, out);
5448 } else {
5449 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005450 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005451 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005452 }
5453}
5454
Orion Hodsondbaa5c72018-05-10 08:22:46 +01005455void LocationsBuilderARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5456 InvokeRuntimeCallingConvention calling_convention;
5457 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5458 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
5459}
5460
5461void InstructionCodeGeneratorARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5462 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
5463}
5464
Orion Hodson18259d72018-04-12 11:18:23 +01005465void LocationsBuilderARM64::VisitLoadMethodType(HLoadMethodType* load) {
5466 InvokeRuntimeCallingConvention calling_convention;
5467 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5468 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
5469}
5470
5471void InstructionCodeGeneratorARM64::VisitLoadMethodType(HLoadMethodType* load) {
5472 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
5473}
5474
David Brazdilcb1c0552015-08-04 16:22:25 +01005475static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005476 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005477}
5478
Alexandre Rames67555f72014-11-18 10:55:16 +00005479void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5480 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005481 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00005482 locations->SetOut(Location::RequiresRegister());
5483}
5484
5485void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005486 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5487}
5488
5489void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005490 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005491}
5492
5493void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5494 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005495}
5496
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005497HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5498 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005499 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005500 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005501 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005502 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01005503 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005504 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005505 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005506 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01005507 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005508 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005509 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005510 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005511 }
5512 return desired_string_load_kind;
5513}
5514
Alexandre Rames67555f72014-11-18 10:55:16 +00005515void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005516 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005517 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005518 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005519 InvokeRuntimeCallingConvention calling_convention;
5520 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5521 } else {
5522 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005523 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5524 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005525 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005526 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005527 } else {
5528 // For non-Baker read barrier we have a temp-clobbering call.
5529 }
5530 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005531 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005532}
5533
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005534// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5535// move.
5536void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005537 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005538 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005539
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005540 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005541 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005542 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5543 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005544 // Add ADRP with its PC-relative String patch.
5545 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005546 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005547 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005548 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005549 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005550 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005551 codegen_->NewBootImageStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005552 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005553 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005554 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005555 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005556 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005557 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markode91ca92020-10-27 13:41:40 +00005558 uint32_t boot_image_offset = CodeGenerator::GetBootImageOffset(load);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005559 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005560 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005561 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005562 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005563 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005564 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
5565 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005566 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005567 case HLoadString::LoadKind::kBssEntry: {
5568 // Add ADRP with its PC-relative String .bss entry patch.
5569 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005570 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00005571 Register temp = XRegisterFrom(out_loc);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005572 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005573 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005574 // Add LDR with its PC-relative String .bss entry patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005575 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005576 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005577 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005578 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005579 codegen_->GenerateGcRootFieldLoad(load,
5580 out_loc,
5581 temp,
5582 /* offset placeholder */ 0u,
5583 ldr_label,
5584 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005585 SlowPathCodeARM64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005586 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005587 codegen_->AddSlowPath(slow_path);
5588 __ Cbz(out.X(), slow_path->GetEntryLabel());
5589 __ Bind(slow_path->GetExitLabel());
Andreas Gampe3db70682018-12-26 15:12:03 -08005590 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005591 return;
5592 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005593 case HLoadString::LoadKind::kJitBootImageAddress: {
5594 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
5595 DCHECK_NE(address, 0u);
5596 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5597 return;
5598 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005599 case HLoadString::LoadKind::kJitTableAddress: {
5600 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005601 load->GetStringIndex(),
5602 load->GetString()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005603 codegen_->GenerateGcRootFieldLoad(load,
5604 out_loc,
5605 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005606 /* offset= */ 0,
5607 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005608 kCompilerReadBarrierOption);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005609 return;
5610 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005611 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005612 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005613 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005614
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005615 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005616 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005617 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005618 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005619 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5620 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005621 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005622}
5623
Alexandre Rames5319def2014-10-23 10:03:10 +01005624void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005625 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01005626 locations->SetOut(Location::ConstantLocation(constant));
5627}
5628
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005629void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005630 // Will be generated at use site.
5631}
5632
Alexandre Rames67555f72014-11-18 10:55:16 +00005633void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005634 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5635 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005636 InvokeRuntimeCallingConvention calling_convention;
5637 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5638}
5639
5640void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005641 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005642 instruction,
5643 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005644 if (instruction->IsEnter()) {
5645 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5646 } else {
5647 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5648 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005649 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005650}
5651
Alexandre Rames42d641b2014-10-27 14:00:51 +00005652void LocationsBuilderARM64::VisitMul(HMul* mul) {
5653 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005654 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005655 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005656 case DataType::Type::kInt32:
5657 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005658 locations->SetInAt(0, Location::RequiresRegister());
5659 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005660 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005661 break;
5662
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005663 case DataType::Type::kFloat32:
5664 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005665 locations->SetInAt(0, Location::RequiresFpuRegister());
5666 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005667 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005668 break;
5669
5670 default:
5671 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5672 }
5673}
5674
5675void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5676 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005677 case DataType::Type::kInt32:
5678 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005679 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5680 break;
5681
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005682 case DataType::Type::kFloat32:
5683 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005684 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005685 break;
5686
5687 default:
5688 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5689 }
5690}
5691
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005692void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5693 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005694 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005695 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005696 case DataType::Type::kInt32:
5697 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005698 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005699 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005700 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005701
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005702 case DataType::Type::kFloat32:
5703 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005704 locations->SetInAt(0, Location::RequiresFpuRegister());
5705 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005706 break;
5707
5708 default:
5709 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5710 }
5711}
5712
5713void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5714 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005715 case DataType::Type::kInt32:
5716 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005717 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5718 break;
5719
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005720 case DataType::Type::kFloat32:
5721 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005722 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005723 break;
5724
5725 default:
5726 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5727 }
5728}
5729
5730void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005731 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5732 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005733 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005734 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005735 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5736 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005737}
5738
5739void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005740 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5741 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005742 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005743 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005744 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005745}
5746
Alexandre Rames5319def2014-10-23 10:03:10 +01005747void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005748 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5749 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005750 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07005751 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005752 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005753}
5754
5755void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005756 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5757 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005758 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005759}
5760
5761void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005762 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005763 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005764 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005765}
5766
5767void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005768 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005769 case DataType::Type::kInt32:
5770 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005771 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005772 break;
5773
5774 default:
5775 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5776 }
5777}
5778
David Brazdil66d126e2015-04-03 16:02:44 +01005779void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005780 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005781 locations->SetInAt(0, Location::RequiresRegister());
5782 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5783}
5784
5785void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005786 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005787}
5788
Alexandre Rames5319def2014-10-23 10:03:10 +01005789void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005790 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5791 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005792}
5793
Calin Juravle2ae48182016-03-16 14:05:09 +00005794void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5795 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005796 return;
5797 }
Artem Serov914d7a82017-02-07 14:33:49 +00005798 {
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01005799 // Ensure that between load and RecordPcInfo there are no pools emitted.
Artem Serov914d7a82017-02-07 14:33:49 +00005800 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5801 Location obj = instruction->GetLocations()->InAt(0);
5802 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5803 RecordPcInfo(instruction, instruction->GetDexPc());
5804 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005805}
5806
Calin Juravle2ae48182016-03-16 14:05:09 +00005807void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005808 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005809 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005810
5811 LocationSummary* locations = instruction->GetLocations();
5812 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005813
5814 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005815}
5816
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005817void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005818 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005819}
5820
Alexandre Rames67555f72014-11-18 10:55:16 +00005821void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5822 HandleBinaryOp(instruction);
5823}
5824
5825void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5826 HandleBinaryOp(instruction);
5827}
5828
Alexandre Rames3e69f162014-12-10 10:36:50 +00005829void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5830 LOG(FATAL) << "Unreachable";
5831}
5832
5833void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005834 if (instruction->GetNext()->IsSuspendCheck() &&
5835 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5836 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5837 // The back edge will generate the suspend check.
5838 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5839 }
5840
Alexandre Rames3e69f162014-12-10 10:36:50 +00005841 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5842}
5843
Alexandre Rames5319def2014-10-23 10:03:10 +01005844void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005845 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005846 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5847 if (location.IsStackSlot()) {
5848 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5849 } else if (location.IsDoubleStackSlot()) {
5850 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5851 }
5852 locations->SetOut(location);
5853}
5854
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005855void InstructionCodeGeneratorARM64::VisitParameterValue(
5856 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005857 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005858}
5859
5860void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5861 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005862 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005863 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005864}
5865
5866void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5867 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5868 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005869}
5870
5871void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005872 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005873 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005874 locations->SetInAt(i, Location::Any());
5875 }
5876 locations->SetOut(Location::Any());
5877}
5878
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005879void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005880 LOG(FATAL) << "Unreachable";
5881}
5882
Serban Constantinescu02164b32014-11-13 14:05:07 +00005883void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005884 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005885 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005886 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005887 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005888 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005889
5890 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005891 case DataType::Type::kInt32:
5892 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005893 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005894 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005895 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5896 break;
5897
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005898 case DataType::Type::kFloat32:
5899 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005900 InvokeRuntimeCallingConvention calling_convention;
5901 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5902 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5903 locations->SetOut(calling_convention.GetReturnLocation(type));
5904
5905 break;
5906 }
5907
Serban Constantinescu02164b32014-11-13 14:05:07 +00005908 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005909 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005910 }
5911}
5912
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005913void InstructionCodeGeneratorARM64::GenerateIntRemForPower2Denom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005914 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005915 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
5916 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
5917
5918 Register out = OutputRegister(instruction);
5919 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005920
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01005921 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01005922 // No need to adjust the result for non-negative dividends or the INT32_MIN/INT64_MIN dividends.
5923 // NOTE: The generated code for HRem correctly works for the INT32_MIN/INT64_MIN dividends.
5924 // INT*_MIN % imm must be 0 for any imm of power 2. 'and' works only with bits
5925 // 0..30 (Int32 case)/0..62 (Int64 case) of a dividend. For INT32_MIN/INT64_MIN they are zeros.
5926 // So 'and' always produces zero.
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005927 __ And(out, dividend, abs_imm - 1);
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01005928 } else {
5929 if (abs_imm == 2) {
5930 __ Cmp(dividend, 0);
5931 __ And(out, dividend, 1);
5932 __ Csneg(out, out, out, ge);
5933 } else {
5934 UseScratchRegisterScope temps(GetVIXLAssembler());
5935 Register temp = temps.AcquireSameSizeAs(out);
5936
5937 __ Negs(temp, dividend);
5938 __ And(out, dividend, abs_imm - 1);
5939 __ And(temp, temp, abs_imm - 1);
5940 __ Csneg(out, out, temp, mi);
5941 }
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005942 }
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005943}
5944
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005945void InstructionCodeGeneratorARM64::GenerateIntRemForConstDenom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005946 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005947
5948 if (imm == 0) {
5949 // Do not generate anything.
5950 // DivZeroCheck would prevent any code to be executed.
5951 return;
5952 }
5953
Evgeny Astigeevichf58dc652018-06-25 17:54:07 +01005954 if (IsPowerOfTwo(AbsOrMin(imm))) {
5955 // Cases imm == -1 or imm == 1 are handled in constant folding by
5956 // InstructionWithAbsorbingInputSimplifier.
5957 // If the cases have survided till code generation they are handled in
5958 // GenerateIntRemForPower2Denom becauses -1 and 1 are the power of 2 (2^0).
5959 // The correct code is generated for them, just more instructions.
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005960 GenerateIntRemForPower2Denom(instruction);
5961 } else {
5962 DCHECK(imm < -2 || imm > 2) << imm;
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01005963 GenerateDivRemWithAnyConstant(instruction, imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005964 }
5965}
5966
5967void InstructionCodeGeneratorARM64::GenerateIntRem(HRem* instruction) {
5968 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
5969 << instruction->GetResultType();
5970
5971 if (instruction->GetLocations()->InAt(1).IsConstant()) {
5972 GenerateIntRemForConstDenom(instruction);
5973 } else {
5974 Register out = OutputRegister(instruction);
5975 Register dividend = InputRegisterAt(instruction, 0);
5976 Register divisor = InputRegisterAt(instruction, 1);
5977 UseScratchRegisterScope temps(GetVIXLAssembler());
5978 Register temp = temps.AcquireSameSizeAs(out);
5979 __ Sdiv(temp, dividend, divisor);
5980 __ Msub(out, temp, divisor, dividend);
5981 }
5982}
5983
Serban Constantinescu02164b32014-11-13 14:05:07 +00005984void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005985 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005986
Serban Constantinescu02164b32014-11-13 14:05:07 +00005987 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005988 case DataType::Type::kInt32:
5989 case DataType::Type::kInt64: {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005990 GenerateIntRem(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005991 break;
5992 }
5993
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005994 case DataType::Type::kFloat32:
5995 case DataType::Type::kFloat64: {
5996 QuickEntrypointEnum entrypoint =
5997 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005998 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005999 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00006000 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6001 } else {
6002 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6003 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00006004 break;
6005 }
6006
Serban Constantinescu02164b32014-11-13 14:05:07 +00006007 default:
6008 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00006009 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00006010 }
6011}
6012
Aart Bik1f8d51b2018-02-15 10:42:37 -08006013void LocationsBuilderARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01006014 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08006015}
6016
Aart Bik1f8d51b2018-02-15 10:42:37 -08006017void InstructionCodeGeneratorARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01006018 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08006019}
6020
6021void LocationsBuilderARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01006022 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08006023}
6024
6025void InstructionCodeGeneratorARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01006026 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08006027}
6028
Aart Bik3dad3412018-02-28 12:01:46 -08006029void LocationsBuilderARM64::VisitAbs(HAbs* abs) {
6030 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
6031 switch (abs->GetResultType()) {
6032 case DataType::Type::kInt32:
6033 case DataType::Type::kInt64:
6034 locations->SetInAt(0, Location::RequiresRegister());
6035 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6036 break;
6037 case DataType::Type::kFloat32:
6038 case DataType::Type::kFloat64:
6039 locations->SetInAt(0, Location::RequiresFpuRegister());
6040 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6041 break;
6042 default:
6043 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
6044 }
6045}
6046
6047void InstructionCodeGeneratorARM64::VisitAbs(HAbs* abs) {
6048 switch (abs->GetResultType()) {
6049 case DataType::Type::kInt32:
6050 case DataType::Type::kInt64: {
6051 Register in_reg = InputRegisterAt(abs, 0);
6052 Register out_reg = OutputRegister(abs);
6053 __ Cmp(in_reg, Operand(0));
6054 __ Cneg(out_reg, in_reg, lt);
6055 break;
6056 }
6057 case DataType::Type::kFloat32:
6058 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01006059 VRegister in_reg = InputFPRegisterAt(abs, 0);
6060 VRegister out_reg = OutputFPRegister(abs);
Aart Bik3dad3412018-02-28 12:01:46 -08006061 __ Fabs(out_reg, in_reg);
6062 break;
6063 }
6064 default:
6065 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
6066 }
6067}
6068
Igor Murashkind01745e2017-04-05 16:40:31 -07006069void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
6070 constructor_fence->SetLocations(nullptr);
6071}
6072
6073void InstructionCodeGeneratorARM64::VisitConstructorFence(
6074 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
6075 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
6076}
6077
Calin Juravle27df7582015-04-17 19:12:31 +01006078void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6079 memory_barrier->SetLocations(nullptr);
6080}
6081
6082void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006083 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01006084}
6085
Alexandre Rames5319def2014-10-23 10:03:10 +01006086void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006087 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006088 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00006089 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01006090}
6091
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00006092void InstructionCodeGeneratorARM64::VisitReturn(HReturn* ret) {
6093 if (GetGraph()->IsCompilingOsr()) {
6094 // To simplify callers of an OSR method, we put the return value in both
6095 // floating point and core register.
6096 switch (ret->InputAt(0)->GetType()) {
6097 case DataType::Type::kFloat32:
6098 __ Fmov(w0, s0);
6099 break;
6100 case DataType::Type::kFloat64:
6101 __ Fmov(x0, d0);
6102 break;
6103 default:
6104 break;
6105 }
6106 }
Alexandre Rames5319def2014-10-23 10:03:10 +01006107 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01006108}
6109
6110void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
6111 instruction->SetLocations(nullptr);
6112}
6113
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006114void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01006115 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01006116}
6117
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006118void LocationsBuilderARM64::VisitRor(HRor* ror) {
6119 HandleBinaryOp(ror);
6120}
6121
6122void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
6123 HandleBinaryOp(ror);
6124}
6125
Serban Constantinescu02164b32014-11-13 14:05:07 +00006126void LocationsBuilderARM64::VisitShl(HShl* shl) {
6127 HandleShift(shl);
6128}
6129
6130void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
6131 HandleShift(shl);
6132}
6133
6134void LocationsBuilderARM64::VisitShr(HShr* shr) {
6135 HandleShift(shr);
6136}
6137
6138void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
6139 HandleShift(shr);
6140}
6141
Alexandre Rames5319def2014-10-23 10:03:10 +01006142void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006143 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006144}
6145
6146void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006147 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006148}
6149
Alexandre Rames67555f72014-11-18 10:55:16 +00006150void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006151 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00006152}
6153
6154void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01006155 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00006156}
6157
6158void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01006159 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006160}
6161
Alexandre Rames67555f72014-11-18 10:55:16 +00006162void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006163 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01006164}
6165
Vladimir Marko552a1342017-10-31 10:56:47 +00006166void LocationsBuilderARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6167 codegen_->CreateStringBuilderAppendLocations(instruction, LocationFrom(x0));
6168}
6169
6170void InstructionCodeGeneratorARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6171 __ Mov(w0, instruction->GetFormat()->GetValue());
6172 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
6173}
6174
Calin Juravlee460d1d2015-09-29 04:52:17 +01006175void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
6176 HUnresolvedInstanceFieldGet* instruction) {
6177 FieldAccessCallingConventionARM64 calling_convention;
6178 codegen_->CreateUnresolvedFieldLocationSummary(
6179 instruction, instruction->GetFieldType(), calling_convention);
6180}
6181
6182void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
6183 HUnresolvedInstanceFieldGet* instruction) {
6184 FieldAccessCallingConventionARM64 calling_convention;
6185 codegen_->GenerateUnresolvedFieldAccess(instruction,
6186 instruction->GetFieldType(),
6187 instruction->GetFieldIndex(),
6188 instruction->GetDexPc(),
6189 calling_convention);
6190}
6191
6192void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
6193 HUnresolvedInstanceFieldSet* instruction) {
6194 FieldAccessCallingConventionARM64 calling_convention;
6195 codegen_->CreateUnresolvedFieldLocationSummary(
6196 instruction, instruction->GetFieldType(), calling_convention);
6197}
6198
6199void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
6200 HUnresolvedInstanceFieldSet* instruction) {
6201 FieldAccessCallingConventionARM64 calling_convention;
6202 codegen_->GenerateUnresolvedFieldAccess(instruction,
6203 instruction->GetFieldType(),
6204 instruction->GetFieldIndex(),
6205 instruction->GetDexPc(),
6206 calling_convention);
6207}
6208
6209void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
6210 HUnresolvedStaticFieldGet* instruction) {
6211 FieldAccessCallingConventionARM64 calling_convention;
6212 codegen_->CreateUnresolvedFieldLocationSummary(
6213 instruction, instruction->GetFieldType(), calling_convention);
6214}
6215
6216void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
6217 HUnresolvedStaticFieldGet* instruction) {
6218 FieldAccessCallingConventionARM64 calling_convention;
6219 codegen_->GenerateUnresolvedFieldAccess(instruction,
6220 instruction->GetFieldType(),
6221 instruction->GetFieldIndex(),
6222 instruction->GetDexPc(),
6223 calling_convention);
6224}
6225
6226void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
6227 HUnresolvedStaticFieldSet* instruction) {
6228 FieldAccessCallingConventionARM64 calling_convention;
6229 codegen_->CreateUnresolvedFieldLocationSummary(
6230 instruction, instruction->GetFieldType(), calling_convention);
6231}
6232
6233void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
6234 HUnresolvedStaticFieldSet* instruction) {
6235 FieldAccessCallingConventionARM64 calling_convention;
6236 codegen_->GenerateUnresolvedFieldAccess(instruction,
6237 instruction->GetFieldType(),
6238 instruction->GetFieldIndex(),
6239 instruction->GetDexPc(),
6240 calling_convention);
6241}
6242
Alexandre Rames5319def2014-10-23 10:03:10 +01006243void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006244 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6245 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01006246 // In suspend check slow path, usually there are no caller-save registers at all.
6247 // If SIMD instructions are present, however, we force spilling all live SIMD
6248 // registers in full width (since the runtime only saves/restores lower part).
6249 locations->SetCustomSlowPathCallerSaves(
6250 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01006251}
6252
6253void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006254 HBasicBlock* block = instruction->GetBlock();
6255 if (block->GetLoopInformation() != nullptr) {
6256 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6257 // The back edge will generate the suspend check.
6258 return;
6259 }
6260 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6261 // The goto will generate the suspend check.
6262 return;
6263 }
6264 GenerateSuspendCheck(instruction, nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08006265 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01006266}
6267
Alexandre Rames67555f72014-11-18 10:55:16 +00006268void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006269 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6270 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00006271 InvokeRuntimeCallingConvention calling_convention;
6272 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
6273}
6274
6275void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00006276 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08006277 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00006278}
6279
6280void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
6281 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006282 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006283 DataType::Type input_type = conversion->GetInputType();
6284 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006285 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6286 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006287 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
6288 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006289 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6290 }
6291
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006292 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006293 locations->SetInAt(0, Location::RequiresFpuRegister());
6294 } else {
6295 locations->SetInAt(0, Location::RequiresRegister());
6296 }
6297
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006298 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006299 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6300 } else {
6301 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6302 }
6303}
6304
6305void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006306 DataType::Type result_type = conversion->GetResultType();
6307 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00006308
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006309 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6310 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006311
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006312 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
6313 int result_size = DataType::Size(result_type);
6314 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00006315 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006316 Register output = OutputRegister(conversion);
6317 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006318 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01006319 // 'int' values are used directly as W registers, discarding the top
6320 // bits, so we don't need to sign-extend and can just perform a move.
6321 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
6322 // top 32 bits of the target register. We theoretically could leave those
6323 // bits unchanged, but we would have to make sure that no code uses a
6324 // 32bit input value as a 64bit value assuming that the top 32 bits are
6325 // zero.
6326 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006327 } else if (DataType::IsUnsignedType(result_type) ||
6328 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
6329 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006330 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00006331 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006332 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006333 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006334 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006335 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6336 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006337 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006338 } else if (DataType::IsFloatingPointType(result_type) &&
6339 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006340 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
6341 } else {
6342 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6343 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006344 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00006345}
Alexandre Rames67555f72014-11-18 10:55:16 +00006346
Serban Constantinescu02164b32014-11-13 14:05:07 +00006347void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
6348 HandleShift(ushr);
6349}
6350
6351void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
6352 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00006353}
6354
6355void LocationsBuilderARM64::VisitXor(HXor* instruction) {
6356 HandleBinaryOp(instruction);
6357}
6358
6359void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
6360 HandleBinaryOp(instruction);
6361}
6362
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006363void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006364 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006365 LOG(FATAL) << "Unreachable";
6366}
6367
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006368void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006369 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006370 LOG(FATAL) << "Unreachable";
6371}
6372
Mark Mendellfe57faa2015-09-18 09:26:15 -04006373// Simple implementation of packed switch - generate cascaded compare/jumps.
6374void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6375 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006376 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006377 locations->SetInAt(0, Location::RequiresRegister());
6378}
6379
6380void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6381 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08006382 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006383 Register value_reg = InputRegisterAt(switch_instr, 0);
6384 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6385
Zheng Xu3927c8b2015-11-18 17:46:25 +08006386 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01006387 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08006388 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
6389 // make sure we don't emit it if the target may run out of range.
6390 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
6391 // ranges and emit the tables only as required.
6392 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04006393
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006394 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08006395 // Current instruction id is an upper bound of the number of HIRs in the graph.
6396 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
6397 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006398 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6399 Register temp = temps.AcquireW();
6400 __ Subs(temp, value_reg, Operand(lower_bound));
6401
Zheng Xu3927c8b2015-11-18 17:46:25 +08006402 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006403 // Jump to successors[0] if value == lower_bound.
6404 __ B(eq, codegen_->GetLabelOf(successors[0]));
6405 int32_t last_index = 0;
6406 for (; num_entries - last_index > 2; last_index += 2) {
6407 __ Subs(temp, temp, Operand(2));
6408 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6409 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
6410 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6411 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
6412 }
6413 if (num_entries - last_index == 2) {
6414 // The last missing case_value.
6415 __ Cmp(temp, Operand(1));
6416 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08006417 }
6418
6419 // And the default for any other value.
6420 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6421 __ B(codegen_->GetLabelOf(default_block));
6422 }
6423 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01006424 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08006425
6426 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6427
6428 // Below instructions should use at most one blocked register. Since there are two blocked
6429 // registers, we are free to block one.
6430 Register temp_w = temps.AcquireW();
6431 Register index;
6432 // Remove the bias.
6433 if (lower_bound != 0) {
6434 index = temp_w;
6435 __ Sub(index, value_reg, Operand(lower_bound));
6436 } else {
6437 index = value_reg;
6438 }
6439
6440 // Jump to default block if index is out of the range.
6441 __ Cmp(index, Operand(num_entries));
6442 __ B(hs, codegen_->GetLabelOf(default_block));
6443
6444 // In current VIXL implementation, it won't require any blocked registers to encode the
6445 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
6446 // register pressure.
6447 Register table_base = temps.AcquireX();
6448 // Load jump offset from the table.
6449 __ Adr(table_base, jump_table->GetTableStartLabel());
6450 Register jump_offset = temp_w;
6451 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
6452
6453 // Jump to target block by branching to table_base(pc related) + offset.
6454 Register target_address = table_base;
6455 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
6456 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006457 }
6458}
6459
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006460void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
6461 HInstruction* instruction,
6462 Location out,
6463 uint32_t offset,
6464 Location maybe_temp,
6465 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006466 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006467 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006468 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006469 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006470 if (kUseBakerReadBarrier) {
6471 // Load with fast path based Baker's read barrier.
6472 // /* HeapReference<Object> */ out = *(out + offset)
6473 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6474 out,
6475 out_reg,
6476 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006477 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006478 /* needs_null_check= */ false,
6479 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006480 } else {
6481 // Load with slow path based read barrier.
6482 // Save the value of `out` into `maybe_temp` before overwriting it
6483 // in the following move operation, as we will need it for the
6484 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006485 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00006486 __ Mov(temp_reg, out_reg);
6487 // /* HeapReference<Object> */ out = *(out + offset)
6488 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6489 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6490 }
6491 } else {
6492 // Plain load with no read barrier.
6493 // /* HeapReference<Object> */ out = *(out + offset)
6494 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6495 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6496 }
6497}
6498
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006499void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
6500 HInstruction* instruction,
6501 Location out,
6502 Location obj,
6503 uint32_t offset,
6504 Location maybe_temp,
6505 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006506 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006507 Register out_reg = RegisterFrom(out, type);
6508 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006509 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006510 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006511 if (kUseBakerReadBarrier) {
6512 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00006513 // /* HeapReference<Object> */ out = *(obj + offset)
6514 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6515 out,
6516 obj_reg,
6517 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006518 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006519 /* needs_null_check= */ false,
6520 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006521 } else {
6522 // Load with slow path based read barrier.
6523 // /* HeapReference<Object> */ out = *(obj + offset)
6524 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6525 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6526 }
6527 } else {
6528 // Plain load with no read barrier.
6529 // /* HeapReference<Object> */ out = *(obj + offset)
6530 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6531 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6532 }
6533}
6534
Vladimir Markoca1e0382018-04-11 09:58:41 +00006535void CodeGeneratorARM64::GenerateGcRootFieldLoad(
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006536 HInstruction* instruction,
6537 Location root,
6538 Register obj,
6539 uint32_t offset,
6540 vixl::aarch64::Label* fixup_label,
6541 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006542 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006543 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006544 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006545 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006546 if (kUseBakerReadBarrier) {
6547 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00006548 // Baker's read barrier are used.
Roland Levillain44015862016-01-22 11:47:17 +00006549
Vladimir Marko008e09f32018-08-06 15:42:43 +01006550 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6551 // the Marking Register) to decide whether we need to enter
6552 // the slow path to mark the GC root.
6553 //
6554 // We use shared thunks for the slow path; shared within the method
6555 // for JIT, across methods for AOT. That thunk checks the reference
6556 // and jumps to the entrypoint if needed.
6557 //
6558 // lr = &return_address;
6559 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6560 // if (mr) { // Thread::Current()->GetIsGcMarking()
6561 // goto gc_root_thunk<root_reg>(lr)
6562 // }
6563 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00006564
Vladimir Marko008e09f32018-08-06 15:42:43 +01006565 UseScratchRegisterScope temps(GetVIXLAssembler());
6566 DCHECK(temps.IsAvailable(ip0));
6567 DCHECK(temps.IsAvailable(ip1));
6568 temps.Exclude(ip0, ip1);
6569 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
Roland Levillain44015862016-01-22 11:47:17 +00006570
Vladimir Marko008e09f32018-08-06 15:42:43 +01006571 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6572 vixl::aarch64::Label return_address;
6573 __ adr(lr, &return_address);
6574 if (fixup_label != nullptr) {
6575 __ bind(fixup_label);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006576 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006577 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
Vladimir Marko94796f82018-08-08 15:15:33 +01006578 "GC root LDR must be 2 instructions (8B) before the return address label.");
Vladimir Marko008e09f32018-08-06 15:42:43 +01006579 __ ldr(root_reg, MemOperand(obj.X(), offset));
6580 EmitBakerReadBarrierCbnz(custom_data);
6581 __ bind(&return_address);
Roland Levillain44015862016-01-22 11:47:17 +00006582 } else {
6583 // GC root loaded through a slow path for read barriers other
6584 // than Baker's.
6585 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006586 if (fixup_label == nullptr) {
6587 __ Add(root_reg.X(), obj.X(), offset);
6588 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006589 EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006590 }
Roland Levillain44015862016-01-22 11:47:17 +00006591 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00006592 GenerateReadBarrierForRootSlow(instruction, root, root);
Roland Levillain44015862016-01-22 11:47:17 +00006593 }
6594 } else {
6595 // Plain GC root load with no read barrier.
6596 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006597 if (fixup_label == nullptr) {
6598 __ Ldr(root_reg, MemOperand(obj, offset));
6599 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006600 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006601 }
Roland Levillain44015862016-01-22 11:47:17 +00006602 // Note that GC roots are not affected by heap poisoning, thus we
6603 // do not have to unpoison `root_reg` here.
6604 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006605 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00006606}
6607
Vladimir Markoc8178f52020-11-24 10:38:16 +00006608void CodeGeneratorARM64::GenerateIntrinsicCasMoveWithBakerReadBarrier(
6609 vixl::aarch64::Register marked_old_value,
Vladimir Marko94796f82018-08-08 15:15:33 +01006610 vixl::aarch64::Register old_value) {
6611 DCHECK(kEmitCompilerReadBarrier);
6612 DCHECK(kUseBakerReadBarrier);
6613
6614 // Similar to the Baker RB path in GenerateGcRootFieldLoad(), with a MOV instead of LDR.
Vladimir Markoc8178f52020-11-24 10:38:16 +00006615 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(marked_old_value.GetCode());
Vladimir Marko94796f82018-08-08 15:15:33 +01006616
6617 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6618 vixl::aarch64::Label return_address;
6619 __ adr(lr, &return_address);
6620 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6621 "GC root LDR must be 2 instructions (8B) before the return address label.");
Vladimir Markoc8178f52020-11-24 10:38:16 +00006622 __ mov(marked_old_value, old_value);
Vladimir Marko94796f82018-08-08 15:15:33 +01006623 EmitBakerReadBarrierCbnz(custom_data);
6624 __ bind(&return_address);
6625}
6626
Roland Levillain44015862016-01-22 11:47:17 +00006627void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6628 Location ref,
Vladimir Marko248141f2018-08-10 10:40:07 +01006629 vixl::aarch64::Register obj,
6630 const vixl::aarch64::MemOperand& src,
Roland Levillain44015862016-01-22 11:47:17 +00006631 bool needs_null_check,
6632 bool use_load_acquire) {
6633 DCHECK(kEmitCompilerReadBarrier);
6634 DCHECK(kUseBakerReadBarrier);
6635
Vladimir Marko0ecac682018-08-07 10:40:38 +01006636 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6637 // Marking Register) to decide whether we need to enter the slow
6638 // path to mark the reference. Then, in the slow path, check the
6639 // gray bit in the lock word of the reference's holder (`obj`) to
6640 // decide whether to mark `ref` or not.
6641 //
6642 // We use shared thunks for the slow path; shared within the method
6643 // for JIT, across methods for AOT. That thunk checks the holder
6644 // and jumps to the entrypoint if needed. If the holder is not gray,
6645 // it creates a fake dependency and returns to the LDR instruction.
6646 //
6647 // lr = &gray_return_address;
6648 // if (mr) { // Thread::Current()->GetIsGcMarking()
6649 // goto field_thunk<holder_reg, base_reg, use_load_acquire>(lr)
6650 // }
6651 // not_gray_return_address:
6652 // // Original reference load. If the offset is too large to fit
6653 // // into LDR, we use an adjusted base register here.
6654 // HeapReference<mirror::Object> reference = *(obj+offset);
6655 // gray_return_address:
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006656
Vladimir Marko248141f2018-08-10 10:40:07 +01006657 DCHECK(src.GetAddrMode() == vixl::aarch64::Offset);
6658 DCHECK_ALIGNED(src.GetOffset(), sizeof(mirror::HeapReference<mirror::Object>));
6659
6660 UseScratchRegisterScope temps(GetVIXLAssembler());
6661 DCHECK(temps.IsAvailable(ip0));
6662 DCHECK(temps.IsAvailable(ip1));
6663 temps.Exclude(ip0, ip1);
6664 uint32_t custom_data = use_load_acquire
6665 ? EncodeBakerReadBarrierAcquireData(src.GetBaseRegister().GetCode(), obj.GetCode())
6666 : EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode());
6667
6668 {
6669 ExactAssemblyScope guard(GetVIXLAssembler(),
6670 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6671 vixl::aarch64::Label return_address;
6672 __ adr(lr, &return_address);
6673 EmitBakerReadBarrierCbnz(custom_data);
6674 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6675 "Field LDR must be 1 instruction (4B) before the return address label; "
6676 " 2 instructions (8B) for heap poisoning.");
6677 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
6678 if (use_load_acquire) {
6679 DCHECK_EQ(src.GetOffset(), 0);
6680 __ ldar(ref_reg, src);
6681 } else {
6682 __ ldr(ref_reg, src);
6683 }
6684 if (needs_null_check) {
6685 MaybeRecordImplicitNullCheck(instruction);
6686 }
6687 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6688 // macro instructions disallowed in ExactAssemblyScope.
6689 if (kPoisonHeapReferences) {
6690 __ neg(ref_reg, Operand(ref_reg));
6691 }
6692 __ bind(&return_address);
6693 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006694 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Vladimir Marko248141f2018-08-10 10:40:07 +01006695}
6696
6697void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6698 Location ref,
6699 Register obj,
6700 uint32_t offset,
6701 Location maybe_temp,
6702 bool needs_null_check,
6703 bool use_load_acquire) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006704 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6705 Register base = obj;
6706 if (use_load_acquire) {
6707 DCHECK(maybe_temp.IsRegister());
6708 base = WRegisterFrom(maybe_temp);
6709 __ Add(base, obj, offset);
6710 offset = 0u;
6711 } else if (offset >= kReferenceLoadMinFarOffset) {
6712 DCHECK(maybe_temp.IsRegister());
6713 base = WRegisterFrom(maybe_temp);
6714 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6715 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6716 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006717 }
Vladimir Marko248141f2018-08-10 10:40:07 +01006718 MemOperand src(base.X(), offset);
6719 GenerateFieldLoadWithBakerReadBarrier(
6720 instruction, ref, obj, src, needs_null_check, use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006721}
6722
Artem Serov0806f582018-10-11 20:14:20 +01006723void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HArrayGet* instruction,
6724 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006725 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006726 uint32_t data_offset,
6727 Location index,
Roland Levillain44015862016-01-22 11:47:17 +00006728 bool needs_null_check) {
6729 DCHECK(kEmitCompilerReadBarrier);
6730 DCHECK(kUseBakerReadBarrier);
6731
Vladimir Marko66d691d2017-04-07 17:53:39 +01006732 static_assert(
6733 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6734 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006735 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006736
Vladimir Marko008e09f32018-08-06 15:42:43 +01006737 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6738 // Marking Register) to decide whether we need to enter the slow
6739 // path to mark the reference. Then, in the slow path, check the
6740 // gray bit in the lock word of the reference's holder (`obj`) to
6741 // decide whether to mark `ref` or not.
6742 //
6743 // We use shared thunks for the slow path; shared within the method
6744 // for JIT, across methods for AOT. That thunk checks the holder
6745 // and jumps to the entrypoint if needed. If the holder is not gray,
6746 // it creates a fake dependency and returns to the LDR instruction.
6747 //
6748 // lr = &gray_return_address;
6749 // if (mr) { // Thread::Current()->GetIsGcMarking()
6750 // goto array_thunk<base_reg>(lr)
6751 // }
6752 // not_gray_return_address:
6753 // // Original reference load. If the offset is too large to fit
6754 // // into LDR, we use an adjusted base register here.
6755 // HeapReference<mirror::Object> reference = data[index];
6756 // gray_return_address:
Vladimir Marko66d691d2017-04-07 17:53:39 +01006757
Vladimir Marko008e09f32018-08-06 15:42:43 +01006758 DCHECK(index.IsValid());
6759 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
6760 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006761
Vladimir Marko008e09f32018-08-06 15:42:43 +01006762 UseScratchRegisterScope temps(GetVIXLAssembler());
6763 DCHECK(temps.IsAvailable(ip0));
6764 DCHECK(temps.IsAvailable(ip1));
6765 temps.Exclude(ip0, ip1);
Artem Serov0806f582018-10-11 20:14:20 +01006766
6767 Register temp;
6768 if (instruction->GetArray()->IsIntermediateAddress()) {
6769 // We do not need to compute the intermediate address from the array: the
6770 // input instruction has done it already. See the comment in
6771 // `TryExtractArrayAccessAddress()`.
6772 if (kIsDebugBuild) {
6773 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
6774 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
6775 }
6776 temp = obj;
6777 } else {
6778 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
6779 __ Add(temp.X(), obj.X(), Operand(data_offset));
6780 }
6781
Vladimir Marko008e09f32018-08-06 15:42:43 +01006782 uint32_t custom_data = EncodeBakerReadBarrierArrayData(temp.GetCode());
Vladimir Marko66d691d2017-04-07 17:53:39 +01006783
Vladimir Marko008e09f32018-08-06 15:42:43 +01006784 {
6785 ExactAssemblyScope guard(GetVIXLAssembler(),
6786 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6787 vixl::aarch64::Label return_address;
6788 __ adr(lr, &return_address);
6789 EmitBakerReadBarrierCbnz(custom_data);
6790 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6791 "Array LDR must be 1 instruction (4B) before the return address label; "
6792 " 2 instructions (8B) for heap poisoning.");
6793 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6794 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6795 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6796 // macro instructions disallowed in ExactAssemblyScope.
6797 if (kPoisonHeapReferences) {
6798 __ neg(ref_reg, Operand(ref_reg));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006799 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006800 __ bind(&return_address);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006801 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006802 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Roland Levillain44015862016-01-22 11:47:17 +00006803}
6804
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006805void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6806 // The following condition is a compile-time one, so it does not have a run-time cost.
6807 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
6808 // The following condition is a run-time one; it is executed after the
6809 // previous compile-time test, to avoid penalizing non-debug builds.
6810 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6811 UseScratchRegisterScope temps(GetVIXLAssembler());
6812 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6813 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6814 }
6815 }
6816}
6817
Vladimir Marko1bff99f2020-11-02 15:07:33 +00006818SlowPathCodeARM64* CodeGeneratorARM64::AddReadBarrierSlowPath(HInstruction* instruction,
6819 Location out,
6820 Location ref,
6821 Location obj,
6822 uint32_t offset,
6823 Location index) {
6824 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
6825 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6826 AddSlowPath(slow_path);
6827 return slow_path;
6828}
6829
Roland Levillain44015862016-01-22 11:47:17 +00006830void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6831 Location out,
6832 Location ref,
6833 Location obj,
6834 uint32_t offset,
6835 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006836 DCHECK(kEmitCompilerReadBarrier);
6837
Roland Levillain44015862016-01-22 11:47:17 +00006838 // Insert a slow path based read barrier *after* the reference load.
6839 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006840 // If heap poisoning is enabled, the unpoisoning of the loaded
6841 // reference will be carried out by the runtime within the slow
6842 // path.
6843 //
6844 // Note that `ref` currently does not get unpoisoned (when heap
6845 // poisoning is enabled), which is alright as the `ref` argument is
6846 // not used by the artReadBarrierSlow entry point.
6847 //
6848 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko1bff99f2020-11-02 15:07:33 +00006849 SlowPathCodeARM64* slow_path = AddReadBarrierSlowPath(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006850
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006851 __ B(slow_path->GetEntryLabel());
6852 __ Bind(slow_path->GetExitLabel());
6853}
6854
Roland Levillain44015862016-01-22 11:47:17 +00006855void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6856 Location out,
6857 Location ref,
6858 Location obj,
6859 uint32_t offset,
6860 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006861 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006862 // Baker's read barriers shall be handled by the fast path
6863 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6864 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006865 // If heap poisoning is enabled, unpoisoning will be taken care of
6866 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006867 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006868 } else if (kPoisonHeapReferences) {
6869 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6870 }
6871}
6872
Roland Levillain44015862016-01-22 11:47:17 +00006873void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6874 Location out,
6875 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006876 DCHECK(kEmitCompilerReadBarrier);
6877
Roland Levillain44015862016-01-22 11:47:17 +00006878 // Insert a slow path based read barrier *after* the GC root load.
6879 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006880 // Note that GC roots are not affected by heap poisoning, so we do
6881 // not need to do anything special for this here.
6882 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006883 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006884 AddSlowPath(slow_path);
6885
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006886 __ B(slow_path->GetEntryLabel());
6887 __ Bind(slow_path->GetExitLabel());
6888}
6889
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006890void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6891 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006892 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006893 locations->SetInAt(0, Location::RequiresRegister());
6894 locations->SetOut(Location::RequiresRegister());
6895}
6896
6897void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6898 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006899 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006900 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006901 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006902 __ Ldr(XRegisterFrom(locations->Out()),
6903 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006904 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006905 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006906 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006907 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6908 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006909 __ Ldr(XRegisterFrom(locations->Out()),
6910 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006911 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006912}
6913
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006914static void PatchJitRootUse(uint8_t* code,
6915 const uint8_t* roots_data,
6916 vixl::aarch64::Literal<uint32_t>* literal,
6917 uint64_t index_in_table) {
6918 uint32_t literal_offset = literal->GetOffset();
6919 uintptr_t address =
6920 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6921 uint8_t* data = code + literal_offset;
6922 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6923}
6924
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006925void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6926 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006927 const StringReference& string_reference = entry.first;
6928 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006929 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006930 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006931 }
6932 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006933 const TypeReference& type_reference = entry.first;
6934 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006935 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006936 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006937 }
6938}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006939
Artem Serov8ba4de12019-12-04 21:10:23 +00006940MemOperand InstructionCodeGeneratorARM64::VecNEONAddress(
Artem Serov1a719e42019-07-18 14:24:55 +01006941 HVecMemoryOperation* instruction,
6942 UseScratchRegisterScope* temps_scope,
6943 size_t size,
6944 bool is_string_char_at,
6945 /*out*/ Register* scratch) {
6946 LocationSummary* locations = instruction->GetLocations();
6947 Register base = InputRegisterAt(instruction, 0);
6948
6949 if (instruction->InputAt(1)->IsIntermediateAddressIndex()) {
6950 DCHECK(!is_string_char_at);
6951 return MemOperand(base.X(), InputRegisterAt(instruction, 1).X());
6952 }
6953
6954 Location index = locations->InAt(1);
6955 uint32_t offset = is_string_char_at
6956 ? mirror::String::ValueOffset().Uint32Value()
6957 : mirror::Array::DataOffset(size).Uint32Value();
6958 size_t shift = ComponentSizeShiftWidth(size);
6959
6960 // HIntermediateAddress optimization is only applied for scalar ArrayGet and ArraySet.
6961 DCHECK(!instruction->InputAt(0)->IsIntermediateAddress());
6962
6963 if (index.IsConstant()) {
6964 offset += Int64FromLocation(index) << shift;
6965 return HeapOperand(base, offset);
6966 } else {
6967 *scratch = temps_scope->AcquireSameSizeAs(base);
6968 __ Add(*scratch, base, Operand(WRegisterFrom(index), LSL, shift));
6969 return HeapOperand(*scratch, offset);
6970 }
6971}
6972
Artem Serov8ba4de12019-12-04 21:10:23 +00006973SVEMemOperand InstructionCodeGeneratorARM64::VecSVEAddress(
6974 HVecMemoryOperation* instruction,
6975 UseScratchRegisterScope* temps_scope,
6976 size_t size,
6977 bool is_string_char_at,
6978 /*out*/ Register* scratch) {
6979 LocationSummary* locations = instruction->GetLocations();
6980 Register base = InputRegisterAt(instruction, 0);
6981 Location index = locations->InAt(1);
6982
Artem Serov8ba4de12019-12-04 21:10:23 +00006983 DCHECK(!instruction->InputAt(1)->IsIntermediateAddressIndex());
Artem Serov8ba4de12019-12-04 21:10:23 +00006984 DCHECK(!index.IsConstant());
6985
6986 uint32_t offset = is_string_char_at
6987 ? mirror::String::ValueOffset().Uint32Value()
6988 : mirror::Array::DataOffset(size).Uint32Value();
6989 size_t shift = ComponentSizeShiftWidth(size);
6990
Raphael Gault0700b692020-09-30 08:33:10 +00006991 if (instruction->InputAt(0)->IsIntermediateAddress()) {
6992 return SVEMemOperand(base.X(), XRegisterFrom(index), LSL, shift);
6993 }
6994
Artem Serov8ba4de12019-12-04 21:10:23 +00006995 *scratch = temps_scope->AcquireSameSizeAs(base);
6996 __ Add(*scratch, base, offset);
6997 return SVEMemOperand(scratch->X(), XRegisterFrom(index), LSL, shift);
6998}
6999
Alexandre Rames67555f72014-11-18 10:55:16 +00007000#undef __
7001#undef QUICK_ENTRY_POINT
7002
Vladimir Markoca1e0382018-04-11 09:58:41 +00007003#define __ assembler.GetVIXLAssembler()->
7004
7005static void EmitGrayCheckAndFastPath(arm64::Arm64Assembler& assembler,
7006 vixl::aarch64::Register base_reg,
7007 vixl::aarch64::MemOperand& lock_word,
Vladimir Marko7a695052018-04-12 10:26:50 +01007008 vixl::aarch64::Label* slow_path,
7009 vixl::aarch64::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00007010 // Load the lock word containing the rb_state.
7011 __ Ldr(ip0.W(), lock_word);
7012 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01007013 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Vladimir Markoca1e0382018-04-11 09:58:41 +00007014 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7015 __ Tbnz(ip0.W(), LockWord::kReadBarrierStateShift, slow_path);
7016 static_assert(
7017 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET,
7018 "Field and array LDR offsets must be the same to reuse the same code.");
Vladimir Marko7a695052018-04-12 10:26:50 +01007019 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
7020 if (throw_npe != nullptr) {
7021 __ Bind(throw_npe);
7022 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00007023 // Adjust the return address back to the LDR (1 instruction; 2 for heap poisoning).
7024 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
7025 "Field LDR must be 1 instruction (4B) before the return address label; "
7026 " 2 instructions (8B) for heap poisoning.");
7027 __ Add(lr, lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
7028 // Introduce a dependency on the lock_word including rb_state,
7029 // to prevent load-load reordering, and without using
7030 // a memory barrier (which would be more expensive).
7031 __ Add(base_reg, base_reg, Operand(ip0, LSR, 32));
7032 __ Br(lr); // And return back to the function.
7033 // Note: The fake dependency is unnecessary for the slow path.
7034}
7035
7036// Load the read barrier introspection entrypoint in register `entrypoint`.
7037static void LoadReadBarrierMarkIntrospectionEntrypoint(arm64::Arm64Assembler& assembler,
7038 vixl::aarch64::Register entrypoint) {
7039 // entrypoint = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
7040 DCHECK_EQ(ip0.GetCode(), 16u);
7041 const int32_t entry_point_offset =
7042 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
7043 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
7044}
7045
7046void CodeGeneratorARM64::CompileBakerReadBarrierThunk(Arm64Assembler& assembler,
7047 uint32_t encoded_data,
7048 /*out*/ std::string* debug_name) {
7049 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
7050 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01007051 case BakerReadBarrierKind::kField:
7052 case BakerReadBarrierKind::kAcquire: {
Artem Serova07de552020-11-01 22:42:43 +00007053 Register base_reg =
7054 vixl::aarch64::XRegister(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00007055 CheckValidReg(base_reg.GetCode());
Artem Serova07de552020-11-01 22:42:43 +00007056 Register holder_reg =
7057 vixl::aarch64::XRegister(BakerReadBarrierSecondRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00007058 CheckValidReg(holder_reg.GetCode());
7059 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
7060 temps.Exclude(ip0, ip1);
Roland Levillain988c3912019-09-25 19:33:35 +01007061 // In the case of a field load (with relaxed semantic), if `base_reg` differs from
7062 // `holder_reg`, the offset was too large and we must have emitted (during the construction
7063 // of the HIR graph, see `art::HInstructionBuilder::BuildInstanceFieldAccess`) and preserved
7064 // (see `art::PrepareForRegisterAllocation::VisitNullCheck`) an explicit null check before
7065 // the load. Otherwise, for implicit null checks, we need to null-check the holder as we do
7066 // not necessarily do that check before going to the thunk.
7067 //
7068 // In the case of a field load with load-acquire semantics (where `base_reg` always differs
7069 // from `holder_reg`), we also need an explicit null check when implicit null checks are
7070 // allowed, as we do not emit one before going to the thunk.
Vladimir Marko7a695052018-04-12 10:26:50 +01007071 vixl::aarch64::Label throw_npe_label;
7072 vixl::aarch64::Label* throw_npe = nullptr;
Roland Levillain988c3912019-09-25 19:33:35 +01007073 if (GetCompilerOptions().GetImplicitNullChecks() &&
7074 (holder_reg.Is(base_reg) || (kind == BakerReadBarrierKind::kAcquire))) {
Vladimir Marko7a695052018-04-12 10:26:50 +01007075 throw_npe = &throw_npe_label;
7076 __ Cbz(holder_reg.W(), throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00007077 }
Vladimir Marko7a695052018-04-12 10:26:50 +01007078 // Check if the holder is gray and, if not, add fake dependency to the base register
7079 // and return to the LDR instruction to load the reference. Otherwise, use introspection
7080 // to load the reference and call the entrypoint that performs further checks on the
7081 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00007082 vixl::aarch64::Label slow_path;
7083 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
Vladimir Marko7a695052018-04-12 10:26:50 +01007084 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00007085 __ Bind(&slow_path);
Vladimir Marko0ecac682018-08-07 10:40:38 +01007086 if (kind == BakerReadBarrierKind::kField) {
7087 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
7088 __ Ldr(ip0.W(), ldr_address); // Load the LDR (immediate) unsigned offset.
7089 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
7090 __ Ubfx(ip0.W(), ip0.W(), 10, 12); // Extract the offset.
7091 __ Ldr(ip0.W(), MemOperand(base_reg, ip0, LSL, 2)); // Load the reference.
7092 } else {
7093 DCHECK(kind == BakerReadBarrierKind::kAcquire);
7094 DCHECK(!base_reg.Is(holder_reg));
7095 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
7096 __ Ldar(ip0.W(), MemOperand(base_reg));
7097 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00007098 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
7099 __ Br(ip1); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00007100 break;
7101 }
7102 case BakerReadBarrierKind::kArray: {
Artem Serova07de552020-11-01 22:42:43 +00007103 Register base_reg =
7104 vixl::aarch64::XRegister(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00007105 CheckValidReg(base_reg.GetCode());
7106 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7107 BakerReadBarrierSecondRegField::Decode(encoded_data));
7108 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
7109 temps.Exclude(ip0, ip1);
7110 vixl::aarch64::Label slow_path;
7111 int32_t data_offset =
7112 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
7113 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
7114 DCHECK_LT(lock_word.GetOffset(), 0);
7115 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path);
7116 __ Bind(&slow_path);
7117 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
7118 __ Ldr(ip0.W(), ldr_address); // Load the LDR (register) unsigned offset.
7119 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
7120 __ Ubfx(ip0, ip0, 16, 6); // Extract the index register, plus 32 (bit 21 is set).
7121 __ Bfi(ip1, ip0, 3, 6); // Insert ip0 to the entrypoint address to create
7122 // a switch case target based on the index register.
7123 __ Mov(ip0, base_reg); // Move the base register to ip0.
7124 __ Br(ip1); // Jump to the entrypoint's array switch case.
7125 break;
7126 }
7127 case BakerReadBarrierKind::kGcRoot: {
7128 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
7129 // and it does not have a forwarding address), call the correct introspection entrypoint;
7130 // otherwise return the reference (or the extracted forwarding address).
7131 // There is no gray bit check for GC roots.
Artem Serova07de552020-11-01 22:42:43 +00007132 Register root_reg =
7133 vixl::aarch64::WRegister(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00007134 CheckValidReg(root_reg.GetCode());
7135 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7136 BakerReadBarrierSecondRegField::Decode(encoded_data));
7137 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
7138 temps.Exclude(ip0, ip1);
7139 vixl::aarch64::Label return_label, not_marked, forwarding_address;
7140 __ Cbz(root_reg, &return_label);
7141 MemOperand lock_word(root_reg.X(), mirror::Object::MonitorOffset().Int32Value());
7142 __ Ldr(ip0.W(), lock_word);
7143 __ Tbz(ip0.W(), LockWord::kMarkBitStateShift, &not_marked);
7144 __ Bind(&return_label);
7145 __ Br(lr);
7146 __ Bind(&not_marked);
7147 __ Tst(ip0.W(), Operand(ip0.W(), LSL, 1));
7148 __ B(&forwarding_address, mi);
7149 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
7150 // Adjust the art_quick_read_barrier_mark_introspection address in IP1 to
7151 // art_quick_read_barrier_mark_introspection_gc_roots.
7152 __ Add(ip1, ip1, Operand(BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRYPOINT_OFFSET));
7153 __ Mov(ip0.W(), root_reg);
7154 __ Br(ip1);
7155 __ Bind(&forwarding_address);
7156 __ Lsl(root_reg, ip0.W(), LockWord::kForwardingAddressShift);
7157 __ Br(lr);
7158 break;
7159 }
7160 default:
7161 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
7162 UNREACHABLE();
7163 }
7164
Vladimir Marko966b46f2018-08-03 10:20:19 +00007165 // For JIT, the slow path is considered part of the compiled method,
Vladimir Markof91fc122020-05-13 09:21:00 +01007166 // so JIT should pass null as `debug_name`.
Vladimir Marko695348f2020-05-19 14:42:02 +01007167 DCHECK(!GetCompilerOptions().IsJitCompiler() || debug_name == nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00007168 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00007169 std::ostringstream oss;
7170 oss << "BakerReadBarrierThunk";
7171 switch (kind) {
7172 case BakerReadBarrierKind::kField:
7173 oss << "Field_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
7174 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
7175 break;
Vladimir Marko0ecac682018-08-07 10:40:38 +01007176 case BakerReadBarrierKind::kAcquire:
7177 oss << "Acquire_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
7178 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
7179 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +00007180 case BakerReadBarrierKind::kArray:
7181 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
7182 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7183 BakerReadBarrierSecondRegField::Decode(encoded_data));
7184 break;
7185 case BakerReadBarrierKind::kGcRoot:
7186 oss << "GcRoot_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
7187 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7188 BakerReadBarrierSecondRegField::Decode(encoded_data));
7189 break;
7190 }
7191 *debug_name = oss.str();
7192 }
7193}
7194
7195#undef __
7196
Alexandre Rames5319def2014-10-23 10:03:10 +01007197} // namespace arm64
7198} // namespace art