blob: e1a4718140a063a362f15013be78dac99ce3c490 [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 ||
Vladimir Marko2d98dc22020-10-01 11:21:37 +0000684 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
Vladimir Marko1bff99f2020-11-02 15:07:33 +0000685 mirror::VarHandle::AccessModeTemplate::kGet ||
686 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
687 mirror::VarHandle::AccessModeTemplate::kCompareAndSet ||
688 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
Vladimir Marko32c2eb82020-11-10 16:58:47 +0000689 mirror::VarHandle::AccessModeTemplate::kCompareAndExchange ||
690 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
691 mirror::VarHandle::AccessModeTemplate::kGetAndUpdate)
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000692 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100693 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100694 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000695 }
696 }
697
698 // We're moving two or three locations to locations that could
699 // overlap, so we need a parallel move resolver.
700 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100701 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000702 parallel_move.AddMove(ref_,
703 LocationFrom(calling_convention.GetRegisterAt(0)),
704 type,
705 nullptr);
706 parallel_move.AddMove(obj_,
707 LocationFrom(calling_convention.GetRegisterAt(1)),
708 type,
709 nullptr);
710 if (index.IsValid()) {
711 parallel_move.AddMove(index,
712 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100713 DataType::Type::kInt32,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000714 nullptr);
715 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
716 } else {
717 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
718 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
719 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000720 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000721 instruction_,
722 instruction_->GetDexPc(),
723 this);
724 CheckEntrypointTypes<
725 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
726 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
727
728 RestoreLiveRegisters(codegen, locations);
729
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000730 __ B(GetExitLabel());
731 }
732
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100733 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000734
735 private:
736 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100737 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
738 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000739 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
740 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
741 return Register(VIXLRegCodeFromART(i), kXRegSize);
742 }
743 }
744 // We shall never fail to find a free caller-save register, as
745 // there are more than two core caller-save registers on ARM64
746 // (meaning it is possible to find one which is different from
747 // `ref` and `obj`).
748 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
749 LOG(FATAL) << "Could not find a free register";
750 UNREACHABLE();
751 }
752
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000753 const Location out_;
754 const Location ref_;
755 const Location obj_;
756 const uint32_t offset_;
757 // An additional location containing an index to an array.
758 // Only used for HArrayGet and the UnsafeGetObject &
759 // UnsafeGetObjectVolatile intrinsics.
760 const Location index_;
761
762 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
763};
764
765// Slow path generating a read barrier for a GC root.
766class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
767 public:
768 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000769 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000770 DCHECK(kEmitCompilerReadBarrier);
771 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000772
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100773 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000774 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100775 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000776 DCHECK(locations->CanCall());
777 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Vladimir Markoa41ea272020-09-07 15:24:36 +0000778 DCHECK(instruction_->IsLoadClass() ||
779 instruction_->IsLoadString() ||
780 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000781 << "Unexpected instruction in read barrier for GC root slow path: "
782 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000783
784 __ Bind(GetEntryLabel());
785 SaveLiveRegisters(codegen, locations);
786
787 InvokeRuntimeCallingConvention calling_convention;
788 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
789 // The argument of the ReadBarrierForRootSlow is not a managed
790 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
791 // thus we need a 64-bit move here, and we cannot use
792 //
793 // arm64_codegen->MoveLocation(
794 // LocationFrom(calling_convention.GetRegisterAt(0)),
795 // root_,
796 // type);
797 //
798 // which would emit a 32-bit move, as `type` is a (32-bit wide)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100799 // reference type (`DataType::Type::kReference`).
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000800 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000801 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000802 instruction_,
803 instruction_->GetDexPc(),
804 this);
805 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
806 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
807
808 RestoreLiveRegisters(codegen, locations);
809 __ B(GetExitLabel());
810 }
811
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100812 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000813
814 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000815 const Location out_;
816 const Location root_;
817
818 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
819};
820
Alexandre Rames5319def2014-10-23 10:03:10 +0100821#undef __
822
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100823Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100824 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100825 if (type == DataType::Type::kVoid) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100826 LOG(FATAL) << "Unreachable type " << type;
827 }
828
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100829 if (DataType::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100830 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
831 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100832 } else if (!DataType::IsFloatingPointType(type) &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000833 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000834 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
835 } else {
836 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100837 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
838 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100839 }
840
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000841 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100842 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100843 return next_location;
844}
845
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100846Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100847 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100848}
849
Vladimir Marko86c87522020-05-11 16:55:55 +0100850Location CriticalNativeCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
851 DCHECK_NE(type, DataType::Type::kReference);
852
853 Location location = Location::NoLocation();
854 if (DataType::IsFloatingPointType(type)) {
855 if (fpr_index_ < kParameterFPRegistersLength) {
856 location = LocationFrom(kParameterFPRegisters[fpr_index_]);
857 ++fpr_index_;
858 }
859 } else {
860 // Native ABI uses the same registers as managed, except that the method register x0
861 // is a normal argument.
862 if (gpr_index_ < 1u + kParameterCoreRegistersLength) {
863 location = LocationFrom(gpr_index_ == 0u ? x0 : kParameterCoreRegisters[gpr_index_ - 1u]);
864 ++gpr_index_;
865 }
866 }
867 if (location.IsInvalid()) {
868 if (DataType::Is64BitType(type)) {
869 location = Location::DoubleStackSlot(stack_offset_);
870 } else {
871 location = Location::StackSlot(stack_offset_);
872 }
873 stack_offset_ += kFramePointerSize;
874
875 if (for_register_allocation_) {
876 location = Location::Any();
877 }
878 }
879 return location;
880}
881
882Location CriticalNativeCallingConventionVisitorARM64::GetReturnLocation(DataType::Type type) const {
883 // We perform conversion to the managed ABI return register after the call if needed.
884 InvokeDexCallingConventionVisitorARM64 dex_calling_convention;
885 return dex_calling_convention.GetReturnLocation(type);
886}
887
888Location CriticalNativeCallingConventionVisitorARM64::GetMethodLocation() const {
889 // Pass the method in the hidden argument x15.
890 return Location::RegisterLocation(x15.GetCode());
891}
892
Serban Constantinescu579885a2015-02-22 20:51:33 +0000893CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100894 const CompilerOptions& compiler_options,
895 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100896 : CodeGenerator(graph,
897 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000898 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000899 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100900 callee_saved_core_registers.GetList(),
901 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100902 compiler_options,
903 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100904 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
905 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov1a719e42019-07-18 14:24:55 +0100906 location_builder_neon_(graph, this),
907 instruction_visitor_neon_(graph, this),
908 location_builder_sve_(graph, this),
909 instruction_visitor_sve_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100910 move_resolver_(graph->GetAllocator(), this),
Artem Serovaa6f4832018-11-21 18:57:54 +0000911 assembler_(graph->GetAllocator(),
912 compiler_options.GetInstructionSetFeatures()->AsArm64InstructionSetFeatures()),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000913 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100914 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000915 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100916 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko8f63f102020-09-28 12:10:28 +0100917 public_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
918 package_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000919 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100920 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeb9eb002020-10-02 13:54:19 +0100921 boot_image_jni_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +0100922 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100923 call_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100924 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100925 uint32_literals_(std::less<uint32_t>(),
926 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
927 uint64_literals_(std::less<uint64_t>(),
928 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000929 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100930 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000931 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +0000932 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
933 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
934 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000935 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000936 AddAllocatedRegister(LocationFrom(lr));
Artem Serov1a719e42019-07-18 14:24:55 +0100937
938 bool use_sve = ShouldUseSVE();
939 if (use_sve) {
940 location_builder_ = &location_builder_sve_;
941 instruction_visitor_ = &instruction_visitor_sve_;
942 } else {
943 location_builder_ = &location_builder_neon_;
944 instruction_visitor_ = &instruction_visitor_neon_;
945 }
946}
947
948bool CodeGeneratorARM64::ShouldUseSVE() const {
Artem Serov8ba4de12019-12-04 21:10:23 +0000949 return GetInstructionSetFeatures().HasSVE();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000950}
Alexandre Rames5319def2014-10-23 10:03:10 +0100951
Artem Serov55ab7e82020-04-27 21:02:28 +0100952size_t CodeGeneratorARM64::GetSIMDRegisterWidth() const {
953 return SupportsPredicatedSIMD()
954 ? GetInstructionSetFeatures().GetSVEVectorLength() / kBitsPerByte
955 : vixl::aarch64::kQRegSizeInBytes;
956}
957
Alexandre Rames67555f72014-11-18 10:55:16 +0000958#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100959
Zheng Xu3927c8b2015-11-18 17:46:25 +0800960void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100961 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800962 jump_table->EmitTable(this);
963 }
964}
965
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000966void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800967 EmitJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +0000968
969 // Emit JIT baker read barrier slow paths.
Vladimir Marko695348f2020-05-19 14:42:02 +0100970 DCHECK(GetCompilerOptions().IsJitCompiler() || jit_baker_read_barrier_slow_paths_.empty());
Vladimir Marko966b46f2018-08-03 10:20:19 +0000971 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
972 uint32_t encoded_data = entry.first;
973 vixl::aarch64::Label* slow_path_entry = &entry.second.label;
974 __ Bind(slow_path_entry);
Andreas Gampe3db70682018-12-26 15:12:03 -0800975 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name= */ nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +0000976 }
977
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000978 // Ensure we emit the literal pool.
979 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000980
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000981 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +0000982
983 // Verify Baker read barrier linker patches.
984 if (kIsDebugBuild) {
985 ArrayRef<const uint8_t> code = allocator->GetMemory();
986 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
987 DCHECK(info.label.IsBound());
988 uint32_t literal_offset = info.label.GetLocation();
989 DCHECK_ALIGNED(literal_offset, 4u);
990
991 auto GetInsn = [&code](uint32_t offset) {
992 DCHECK_ALIGNED(offset, 4u);
993 return
994 (static_cast<uint32_t>(code[offset + 0]) << 0) +
995 (static_cast<uint32_t>(code[offset + 1]) << 8) +
996 (static_cast<uint32_t>(code[offset + 2]) << 16)+
997 (static_cast<uint32_t>(code[offset + 3]) << 24);
998 };
999
1000 const uint32_t encoded_data = info.custom_data;
1001 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
1002 // Check that the next instruction matches the expected LDR.
1003 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01001004 case BakerReadBarrierKind::kField:
1005 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00001006 DCHECK_GE(code.size() - literal_offset, 8u);
1007 uint32_t next_insn = GetInsn(literal_offset + 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001008 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1009 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko0ecac682018-08-07 10:40:38 +01001010 if (kind == BakerReadBarrierKind::kField) {
1011 // LDR (immediate) with correct base_reg.
1012 CHECK_EQ(next_insn & 0xffc003e0u, 0xb9400000u | (base_reg << 5));
1013 } else {
1014 DCHECK(kind == BakerReadBarrierKind::kAcquire);
1015 // LDAR with correct base_reg.
1016 CHECK_EQ(next_insn & 0xffffffe0u, 0x88dffc00u | (base_reg << 5));
1017 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001018 break;
1019 }
1020 case BakerReadBarrierKind::kArray: {
1021 DCHECK_GE(code.size() - literal_offset, 8u);
1022 uint32_t next_insn = GetInsn(literal_offset + 4u);
1023 // LDR (register) with the correct base_reg, size=10 (32-bit), option=011 (extend = LSL),
1024 // and S=1 (shift amount = 2 for 32-bit version), i.e. LDR Wt, [Xn, Xm, LSL #2].
1025 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1026 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1027 CHECK_EQ(next_insn & 0xffe0ffe0u, 0xb8607800u | (base_reg << 5));
1028 CheckValidReg((next_insn >> 16) & 0x1f); // Check index register
1029 break;
1030 }
1031 case BakerReadBarrierKind::kGcRoot: {
1032 DCHECK_GE(literal_offset, 4u);
1033 uint32_t prev_insn = GetInsn(literal_offset - 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001034 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko94796f82018-08-08 15:15:33 +01001035 // Usually LDR (immediate) with correct root_reg but
Vladimir Markoc8178f52020-11-24 10:38:16 +00001036 // we may have a "MOV marked, old_value" for intrinsic CAS.
Vladimir Marko94796f82018-08-08 15:15:33 +01001037 if ((prev_insn & 0xffe0ffff) != (0x2a0003e0 | root_reg)) { // MOV?
1038 CHECK_EQ(prev_insn & 0xffc0001fu, 0xb9400000u | root_reg); // LDR?
1039 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001040 break;
1041 }
1042 default:
1043 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
1044 UNREACHABLE();
1045 }
1046 }
1047 }
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001048}
1049
Zheng Xuad4450e2015-04-17 18:48:56 +08001050void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1051 // Note: There are 6 kinds of moves:
1052 // 1. constant -> GPR/FPR (non-cycle)
1053 // 2. constant -> stack (non-cycle)
1054 // 3. GPR/FPR -> GPR/FPR
1055 // 4. GPR/FPR -> stack
1056 // 5. stack -> GPR/FPR
1057 // 6. stack -> stack (non-cycle)
1058 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1059 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1060 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1061 // dependency.
1062 vixl_temps_.Open(GetVIXLAssembler());
1063}
1064
1065void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1066 vixl_temps_.Close();
1067}
1068
1069Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001070 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1071 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1072 || kind == Location::kSIMDStackSlot);
1073 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1074 ? Location::kFpuRegister
1075 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001076 Location scratch = GetScratchLocation(kind);
1077 if (!scratch.Equals(Location::NoLocation())) {
1078 return scratch;
1079 }
1080 // Allocate from VIXL temp registers.
1081 if (kind == Location::kRegister) {
1082 scratch = LocationFrom(vixl_temps_.AcquireX());
1083 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001084 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serov1a719e42019-07-18 14:24:55 +01001085 scratch = codegen_->GetGraph()->HasSIMD()
1086 ? codegen_->GetInstructionCodeGeneratorArm64()->AllocateSIMDScratchLocation(&vixl_temps_)
1087 : LocationFrom(vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001088 }
1089 AddScratchLocation(scratch);
1090 return scratch;
1091}
1092
1093void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1094 if (loc.IsRegister()) {
1095 vixl_temps_.Release(XRegisterFrom(loc));
1096 } else {
1097 DCHECK(loc.IsFpuRegister());
Artem Serov1a719e42019-07-18 14:24:55 +01001098 if (codegen_->GetGraph()->HasSIMD()) {
1099 codegen_->GetInstructionCodeGeneratorArm64()->FreeSIMDScratchLocation(loc, &vixl_temps_);
1100 } else {
1101 vixl_temps_.Release(DRegisterFrom(loc));
1102 }
Zheng Xuad4450e2015-04-17 18:48:56 +08001103 }
1104 RemoveScratchLocation(loc);
1105}
1106
Alexandre Rames3e69f162014-12-10 10:36:50 +00001107void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001108 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001109 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001110}
1111
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001112void CodeGeneratorARM64::MaybeIncrementHotness(bool is_frame_entry) {
1113 MacroAssembler* masm = GetVIXLAssembler();
1114 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1115 UseScratchRegisterScope temps(masm);
1116 Register counter = temps.AcquireX();
1117 Register method = is_frame_entry ? kArtMethodRegister : temps.AcquireX();
1118 if (!is_frame_entry) {
1119 __ Ldr(method, MemOperand(sp, 0));
1120 }
1121 __ Ldrh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
1122 __ Add(counter, counter, 1);
1123 // Subtract one if the counter would overflow.
1124 __ Sub(counter, counter, Operand(counter, LSR, 16));
1125 __ Strh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
1126 }
1127
1128 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffray095dc462020-08-17 16:40:28 +01001129 ScopedProfilingInfoUse spiu(
1130 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
1131 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001132 if (info != nullptr) {
Nicolas Geoffrayc1cd1332020-01-25 13:08:24 +00001133 uint64_t address = reinterpret_cast64<uint64_t>(info);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001134 vixl::aarch64::Label done;
1135 UseScratchRegisterScope temps(masm);
1136 Register temp = temps.AcquireX();
1137 Register counter = temps.AcquireW();
1138 __ Mov(temp, address);
1139 __ Ldrh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
1140 __ Add(counter, counter, 1);
Nicolas Geoffray4313ccb2020-08-26 17:01:15 +01001141 __ And(counter, counter, interpreter::kTieredHotnessMask);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001142 __ Strh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
Nicolas Geoffray4313ccb2020-08-26 17:01:15 +01001143 __ Cbnz(counter, &done);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001144 if (is_frame_entry) {
1145 if (HasEmptyFrame()) {
Vladimir Markodec78172020-06-19 15:31:23 +01001146 // The entrypoint expects the method at the bottom of the stack. We
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001147 // claim stack space necessary for alignment.
Vladimir Markodec78172020-06-19 15:31:23 +01001148 IncreaseFrame(kStackAlignment);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001149 __ Stp(kArtMethodRegister, lr, MemOperand(sp, 0));
1150 } else if (!RequiresCurrentMethod()) {
1151 __ Str(kArtMethodRegister, MemOperand(sp, 0));
1152 }
1153 } else {
1154 CHECK(RequiresCurrentMethod());
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001155 }
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001156 uint32_t entrypoint_offset =
1157 GetThreadOffset<kArm64PointerSize>(kQuickCompileOptimized).Int32Value();
1158 __ Ldr(lr, MemOperand(tr, entrypoint_offset));
1159 // Note: we don't record the call here (and therefore don't generate a stack
1160 // map), as the entrypoint should never be suspended.
1161 __ Blr(lr);
1162 if (HasEmptyFrame()) {
1163 CHECK(is_frame_entry);
1164 __ Ldr(lr, MemOperand(sp, 8));
Vladimir Markodec78172020-06-19 15:31:23 +01001165 DecreaseFrame(kStackAlignment);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001166 }
1167 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001168 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001169 }
1170}
1171
Alexandre Rames5319def2014-10-23 10:03:10 +01001172void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001173 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001174 __ Bind(&frame_entry_label_);
1175
Vladimir Marko33bff252017-11-01 14:35:42 +00001176 bool do_overflow_check =
1177 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001178 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001179 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001180 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001181 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001182 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001183 {
1184 // Ensure that between load and RecordPcInfo there are no pools emitted.
1185 ExactAssemblyScope eas(GetVIXLAssembler(),
1186 kInstructionSize,
1187 CodeBufferCheckScope::kExactSize);
1188 __ ldr(wzr, MemOperand(temp, 0));
1189 RecordPcInfo(nullptr, 0);
1190 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001191 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001192
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001193 if (!HasEmptyFrame()) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001194 // Stack layout:
1195 // sp[frame_size - 8] : lr.
1196 // ... : other preserved core registers.
1197 // ... : other preserved fp registers.
1198 // ... : reserved frame space.
1199 // sp[0] : current method.
Vladimir Marko1a225a72019-07-05 13:37:42 +01001200 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1201 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1202 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1203 DCHECK(!preserved_core_registers.IsEmpty());
1204 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1205 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001206
Vladimir Marko1a225a72019-07-05 13:37:42 +01001207 // Save the current method if we need it, or if using STP reduces code
1208 // size. Note that we do not do this in HCurrentMethod, as the
1209 // instruction might have been removed in the SSA graph.
1210 CPURegister lowest_spill;
1211 if (core_spills_offset == kXRegSizeInBytes) {
1212 // If there is no gap between the method and the lowest core spill, use
1213 // aligned STP pre-index to store both. Max difference is 512. We do
1214 // that to reduce code size even if we do not have to save the method.
1215 DCHECK_LE(frame_size, 512); // 32 core registers are only 256 bytes.
1216 lowest_spill = preserved_core_registers.PopLowestIndex();
1217 __ Stp(kArtMethodRegister, lowest_spill, MemOperand(sp, -frame_size, PreIndex));
1218 } else if (RequiresCurrentMethod()) {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001219 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001220 } else {
1221 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001222 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001223 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Vladimir Marko1a225a72019-07-05 13:37:42 +01001224 if (lowest_spill.IsValid()) {
1225 GetAssembler()->cfi().RelOffset(DWARFReg(lowest_spill), core_spills_offset);
1226 core_spills_offset += kXRegSizeInBytes;
1227 }
1228 GetAssembler()->SpillRegisters(preserved_core_registers, core_spills_offset);
1229 GetAssembler()->SpillRegisters(preserved_fp_registers, fp_spills_offset);
Mingyao Yang063fc772016-08-02 11:02:54 -07001230
1231 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1232 // Initialize should_deoptimize flag to 0.
1233 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1234 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1235 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001236 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001237 MaybeIncrementHotness(/* is_frame_entry= */ true);
Andreas Gampe3db70682018-12-26 15:12:03 -08001238 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001239}
1240
1241void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001242 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001243 if (!HasEmptyFrame()) {
Vladimir Marko1a225a72019-07-05 13:37:42 +01001244 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1245 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1246 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1247 DCHECK(!preserved_core_registers.IsEmpty());
1248 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1249 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
1250
1251 CPURegister lowest_spill;
1252 if (core_spills_offset == kXRegSizeInBytes) {
1253 // If there is no gap between the method and the lowest core spill, use
1254 // aligned LDP pre-index to pop both. Max difference is 504. We do
1255 // that to reduce code size even though the loaded method is unused.
1256 DCHECK_LE(frame_size, 504); // 32 core registers are only 256 bytes.
1257 lowest_spill = preserved_core_registers.PopLowestIndex();
1258 core_spills_offset += kXRegSizeInBytes;
1259 }
1260 GetAssembler()->UnspillRegisters(preserved_fp_registers, fp_spills_offset);
1261 GetAssembler()->UnspillRegisters(preserved_core_registers, core_spills_offset);
1262 if (lowest_spill.IsValid()) {
1263 __ Ldp(xzr, lowest_spill, MemOperand(sp, frame_size, PostIndex));
1264 GetAssembler()->cfi().Restore(DWARFReg(lowest_spill));
1265 } else {
1266 __ Drop(frame_size);
1267 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001268 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001269 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001270 __ Ret();
1271 GetAssembler()->cfi().RestoreState();
1272 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001273}
1274
Scott Wakeling97c72b72016-06-24 16:19:36 +01001275CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001276 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001277 return CPURegList(CPURegister::kRegister, kXRegSize,
1278 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001279}
1280
Scott Wakeling97c72b72016-06-24 16:19:36 +01001281CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001282 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1283 GetNumberOfFloatingPointRegisters()));
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001284 return CPURegList(CPURegister::kVRegister, kDRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001285 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001286}
1287
Alexandre Rames5319def2014-10-23 10:03:10 +01001288void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1289 __ Bind(GetLabelOf(block));
1290}
1291
Calin Juravle175dc732015-08-25 15:42:32 +01001292void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1293 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001294 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001295}
1296
Calin Juravlee460d1d2015-09-29 04:52:17 +01001297void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1298 if (location.IsRegister()) {
1299 locations->AddTemp(location);
1300 } else {
1301 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1302 }
1303}
1304
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001305void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001306 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001307 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001308 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001309 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001310 if (value_can_be_null) {
1311 __ Cbz(value, &done);
1312 }
Roland Levillainc73f0522018-08-14 15:16:50 +01001313 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07001314 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01001315 // Calculate the offset (in the card table) of the card corresponding to
1316 // `object`.
Alexandre Rames5319def2014-10-23 10:03:10 +01001317 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Roland Levillainc73f0522018-08-14 15:16:50 +01001318 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
1319 // `object`'s card.
1320 //
1321 // Register `card` contains the address of the card table. Note that the card
1322 // table's base is biased during its creation so that it always starts at an
1323 // address whose least-significant byte is equal to `kCardDirty` (see
1324 // art::gc::accounting::CardTable::Create). Therefore the STRB instruction
1325 // below writes the `kCardDirty` (byte) value into the `object`'s card
1326 // (located at `card + object >> kCardShift`).
1327 //
1328 // This dual use of the value in register `card` (1. to calculate the location
1329 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
1330 // (no need to explicitly load `kCardDirty` as an immediate value).
Serban Constantinescu02164b32014-11-13 14:05:07 +00001331 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001332 if (value_can_be_null) {
1333 __ Bind(&done);
1334 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001335}
1336
David Brazdil58282f42016-01-14 12:45:10 +00001337void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001338 // Blocked core registers:
1339 // lr : Runtime reserved.
1340 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001341 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001342 // ip1 : VIXL core temp.
1343 // ip0 : VIXL core temp.
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001344 // x18 : Platform register.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001345 //
1346 // Blocked fp registers:
1347 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001348 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1349 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001350 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001351 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001352 }
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001353 blocked_core_registers_[X18] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001354
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001355 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001356 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001357 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001358 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001359
David Brazdil58282f42016-01-14 12:45:10 +00001360 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001361 // Stubs do not save callee-save floating point registers. If the graph
1362 // is debuggable, we need to deal with these registers differently. For
1363 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001364 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1365 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001366 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001367 }
1368 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001369}
1370
Alexandre Rames3e69f162014-12-10 10:36:50 +00001371size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1372 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1373 __ Str(reg, MemOperand(sp, stack_index));
1374 return kArm64WordSize;
1375}
1376
1377size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1378 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1379 __ Ldr(reg, MemOperand(sp, stack_index));
1380 return kArm64WordSize;
1381}
1382
Artem Serov9df37b92019-07-23 16:41:54 +01001383size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1384 uint32_t reg_id ATTRIBUTE_UNUSED) {
1385 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1386 << "use SaveRestoreLiveRegistersHelper";
1387 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001388}
1389
Artem Serov9df37b92019-07-23 16:41:54 +01001390size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1391 uint32_t reg_id ATTRIBUTE_UNUSED) {
1392 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1393 << "use SaveRestoreLiveRegistersHelper";
1394 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001395}
1396
Alexandre Rames5319def2014-10-23 10:03:10 +01001397void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001398 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001399}
1400
1401void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001402 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001403}
1404
Vladimir Markoa0431112018-06-25 09:32:54 +01001405const Arm64InstructionSetFeatures& CodeGeneratorARM64::GetInstructionSetFeatures() const {
1406 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArm64InstructionSetFeatures();
1407}
1408
Alexandre Rames67555f72014-11-18 10:55:16 +00001409void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001410 if (constant->IsIntConstant()) {
1411 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1412 } else if (constant->IsLongConstant()) {
1413 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1414 } else if (constant->IsNullConstant()) {
1415 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001416 } else if (constant->IsFloatConstant()) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001417 __ Fmov(VRegister(destination), constant->AsFloatConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001418 } else {
1419 DCHECK(constant->IsDoubleConstant());
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001420 __ Fmov(VRegister(destination), constant->AsDoubleConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001421 }
1422}
1423
Alexandre Rames3e69f162014-12-10 10:36:50 +00001424
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001425static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001426 DCHECK(constant.IsConstant());
1427 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001428 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001429 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001430 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1431 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1432 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1433 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001434}
1435
Roland Levillain952b2352017-05-03 19:49:14 +01001436// Allocate a scratch register from the VIXL pool, querying first
1437// the floating-point register pool, and then the core register
1438// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001439// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1440// using a different allocation strategy.
1441static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1442 vixl::aarch64::UseScratchRegisterScope* temps,
1443 int size_in_bits) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001444 return masm->GetScratchVRegisterList()->IsEmpty()
Roland Levillain558dea12017-01-27 19:40:44 +00001445 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1446 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1447}
1448
Calin Juravlee460d1d2015-09-29 04:52:17 +01001449void CodeGeneratorARM64::MoveLocation(Location destination,
1450 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001451 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001452 if (source.Equals(destination)) {
1453 return;
1454 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001455
1456 // A valid move can always be inferred from the destination and source
1457 // locations. When moving from and to a register, the argument type can be
1458 // used to generate 32bit instead of 64bit moves. In debug mode we also
1459 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001460 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001461
1462 if (destination.IsRegister() || destination.IsFpuRegister()) {
1463 if (unspecified_type) {
1464 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1465 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001466 (src_cst != nullptr && (src_cst->IsIntConstant()
1467 || src_cst->IsFloatConstant()
1468 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001469 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001470 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001471 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001472 // If the source is a double stack slot or a 64bit constant, a 64bit
1473 // type is appropriate. Else the source is a register, and since the
1474 // type has not been specified, we chose a 64bit type to force a 64bit
1475 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001476 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001477 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001478 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001479 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1480 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001481 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001482 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1483 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1484 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001485 } else if (source.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001486 GetInstructionCodeGeneratorArm64()->LoadSIMDRegFromStack(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001487 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001488 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001489 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001490 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001491 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001492 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001493 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001494 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001495 DataType::Type source_type = DataType::Is64BitType(dst_type)
1496 ? DataType::Type::kInt64
1497 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001498 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1499 }
1500 } else {
1501 DCHECK(source.IsFpuRegister());
1502 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001503 DataType::Type source_type = DataType::Is64BitType(dst_type)
1504 ? DataType::Type::kFloat64
1505 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001506 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1507 } else {
1508 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001509 if (GetGraph()->HasSIMD()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001510 GetInstructionCodeGeneratorArm64()->MoveSIMDRegToSIMDReg(destination, source);
Artem Serovd4bccf12017-04-03 18:47:32 +01001511 } else {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001512 __ Fmov(VRegister(dst), FPRegisterFrom(source, dst_type));
Artem Serovd4bccf12017-04-03 18:47:32 +01001513 }
1514 }
1515 }
1516 } else if (destination.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001517 GetInstructionCodeGeneratorArm64()->MoveToSIMDStackSlot(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001518 } else { // The destination is not a register. It must be a stack slot.
1519 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1520 if (source.IsRegister() || source.IsFpuRegister()) {
1521 if (unspecified_type) {
1522 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001523 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001524 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001525 dst_type =
1526 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001527 }
1528 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001529 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1530 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001531 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001532 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001533 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1534 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001535 UseScratchRegisterScope temps(GetVIXLAssembler());
1536 HConstant* src_cst = source.GetConstant();
1537 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001538 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001539 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1540 ? Register(xzr)
1541 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001542 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001543 if (src_cst->IsIntConstant()) {
1544 temp = temps.AcquireW();
1545 } else if (src_cst->IsLongConstant()) {
1546 temp = temps.AcquireX();
1547 } else if (src_cst->IsFloatConstant()) {
1548 temp = temps.AcquireS();
1549 } else {
1550 DCHECK(src_cst->IsDoubleConstant());
1551 temp = temps.AcquireD();
1552 }
1553 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001554 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001555 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001556 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001557 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001558 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001559 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001560 // Use any scratch register (a core or a floating-point one)
1561 // from VIXL scratch register pools as a temporary.
1562 //
1563 // We used to only use the FP scratch register pool, but in some
1564 // rare cases the only register from this pool (D31) would
1565 // already be used (e.g. within a ParallelMove instruction, when
1566 // a move is blocked by a another move requiring a scratch FP
1567 // register, which would reserve D31). To prevent this issue, we
1568 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001569 //
1570 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001571 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001572 // use AcquireFPOrCoreCPURegisterOfSize instead of
1573 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1574 // allocates core scratch registers first.
1575 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1576 GetVIXLAssembler(),
1577 &temps,
1578 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001579 __ Ldr(temp, StackOperandFrom(source));
1580 __ Str(temp, StackOperandFrom(destination));
1581 }
1582 }
1583}
1584
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001585void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001586 CPURegister dst,
1587 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001588 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001589 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001590 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001591 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001592 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001593 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001594 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001595 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001596 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001597 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001598 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001599 case DataType::Type::kInt16:
1600 __ Ldrsh(Register(dst), src);
1601 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001602 case DataType::Type::kInt32:
1603 case DataType::Type::kReference:
1604 case DataType::Type::kInt64:
1605 case DataType::Type::kFloat32:
1606 case DataType::Type::kFloat64:
1607 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001608 __ Ldr(dst, src);
1609 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001610 case DataType::Type::kUint32:
1611 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001612 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001613 LOG(FATAL) << "Unreachable type " << type;
1614 }
1615}
1616
Calin Juravle77520bc2015-01-12 18:45:46 +00001617void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Vladimir Marko98873af2020-12-16 12:10:03 +00001618 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001619 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001620 const MemOperand& src,
1621 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001622 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001623 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001624 Register temp_base = temps.AcquireX();
1625
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001626 DCHECK(!src.IsPreIndex());
1627 DCHECK(!src.IsPostIndex());
1628
1629 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001630 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001631 {
1632 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1633 MemOperand base = MemOperand(temp_base);
1634 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001635 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001636 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001637 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001638 {
1639 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1640 __ ldarb(Register(dst), base);
1641 if (needs_null_check) {
1642 MaybeRecordImplicitNullCheck(instruction);
1643 }
1644 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001645 if (type == DataType::Type::kInt8) {
1646 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001647 }
1648 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001649 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001650 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001651 {
1652 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1653 __ ldarh(Register(dst), base);
1654 if (needs_null_check) {
1655 MaybeRecordImplicitNullCheck(instruction);
1656 }
1657 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001658 if (type == DataType::Type::kInt16) {
1659 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
1660 }
Artem Serov914d7a82017-02-07 14:33:49 +00001661 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001662 case DataType::Type::kInt32:
1663 case DataType::Type::kReference:
1664 case DataType::Type::kInt64:
1665 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001666 {
1667 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1668 __ ldar(Register(dst), base);
1669 if (needs_null_check) {
1670 MaybeRecordImplicitNullCheck(instruction);
1671 }
1672 }
1673 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001674 case DataType::Type::kFloat32:
1675 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00001676 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001677 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001678
Artem Serov914d7a82017-02-07 14:33:49 +00001679 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1680 {
1681 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1682 __ ldar(temp, base);
1683 if (needs_null_check) {
1684 MaybeRecordImplicitNullCheck(instruction);
1685 }
1686 }
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001687 __ Fmov(VRegister(dst), temp);
Artem Serov914d7a82017-02-07 14:33:49 +00001688 break;
Roland Levillain44015862016-01-22 11:47:17 +00001689 }
Aart Bik66c158e2018-01-31 12:55:04 -08001690 case DataType::Type::kUint32:
1691 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001692 case DataType::Type::kVoid:
Artem Serov914d7a82017-02-07 14:33:49 +00001693 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001694 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001695 }
1696}
1697
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001698void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001699 CPURegister src,
1700 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001701 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001702 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001703 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001704 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001705 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001706 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001707 case DataType::Type::kUint16:
1708 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001709 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001710 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001711 case DataType::Type::kInt32:
1712 case DataType::Type::kReference:
1713 case DataType::Type::kInt64:
1714 case DataType::Type::kFloat32:
1715 case DataType::Type::kFloat64:
1716 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001717 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001718 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001719 case DataType::Type::kUint32:
1720 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001721 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001722 LOG(FATAL) << "Unreachable type " << type;
1723 }
1724}
1725
Artem Serov914d7a82017-02-07 14:33:49 +00001726void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001727 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001728 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00001729 const MemOperand& dst,
1730 bool needs_null_check) {
1731 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001732 UseScratchRegisterScope temps(GetVIXLAssembler());
1733 Register temp_base = temps.AcquireX();
1734
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001735 DCHECK(!dst.IsPreIndex());
1736 DCHECK(!dst.IsPostIndex());
1737
1738 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001739 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001740 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001741 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00001742 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001743 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001744 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001745 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001746 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001747 {
1748 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1749 __ stlrb(Register(src), base);
1750 if (needs_null_check) {
1751 MaybeRecordImplicitNullCheck(instruction);
1752 }
1753 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001754 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001755 case DataType::Type::kUint16:
1756 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001757 {
1758 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1759 __ stlrh(Register(src), base);
1760 if (needs_null_check) {
1761 MaybeRecordImplicitNullCheck(instruction);
1762 }
1763 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001764 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001765 case DataType::Type::kInt32:
1766 case DataType::Type::kReference:
1767 case DataType::Type::kInt64:
1768 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001769 {
1770 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1771 __ stlr(Register(src), base);
1772 if (needs_null_check) {
1773 MaybeRecordImplicitNullCheck(instruction);
1774 }
1775 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001776 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001777 case DataType::Type::kFloat32:
1778 case DataType::Type::kFloat64: {
1779 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001780 Register temp_src;
1781 if (src.IsZero()) {
1782 // The zero register is used to avoid synthesizing zero constants.
1783 temp_src = Register(src);
1784 } else {
1785 DCHECK(src.IsFPRegister());
1786 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001787 __ Fmov(temp_src, VRegister(src));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001788 }
Artem Serov914d7a82017-02-07 14:33:49 +00001789 {
1790 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1791 __ stlr(temp_src, base);
1792 if (needs_null_check) {
1793 MaybeRecordImplicitNullCheck(instruction);
1794 }
1795 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001796 break;
1797 }
Aart Bik66c158e2018-01-31 12:55:04 -08001798 case DataType::Type::kUint32:
1799 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001800 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001801 LOG(FATAL) << "Unreachable type " << type;
1802 }
1803}
1804
Calin Juravle175dc732015-08-25 15:42:32 +01001805void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1806 HInstruction* instruction,
1807 uint32_t dex_pc,
1808 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001809 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00001810
Vladimir Markof6675082019-05-17 12:05:28 +01001811 ThreadOffset64 entrypoint_offset = GetThreadOffset<kArm64PointerSize>(entrypoint);
1812 // Reduce code size for AOT by using shared trampolines for slow path runtime calls across the
1813 // entire oat file. This adds an extra branch and we do not want to slow down the main path.
1814 // For JIT, thunk sharing is per-method, so the gains would be smaller or even negative.
Vladimir Marko695348f2020-05-19 14:42:02 +01001815 if (slow_path == nullptr || GetCompilerOptions().IsJitCompiler()) {
Vladimir Markof6675082019-05-17 12:05:28 +01001816 __ Ldr(lr, MemOperand(tr, entrypoint_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00001817 // Ensure the pc position is recorded immediately after the `blr` instruction.
1818 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1819 __ blr(lr);
1820 if (EntrypointRequiresStackMap(entrypoint)) {
1821 RecordPcInfo(instruction, dex_pc, slow_path);
1822 }
Vladimir Markof6675082019-05-17 12:05:28 +01001823 } else {
1824 // Ensure the pc position is recorded immediately after the `bl` instruction.
1825 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1826 EmitEntrypointThunkCall(entrypoint_offset);
1827 if (EntrypointRequiresStackMap(entrypoint)) {
1828 RecordPcInfo(instruction, dex_pc, slow_path);
1829 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001830 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001831}
1832
Roland Levillaindec8f632016-07-22 17:10:06 +01001833void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1834 HInstruction* instruction,
1835 SlowPathCode* slow_path) {
1836 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01001837 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1838 __ Blr(lr);
1839}
1840
Alexandre Rames67555f72014-11-18 10:55:16 +00001841void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001842 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001843 UseScratchRegisterScope temps(GetVIXLAssembler());
1844 Register temp = temps.AcquireW();
Vladimir Markodc682aa2018-01-04 18:42:57 +00001845 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001846 const size_t status_byte_offset =
1847 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1848 constexpr uint32_t shifted_visibly_initialized_value =
1849 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001850
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001851 // CMP (immediate) is limited to imm12 or imm12<<12, so we would need to materialize
1852 // the constant 0xf0000000 for comparison with the full 32-bit field. To reduce the code
1853 // size, load only the high byte of the field and compare with 0xf0.
1854 // Note: The same code size could be achieved with LDR+MNV(asr #24)+CBNZ but benchmarks
1855 // show that this pattern is slower (tested on little cores).
1856 __ Ldrb(temp, HeapOperand(class_reg, status_byte_offset));
1857 __ Cmp(temp, shifted_visibly_initialized_value);
1858 __ B(lo, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001859 __ Bind(slow_path->GetExitLabel());
1860}
Alexandre Rames5319def2014-10-23 10:03:10 +01001861
Vladimir Marko175e7862018-03-27 09:03:13 +00001862void InstructionCodeGeneratorARM64::GenerateBitstringTypeCheckCompare(
1863 HTypeCheckInstruction* check, vixl::aarch64::Register temp) {
1864 uint32_t path_to_root = check->GetBitstringPathToRoot();
1865 uint32_t mask = check->GetBitstringMask();
1866 DCHECK(IsPowerOfTwo(mask + 1));
1867 size_t mask_bits = WhichPowerOf2(mask + 1);
1868
1869 if (mask_bits == 16u) {
1870 // Load only the bitstring part of the status word.
1871 __ Ldrh(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1872 } else {
1873 // /* uint32_t */ temp = temp->status_
1874 __ Ldr(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1875 // Extract the bitstring bits.
1876 __ Ubfx(temp, temp, 0, mask_bits);
1877 }
1878 // Compare the bitstring bits to `path_to_root`.
1879 __ Cmp(temp, path_to_root);
1880}
1881
Roland Levillain44015862016-01-22 11:47:17 +00001882void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001883 BarrierType type = BarrierAll;
1884
1885 switch (kind) {
1886 case MemBarrierKind::kAnyAny:
1887 case MemBarrierKind::kAnyStore: {
1888 type = BarrierAll;
1889 break;
1890 }
1891 case MemBarrierKind::kLoadAny: {
1892 type = BarrierReads;
1893 break;
1894 }
1895 case MemBarrierKind::kStoreStore: {
1896 type = BarrierWrites;
1897 break;
1898 }
1899 default:
1900 LOG(FATAL) << "Unexpected memory barrier " << kind;
1901 }
1902 __ Dmb(InnerShareable, type);
1903}
1904
Serban Constantinescu02164b32014-11-13 14:05:07 +00001905void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1906 HBasicBlock* successor) {
1907 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001908 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1909 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001910 slow_path =
1911 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001912 instruction->SetSlowPath(slow_path);
1913 codegen_->AddSlowPath(slow_path);
1914 if (successor != nullptr) {
1915 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001916 }
1917 } else {
1918 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1919 }
1920
Serban Constantinescu02164b32014-11-13 14:05:07 +00001921 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1922 Register temp = temps.AcquireW();
1923
Andreas Gampe542451c2016-07-26 09:02:02 -07001924 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001925 if (successor == nullptr) {
1926 __ Cbnz(temp, slow_path->GetEntryLabel());
1927 __ Bind(slow_path->GetReturnLabel());
1928 } else {
1929 __ Cbz(temp, codegen_->GetLabelOf(successor));
1930 __ B(slow_path->GetEntryLabel());
1931 // slow_path will return to GetLabelOf(successor).
1932 }
1933}
1934
Alexandre Rames5319def2014-10-23 10:03:10 +01001935InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1936 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001937 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001938 assembler_(codegen->GetAssembler()),
1939 codegen_(codegen) {}
1940
Alexandre Rames67555f72014-11-18 10:55:16 +00001941void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001942 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001943 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001944 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001945 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001946 case DataType::Type::kInt32:
1947 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01001948 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001949 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001950 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001951 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001952
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001953 case DataType::Type::kFloat32:
1954 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001955 locations->SetInAt(0, Location::RequiresFpuRegister());
1956 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001957 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001958 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001959
Alexandre Rames5319def2014-10-23 10:03:10 +01001960 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001961 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001962 }
1963}
1964
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001965void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
1966 const FieldInfo& field_info) {
Alex Light3a73ffb2021-01-25 14:11:05 +00001967 DCHECK(instruction->IsInstanceFieldGet() ||
1968 instruction->IsStaticFieldGet() ||
1969 instruction->IsPredicatedInstanceFieldGet());
1970
1971 bool is_predicated = instruction->IsPredicatedInstanceFieldGet();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001972
1973 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001974 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01001975 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001976 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
1977 object_field_get_with_read_barrier
1978 ? LocationSummary::kCallOnSlowPath
1979 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001980 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001981 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko0ecac682018-08-07 10:40:38 +01001982 // We need a temporary register for the read barrier load in
1983 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
1984 // only if the field is volatile or the offset is too big.
1985 if (field_info.IsVolatile() ||
1986 field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
1987 locations->AddTemp(FixedTempLocation());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001988 }
Vladimir Marko70e97462016-08-09 11:04:26 +01001989 }
Alex Light3a73ffb2021-01-25 14:11:05 +00001990 // Input for object receiver.
1991 locations->SetInAt(is_predicated ? 1 : 0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001992 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alex Light3a73ffb2021-01-25 14:11:05 +00001993 if (is_predicated) {
1994 locations->SetInAt(0, Location::RequiresFpuRegister());
1995 locations->SetOut(Location::SameAsFirstInput());
1996 } else {
1997 locations->SetOut(Location::RequiresFpuRegister());
1998 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001999 } else {
Alex Light3a73ffb2021-01-25 14:11:05 +00002000 if (is_predicated) {
2001 locations->SetInAt(0, Location::RequiresRegister());
2002 locations->SetOut(Location::SameAsFirstInput());
2003 } else {
2004 // The output overlaps for an object field get when read barriers
2005 // are enabled: we do not want the load to overwrite the object's
2006 // location, as we need it to emit the read barrier.
2007 locations->SetOut(Location::RequiresRegister(),
2008 object_field_get_with_read_barrier ? Location::kOutputOverlap
2009 : Location::kNoOutputOverlap);
2010 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002011 }
2012}
2013
2014void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2015 const FieldInfo& field_info) {
Alex Light3a73ffb2021-01-25 14:11:05 +00002016 DCHECK(instruction->IsInstanceFieldGet() ||
2017 instruction->IsStaticFieldGet() ||
2018 instruction->IsPredicatedInstanceFieldGet());
2019 bool is_predicated = instruction->IsPredicatedInstanceFieldGet();
Roland Levillain44015862016-01-22 11:47:17 +00002020 LocationSummary* locations = instruction->GetLocations();
Alex Light3a73ffb2021-01-25 14:11:05 +00002021 uint32_t receiver_input = is_predicated ? 1 : 0;
2022 Location base_loc = locations->InAt(receiver_input);
Roland Levillain44015862016-01-22 11:47:17 +00002023 Location out = locations->Out();
2024 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01002025 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
2026 DataType::Type load_type = instruction->GetType();
Alex Light3a73ffb2021-01-25 14:11:05 +00002027 MemOperand field =
2028 HeapOperand(InputRegisterAt(instruction, receiver_input), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002029
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002030 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01002031 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002032 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002033 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002034 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002035 Location maybe_temp =
2036 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002037 // Note that potential implicit null checks are handled in this
2038 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2039 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2040 instruction,
2041 out,
2042 base,
2043 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002044 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002045 /* needs_null_check= */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002046 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002047 } else {
2048 // General case.
2049 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002050 // Note that a potential implicit null check is handled in this
2051 // CodeGeneratorARM64::LoadAcquire call.
2052 // NB: LoadAcquire will record the pc info if needed.
Vladimir Marko98873af2020-12-16 12:10:03 +00002053 codegen_->LoadAcquire(instruction,
2054 load_type,
2055 OutputCPURegister(instruction),
2056 field,
2057 /* needs_null_check= */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002058 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002059 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2060 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01002061 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002062 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002063 }
Vladimir Marko61b92282017-10-11 13:23:17 +01002064 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002065 // If read barriers are enabled, emit read barriers other than
2066 // Baker's using a slow path (and also unpoison the loaded
2067 // reference, if heap poisoning is enabled).
2068 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2069 }
Roland Levillain4d027112015-07-01 15:41:14 +01002070 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002071}
2072
2073void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2074 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002075 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01002076 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002077 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2078 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002079 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002080 locations->SetInAt(1, Location::RequiresFpuRegister());
2081 } else {
2082 locations->SetInAt(1, Location::RequiresRegister());
2083 }
2084}
2085
2086void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002087 const FieldInfo& field_info,
2088 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002089 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alex Light3a73ffb2021-01-25 14:11:05 +00002090 bool is_predicated =
2091 instruction->IsInstanceFieldSet() && instruction->AsInstanceFieldSet()->GetIsPredicatedSet();
Alexandre Rames09a99962015-04-15 11:47:56 +01002092
2093 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002094 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002095 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002096 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002097 DataType::Type field_type = field_info.GetFieldType();
Alex Light3a73ffb2021-01-25 14:11:05 +00002098 std::optional<vixl::aarch64::Label> pred_is_null;
2099 if (is_predicated) {
2100 pred_is_null.emplace();
2101 __ Cbz(obj, &*pred_is_null);
2102 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002103
Roland Levillain4d027112015-07-01 15:41:14 +01002104 {
2105 // We use a block to end the scratch scope before the write barrier, thus
2106 // freeing the temporary registers so they can be used in `MarkGCCard`.
2107 UseScratchRegisterScope temps(GetVIXLAssembler());
2108
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002109 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002110 DCHECK(value.IsW());
2111 Register temp = temps.AcquireW();
2112 __ Mov(temp, value.W());
2113 GetAssembler()->PoisonHeapReference(temp.W());
2114 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002115 }
Roland Levillain4d027112015-07-01 15:41:14 +01002116
2117 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002118 codegen_->StoreRelease(
Andreas Gampe3db70682018-12-26 15:12:03 -08002119 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check= */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002120 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002121 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2122 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002123 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2124 codegen_->MaybeRecordImplicitNullCheck(instruction);
2125 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002126 }
2127
2128 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002129 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002130 }
Alex Light3a73ffb2021-01-25 14:11:05 +00002131
2132 if (is_predicated) {
2133 __ Bind(&*pred_is_null);
2134 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002135}
2136
Alexandre Rames67555f72014-11-18 10:55:16 +00002137void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002138 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002139
2140 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002141 case DataType::Type::kInt32:
2142 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002143 Register dst = OutputRegister(instr);
2144 Register lhs = InputRegisterAt(instr, 0);
2145 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002146 if (instr->IsAdd()) {
2147 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002148 } else if (instr->IsAnd()) {
2149 __ And(dst, lhs, rhs);
2150 } else if (instr->IsOr()) {
2151 __ Orr(dst, lhs, rhs);
2152 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002153 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002154 } else if (instr->IsRor()) {
2155 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002156 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002157 __ Ror(dst, lhs, shift);
2158 } else {
2159 // Ensure shift distance is in the same size register as the result. If
2160 // we are rotating a long and the shift comes in a w register originally,
2161 // we don't need to sxtw for use as an x since the shift distances are
2162 // all & reg_bits - 1.
2163 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2164 }
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002165 } else if (instr->IsMin() || instr->IsMax()) {
2166 __ Cmp(lhs, rhs);
2167 __ Csel(dst, lhs, rhs, instr->IsMin() ? lt : gt);
Alexandre Rames67555f72014-11-18 10:55:16 +00002168 } else {
2169 DCHECK(instr->IsXor());
2170 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002171 }
2172 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002173 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002174 case DataType::Type::kFloat32:
2175 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002176 VRegister dst = OutputFPRegister(instr);
2177 VRegister lhs = InputFPRegisterAt(instr, 0);
2178 VRegister rhs = InputFPRegisterAt(instr, 1);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002179 if (instr->IsAdd()) {
2180 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002181 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002182 __ Fsub(dst, lhs, rhs);
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002183 } else if (instr->IsMin()) {
2184 __ Fmin(dst, lhs, rhs);
2185 } else if (instr->IsMax()) {
2186 __ Fmax(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002187 } else {
2188 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002189 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002190 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002191 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002192 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002193 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002194 }
2195}
2196
Serban Constantinescu02164b32014-11-13 14:05:07 +00002197void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2198 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2199
Vladimir Markoca6fff82017-10-03 14:49:14 +01002200 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002201 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002202 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002203 case DataType::Type::kInt32:
2204 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002205 locations->SetInAt(0, Location::RequiresRegister());
2206 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002207 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002208 break;
2209 }
2210 default:
2211 LOG(FATAL) << "Unexpected shift type " << type;
2212 }
2213}
2214
2215void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2216 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2217
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002218 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002219 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002220 case DataType::Type::kInt32:
2221 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002222 Register dst = OutputRegister(instr);
2223 Register lhs = InputRegisterAt(instr, 0);
2224 Operand rhs = InputOperandAt(instr, 1);
2225 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002226 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002227 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002228 if (instr->IsShl()) {
2229 __ Lsl(dst, lhs, shift_value);
2230 } else if (instr->IsShr()) {
2231 __ Asr(dst, lhs, shift_value);
2232 } else {
2233 __ Lsr(dst, lhs, shift_value);
2234 }
2235 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002236 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002237
2238 if (instr->IsShl()) {
2239 __ Lsl(dst, lhs, rhs_reg);
2240 } else if (instr->IsShr()) {
2241 __ Asr(dst, lhs, rhs_reg);
2242 } else {
2243 __ Lsr(dst, lhs, rhs_reg);
2244 }
2245 }
2246 break;
2247 }
2248 default:
2249 LOG(FATAL) << "Unexpected shift operation type " << type;
2250 }
2251}
2252
Alexandre Rames5319def2014-10-23 10:03:10 +01002253void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002254 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002255}
2256
2257void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002258 HandleBinaryOp(instruction);
2259}
2260
2261void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2262 HandleBinaryOp(instruction);
2263}
2264
2265void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2266 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002267}
2268
Artem Serov7fc63502016-02-09 17:15:29 +00002269void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002270 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002271 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002272 locations->SetInAt(0, Location::RequiresRegister());
2273 // There is no immediate variant of negated bitwise instructions in AArch64.
2274 locations->SetInAt(1, Location::RequiresRegister());
2275 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2276}
2277
Artem Serov7fc63502016-02-09 17:15:29 +00002278void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002279 Register dst = OutputRegister(instr);
2280 Register lhs = InputRegisterAt(instr, 0);
2281 Register rhs = InputRegisterAt(instr, 1);
2282
2283 switch (instr->GetOpKind()) {
2284 case HInstruction::kAnd:
2285 __ Bic(dst, lhs, rhs);
2286 break;
2287 case HInstruction::kOr:
2288 __ Orn(dst, lhs, rhs);
2289 break;
2290 case HInstruction::kXor:
2291 __ Eon(dst, lhs, rhs);
2292 break;
2293 default:
2294 LOG(FATAL) << "Unreachable";
2295 }
2296}
2297
Anton Kirilov74234da2017-01-13 14:42:47 +00002298void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2299 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002300 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2301 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002302 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002303 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002304 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2305 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2306 } else {
2307 locations->SetInAt(0, Location::RequiresRegister());
2308 }
2309 locations->SetInAt(1, Location::RequiresRegister());
2310 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2311}
2312
Anton Kirilov74234da2017-01-13 14:42:47 +00002313void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2314 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002315 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002316 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002317 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002318 Register out = OutputRegister(instruction);
2319 Register left;
2320 if (kind != HInstruction::kNeg) {
2321 left = InputRegisterAt(instruction, 0);
2322 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002323 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002324 // shifter operand operation, the IR generating `right_reg` (input to the type
2325 // conversion) can have a different type from the current instruction's type,
2326 // so we manually indicate the type.
2327 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002328 Operand right_operand(0);
2329
Anton Kirilov74234da2017-01-13 14:42:47 +00002330 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2331 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002332 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2333 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002334 right_operand = Operand(right_reg,
2335 helpers::ShiftFromOpKind(op_kind),
2336 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002337 }
2338
2339 // Logical binary operations do not support extension operations in the
2340 // operand. Note that VIXL would still manage if it was passed by generating
2341 // the extension as a separate instruction.
2342 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2343 DCHECK(!right_operand.IsExtendedRegister() ||
2344 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2345 kind != HInstruction::kNeg));
2346 switch (kind) {
2347 case HInstruction::kAdd:
2348 __ Add(out, left, right_operand);
2349 break;
2350 case HInstruction::kAnd:
2351 __ And(out, left, right_operand);
2352 break;
2353 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002354 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002355 __ Neg(out, right_operand);
2356 break;
2357 case HInstruction::kOr:
2358 __ Orr(out, left, right_operand);
2359 break;
2360 case HInstruction::kSub:
2361 __ Sub(out, left, right_operand);
2362 break;
2363 case HInstruction::kXor:
2364 __ Eor(out, left, right_operand);
2365 break;
2366 default:
2367 LOG(FATAL) << "Unexpected operation kind: " << kind;
2368 UNREACHABLE();
2369 }
2370}
2371
Artem Serov328429f2016-07-06 16:23:04 +01002372void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002373 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002374 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002375 locations->SetInAt(0, Location::RequiresRegister());
2376 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002377 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002378}
2379
Roland Levillain19c54192016-11-04 13:44:09 +00002380void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002381 __ Add(OutputRegister(instruction),
2382 InputRegisterAt(instruction, 0),
2383 Operand(InputOperandAt(instruction, 1)));
2384}
2385
Artem Serove1811ed2017-04-27 16:50:47 +01002386void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2387 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002388 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002389
2390 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2391
2392 locations->SetInAt(0, Location::RequiresRegister());
2393 // For byte case we don't need to shift the index variable so we can encode the data offset into
2394 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2395 // data offset constant generation out of the loop and reduce the critical path length in the
2396 // loop.
2397 locations->SetInAt(1, shift->GetValue() == 0
2398 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2399 : Location::RequiresRegister());
2400 locations->SetInAt(2, Location::ConstantLocation(shift));
2401 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2402}
2403
2404void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2405 HIntermediateAddressIndex* instruction) {
2406 Register index_reg = InputRegisterAt(instruction, 0);
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002407 uint32_t shift = Int64FromLocation(instruction->GetLocations()->InAt(2));
Artem Serove1811ed2017-04-27 16:50:47 +01002408 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2409
2410 if (shift == 0) {
2411 __ Add(OutputRegister(instruction), index_reg, offset);
2412 } else {
2413 Register offset_reg = InputRegisterAt(instruction, 1);
2414 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2415 }
2416}
2417
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002418void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002419 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002420 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002421 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2422 if (instr->GetOpKind() == HInstruction::kSub &&
2423 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002424 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002425 // Don't allocate register for Mneg instruction.
2426 } else {
2427 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2428 Location::RequiresRegister());
2429 }
2430 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2431 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002432 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2433}
2434
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002435void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002436 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002437 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2438 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002439
2440 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2441 // This fixup should be carried out for all multiply-accumulate instructions:
2442 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002443 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002444 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2445 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002446 vixl::aarch64::Instruction* prev =
2447 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002448 if (prev->IsLoadOrStore()) {
2449 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002450 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002451 __ nop();
2452 }
2453 }
2454
2455 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002456 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002457 __ Madd(res, mul_left, mul_right, accumulator);
2458 } else {
2459 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002460 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002461 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002462 __ Mneg(res, mul_left, mul_right);
2463 } else {
2464 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2465 __ Msub(res, mul_left, mul_right, accumulator);
2466 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002467 }
2468}
2469
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002470void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002471 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002472 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002473 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002474 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2475 object_array_get_with_read_barrier
2476 ? LocationSummary::kCallOnSlowPath
2477 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002478 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002479 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002480 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002481 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002482 // We need a temporary register for the read barrier load in
2483 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2484 // only if the offset is too big.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002485 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2486 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002487 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002488 if (offset >= kReferenceLoadMinFarOffset) {
2489 locations->AddTemp(FixedTempLocation());
2490 }
Artem Serov0806f582018-10-11 20:14:20 +01002491 } else if (!instruction->GetArray()->IsIntermediateAddress()) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01002492 // We need a non-scratch temporary for the array data pointer in
Artem Serov0806f582018-10-11 20:14:20 +01002493 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier() for the case with no
2494 // intermediate address.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002495 locations->AddTemp(Location::RequiresRegister());
2496 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002497 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002498 locations->SetInAt(0, Location::RequiresRegister());
2499 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002500 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002501 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2502 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002503 // The output overlaps in the case of an object array get with
2504 // read barriers enabled: we do not want the move to overwrite the
2505 // array's location, as we need it to emit the read barrier.
2506 locations->SetOut(
2507 Location::RequiresRegister(),
2508 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002509 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002510}
2511
2512void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002513 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002514 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002515 LocationSummary* locations = instruction->GetLocations();
2516 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002517 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002518 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002519 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2520 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002521 MacroAssembler* masm = GetVIXLAssembler();
2522 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002523
Artem Serov0806f582018-10-11 20:14:20 +01002524 // The non-Baker read barrier instrumentation of object ArrayGet instructions
Roland Levillain19c54192016-11-04 13:44:09 +00002525 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002526 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002527 instruction->GetArray()->IsIntermediateAddress() &&
Artem Serov0806f582018-10-11 20:14:20 +01002528 kEmitCompilerReadBarrier &&
2529 !kUseBakerReadBarrier));
Roland Levillain19c54192016-11-04 13:44:09 +00002530
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002531 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002532 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002533 // Note that a potential implicit null check is handled in the
2534 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002535 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002536 if (index.IsConstant()) {
Artem Serov0806f582018-10-11 20:14:20 +01002537 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002538 // Array load with a constant index can be treated as a field load.
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002539 offset += Int64FromLocation(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002540 Location maybe_temp =
2541 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2542 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2543 out,
2544 obj.W(),
2545 offset,
2546 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002547 /* needs_null_check= */ false,
2548 /* use_load_acquire= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002549 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002550 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08002551 instruction, out, obj.W(), offset, index, /* needs_null_check= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002552 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002553 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002554 // General case.
2555 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002556 Register length;
2557 if (maybe_compressed_char_at) {
2558 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2559 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002560 {
2561 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2562 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2563
2564 if (instruction->GetArray()->IsIntermediateAddress()) {
2565 DCHECK_LT(count_offset, offset);
2566 int64_t adjusted_offset =
2567 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2568 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2569 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2570 } else {
2571 __ Ldr(length, HeapOperand(obj, count_offset));
2572 }
2573 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002574 }
jessicahandojo05765752016-09-09 19:01:32 -07002575 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002576 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002577 if (maybe_compressed_char_at) {
2578 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002579 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2580 "Expecting 0=compressed, 1=uncompressed");
2581 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002582 __ Ldrb(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002583 HeapOperand(obj, offset + Int64FromLocation(index)));
jessicahandojo05765752016-09-09 19:01:32 -07002584 __ B(&done);
2585 __ Bind(&uncompressed_load);
2586 __ Ldrh(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002587 HeapOperand(obj, offset + (Int64FromLocation(index) << 1)));
jessicahandojo05765752016-09-09 19:01:32 -07002588 __ Bind(&done);
2589 } else {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002590 offset += Int64FromLocation(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002591 source = HeapOperand(obj, offset);
2592 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002593 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002594 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002595 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002596 // We do not need to compute the intermediate address from the array: the
2597 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002598 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002599 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002600 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2601 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
Roland Levillain44015862016-01-22 11:47:17 +00002602 }
2603 temp = obj;
2604 } else {
2605 __ Add(temp, obj, offset);
2606 }
jessicahandojo05765752016-09-09 19:01:32 -07002607 if (maybe_compressed_char_at) {
2608 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002609 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2610 "Expecting 0=compressed, 1=uncompressed");
2611 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002612 __ Ldrb(Register(OutputCPURegister(instruction)),
2613 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2614 __ B(&done);
2615 __ Bind(&uncompressed_load);
2616 __ Ldrh(Register(OutputCPURegister(instruction)),
2617 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2618 __ Bind(&done);
2619 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002620 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002621 }
Roland Levillain44015862016-01-22 11:47:17 +00002622 }
jessicahandojo05765752016-09-09 19:01:32 -07002623 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002624 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2625 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002626 codegen_->Load(type, OutputCPURegister(instruction), source);
2627 codegen_->MaybeRecordImplicitNullCheck(instruction);
2628 }
Roland Levillain44015862016-01-22 11:47:17 +00002629
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002630 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002631 static_assert(
2632 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2633 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2634 Location obj_loc = locations->InAt(0);
2635 if (index.IsConstant()) {
2636 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2637 } else {
2638 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2639 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002640 }
Roland Levillain4d027112015-07-01 15:41:14 +01002641 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002642}
2643
Alexandre Rames5319def2014-10-23 10:03:10 +01002644void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002645 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002646 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002647 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002648}
2649
2650void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002651 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002652 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002653 {
2654 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2655 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2656 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2657 codegen_->MaybeRecordImplicitNullCheck(instruction);
2658 }
jessicahandojo05765752016-09-09 19:01:32 -07002659 // Mask out compression flag from String's array length.
2660 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002661 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002662 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002663}
2664
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002665void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002666 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002667
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002668 bool needs_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002669 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002670 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002671 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002672 locations->SetInAt(0, Location::RequiresRegister());
2673 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002674 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2675 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002676 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002677 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002678 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002679 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002680 }
2681}
2682
2683void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002684 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002685 LocationSummary* locations = instruction->GetLocations();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002686 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002687 bool needs_write_barrier =
2688 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002689
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002690 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002691 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002692 CPURegister source = value;
2693 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002694 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002695 MemOperand destination = HeapOperand(array);
2696 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002697
2698 if (!needs_write_barrier) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002699 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002700 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002701 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002702 destination = HeapOperand(array, offset);
2703 } else {
2704 UseScratchRegisterScope temps(masm);
2705 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002706 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002707 // We do not need to compute the intermediate address from the array: the
2708 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002709 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002710 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002711 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2712 DCHECK(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002713 }
2714 temp = array;
2715 } else {
2716 __ Add(temp, array, offset);
2717 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002718 destination = HeapOperand(temp,
2719 XRegisterFrom(index),
2720 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002721 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002722 }
Artem Serov914d7a82017-02-07 14:33:49 +00002723 {
2724 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2725 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2726 codegen_->Store(value_type, value, destination);
2727 codegen_->MaybeRecordImplicitNullCheck(instruction);
2728 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002729 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002730 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002731
2732 bool can_value_be_null = instruction->GetValueCanBeNull();
2733 vixl::aarch64::Label do_store;
2734 if (can_value_be_null) {
2735 __ Cbz(Register(value), &do_store);
2736 }
2737
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002738 SlowPathCodeARM64* slow_path = nullptr;
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002739 if (needs_type_check) {
2740 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
2741 codegen_->AddSlowPath(slow_path);
2742
2743 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2744 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2745 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2746
Alexandre Rames97833a02015-04-16 15:07:12 +01002747 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002748 Register temp = temps.AcquireSameSizeAs(array);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002749 Register temp2 = temps.AcquireSameSizeAs(array);
2750
2751 // Note that when Baker read barriers are enabled, the type
2752 // checks are performed without read barriers. This is fine,
2753 // even in the case where a class object is in the from-space
2754 // after the flip, as a comparison involving such a type would
2755 // not produce a false positive; it may of course produce a
2756 // false negative, in which case we would take the ArraySet
2757 // slow path.
2758
2759 // /* HeapReference<Class> */ temp = array->klass_
2760 {
2761 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2762 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2763 __ Ldr(temp, HeapOperand(array, class_offset));
2764 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames97833a02015-04-16 15:07:12 +01002765 }
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002766 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Alexandre Rames97833a02015-04-16 15:07:12 +01002767
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002768 // /* HeapReference<Class> */ temp = temp->component_type_
2769 __ Ldr(temp, HeapOperand(temp, component_offset));
2770 // /* HeapReference<Class> */ temp2 = value->klass_
2771 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2772 // If heap poisoning is enabled, no need to unpoison `temp`
2773 // nor `temp2`, as we are comparing two poisoned references.
2774 __ Cmp(temp, temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002775
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002776 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2777 vixl::aarch64::Label do_put;
2778 __ B(eq, &do_put);
2779 // If heap poisoning is enabled, the `temp` reference has
2780 // not been unpoisoned yet; unpoison it now.
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002781 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002782
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002783 // /* HeapReference<Class> */ temp = temp->super_class_
2784 __ Ldr(temp, HeapOperand(temp, super_offset));
2785 // If heap poisoning is enabled, no need to unpoison
2786 // `temp`, as we are comparing against null below.
2787 __ Cbnz(temp, slow_path->GetEntryLabel());
2788 __ Bind(&do_put);
Vladimir Markod1ef8732017-04-18 13:55:13 +01002789 } else {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002790 __ B(ne, slow_path->GetEntryLabel());
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002791 }
2792 }
2793
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002794 codegen_->MarkGCCard(array, value.W(), /* value_can_be_null= */ false);
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002795
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002796 if (can_value_be_null) {
2797 DCHECK(do_store.IsLinked());
2798 __ Bind(&do_store);
2799 }
2800
2801 UseScratchRegisterScope temps(masm);
2802 if (kPoisonHeapReferences) {
2803 Register temp_source = temps.AcquireSameSizeAs(array);
2804 DCHECK(value.IsW());
2805 __ Mov(temp_source, value.W());
2806 GetAssembler()->PoisonHeapReference(temp_source);
2807 source = temp_source;
2808 }
2809
2810 if (index.IsConstant()) {
2811 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
2812 destination = HeapOperand(array, offset);
2813 } else {
2814 Register temp_base = temps.AcquireSameSizeAs(array);
2815 __ Add(temp_base, array, offset);
2816 destination = HeapOperand(temp_base,
2817 XRegisterFrom(index),
2818 LSL,
2819 DataType::SizeShift(value_type));
2820 }
2821
2822 {
2823 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2824 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2825 __ Str(source, destination);
2826
2827 if (can_value_be_null || !needs_type_check) {
2828 codegen_->MaybeRecordImplicitNullCheck(instruction);
2829 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002830 }
2831
2832 if (slow_path != nullptr) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002833 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002834 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002835 }
2836}
2837
Alexandre Rames67555f72014-11-18 10:55:16 +00002838void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002839 RegisterSet caller_saves = RegisterSet::Empty();
2840 InvokeRuntimeCallingConvention calling_convention;
2841 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2842 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2843 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002844
2845 // If both index and length are constant, we can check the bounds statically and
2846 // generate code accordingly. We want to make sure we generate constant locations
2847 // in that case, regardless of whether they are encodable in the comparison or not.
2848 HInstruction* index = instruction->InputAt(0);
2849 HInstruction* length = instruction->InputAt(1);
2850 bool both_const = index->IsConstant() && length->IsConstant();
2851 locations->SetInAt(0, both_const
2852 ? Location::ConstantLocation(index->AsConstant())
2853 : ARM64EncodableConstantOrRegister(index, instruction));
2854 locations->SetInAt(1, both_const
2855 ? Location::ConstantLocation(length->AsConstant())
2856 : ARM64EncodableConstantOrRegister(length, instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002857}
2858
2859void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Georgia Kouvelibe530852019-01-17 10:46:41 +00002860 LocationSummary* locations = instruction->GetLocations();
2861 Location index_loc = locations->InAt(0);
2862 Location length_loc = locations->InAt(1);
2863
2864 int cmp_first_input = 0;
2865 int cmp_second_input = 1;
2866 Condition cond = hs;
2867
2868 if (index_loc.IsConstant()) {
2869 int64_t index = Int64FromLocation(index_loc);
2870 if (length_loc.IsConstant()) {
2871 int64_t length = Int64FromLocation(length_loc);
2872 if (index < 0 || index >= length) {
2873 BoundsCheckSlowPathARM64* slow_path =
2874 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
2875 codegen_->AddSlowPath(slow_path);
2876 __ B(slow_path->GetEntryLabel());
2877 } else {
2878 // BCE will remove the bounds check if we are guaranteed to pass.
2879 // However, some optimization after BCE may have generated this, and we should not
2880 // generate a bounds check if it is a valid range.
2881 }
2882 return;
2883 }
2884 // Only the index is constant: change the order of the operands and commute the condition
2885 // so we can use an immediate constant for the index (only the second input to a cmp
2886 // instruction can be an immediate).
2887 cmp_first_input = 1;
2888 cmp_second_input = 0;
2889 cond = ls;
2890 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002891 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002892 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002893 __ Cmp(InputRegisterAt(instruction, cmp_first_input),
2894 InputOperandAt(instruction, cmp_second_input));
Alexandre Rames67555f72014-11-18 10:55:16 +00002895 codegen_->AddSlowPath(slow_path);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002896 __ B(slow_path->GetEntryLabel(), cond);
Alexandre Rames67555f72014-11-18 10:55:16 +00002897}
2898
Alexandre Rames67555f72014-11-18 10:55:16 +00002899void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2900 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002901 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00002902 locations->SetInAt(0, Location::RequiresRegister());
2903 if (check->HasUses()) {
2904 locations->SetOut(Location::SameAsFirstInput());
2905 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01002906 // Rely on the type initialization to save everything we need.
2907 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Alexandre Rames67555f72014-11-18 10:55:16 +00002908}
2909
2910void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2911 // We assume the class is not null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01002912 SlowPathCodeARM64* slow_path =
2913 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(check->GetLoadClass(), check);
Alexandre Rames67555f72014-11-18 10:55:16 +00002914 codegen_->AddSlowPath(slow_path);
2915 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2916}
2917
Roland Levillain1a653882016-03-18 18:05:57 +00002918static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2919 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2920 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2921}
2922
2923void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002924 VRegister lhs_reg = InputFPRegisterAt(instruction, 0);
Roland Levillain1a653882016-03-18 18:05:57 +00002925 Location rhs_loc = instruction->GetLocations()->InAt(1);
2926 if (rhs_loc.IsConstant()) {
2927 // 0.0 is the only immediate that can be encoded directly in
2928 // an FCMP instruction.
2929 //
2930 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2931 // specify that in a floating-point comparison, positive zero
2932 // and negative zero are considered equal, so we can use the
2933 // literal 0.0 for both cases here.
2934 //
2935 // Note however that some methods (Float.equal, Float.compare,
2936 // Float.compareTo, Double.equal, Double.compare,
2937 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2938 // StrictMath.min) consider 0.0 to be (strictly) greater than
2939 // -0.0. So if we ever translate calls to these methods into a
2940 // HCompare instruction, we must handle the -0.0 case with
2941 // care here.
2942 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2943 __ Fcmp(lhs_reg, 0.0);
2944 } else {
2945 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2946 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002947}
2948
Serban Constantinescu02164b32014-11-13 14:05:07 +00002949void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002950 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002951 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002952 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002953 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002954 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002955 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002956 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002957 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002958 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002959 case DataType::Type::kInt32:
2960 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002961 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002962 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002963 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2964 break;
2965 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002966 case DataType::Type::kFloat32:
2967 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002968 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002969 locations->SetInAt(1,
2970 IsFloatingPointZeroConstant(compare->InputAt(1))
2971 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2972 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002973 locations->SetOut(Location::RequiresRegister());
2974 break;
2975 }
2976 default:
2977 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2978 }
2979}
2980
2981void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002982 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002983
2984 // 0 if: left == right
2985 // 1 if: left > right
2986 // -1 if: left < right
2987 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002988 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002989 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002990 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002991 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002992 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002993 case DataType::Type::kInt32:
2994 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002995 Register result = OutputRegister(compare);
2996 Register left = InputRegisterAt(compare, 0);
2997 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002998 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002999 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3000 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003001 break;
3002 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003003 case DataType::Type::kFloat32:
3004 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003005 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003006 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003007 __ Cset(result, ne);
3008 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003009 break;
3010 }
3011 default:
3012 LOG(FATAL) << "Unimplemented compare type " << in_type;
3013 }
3014}
3015
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003016void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003017 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003018
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003019 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003020 locations->SetInAt(0, Location::RequiresFpuRegister());
3021 locations->SetInAt(1,
3022 IsFloatingPointZeroConstant(instruction->InputAt(1))
3023 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3024 : Location::RequiresFpuRegister());
3025 } else {
3026 // Integer cases.
3027 locations->SetInAt(0, Location::RequiresRegister());
3028 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3029 }
3030
David Brazdilb3e773e2016-01-26 11:28:37 +00003031 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003032 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003033 }
3034}
3035
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003036void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003037 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003038 return;
3039 }
3040
3041 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003042 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003043 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003044
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003045 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003046 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003047 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003048 } else {
3049 // Integer cases.
3050 Register lhs = InputRegisterAt(instruction, 0);
3051 Operand rhs = InputOperandAt(instruction, 1);
3052 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003053 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003054 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003055}
3056
3057#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3058 M(Equal) \
3059 M(NotEqual) \
3060 M(LessThan) \
3061 M(LessThanOrEqual) \
3062 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003063 M(GreaterThanOrEqual) \
3064 M(Below) \
3065 M(BelowOrEqual) \
3066 M(Above) \
3067 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003068#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003069void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3070void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003071FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003072#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003073#undef FOR_EACH_CONDITION_INSTRUCTION
3074
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003075void InstructionCodeGeneratorARM64::GenerateIntDivForPower2Denom(HDiv* instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003076 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003077 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003078 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
3079
3080 Register out = OutputRegister(instruction);
3081 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003082
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003083 Register final_dividend;
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003084 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003085 // No need to adjust the result for non-negative dividends or the INT32_MIN/INT64_MIN dividends.
3086 // NOTE: The generated code for HDiv correctly works for the INT32_MIN/INT64_MIN dividends:
3087 // imm == 2
3088 // add out, dividend(0x80000000), dividend(0x80000000), lsr #31 => out = 0x80000001
3089 // asr out, out(0x80000001), #1 => out = 0xc0000000
3090 // This is the same as 'asr out, 0x80000000, #1'
3091 //
3092 // imm > 2
3093 // add temp, dividend(0x80000000), imm - 1 => temp = 0b10..01..1, where the number
3094 // of the rightmost 1s is ctz_imm.
3095 // cmp dividend(0x80000000), 0 => N = 1, V = 0 (lt is true)
3096 // csel out, temp(0b10..01..1), dividend(0x80000000), lt => out = 0b10..01..1
3097 // asr out, out(0b10..01..1), #ctz_imm => out = 0b1..10..0, where the number of the
3098 // leftmost 1s is ctz_imm + 1.
3099 // This is the same as 'asr out, dividend(0x80000000), #ctz_imm'.
3100 //
3101 // imm == INT32_MIN
3102 // add tmp, dividend(0x80000000), #0x7fffffff => tmp = -1
3103 // cmp dividend(0x80000000), 0 => N = 1, V = 0 (lt is true)
3104 // csel out, temp(-1), dividend(0x80000000), lt => out = -1
3105 // neg out, out(-1), asr #31 => out = 1
3106 // This is the same as 'neg out, dividend(0x80000000), asr #31'.
3107 final_dividend = dividend;
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003108 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003109 if (abs_imm == 2) {
3110 int bits = DataType::Size(instruction->GetResultType()) * kBitsPerByte;
3111 __ Add(out, dividend, Operand(dividend, LSR, bits - 1));
3112 } else {
3113 UseScratchRegisterScope temps(GetVIXLAssembler());
3114 Register temp = temps.AcquireSameSizeAs(out);
3115 __ Add(temp, dividend, abs_imm - 1);
3116 __ Cmp(dividend, 0);
3117 __ Csel(out, temp, dividend, lt);
3118 }
3119 final_dividend = out;
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003120 }
3121
Zheng Xuc6667102015-05-15 16:08:45 +08003122 int ctz_imm = CTZ(abs_imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003123 if (imm > 0) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003124 __ Asr(out, final_dividend, ctz_imm);
Zheng Xuc6667102015-05-15 16:08:45 +08003125 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003126 __ Neg(out, Operand(final_dividend, ASR, ctz_imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003127 }
3128}
3129
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003130// Return true if the magic number was modified by subtracting 2^32(Int32 div) or 2^64(Int64 div).
3131// So dividend needs to be added.
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003132static inline bool NeedToAddDividend(int64_t magic_number, int64_t divisor) {
3133 return divisor > 0 && magic_number < 0;
3134}
3135
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003136// Return true if the magic number was modified by adding 2^32(Int32 div) or 2^64(Int64 div).
3137// So dividend needs to be subtracted.
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003138static inline bool NeedToSubDividend(int64_t magic_number, int64_t divisor) {
3139 return divisor < 0 && magic_number > 0;
3140}
3141
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003142// Generate code which increments the value in register 'in' by 1 if the value is negative.
3143// It is done with 'add out, in, in, lsr #31 or #63'.
3144// If the value is a result of an operation setting the N flag, CINC MI can be used
3145// instead of ADD. 'use_cond_inc' controls this.
3146void InstructionCodeGeneratorARM64::GenerateIncrementNegativeByOne(
3147 Register out,
3148 Register in,
3149 bool use_cond_inc) {
3150 if (use_cond_inc) {
3151 __ Cinc(out, in, mi);
3152 } else {
3153 __ Add(out, in, Operand(in, LSR, in.GetSizeInBits() - 1));
3154 }
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003155}
3156
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003157// Helper to generate code producing the result of HRem with a constant divisor.
3158void InstructionCodeGeneratorARM64::GenerateResultRemWithAnyConstant(
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003159 Register out,
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003160 Register dividend,
3161 Register quotient,
3162 int64_t divisor,
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003163 UseScratchRegisterScope* temps_scope) {
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003164 Register temp_imm = temps_scope->AcquireSameSizeAs(out);
3165 __ Mov(temp_imm, divisor);
3166 __ Msub(out, quotient, temp_imm, dividend);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003167}
3168
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003169// Helper to generate code for HDiv/HRem instructions when a dividend is non-negative and
3170// a divisor is a positive constant, not power of 2.
3171void InstructionCodeGeneratorARM64::GenerateInt64UnsignedDivRemWithAnyPositiveConstant(
3172 HBinaryOperation* instruction) {
3173 DCHECK(instruction->IsDiv() || instruction->IsRem());
3174 DCHECK(instruction->GetResultType() == DataType::Type::kInt64);
3175
3176 LocationSummary* locations = instruction->GetLocations();
3177 Location second = locations->InAt(1);
3178 DCHECK(second.IsConstant());
3179
3180 Register out = OutputRegister(instruction);
3181 Register dividend = InputRegisterAt(instruction, 0);
3182 int64_t imm = Int64FromConstant(second.GetConstant());
3183 DCHECK_GT(imm, 0);
3184
3185 int64_t magic;
3186 int shift;
3187 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ true, &magic, &shift);
3188
3189 UseScratchRegisterScope temps(GetVIXLAssembler());
3190 Register temp = temps.AcquireSameSizeAs(out);
3191
3192 auto generate_unsigned_div_code = [this, magic, shift](Register out,
3193 Register dividend,
3194 Register temp) {
3195 // temp = get_high(dividend * magic)
3196 __ Mov(temp, magic);
3197 if (magic > 0 && shift == 0) {
3198 __ Smulh(out, dividend, temp);
3199 } else {
3200 __ Smulh(temp, dividend, temp);
3201 if (magic < 0) {
3202 // The negative magic means that the multiplier m is greater than INT64_MAX.
3203 // In such a case shift is never 0. See the proof in
3204 // InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant.
3205 __ Add(temp, temp, dividend);
3206 }
3207 DCHECK_NE(shift, 0);
3208 __ Lsr(out, temp, shift);
3209 }
3210 };
3211
3212 if (instruction->IsDiv()) {
3213 generate_unsigned_div_code(out, dividend, temp);
3214 } else {
3215 generate_unsigned_div_code(temp, dividend, temp);
3216 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3217 }
3218}
3219
3220// Helper to generate code for HDiv/HRem instructions for any dividend and a constant divisor
3221// (not power of 2).
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003222void InstructionCodeGeneratorARM64::GenerateInt64DivRemWithAnyConstant(
3223 HBinaryOperation* instruction) {
Zheng Xuc6667102015-05-15 16:08:45 +08003224 DCHECK(instruction->IsDiv() || instruction->IsRem());
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003225 DCHECK(instruction->GetResultType() == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003226
3227 LocationSummary* locations = instruction->GetLocations();
3228 Location second = locations->InAt(1);
3229 DCHECK(second.IsConstant());
3230
3231 Register out = OutputRegister(instruction);
3232 Register dividend = InputRegisterAt(instruction, 0);
3233 int64_t imm = Int64FromConstant(second.GetConstant());
3234
Zheng Xuc6667102015-05-15 16:08:45 +08003235 int64_t magic;
3236 int shift;
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003237 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ true, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08003238
3239 UseScratchRegisterScope temps(GetVIXLAssembler());
3240 Register temp = temps.AcquireSameSizeAs(out);
3241
3242 // temp = get_high(dividend * magic)
3243 __ Mov(temp, magic);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003244 __ Smulh(temp, dividend, temp);
3245
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003246 // The multiplication result might need some corrections to be finalized.
3247 // The last correction is to increment by 1, if the result is negative.
3248 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3249 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3250 // However if one of the corrections is ADD or SUB, the sign can be detected
3251 // with ADDS/SUBS. They set the N flag if the result is negative.
3252 // This allows to use CINC MI which has latency 1.
3253 bool use_cond_inc = false;
3254
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003255 // Some combinations of magic_number and the divisor require to correct the result.
3256 // Check whether the correction is needed.
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003257 if (NeedToAddDividend(magic, imm)) {
3258 __ Adds(temp, temp, dividend);
3259 use_cond_inc = true;
3260 } else if (NeedToSubDividend(magic, imm)) {
3261 __ Subs(temp, temp, dividend);
3262 use_cond_inc = true;
3263 }
3264
3265 if (shift != 0) {
3266 __ Asr(temp, temp, shift);
3267 }
3268
3269 if (instruction->IsRem()) {
3270 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3271 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3272 } else {
3273 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3274 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003275}
3276
3277void InstructionCodeGeneratorARM64::GenerateInt32DivRemWithAnyConstant(
3278 HBinaryOperation* instruction) {
3279 DCHECK(instruction->IsDiv() || instruction->IsRem());
3280 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
3281
3282 LocationSummary* locations = instruction->GetLocations();
3283 Location second = locations->InAt(1);
3284 DCHECK(second.IsConstant());
3285
3286 Register out = OutputRegister(instruction);
3287 Register dividend = InputRegisterAt(instruction, 0);
3288 int64_t imm = Int64FromConstant(second.GetConstant());
3289
3290 int64_t magic;
3291 int shift;
3292 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
3293 UseScratchRegisterScope temps(GetVIXLAssembler());
3294 Register temp = temps.AcquireSameSizeAs(out);
3295
3296 // temp = get_high(dividend * magic)
3297 __ Mov(temp, magic);
3298 __ Smull(temp.X(), dividend, temp);
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003299
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003300 // The multiplication result might need some corrections to be finalized.
3301 // The last correction is to increment by 1, if the result is negative.
3302 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3303 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3304 // However if one of the corrections is ADD or SUB, the sign can be detected
3305 // with ADDS/SUBS. They set the N flag if the result is negative.
3306 // This allows to use CINC MI which has latency 1.
3307 bool use_cond_inc = false;
3308
3309 // ADD/SUB correction is performed in the high 32 bits
3310 // as high 32 bits are ignored because type are kInt32.
3311 if (NeedToAddDividend(magic, imm)) {
3312 __ Adds(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3313 use_cond_inc = true;
3314 } else if (NeedToSubDividend(magic, imm)) {
3315 __ Subs(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3316 use_cond_inc = true;
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003317 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003318
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003319 // Extract the result from the high 32 bits and apply the final right shift.
3320 DCHECK_LT(shift, 32);
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003321 if (imm > 0 && HasNonNegativeInputAt(instruction, 0)) {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01003322 // No need to adjust the result for a non-negative dividend and a positive divisor.
3323 if (instruction->IsDiv()) {
3324 __ Lsr(out.X(), temp.X(), 32 + shift);
3325 } else {
3326 __ Lsr(temp.X(), temp.X(), 32 + shift);
3327 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3328 }
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003329 } else {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01003330 __ Asr(temp.X(), temp.X(), 32 + shift);
3331
3332 if (instruction->IsRem()) {
3333 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3334 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3335 } else {
3336 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3337 }
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003338 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003339}
3340
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003341void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction,
3342 int64_t divisor) {
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003343 DCHECK(instruction->IsDiv() || instruction->IsRem());
3344 if (instruction->GetResultType() == DataType::Type::kInt64) {
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003345 if (divisor > 0 && HasNonNegativeInputAt(instruction, 0)) {
3346 GenerateInt64UnsignedDivRemWithAnyPositiveConstant(instruction);
3347 } else {
3348 GenerateInt64DivRemWithAnyConstant(instruction);
3349 }
Zheng Xuc6667102015-05-15 16:08:45 +08003350 } else {
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003351 GenerateInt32DivRemWithAnyConstant(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003352 }
3353}
3354
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003355void InstructionCodeGeneratorARM64::GenerateIntDivForConstDenom(HDiv *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003356 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Zheng Xuc6667102015-05-15 16:08:45 +08003357
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003358 if (imm == 0) {
3359 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3360 return;
3361 }
Zheng Xuc6667102015-05-15 16:08:45 +08003362
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003363 if (IsPowerOfTwo(AbsOrMin(imm))) {
3364 GenerateIntDivForPower2Denom(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003365 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003366 // Cases imm == -1 or imm == 1 are handled by InstructionSimplifier.
3367 DCHECK(imm < -2 || imm > 2) << imm;
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003368 GenerateDivRemWithAnyConstant(instruction, imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003369 }
3370}
3371
3372void InstructionCodeGeneratorARM64::GenerateIntDiv(HDiv *instruction) {
3373 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
3374 << instruction->GetResultType();
3375
3376 if (instruction->GetLocations()->InAt(1).IsConstant()) {
3377 GenerateIntDivForConstDenom(instruction);
3378 } else {
3379 Register out = OutputRegister(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003380 Register dividend = InputRegisterAt(instruction, 0);
3381 Register divisor = InputRegisterAt(instruction, 1);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003382 __ Sdiv(out, dividend, divisor);
Zheng Xuc6667102015-05-15 16:08:45 +08003383 }
3384}
3385
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003386void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3387 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003388 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003389 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003390 case DataType::Type::kInt32:
3391 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003392 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003393 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003394 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3395 break;
3396
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003397 case DataType::Type::kFloat32:
3398 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003399 locations->SetInAt(0, Location::RequiresFpuRegister());
3400 locations->SetInAt(1, Location::RequiresFpuRegister());
3401 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3402 break;
3403
3404 default:
3405 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3406 }
3407}
3408
3409void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003410 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003411 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003412 case DataType::Type::kInt32:
3413 case DataType::Type::kInt64:
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003414 GenerateIntDiv(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003415 break;
3416
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003417 case DataType::Type::kFloat32:
3418 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003419 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3420 break;
3421
3422 default:
3423 LOG(FATAL) << "Unexpected div type " << type;
3424 }
3425}
3426
Alexandre Rames67555f72014-11-18 10:55:16 +00003427void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003428 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003429 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003430}
3431
3432void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3433 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003434 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003435 codegen_->AddSlowPath(slow_path);
3436 Location value = instruction->GetLocations()->InAt(0);
3437
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003438 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003439
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003440 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003441 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Elliott Hughesc1896c92018-11-29 11:33:18 -08003442 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003443 }
3444
Alexandre Rames67555f72014-11-18 10:55:16 +00003445 if (value.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003446 int64_t divisor = Int64FromLocation(value);
Alexandre Rames67555f72014-11-18 10:55:16 +00003447 if (divisor == 0) {
3448 __ B(slow_path->GetEntryLabel());
3449 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003450 // A division by a non-null constant is valid. We don't need to perform
3451 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003452 }
3453 } else {
3454 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3455 }
3456}
3457
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003458void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3459 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003460 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003461 locations->SetOut(Location::ConstantLocation(constant));
3462}
3463
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003464void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3465 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003466 // Will be generated at use site.
3467}
3468
Alexandre Rames5319def2014-10-23 10:03:10 +01003469void LocationsBuilderARM64::VisitExit(HExit* exit) {
3470 exit->SetLocations(nullptr);
3471}
3472
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003473void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003474}
3475
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003476void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3477 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003478 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003479 locations->SetOut(Location::ConstantLocation(constant));
3480}
3481
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003482void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003483 // Will be generated at use site.
3484}
3485
David Brazdilfc6a86a2015-06-26 10:33:45 +00003486void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003487 if (successor->IsExitBlock()) {
3488 DCHECK(got->GetPrevious()->AlwaysThrows());
3489 return; // no code needed
3490 }
3491
Serban Constantinescu02164b32014-11-13 14:05:07 +00003492 HBasicBlock* block = got->GetBlock();
3493 HInstruction* previous = got->GetPrevious();
3494 HLoopInformation* info = block->GetLoopInformation();
3495
David Brazdil46e2a392015-03-16 17:31:52 +00003496 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00003497 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003498 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3499 return;
3500 }
3501 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3502 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08003503 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003504 }
3505 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003506 __ B(codegen_->GetLabelOf(successor));
3507 }
3508}
3509
David Brazdilfc6a86a2015-06-26 10:33:45 +00003510void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3511 got->SetLocations(nullptr);
3512}
3513
3514void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3515 HandleGoto(got, got->GetSuccessor());
3516}
3517
3518void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3519 try_boundary->SetLocations(nullptr);
3520}
3521
3522void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3523 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3524 if (!successor->IsExitBlock()) {
3525 HandleGoto(try_boundary, successor);
3526 }
3527}
3528
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003529void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003530 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003531 vixl::aarch64::Label* true_target,
3532 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003533 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003534
David Brazdil0debae72015-11-12 18:37:00 +00003535 if (true_target == nullptr && false_target == nullptr) {
3536 // Nothing to do. The code always falls through.
3537 return;
3538 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003539 // Constant condition, statically compared against "true" (integer value 1).
3540 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003541 if (true_target != nullptr) {
3542 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003543 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003544 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003545 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003546 if (false_target != nullptr) {
3547 __ B(false_target);
3548 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003549 }
David Brazdil0debae72015-11-12 18:37:00 +00003550 return;
3551 }
3552
3553 // The following code generates these patterns:
3554 // (1) true_target == nullptr && false_target != nullptr
3555 // - opposite condition true => branch to false_target
3556 // (2) true_target != nullptr && false_target == nullptr
3557 // - condition true => branch to true_target
3558 // (3) true_target != nullptr && false_target != nullptr
3559 // - condition true => branch to true_target
3560 // - branch to false_target
3561 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003562 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003563 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003564 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003565 if (true_target == nullptr) {
3566 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3567 } else {
3568 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3569 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003570 } else {
3571 // The condition instruction has not been materialized, use its inputs as
3572 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003573 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003574
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003575 DataType::Type type = condition->InputAt(0)->GetType();
3576 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003577 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003578 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003579 IfCondition opposite_condition = condition->GetOppositeCondition();
3580 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003581 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003582 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003583 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003584 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003585 // Integer cases.
3586 Register lhs = InputRegisterAt(condition, 0);
3587 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003588
3589 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003590 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003591 if (true_target == nullptr) {
3592 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3593 non_fallthrough_target = false_target;
3594 } else {
3595 arm64_cond = ARM64Condition(condition->GetCondition());
3596 non_fallthrough_target = true_target;
3597 }
3598
Aart Bik086d27e2016-01-20 17:02:00 -08003599 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003600 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003601 switch (arm64_cond) {
3602 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003603 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003604 break;
3605 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003606 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003607 break;
3608 case lt:
3609 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003610 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003611 break;
3612 case ge:
3613 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003614 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003615 break;
3616 default:
3617 // Without the `static_cast` the compiler throws an error for
3618 // `-Werror=sign-promo`.
3619 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3620 }
3621 } else {
3622 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003623 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003624 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003625 }
3626 }
David Brazdil0debae72015-11-12 18:37:00 +00003627
3628 // If neither branch falls through (case 3), the conditional branch to `true_target`
3629 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3630 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003631 __ B(false_target);
3632 }
3633}
3634
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003635void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003636 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003637 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003638 locations->SetInAt(0, Location::RequiresRegister());
3639 }
3640}
3641
3642void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003643 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3644 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003645 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3646 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3647 true_target = nullptr;
3648 }
3649 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3650 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3651 false_target = nullptr;
3652 }
Andreas Gampe3db70682018-12-26 15:12:03 -08003653 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003654}
3655
3656void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003657 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003658 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003659 InvokeRuntimeCallingConvention calling_convention;
3660 RegisterSet caller_saves = RegisterSet::Empty();
3661 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3662 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003663 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003664 locations->SetInAt(0, Location::RequiresRegister());
3665 }
3666}
3667
3668void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003669 SlowPathCodeARM64* slow_path =
3670 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003671 GenerateTestAndBranch(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08003672 /* condition_input_index= */ 0,
David Brazdil0debae72015-11-12 18:37:00 +00003673 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08003674 /* false_target= */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003675}
3676
Mingyao Yang063fc772016-08-02 11:02:54 -07003677void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003678 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003679 LocationSummary(flag, LocationSummary::kNoCall);
3680 locations->SetOut(Location::RequiresRegister());
3681}
3682
3683void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3684 __ Ldr(OutputRegister(flag),
3685 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3686}
3687
David Brazdilc0b601b2016-02-08 14:20:45 +00003688static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3689 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003690 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003691}
3692
Alexandre Rames880f1192016-06-13 16:04:50 +01003693static inline Condition GetConditionForSelect(HCondition* condition) {
3694 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003695 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3696 : ARM64Condition(cond);
3697}
3698
David Brazdil74eb1b22015-12-14 11:44:01 +00003699void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003700 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003701 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003702 locations->SetInAt(0, Location::RequiresFpuRegister());
3703 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003704 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003705 } else {
3706 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3707 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3708 bool is_true_value_constant = cst_true_value != nullptr;
3709 bool is_false_value_constant = cst_false_value != nullptr;
3710 // Ask VIXL whether we should synthesize constants in registers.
3711 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3712 Operand true_op = is_true_value_constant ?
3713 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3714 Operand false_op = is_false_value_constant ?
3715 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3716 bool true_value_in_register = false;
3717 bool false_value_in_register = false;
3718 MacroAssembler::GetCselSynthesisInformation(
3719 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3720 true_value_in_register |= !is_true_value_constant;
3721 false_value_in_register |= !is_false_value_constant;
3722
3723 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3724 : Location::ConstantLocation(cst_true_value));
3725 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3726 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003727 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003728 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003729
David Brazdil74eb1b22015-12-14 11:44:01 +00003730 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3731 locations->SetInAt(2, Location::RequiresRegister());
3732 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003733}
3734
3735void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003736 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003737 Condition csel_cond;
3738
3739 if (IsBooleanValueOrMaterializedCondition(cond)) {
3740 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003741 // Use the condition flags set by the previous instruction.
3742 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003743 } else {
3744 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003745 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003746 }
3747 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003748 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003749 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003750 } else {
3751 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003752 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003753 }
3754
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003755 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003756 __ Fcsel(OutputFPRegister(select),
3757 InputFPRegisterAt(select, 1),
3758 InputFPRegisterAt(select, 0),
3759 csel_cond);
3760 } else {
3761 __ Csel(OutputRegister(select),
3762 InputOperandAt(select, 1),
3763 InputOperandAt(select, 0),
3764 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003765 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003766}
3767
David Srbecky0cf44932015-12-09 14:09:59 +00003768void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003769 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00003770}
3771
David Srbeckyd28f4a02016-03-14 17:14:24 +00003772void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3773 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003774}
3775
Vladimir Markodec78172020-06-19 15:31:23 +01003776void CodeGeneratorARM64::IncreaseFrame(size_t adjustment) {
3777 __ Claim(adjustment);
3778 GetAssembler()->cfi().AdjustCFAOffset(adjustment);
3779}
3780
3781void CodeGeneratorARM64::DecreaseFrame(size_t adjustment) {
3782 __ Drop(adjustment);
3783 GetAssembler()->cfi().AdjustCFAOffset(-adjustment);
3784}
3785
David Srbeckyc7098ff2016-02-09 14:30:11 +00003786void CodeGeneratorARM64::GenerateNop() {
3787 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003788}
3789
Alex Light3a73ffb2021-01-25 14:11:05 +00003790void LocationsBuilderARM64::VisitPredicatedInstanceFieldGet(
3791 HPredicatedInstanceFieldGet* instruction) {
3792 HandleFieldGet(instruction, instruction->GetFieldInfo());
3793}
3794
Alexandre Rames5319def2014-10-23 10:03:10 +01003795void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003796 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003797}
3798
Alex Light3a73ffb2021-01-25 14:11:05 +00003799void InstructionCodeGeneratorARM64::VisitPredicatedInstanceFieldGet(
3800 HPredicatedInstanceFieldGet* instruction) {
3801 vixl::aarch64::Label finish;
3802 __ Cbz(InputRegisterAt(instruction, 1), &finish);
3803 HandleFieldGet(instruction, instruction->GetFieldInfo());
3804 __ Bind(&finish);
3805}
3806
Alexandre Rames5319def2014-10-23 10:03:10 +01003807void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003808 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003809}
3810
3811void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003812 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003813}
3814
3815void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003816 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003817}
3818
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003819// Temp is used for read barrier.
3820static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3821 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003822 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003823 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3824 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3825 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3826 return 1;
3827 }
3828 return 0;
3829}
3830
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003831// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003832// interface pointer, one for loading the current interface.
3833// The other checks have one temp for loading the object's class.
3834static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3835 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3836 return 3;
3837 }
3838 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003839}
3840
Alexandre Rames67555f72014-11-18 10:55:16 +00003841void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003842 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003843 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003844 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003845 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003846 case TypeCheckKind::kExactCheck:
3847 case TypeCheckKind::kAbstractClassCheck:
3848 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00003849 case TypeCheckKind::kArrayObjectCheck: {
3850 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
3851 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
3852 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003853 break;
Vladimir Marko87584542017-12-12 17:47:52 +00003854 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003855 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003856 case TypeCheckKind::kUnresolvedCheck:
3857 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003858 call_kind = LocationSummary::kCallOnSlowPath;
3859 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00003860 case TypeCheckKind::kBitstringCheck:
3861 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003862 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003863
Vladimir Markoca6fff82017-10-03 14:49:14 +01003864 LocationSummary* locations =
3865 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003866 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003867 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003868 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003869 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003870 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3871 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3872 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3873 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3874 } else {
3875 locations->SetInAt(1, Location::RequiresRegister());
3876 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003877 // The "out" register is used as a temporary, so it overlaps with the inputs.
3878 // Note that TypeCheckSlowPathARM64 uses this register too.
3879 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003880 // Add temps if necessary for read barriers.
3881 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003882}
3883
3884void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003885 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003886 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003887 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003888 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003889 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3890 ? Register()
3891 : InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003892 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003893 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003894 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3895 DCHECK_LE(num_temps, 1u);
3896 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003897 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3898 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3899 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3900 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003901
Scott Wakeling97c72b72016-06-24 16:19:36 +01003902 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003903 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003904
3905 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003906 // Avoid null check if we know `obj` is not null.
3907 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003908 __ Cbz(obj, &zero);
3909 }
3910
Roland Levillain44015862016-01-22 11:47:17 +00003911 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003912 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003913 ReadBarrierOption read_barrier_option =
3914 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003915 // /* HeapReference<Class> */ out = obj->klass_
3916 GenerateReferenceLoadTwoRegisters(instruction,
3917 out_loc,
3918 obj_loc,
3919 class_offset,
3920 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003921 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003922 __ Cmp(out, cls);
3923 __ Cset(out, eq);
3924 if (zero.IsLinked()) {
3925 __ B(&done);
3926 }
3927 break;
3928 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003929
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003930 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003931 ReadBarrierOption read_barrier_option =
3932 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003933 // /* HeapReference<Class> */ out = obj->klass_
3934 GenerateReferenceLoadTwoRegisters(instruction,
3935 out_loc,
3936 obj_loc,
3937 class_offset,
3938 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003939 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003940 // If the class is abstract, we eagerly fetch the super class of the
3941 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003942 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003943 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003944 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003945 GenerateReferenceLoadOneRegister(instruction,
3946 out_loc,
3947 super_offset,
3948 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003949 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003950 // If `out` is null, we use it for the result, and jump to `done`.
3951 __ Cbz(out, &done);
3952 __ Cmp(out, cls);
3953 __ B(ne, &loop);
3954 __ Mov(out, 1);
3955 if (zero.IsLinked()) {
3956 __ B(&done);
3957 }
3958 break;
3959 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003960
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003961 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003962 ReadBarrierOption read_barrier_option =
3963 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003964 // /* HeapReference<Class> */ out = obj->klass_
3965 GenerateReferenceLoadTwoRegisters(instruction,
3966 out_loc,
3967 obj_loc,
3968 class_offset,
3969 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003970 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003971 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003972 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003973 __ Bind(&loop);
3974 __ Cmp(out, cls);
3975 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003976 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003977 GenerateReferenceLoadOneRegister(instruction,
3978 out_loc,
3979 super_offset,
3980 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003981 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003982 __ Cbnz(out, &loop);
3983 // If `out` is null, we use it for the result, and jump to `done`.
3984 __ B(&done);
3985 __ Bind(&success);
3986 __ Mov(out, 1);
3987 if (zero.IsLinked()) {
3988 __ B(&done);
3989 }
3990 break;
3991 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003992
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003993 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003994 ReadBarrierOption read_barrier_option =
3995 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003996 // /* HeapReference<Class> */ out = obj->klass_
3997 GenerateReferenceLoadTwoRegisters(instruction,
3998 out_loc,
3999 obj_loc,
4000 class_offset,
4001 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004002 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004003 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004004 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004005 __ Cmp(out, cls);
4006 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004007 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004008 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004009 GenerateReferenceLoadOneRegister(instruction,
4010 out_loc,
4011 component_offset,
4012 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004013 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004014 // If `out` is null, we use it for the result, and jump to `done`.
4015 __ Cbz(out, &done);
4016 __ Ldrh(out, HeapOperand(out, primitive_offset));
4017 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4018 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004019 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004020 __ Mov(out, 1);
4021 __ B(&done);
4022 break;
4023 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004024
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004025 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004026 // No read barrier since the slow path will retry upon failure.
4027 // /* HeapReference<Class> */ out = obj->klass_
4028 GenerateReferenceLoadTwoRegisters(instruction,
4029 out_loc,
4030 obj_loc,
4031 class_offset,
4032 maybe_temp_loc,
4033 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004034 __ Cmp(out, cls);
4035 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004036 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08004037 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004038 codegen_->AddSlowPath(slow_path);
4039 __ B(ne, slow_path->GetEntryLabel());
4040 __ Mov(out, 1);
4041 if (zero.IsLinked()) {
4042 __ B(&done);
4043 }
4044 break;
4045 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004046
Calin Juravle98893e12015-10-02 21:05:03 +01004047 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004048 case TypeCheckKind::kInterfaceCheck: {
4049 // Note that we indeed only call on slow path, but we always go
4050 // into the slow path for the unresolved and interface check
4051 // cases.
4052 //
4053 // We cannot directly call the InstanceofNonTrivial runtime
4054 // entry point without resorting to a type checking slow path
4055 // here (i.e. by calling InvokeRuntime directly), as it would
4056 // require to assign fixed registers for the inputs of this
4057 // HInstanceOf instruction (following the runtime calling
4058 // convention), which might be cluttered by the potential first
4059 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004060 //
4061 // TODO: Introduce a new runtime entry point taking the object
4062 // to test (instead of its class) as argument, and let it deal
4063 // with the read barrier issues. This will let us refactor this
4064 // case of the `switch` code as it was previously (with a direct
4065 // call to the runtime not using a type checking slow path).
4066 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004067 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004068 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08004069 instruction, /* is_fatal= */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004070 codegen_->AddSlowPath(slow_path);
4071 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004072 if (zero.IsLinked()) {
4073 __ B(&done);
4074 }
4075 break;
4076 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004077
4078 case TypeCheckKind::kBitstringCheck: {
4079 // /* HeapReference<Class> */ temp = obj->klass_
4080 GenerateReferenceLoadTwoRegisters(instruction,
4081 out_loc,
4082 obj_loc,
4083 class_offset,
4084 maybe_temp_loc,
4085 kWithoutReadBarrier);
4086
4087 GenerateBitstringTypeCheckCompare(instruction, out);
4088 __ Cset(out, eq);
4089 if (zero.IsLinked()) {
4090 __ B(&done);
4091 }
4092 break;
4093 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004094 }
4095
4096 if (zero.IsLinked()) {
4097 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004098 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004099 }
4100
4101 if (done.IsLinked()) {
4102 __ Bind(&done);
4103 }
4104
4105 if (slow_path != nullptr) {
4106 __ Bind(slow_path->GetExitLabel());
4107 }
4108}
4109
4110void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004111 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00004112 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004113 LocationSummary* locations =
4114 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004115 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00004116 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
4117 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
4118 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
4119 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
4120 } else {
4121 locations->SetInAt(1, Location::RequiresRegister());
4122 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004123 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4124 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004125}
4126
4127void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004128 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004129 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004130 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004131 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00004132 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
4133 ? Register()
4134 : InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004135 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4136 DCHECK_GE(num_temps, 1u);
4137 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004138 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004139 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4140 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004141 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004142 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4143 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4144 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4145 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4146 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4147 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4148 const uint32_t object_array_data_offset =
4149 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004150
Vladimir Marko87584542017-12-12 17:47:52 +00004151 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004152 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004153 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4154 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004155 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004156
Scott Wakeling97c72b72016-06-24 16:19:36 +01004157 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004158 // Avoid null check if we know obj is not null.
4159 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004160 __ Cbz(obj, &done);
4161 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004162
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004163 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004164 case TypeCheckKind::kExactCheck:
4165 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004166 // /* HeapReference<Class> */ temp = obj->klass_
4167 GenerateReferenceLoadTwoRegisters(instruction,
4168 temp_loc,
4169 obj_loc,
4170 class_offset,
4171 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004172 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004173
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004174 __ Cmp(temp, cls);
4175 // Jump to slow path for throwing the exception or doing a
4176 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004177 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004178 break;
4179 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004180
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004181 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004182 // /* HeapReference<Class> */ temp = obj->klass_
4183 GenerateReferenceLoadTwoRegisters(instruction,
4184 temp_loc,
4185 obj_loc,
4186 class_offset,
4187 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004188 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004189
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004190 // If the class is abstract, we eagerly fetch the super class of the
4191 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004192 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004193 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004194 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004195 GenerateReferenceLoadOneRegister(instruction,
4196 temp_loc,
4197 super_offset,
4198 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004199 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004200
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004201 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4202 // exception.
4203 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4204 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004205 __ Cmp(temp, cls);
4206 __ B(ne, &loop);
4207 break;
4208 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004209
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004210 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004211 // /* HeapReference<Class> */ temp = obj->klass_
4212 GenerateReferenceLoadTwoRegisters(instruction,
4213 temp_loc,
4214 obj_loc,
4215 class_offset,
4216 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004217 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004218
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004219 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004220 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004221 __ Bind(&loop);
4222 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004223 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004224
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004225 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004226 GenerateReferenceLoadOneRegister(instruction,
4227 temp_loc,
4228 super_offset,
4229 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004230 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004231
4232 // If the class reference currently in `temp` is not null, jump
4233 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004234 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004235 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004236 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004237 break;
4238 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004239
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004240 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004241 // /* HeapReference<Class> */ temp = obj->klass_
4242 GenerateReferenceLoadTwoRegisters(instruction,
4243 temp_loc,
4244 obj_loc,
4245 class_offset,
4246 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004247 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004248
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004249 // Do an exact check.
4250 __ Cmp(temp, cls);
4251 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004252
4253 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004254 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004255 GenerateReferenceLoadOneRegister(instruction,
4256 temp_loc,
4257 component_offset,
4258 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004259 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004260
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004261 // If the component type is null, jump to the slow path to throw the exception.
4262 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4263 // Otherwise, the object is indeed an array. Further check that this component type is not a
4264 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004265 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4266 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004267 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004268 break;
4269 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004270
Calin Juravle98893e12015-10-02 21:05:03 +01004271 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004272 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004273 //
4274 // We cannot directly call the CheckCast runtime entry point
4275 // without resorting to a type checking slow path here (i.e. by
4276 // calling InvokeRuntime directly), as it would require to
4277 // assign fixed registers for the inputs of this HInstanceOf
4278 // instruction (following the runtime calling convention), which
4279 // might be cluttered by the potential first read barrier
4280 // emission at the beginning of this method.
4281 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004282 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004283 case TypeCheckKind::kInterfaceCheck: {
4284 // /* HeapReference<Class> */ temp = obj->klass_
4285 GenerateReferenceLoadTwoRegisters(instruction,
4286 temp_loc,
4287 obj_loc,
4288 class_offset,
4289 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004290 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004291
4292 // /* HeapReference<Class> */ temp = temp->iftable_
4293 GenerateReferenceLoadTwoRegisters(instruction,
4294 temp_loc,
4295 temp_loc,
4296 iftable_offset,
4297 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004298 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004299 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004300 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004301 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004302 vixl::aarch64::Label start_loop;
4303 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004304 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004305 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4306 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004307 // Go to next interface.
4308 __ Add(temp, temp, 2 * kHeapReferenceSize);
4309 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004310 // Compare the classes and continue the loop if they do not match.
4311 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4312 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004313 break;
4314 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004315
4316 case TypeCheckKind::kBitstringCheck: {
4317 // /* HeapReference<Class> */ temp = obj->klass_
4318 GenerateReferenceLoadTwoRegisters(instruction,
4319 temp_loc,
4320 obj_loc,
4321 class_offset,
4322 maybe_temp2_loc,
4323 kWithoutReadBarrier);
4324
4325 GenerateBitstringTypeCheckCompare(instruction, temp);
4326 __ B(ne, type_check_slow_path->GetEntryLabel());
4327 break;
4328 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004329 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004330 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004331
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004332 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004333}
4334
Alexandre Rames5319def2014-10-23 10:03:10 +01004335void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004336 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004337 locations->SetOut(Location::ConstantLocation(constant));
4338}
4339
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004340void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004341 // Will be generated at use site.
4342}
4343
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004344void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004345 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004346 locations->SetOut(Location::ConstantLocation(constant));
4347}
4348
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004349void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004350 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004351}
4352
Calin Juravle175dc732015-08-25 15:42:32 +01004353void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4354 // The trampoline uses the same calling convention as dex calling conventions,
4355 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4356 // the method_idx.
4357 HandleInvoke(invoke);
4358}
4359
4360void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4361 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004362 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004363}
4364
Alexandre Rames5319def2014-10-23 10:03:10 +01004365void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004366 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004367 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004368}
4369
Alexandre Rames67555f72014-11-18 10:55:16 +00004370void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4371 HandleInvoke(invoke);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004372 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
4373 // We cannot request ip1 as it's blocked by the register allocator.
4374 invoke->GetLocations()->SetInAt(invoke->GetNumberOfArguments() - 1, Location::Any());
4375 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004376}
4377
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004378void CodeGeneratorARM64::MaybeGenerateInlineCacheCheck(HInstruction* instruction,
4379 Register klass) {
4380 DCHECK_EQ(klass.GetCode(), 0u);
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004381 // We know the destination of an intrinsic, so no need to record inline
4382 // caches.
4383 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00004384 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004385 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004386 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
Nicolas Geoffray095dc462020-08-17 16:40:28 +01004387 ScopedProfilingInfoUse spiu(
4388 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
4389 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00004390 if (info != nullptr) {
4391 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
4392 uint64_t address = reinterpret_cast64<uint64_t>(cache);
4393 vixl::aarch64::Label done;
4394 __ Mov(x8, address);
4395 __ Ldr(x9, MemOperand(x8, InlineCache::ClassesOffset().Int32Value()));
4396 // Fast path for a monomorphic cache.
4397 __ Cmp(klass, x9);
4398 __ B(eq, &done);
4399 InvokeRuntime(kQuickUpdateInlineCache, instruction, instruction->GetDexPc());
4400 __ Bind(&done);
4401 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004402 }
4403}
4404
Alexandre Rames67555f72014-11-18 10:55:16 +00004405void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4406 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004407 LocationSummary* locations = invoke->GetLocations();
4408 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004409 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004410 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004411 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004412
Artem Serov914d7a82017-02-07 14:33:49 +00004413 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004414 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004415 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004416 {
4417 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4418 // /* HeapReference<Class> */ temp = temp->klass_
4419 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4420 codegen_->MaybeRecordImplicitNullCheck(invoke);
4421 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004422 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004423 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004424 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004425 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004426 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004427 }
Artem Serov914d7a82017-02-07 14:33:49 +00004428
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004429 // Instead of simply (possibly) unpoisoning `temp` here, we should
4430 // emit a read barrier for the previous class reference load.
4431 // However this is not required in practice, as this is an
4432 // intermediate/temporary reference and because the current
4433 // concurrent copying collector keeps the from-space memory
4434 // intact/accessible until the end of the marking phase (the
4435 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004436 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004437
4438 // If we're compiling baseline, update the inline cache.
4439 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
4440
4441 // The register ip1 is required to be used for the hidden argument in
4442 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
4443 MacroAssembler* masm = GetVIXLAssembler();
4444 UseScratchRegisterScope scratch_scope(masm);
4445 scratch_scope.Exclude(ip1);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004446 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
4447 Location interface_method = locations->InAt(invoke->GetNumberOfArguments() - 1);
4448 if (interface_method.IsStackSlot()) {
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00004449 __ Ldr(ip1, StackOperandFrom(interface_method));
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004450 } else {
4451 __ Mov(ip1, XRegisterFrom(interface_method));
4452 }
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00004453 // If the load kind is through a runtime call, we will pass the method we
4454 // fetch the IMT, which will either be a no-op if we don't hit the conflict
4455 // stub, or will make us always go through the trampoline when there is a
4456 // conflict.
4457 } else if (invoke->GetHiddenArgumentLoadKind() != MethodLoadKind::kRuntimeCall) {
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004458 codegen_->LoadMethod(
4459 invoke->GetHiddenArgumentLoadKind(), Location::RegisterLocation(ip1.GetCode()), invoke);
4460 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004461
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004462 __ Ldr(temp,
4463 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4464 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004465 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004466 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004467 __ Ldr(temp, MemOperand(temp, method_offset));
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00004468 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRuntimeCall) {
4469 // We pass the method from the IMT in case of a conflict. This will ensure
4470 // we go into the runtime to resolve the actual method.
4471 __ Mov(ip1, temp);
4472 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004473 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004474 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004475
4476 {
4477 // Ensure the pc position is recorded immediately after the `blr` instruction.
4478 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4479
4480 // lr();
4481 __ blr(lr);
4482 DCHECK(!codegen_->IsLeafMethod());
4483 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4484 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004485
Andreas Gampe3db70682018-12-26 15:12:03 -08004486 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004487}
4488
4489void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004490 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004491 if (intrinsic.TryDispatch(invoke)) {
4492 return;
4493 }
4494
Alexandre Rames67555f72014-11-18 10:55:16 +00004495 HandleInvoke(invoke);
4496}
4497
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004498void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004499 // Explicit clinit checks triggered by static invokes must have been pruned by
4500 // art::PrepareForRegisterAllocation.
4501 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004502
Vladimir Markoca6fff82017-10-03 14:49:14 +01004503 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004504 if (intrinsic.TryDispatch(invoke)) {
4505 return;
4506 }
4507
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004508 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
Vladimir Marko86c87522020-05-11 16:55:55 +01004509 CriticalNativeCallingConventionVisitorARM64 calling_convention_visitor(
4510 /*for_register_allocation=*/ true);
4511 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4512 } else {
4513 HandleInvoke(invoke);
4514 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004515}
4516
Andreas Gampe878d58c2015-01-15 23:24:00 -08004517static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4518 if (invoke->GetLocations()->Intrinsified()) {
4519 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4520 intrinsic.Dispatch(invoke);
4521 return true;
4522 }
4523 return false;
4524}
4525
Vladimir Markodc151b22015-10-15 18:02:30 +01004526HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4527 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004528 ArtMethod* method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004529 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004530 return desired_dispatch_info;
4531}
4532
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004533void CodeGeneratorARM64::LoadMethod(MethodLoadKind load_kind, Location temp, HInvoke* invoke) {
4534 switch (load_kind) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004535 case MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01004536 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01004537 // Add ADRP with its PC-relative method patch.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004538 vixl::aarch64::Label* adrp_label =
4539 NewBootImageMethodPatch(invoke->GetResolvedMethodReference());
Vladimir Marko65979462017-05-19 17:25:12 +01004540 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4541 // Add ADD with its PC-relative method patch.
4542 vixl::aarch64::Label* add_label =
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004543 NewBootImageMethodPatch(invoke->GetResolvedMethodReference(), adrp_label);
Vladimir Marko65979462017-05-19 17:25:12 +01004544 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4545 break;
4546 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004547 case MethodLoadKind::kBootImageRelRo: {
Vladimir Markob066d432018-01-03 13:14:37 +00004548 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004549 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00004550 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_offset);
4551 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4552 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
4553 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_offset, adrp_label);
4554 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
4555 EmitLdrOffsetPlaceholder(ldr_label, WRegisterFrom(temp), XRegisterFrom(temp));
4556 break;
4557 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004558 case MethodLoadKind::kBssEntry: {
Vladimir Markob066d432018-01-03 13:14:37 +00004559 // Add ADRP with its PC-relative .bss entry patch.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004560 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(invoke->GetMethodReference());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004561 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Markob066d432018-01-03 13:14:37 +00004562 // Add LDR with its PC-relative .bss entry patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004563 vixl::aarch64::Label* ldr_label =
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004564 NewMethodBssEntryPatch(invoke->GetMethodReference(), adrp_label);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01004565 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004566 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004567 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004568 }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004569 case MethodLoadKind::kJitDirectAddress: {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004570 // Load method address from literal pool.
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004571 __ Ldr(XRegisterFrom(temp),
4572 DeduplicateUint64Literal(reinterpret_cast<uint64_t>(invoke->GetResolvedMethod())));
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004573 break;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004574 }
4575 case MethodLoadKind::kRuntimeCall: {
4576 // Test situation, don't do anything.
4577 break;
4578 }
4579 default: {
4580 LOG(FATAL) << "Load kind should have already been handled " << load_kind;
4581 UNREACHABLE();
4582 }
4583 }
4584}
4585
4586void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4587 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
4588 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
4589 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4590 switch (invoke->GetMethodLoadKind()) {
4591 case MethodLoadKind::kStringInit: {
4592 uint32_t offset =
4593 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4594 // temp = thread->string_init_entrypoint
4595 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
4596 break;
4597 }
4598 case MethodLoadKind::kRecursive: {
4599 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
4600 break;
4601 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004602 case MethodLoadKind::kRuntimeCall: {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004603 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4604 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004605 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004606 case MethodLoadKind::kBootImageLinkTimePcRelative:
4607 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
4608 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
4609 // Do not materialize the method pointer, load directly the entrypoint.
4610 // Add ADRP with its PC-relative JNI entrypoint patch.
4611 vixl::aarch64::Label* adrp_label =
4612 NewBootImageJniEntrypointPatch(invoke->GetResolvedMethodReference());
4613 EmitAdrpPlaceholder(adrp_label, lr);
4614 // Add the LDR with its PC-relative method patch.
4615 vixl::aarch64::Label* add_label =
4616 NewBootImageJniEntrypointPatch(invoke->GetResolvedMethodReference(), adrp_label);
4617 EmitLdrOffsetPlaceholder(add_label, lr, lr);
4618 break;
4619 }
4620 FALLTHROUGH_INTENDED;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004621 default: {
4622 LoadMethod(invoke->GetMethodLoadKind(), temp, invoke);
4623 break;
4624 }
Vladimir Marko58155012015-08-19 12:49:41 +00004625 }
4626
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004627 auto call_lr = [&]() {
4628 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4629 ExactAssemblyScope eas(GetVIXLAssembler(),
4630 kInstructionSize,
4631 CodeBufferCheckScope::kExactSize);
4632 // lr()
4633 __ blr(lr);
4634 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko86c87522020-05-11 16:55:55 +01004635 };
Vladimir Marko58155012015-08-19 12:49:41 +00004636 switch (invoke->GetCodePtrLocation()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004637 case CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004638 {
4639 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4640 ExactAssemblyScope eas(GetVIXLAssembler(),
4641 kInstructionSize,
4642 CodeBufferCheckScope::kExactSize);
4643 __ bl(&frame_entry_label_);
4644 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4645 }
Vladimir Marko58155012015-08-19 12:49:41 +00004646 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004647 case CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01004648 size_t out_frame_size =
4649 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorARM64,
4650 kAapcs64StackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01004651 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004652 if (invoke->GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative) {
4653 call_lr();
4654 } else {
4655 // LR = callee_method->ptr_sized_fields_.data_; // EntryPointFromJni
4656 MemberOffset offset = ArtMethod::EntryPointFromJniOffset(kArm64PointerSize);
4657 __ Ldr(lr, MemOperand(XRegisterFrom(callee_method), offset.Int32Value()));
4658 // lr()
4659 call_lr();
4660 }
Vladimir Marko86c87522020-05-11 16:55:55 +01004661 // Zero-/sign-extend the result when needed due to native and managed ABI mismatch.
4662 switch (invoke->GetType()) {
4663 case DataType::Type::kBool:
4664 __ Ubfx(w0, w0, 0, 8);
4665 break;
4666 case DataType::Type::kInt8:
4667 __ Sbfx(w0, w0, 0, 8);
4668 break;
4669 case DataType::Type::kUint16:
4670 __ Ubfx(w0, w0, 0, 16);
4671 break;
4672 case DataType::Type::kInt16:
4673 __ Sbfx(w0, w0, 0, 16);
4674 break;
4675 case DataType::Type::kInt32:
4676 case DataType::Type::kInt64:
4677 case DataType::Type::kFloat32:
4678 case DataType::Type::kFloat64:
4679 case DataType::Type::kVoid:
4680 break;
4681 default:
4682 DCHECK(false) << invoke->GetType();
4683 break;
4684 }
4685 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01004686 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01004687 }
4688 break;
4689 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004690 case CodePtrLocation::kCallArtMethod: {
4691 // LR = callee_method->ptr_sized_fields_.entry_point_from_quick_compiled_code_;
4692 MemberOffset offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
4693 __ Ldr(lr, MemOperand(XRegisterFrom(callee_method), offset.Int32Value()));
4694 // lr()
4695 call_lr();
Vladimir Marko58155012015-08-19 12:49:41 +00004696 break;
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004697 }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004698 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004699
Andreas Gampe878d58c2015-01-15 23:24:00 -08004700 DCHECK(!IsLeafMethod());
4701}
4702
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004703void CodeGeneratorARM64::GenerateVirtualCall(
4704 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004705 // Use the calling convention instead of the location of the receiver, as
4706 // intrinsics may have put the receiver in a different register. In the intrinsics
4707 // slow path, the arguments have been moved to the right place, so here we are
4708 // guaranteed that the receiver is the first register of the calling convention.
4709 InvokeDexCallingConvention calling_convention;
4710 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004711 Register temp = XRegisterFrom(temp_in);
4712 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4713 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4714 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004715 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004716
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004717 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004718
4719 {
4720 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4721 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4722 // /* HeapReference<Class> */ temp = receiver->klass_
4723 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4724 MaybeRecordImplicitNullCheck(invoke);
4725 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004726 // Instead of simply (possibly) unpoisoning `temp` here, we should
4727 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004728 // intermediate/temporary reference and because the current
4729 // concurrent copying collector keeps the from-space memory
4730 // intact/accessible until the end of the marking phase (the
4731 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004732 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004733
4734 // If we're compiling baseline, update the inline cache.
4735 MaybeGenerateInlineCacheCheck(invoke, temp);
4736
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004737 // temp = temp->GetMethodAt(method_offset);
4738 __ Ldr(temp, MemOperand(temp, method_offset));
4739 // lr = temp->GetEntryPoint();
4740 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004741 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004742 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004743 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4744 // lr();
4745 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004746 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004747 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004748}
4749
Vladimir Marko9922f002020-06-08 15:05:15 +01004750void CodeGeneratorARM64::MoveFromReturnRegister(Location trg, DataType::Type type) {
4751 if (!trg.IsValid()) {
4752 DCHECK(type == DataType::Type::kVoid);
4753 return;
4754 }
4755
4756 DCHECK_NE(type, DataType::Type::kVoid);
4757
4758 if (DataType::IsIntegralType(type) || type == DataType::Type::kReference) {
4759 Register trg_reg = RegisterFrom(trg, type);
4760 Register res_reg = RegisterFrom(ARM64ReturnLocation(type), type);
4761 __ Mov(trg_reg, res_reg, kDiscardForSameWReg);
4762 } else {
4763 VRegister trg_reg = FPRegisterFrom(trg, type);
4764 VRegister res_reg = FPRegisterFrom(ARM64ReturnLocation(type), type);
4765 __ Fmov(trg_reg, res_reg);
4766 }
4767}
4768
Orion Hodsonac141392017-01-13 11:53:47 +00004769void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00004770 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
4771 if (intrinsic.TryDispatch(invoke)) {
4772 return;
4773 }
Orion Hodsonac141392017-01-13 11:53:47 +00004774 HandleInvoke(invoke);
4775}
4776
4777void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00004778 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4779 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
4780 return;
4781 }
Orion Hodsonac141392017-01-13 11:53:47 +00004782 codegen_->GenerateInvokePolymorphicCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004783 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004784}
4785
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004786void LocationsBuilderARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4787 HandleInvoke(invoke);
4788}
4789
4790void InstructionCodeGeneratorARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4791 codegen_->GenerateInvokeCustomCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004792 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004793}
4794
Vladimir Marko6fd16062018-06-26 11:02:04 +01004795vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageIntrinsicPatch(
4796 uint32_t intrinsic_data,
4797 vixl::aarch64::Label* adrp_label) {
4798 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004799 /* dex_file= */ nullptr, intrinsic_data, adrp_label, &boot_image_other_patches_);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004800}
4801
Vladimir Markob066d432018-01-03 13:14:37 +00004802vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageRelRoPatch(
4803 uint32_t boot_image_offset,
4804 vixl::aarch64::Label* adrp_label) {
4805 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004806 /* dex_file= */ nullptr, boot_image_offset, adrp_label, &boot_image_other_patches_);
Vladimir Markob066d432018-01-03 13:14:37 +00004807}
4808
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004809vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004810 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004811 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004812 return NewPcRelativePatch(
4813 target_method.dex_file, target_method.index, adrp_label, &boot_image_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004814}
4815
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004816vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4817 MethodReference target_method,
4818 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004819 return NewPcRelativePatch(
4820 target_method.dex_file, target_method.index, adrp_label, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004821}
4822
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004823vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageTypePatch(
Scott Wakeling97c72b72016-06-24 16:19:36 +01004824 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004825 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004826 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004827 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &boot_image_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004828}
4829
Vladimir Marko1998cd02017-01-13 13:02:58 +00004830vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
Vladimir Marko8f63f102020-09-28 12:10:28 +01004831 HLoadClass* load_class,
Vladimir Marko1998cd02017-01-13 13:02:58 +00004832 vixl::aarch64::Label* adrp_label) {
Vladimir Marko8f63f102020-09-28 12:10:28 +01004833 const DexFile& dex_file = load_class->GetDexFile();
4834 dex::TypeIndex type_index = load_class->GetTypeIndex();
4835 ArenaDeque<PcRelativePatchInfo>* patches = nullptr;
4836 switch (load_class->GetLoadKind()) {
4837 case HLoadClass::LoadKind::kBssEntry:
4838 patches = &type_bss_entry_patches_;
4839 break;
4840 case HLoadClass::LoadKind::kBssEntryPublic:
4841 patches = &public_type_bss_entry_patches_;
4842 break;
4843 case HLoadClass::LoadKind::kBssEntryPackage:
4844 patches = &package_type_bss_entry_patches_;
4845 break;
4846 default:
4847 LOG(FATAL) << "Unexpected load kind: " << load_class->GetLoadKind();
4848 UNREACHABLE();
4849 }
4850 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004851}
4852
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004853vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004854 const DexFile& dex_file,
4855 dex::StringIndex string_index,
4856 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004857 return NewPcRelativePatch(
4858 &dex_file, string_index.index_, adrp_label, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01004859}
4860
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004861vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4862 const DexFile& dex_file,
4863 dex::StringIndex string_index,
4864 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004865 return NewPcRelativePatch(&dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004866}
4867
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004868vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageJniEntrypointPatch(
4869 MethodReference target_method,
4870 vixl::aarch64::Label* adrp_label) {
4871 return NewPcRelativePatch(
4872 target_method.dex_file, target_method.index, adrp_label, &boot_image_jni_entrypoint_patches_);
4873}
4874
Vladimir Markof6675082019-05-17 12:05:28 +01004875void CodeGeneratorARM64::EmitEntrypointThunkCall(ThreadOffset64 entrypoint_offset) {
4876 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01004877 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markof6675082019-05-17 12:05:28 +01004878 call_entrypoint_patches_.emplace_back(/*dex_file*/ nullptr, entrypoint_offset.Uint32Value());
4879 vixl::aarch64::Label* bl_label = &call_entrypoint_patches_.back().label;
4880 __ bind(bl_label);
4881 __ bl(static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4882}
4883
Vladimir Marko966b46f2018-08-03 10:20:19 +00004884void CodeGeneratorARM64::EmitBakerReadBarrierCbnz(uint32_t custom_data) {
Vladimir Marko94796f82018-08-08 15:15:33 +01004885 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01004886 if (GetCompilerOptions().IsJitCompiler()) {
Vladimir Marko966b46f2018-08-03 10:20:19 +00004887 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
4888 vixl::aarch64::Label* slow_path_entry = &it->second.label;
4889 __ cbnz(mr, slow_path_entry);
4890 } else {
4891 baker_read_barrier_patches_.emplace_back(custom_data);
4892 vixl::aarch64::Label* cbnz_label = &baker_read_barrier_patches_.back().label;
4893 __ bind(cbnz_label);
4894 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4895 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004896}
4897
Scott Wakeling97c72b72016-06-24 16:19:36 +01004898vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004899 const DexFile* dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004900 uint32_t offset_or_index,
4901 vixl::aarch64::Label* adrp_label,
4902 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004903 // Add a patch entry and return the label.
4904 patches->emplace_back(dex_file, offset_or_index);
4905 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004906 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004907 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4908 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4909 return label;
4910}
4911
Scott Wakeling97c72b72016-06-24 16:19:36 +01004912vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4913 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004914 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004915}
4916
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004917vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004918 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004919 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004920 return jit_string_patches_.GetOrCreate(
4921 StringReference(&dex_file, string_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004922 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004923}
4924
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004925vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004926 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004927 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004928 return jit_class_patches_.GetOrCreate(
4929 TypeReference(&dex_file, type_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004930 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004931}
4932
Vladimir Markoaad75c62016-10-03 08:46:48 +00004933void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4934 vixl::aarch64::Register reg) {
4935 DCHECK(reg.IsX());
4936 SingleEmissionCheckScope guard(GetVIXLAssembler());
4937 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004938 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004939}
4940
4941void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4942 vixl::aarch64::Register out,
4943 vixl::aarch64::Register base) {
4944 DCHECK(out.IsX());
4945 DCHECK(base.IsX());
4946 SingleEmissionCheckScope guard(GetVIXLAssembler());
4947 __ Bind(fixup_label);
4948 __ add(out, base, Operand(/* offset placeholder */ 0));
4949}
4950
4951void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4952 vixl::aarch64::Register out,
4953 vixl::aarch64::Register base) {
4954 DCHECK(base.IsX());
4955 SingleEmissionCheckScope guard(GetVIXLAssembler());
4956 __ Bind(fixup_label);
4957 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4958}
4959
Vladimir Markoeebb8212018-06-05 14:57:24 +01004960void CodeGeneratorARM64::LoadBootImageAddress(vixl::aarch64::Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01004961 uint32_t boot_image_reference) {
4962 if (GetCompilerOptions().IsBootImage()) {
4963 // Add ADRP with its PC-relative type patch.
4964 vixl::aarch64::Label* adrp_label = NewBootImageIntrinsicPatch(boot_image_reference);
4965 EmitAdrpPlaceholder(adrp_label, reg.X());
4966 // Add ADD with its PC-relative type patch.
4967 vixl::aarch64::Label* add_label = NewBootImageIntrinsicPatch(boot_image_reference, adrp_label);
4968 EmitAddPlaceholder(add_label, reg.X(), reg.X());
Vladimir Markoa2da9b92018-10-10 14:21:55 +01004969 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01004970 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004971 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004972 EmitAdrpPlaceholder(adrp_label, reg.X());
4973 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004974 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_reference, adrp_label);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004975 EmitLdrOffsetPlaceholder(ldr_label, reg.W(), reg.X());
4976 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01004977 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01004978 gc::Heap* heap = Runtime::Current()->GetHeap();
4979 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004980 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01004981 __ Ldr(reg.W(), DeduplicateBootImageAddressLiteral(reinterpret_cast<uintptr_t>(address)));
4982 }
4983}
4984
Vladimir Marko98873af2020-12-16 12:10:03 +00004985void CodeGeneratorARM64::LoadTypeForBootImageIntrinsic(vixl::aarch64::Register reg,
4986 TypeReference target_type) {
4987 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
4988 DCHECK(GetCompilerOptions().IsBootImage());
4989 // Add ADRP with its PC-relative type patch.
4990 vixl::aarch64::Label* adrp_label =
4991 NewBootImageTypePatch(*target_type.dex_file, target_type.TypeIndex());
4992 EmitAdrpPlaceholder(adrp_label, reg.X());
4993 // Add ADD with its PC-relative type patch.
4994 vixl::aarch64::Label* add_label =
4995 NewBootImageTypePatch(*target_type.dex_file, target_type.TypeIndex(), adrp_label);
4996 EmitAddPlaceholder(add_label, reg.X(), reg.X());
4997}
4998
Vladimir Markode91ca92020-10-27 13:41:40 +00004999void CodeGeneratorARM64::LoadIntrinsicDeclaringClass(vixl::aarch64::Register reg, HInvoke* invoke) {
5000 DCHECK_NE(invoke->GetIntrinsic(), Intrinsics::kNone);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005001 if (GetCompilerOptions().IsBootImage()) {
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005002 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01005003 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
Vladimir Marko98873af2020-12-16 12:10:03 +00005004 LoadTypeForBootImageIntrinsic(reg, TypeReference(target_method.dex_file, type_idx));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005005 } else {
Vladimir Markode91ca92020-10-27 13:41:40 +00005006 uint32_t boot_image_offset = GetBootImageOffsetOfIntrinsicDeclaringClass(invoke);
5007 LoadBootImageAddress(reg, boot_image_offset);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005008 }
Vladimir Marko6fd16062018-06-26 11:02:04 +01005009}
5010
Vladimir Marko98873af2020-12-16 12:10:03 +00005011void CodeGeneratorARM64::LoadClassRootForIntrinsic(vixl::aarch64::Register reg,
5012 ClassRoot class_root) {
5013 if (GetCompilerOptions().IsBootImage()) {
5014 ScopedObjectAccess soa(Thread::Current());
5015 ObjPtr<mirror::Class> klass = GetClassRoot(class_root);
5016 TypeReference target_type(&klass->GetDexFile(), klass->GetDexTypeIndex());
5017 LoadTypeForBootImageIntrinsic(reg, target_type);
5018 } else {
5019 uint32_t boot_image_offset = GetBootImageOffset(class_root);
5020 LoadBootImageAddress(reg, boot_image_offset);
5021 }
5022}
5023
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005024template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005025inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
5026 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005027 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005028 for (const PcRelativePatchInfo& info : infos) {
5029 linker_patches->push_back(Factory(info.label.GetLocation(),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005030 info.target_dex_file,
Vladimir Markoaad75c62016-10-03 08:46:48 +00005031 info.pc_insn_label->GetLocation(),
5032 info.offset_or_index));
5033 }
5034}
5035
Vladimir Marko6fd16062018-06-26 11:02:04 +01005036template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5037linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5038 const DexFile* target_dex_file,
5039 uint32_t pc_insn_offset,
5040 uint32_t boot_image_offset) {
5041 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5042 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005043}
5044
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005045void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005046 DCHECK(linker_patches->empty());
5047 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005048 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005049 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005050 boot_image_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005051 type_bss_entry_patches_.size() +
Vladimir Marko8f63f102020-09-28 12:10:28 +01005052 public_type_bss_entry_patches_.size() +
5053 package_type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005054 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005055 string_bss_entry_patches_.size() +
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005056 boot_image_jni_entrypoint_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005057 boot_image_other_patches_.size() +
Vladimir Markof6675082019-05-17 12:05:28 +01005058 call_entrypoint_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005059 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00005060 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005061 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005062 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005063 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005064 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005065 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005066 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005067 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01005068 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005069 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005070 DCHECK(boot_image_type_patches_.empty());
5071 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005072 }
5073 if (GetCompilerOptions().IsBootImage()) {
5074 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5075 boot_image_other_patches_, linker_patches);
5076 } else {
5077 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5078 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005079 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005080 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5081 method_bss_entry_patches_, linker_patches);
5082 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5083 type_bss_entry_patches_, linker_patches);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005084 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PublicTypeBssEntryPatch>(
5085 public_type_bss_entry_patches_, linker_patches);
5086 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PackageTypeBssEntryPatch>(
5087 package_type_bss_entry_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005088 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5089 string_bss_entry_patches_, linker_patches);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005090 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeJniEntrypointPatch>(
5091 boot_image_jni_entrypoint_patches_, linker_patches);
Vladimir Markof6675082019-05-17 12:05:28 +01005092 for (const PatchInfo<vixl::aarch64::Label>& info : call_entrypoint_patches_) {
5093 DCHECK(info.target_dex_file == nullptr);
5094 linker_patches->push_back(linker::LinkerPatch::CallEntrypointPatch(
5095 info.label.GetLocation(), info.offset_or_index));
5096 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005097 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005098 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
5099 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005100 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00005101 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005102}
5103
Vladimir Markoca1e0382018-04-11 09:58:41 +00005104bool CodeGeneratorARM64::NeedsThunkCode(const linker::LinkerPatch& patch) const {
Vladimir Markof6675082019-05-17 12:05:28 +01005105 return patch.GetType() == linker::LinkerPatch::Type::kCallEntrypoint ||
5106 patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
Vladimir Markoca1e0382018-04-11 09:58:41 +00005107 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
5108}
5109
5110void CodeGeneratorARM64::EmitThunkCode(const linker::LinkerPatch& patch,
5111 /*out*/ ArenaVector<uint8_t>* code,
5112 /*out*/ std::string* debug_name) {
5113 Arm64Assembler assembler(GetGraph()->GetAllocator());
5114 switch (patch.GetType()) {
5115 case linker::LinkerPatch::Type::kCallRelative: {
5116 // The thunk just uses the entry point in the ArtMethod. This works even for calls
5117 // to the generic JNI and interpreter trampolines.
5118 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
5119 kArm64PointerSize).Int32Value());
5120 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
David Srbecky889da942021-04-30 13:03:14 +01005121 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00005122 *debug_name = "MethodCallThunk";
5123 }
5124 break;
5125 }
Vladimir Markof6675082019-05-17 12:05:28 +01005126 case linker::LinkerPatch::Type::kCallEntrypoint: {
5127 Offset offset(patch.EntrypointOffset());
5128 assembler.JumpTo(ManagedRegister(arm64::TR), offset, ManagedRegister(arm64::IP0));
David Srbecky889da942021-04-30 13:03:14 +01005129 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markof6675082019-05-17 12:05:28 +01005130 *debug_name = "EntrypointCallThunk_" + std::to_string(offset.Uint32Value());
5131 }
5132 break;
5133 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00005134 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
5135 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
5136 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
5137 break;
5138 }
5139 default:
5140 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
5141 UNREACHABLE();
5142 }
5143
5144 // Ensure we emit the literal pool if any.
5145 assembler.FinalizeCode();
5146 code->resize(assembler.CodeSize());
5147 MemoryRegion code_region(code->data(), code->size());
5148 assembler.FinalizeInstructions(code_region);
5149}
5150
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005151vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
5152 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005153 value,
5154 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
5155}
5156
Scott Wakeling97c72b72016-06-24 16:19:36 +01005157vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005158 return uint64_literals_.GetOrCreate(
5159 value,
5160 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00005161}
5162
Andreas Gampe878d58c2015-01-15 23:24:00 -08005163void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005164 // Explicit clinit checks triggered by static invokes must have been pruned by
5165 // art::PrepareForRegisterAllocation.
5166 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01005167
Andreas Gampe878d58c2015-01-15 23:24:00 -08005168 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08005169 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08005170 return;
5171 }
5172
Vladimir Marko86c87522020-05-11 16:55:55 +01005173 LocationSummary* locations = invoke->GetLocations();
5174 codegen_->GenerateStaticOrDirectCall(
5175 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005176
Andreas Gampe3db70682018-12-26 15:12:03 -08005177 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005178}
5179
5180void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08005181 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08005182 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08005183 return;
5184 }
5185
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005186 {
5187 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
5188 // are no pools emitted.
5189 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
5190 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
5191 DCHECK(!codegen_->IsLeafMethod());
5192 }
5193
Andreas Gampe3db70682018-12-26 15:12:03 -08005194 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005195}
5196
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005197HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
5198 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005199 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005200 case HLoadClass::LoadKind::kInvalid:
5201 LOG(FATAL) << "UNREACHABLE";
5202 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005203 case HLoadClass::LoadKind::kReferrersClass:
5204 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005205 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005206 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005207 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8f63f102020-09-28 12:10:28 +01005208 case HLoadClass::LoadKind::kBssEntryPublic:
5209 case HLoadClass::LoadKind::kBssEntryPackage:
Vladimir Marko695348f2020-05-19 14:42:02 +01005210 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005211 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005212 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005213 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01005214 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005215 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005216 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005217 break;
5218 }
5219 return desired_class_load_kind;
5220}
5221
Alexandre Rames67555f72014-11-18 10:55:16 +00005222void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005223 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005224 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005225 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005226 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005227 cls,
5228 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005229 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00005230 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005231 return;
5232 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005233 DCHECK_EQ(cls->NeedsAccessCheck(),
5234 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
5235 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005236
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005237 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5238 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005239 ? LocationSummary::kCallOnSlowPath
5240 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005241 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005242 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005243 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005244 }
5245
Vladimir Marko41559982017-01-06 14:04:23 +00005246 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005247 locations->SetInAt(0, Location::RequiresRegister());
5248 }
5249 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005250 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
5251 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5252 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005253 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005254 } else {
5255 // For non-Baker read barrier we have a temp-clobbering call.
5256 }
5257 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005258}
5259
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005260// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5261// move.
5262void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005263 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005264 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005265 codegen_->GenerateLoadClassRuntimeCall(cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08005266 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01005267 return;
5268 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005269 DCHECK_EQ(cls->NeedsAccessCheck(),
5270 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
5271 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Calin Juravle580b6092015-10-06 17:35:58 +01005272
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005273 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01005274 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00005275
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005276 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5277 ? kWithoutReadBarrier
5278 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005279 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005280 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005281 case HLoadClass::LoadKind::kReferrersClass: {
5282 DCHECK(!cls->CanCallRuntime());
5283 DCHECK(!cls->MustGenerateClinitCheck());
5284 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5285 Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00005286 codegen_->GenerateGcRootFieldLoad(cls,
5287 out_loc,
5288 current_method,
5289 ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005290 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005291 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005292 break;
5293 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005294 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005295 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5296 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005297 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005298 // Add ADRP with its PC-relative type patch.
5299 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08005300 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005301 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005302 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005303 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005304 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005305 codegen_->NewBootImageTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005306 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005307 break;
5308 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005309 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005310 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markode91ca92020-10-27 13:41:40 +00005311 uint32_t boot_image_offset = CodeGenerator::GetBootImageOffset(cls);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005312 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5313 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005314 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005315 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005316 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005317 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005318 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005319 break;
5320 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005321 case HLoadClass::LoadKind::kBssEntry:
5322 case HLoadClass::LoadKind::kBssEntryPublic:
5323 case HLoadClass::LoadKind::kBssEntryPackage: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005324 // Add ADRP with its PC-relative Class .bss entry patch.
Vladimir Markof3c52b42017-11-17 17:32:12 +00005325 vixl::aarch64::Register temp = XRegisterFrom(out_loc);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005326 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(cls);
Vladimir Markof3c52b42017-11-17 17:32:12 +00005327 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005328 // Add LDR with its PC-relative Class .bss entry patch.
Vladimir Marko8f63f102020-09-28 12:10:28 +01005329 vixl::aarch64::Label* ldr_label = codegen_->NewBssEntryTypePatch(cls, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005330 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005331 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005332 codegen_->GenerateGcRootFieldLoad(cls,
5333 out_loc,
5334 temp,
5335 /* offset placeholder */ 0u,
5336 ldr_label,
5337 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005338 generate_null_check = true;
5339 break;
5340 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005341 case HLoadClass::LoadKind::kJitBootImageAddress: {
5342 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5343 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
5344 DCHECK_NE(address, 0u);
5345 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5346 break;
5347 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005348 case HLoadClass::LoadKind::kJitTableAddress: {
5349 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5350 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005351 cls->GetClass()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005352 codegen_->GenerateGcRootFieldLoad(cls,
5353 out_loc,
5354 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005355 /* offset= */ 0,
5356 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005357 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005358 break;
5359 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005360 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005361 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005362 LOG(FATAL) << "UNREACHABLE";
5363 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005364 }
5365
Vladimir Markoea4c1262017-02-06 19:59:33 +00005366 bool do_clinit = cls->MustGenerateClinitCheck();
5367 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005368 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01005369 SlowPathCodeARM64* slow_path =
5370 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005371 codegen_->AddSlowPath(slow_path);
5372 if (generate_null_check) {
5373 __ Cbz(out, slow_path->GetEntryLabel());
5374 }
5375 if (cls->MustGenerateClinitCheck()) {
5376 GenerateClassInitializationCheck(slow_path, out);
5377 } else {
5378 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005379 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005380 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005381 }
5382}
5383
Orion Hodsondbaa5c72018-05-10 08:22:46 +01005384void LocationsBuilderARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5385 InvokeRuntimeCallingConvention calling_convention;
5386 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5387 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
5388}
5389
5390void InstructionCodeGeneratorARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5391 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
5392}
5393
Orion Hodson18259d72018-04-12 11:18:23 +01005394void LocationsBuilderARM64::VisitLoadMethodType(HLoadMethodType* load) {
5395 InvokeRuntimeCallingConvention calling_convention;
5396 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5397 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
5398}
5399
5400void InstructionCodeGeneratorARM64::VisitLoadMethodType(HLoadMethodType* load) {
5401 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
5402}
5403
David Brazdilcb1c0552015-08-04 16:22:25 +01005404static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005405 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005406}
5407
Alexandre Rames67555f72014-11-18 10:55:16 +00005408void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5409 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005410 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00005411 locations->SetOut(Location::RequiresRegister());
5412}
5413
5414void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005415 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5416}
5417
5418void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005419 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005420}
5421
5422void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5423 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005424}
5425
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005426HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5427 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005428 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005429 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005430 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005431 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01005432 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005433 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005434 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005435 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01005436 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005437 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005438 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005439 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005440 }
5441 return desired_string_load_kind;
5442}
5443
Alexandre Rames67555f72014-11-18 10:55:16 +00005444void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005445 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005446 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005447 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005448 InvokeRuntimeCallingConvention calling_convention;
5449 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5450 } else {
5451 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005452 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5453 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005454 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005455 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005456 } else {
5457 // For non-Baker read barrier we have a temp-clobbering call.
5458 }
5459 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005460 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005461}
5462
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005463// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5464// move.
5465void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005466 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005467 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005468
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005469 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005470 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005471 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5472 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005473 // Add ADRP with its PC-relative String patch.
5474 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005475 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005476 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005477 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005478 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005479 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005480 codegen_->NewBootImageStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005481 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005482 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005483 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005484 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005485 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005486 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markode91ca92020-10-27 13:41:40 +00005487 uint32_t boot_image_offset = CodeGenerator::GetBootImageOffset(load);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005488 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005489 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005490 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005491 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005492 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005493 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
5494 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005495 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005496 case HLoadString::LoadKind::kBssEntry: {
5497 // Add ADRP with its PC-relative String .bss entry patch.
5498 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005499 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00005500 Register temp = XRegisterFrom(out_loc);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005501 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005502 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005503 // Add LDR with its PC-relative String .bss entry patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005504 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005505 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005506 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005507 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005508 codegen_->GenerateGcRootFieldLoad(load,
5509 out_loc,
5510 temp,
5511 /* offset placeholder */ 0u,
5512 ldr_label,
5513 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005514 SlowPathCodeARM64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005515 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005516 codegen_->AddSlowPath(slow_path);
5517 __ Cbz(out.X(), slow_path->GetEntryLabel());
5518 __ Bind(slow_path->GetExitLabel());
Andreas Gampe3db70682018-12-26 15:12:03 -08005519 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005520 return;
5521 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005522 case HLoadString::LoadKind::kJitBootImageAddress: {
5523 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
5524 DCHECK_NE(address, 0u);
5525 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5526 return;
5527 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005528 case HLoadString::LoadKind::kJitTableAddress: {
5529 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005530 load->GetStringIndex(),
5531 load->GetString()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005532 codegen_->GenerateGcRootFieldLoad(load,
5533 out_loc,
5534 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005535 /* offset= */ 0,
5536 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005537 kCompilerReadBarrierOption);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005538 return;
5539 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005540 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005541 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005542 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005543
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005544 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005545 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005546 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005547 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005548 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5549 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005550 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005551}
5552
Alexandre Rames5319def2014-10-23 10:03:10 +01005553void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005554 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01005555 locations->SetOut(Location::ConstantLocation(constant));
5556}
5557
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005558void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005559 // Will be generated at use site.
5560}
5561
Alexandre Rames67555f72014-11-18 10:55:16 +00005562void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005563 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5564 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005565 InvokeRuntimeCallingConvention calling_convention;
5566 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5567}
5568
5569void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005570 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005571 instruction,
5572 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005573 if (instruction->IsEnter()) {
5574 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5575 } else {
5576 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5577 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005578 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005579}
5580
Alexandre Rames42d641b2014-10-27 14:00:51 +00005581void LocationsBuilderARM64::VisitMul(HMul* mul) {
5582 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005583 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005584 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005585 case DataType::Type::kInt32:
5586 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005587 locations->SetInAt(0, Location::RequiresRegister());
5588 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005589 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005590 break;
5591
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005592 case DataType::Type::kFloat32:
5593 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005594 locations->SetInAt(0, Location::RequiresFpuRegister());
5595 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005596 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005597 break;
5598
5599 default:
5600 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5601 }
5602}
5603
5604void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5605 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005606 case DataType::Type::kInt32:
5607 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005608 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5609 break;
5610
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005611 case DataType::Type::kFloat32:
5612 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005613 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005614 break;
5615
5616 default:
5617 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5618 }
5619}
5620
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005621void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5622 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005623 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005624 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005625 case DataType::Type::kInt32:
5626 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005627 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005628 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005629 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005630
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005631 case DataType::Type::kFloat32:
5632 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005633 locations->SetInAt(0, Location::RequiresFpuRegister());
5634 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005635 break;
5636
5637 default:
5638 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5639 }
5640}
5641
5642void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5643 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005644 case DataType::Type::kInt32:
5645 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005646 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5647 break;
5648
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005649 case DataType::Type::kFloat32:
5650 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005651 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005652 break;
5653
5654 default:
5655 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5656 }
5657}
5658
5659void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005660 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5661 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005662 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005663 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005664 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5665 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005666}
5667
5668void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005669 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5670 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005671 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005672 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005673 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005674}
5675
Alexandre Rames5319def2014-10-23 10:03:10 +01005676void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005677 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5678 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005679 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07005680 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005681 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005682}
5683
5684void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005685 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5686 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005687 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005688}
5689
5690void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005691 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005692 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005693 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005694}
5695
5696void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005697 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005698 case DataType::Type::kInt32:
5699 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005700 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005701 break;
5702
5703 default:
5704 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5705 }
5706}
5707
David Brazdil66d126e2015-04-03 16:02:44 +01005708void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005709 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005710 locations->SetInAt(0, Location::RequiresRegister());
5711 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5712}
5713
5714void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005715 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005716}
5717
Alexandre Rames5319def2014-10-23 10:03:10 +01005718void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005719 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5720 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005721}
5722
Calin Juravle2ae48182016-03-16 14:05:09 +00005723void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5724 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005725 return;
5726 }
Artem Serov914d7a82017-02-07 14:33:49 +00005727 {
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01005728 // Ensure that between load and RecordPcInfo there are no pools emitted.
Artem Serov914d7a82017-02-07 14:33:49 +00005729 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5730 Location obj = instruction->GetLocations()->InAt(0);
5731 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5732 RecordPcInfo(instruction, instruction->GetDexPc());
5733 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005734}
5735
Calin Juravle2ae48182016-03-16 14:05:09 +00005736void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005737 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005738 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005739
5740 LocationSummary* locations = instruction->GetLocations();
5741 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005742
5743 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005744}
5745
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005746void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005747 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005748}
5749
Alexandre Rames67555f72014-11-18 10:55:16 +00005750void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5751 HandleBinaryOp(instruction);
5752}
5753
5754void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5755 HandleBinaryOp(instruction);
5756}
5757
Alexandre Rames3e69f162014-12-10 10:36:50 +00005758void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5759 LOG(FATAL) << "Unreachable";
5760}
5761
5762void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005763 if (instruction->GetNext()->IsSuspendCheck() &&
5764 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5765 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5766 // The back edge will generate the suspend check.
5767 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5768 }
5769
Alexandre Rames3e69f162014-12-10 10:36:50 +00005770 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5771}
5772
Alexandre Rames5319def2014-10-23 10:03:10 +01005773void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005774 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005775 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5776 if (location.IsStackSlot()) {
5777 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5778 } else if (location.IsDoubleStackSlot()) {
5779 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5780 }
5781 locations->SetOut(location);
5782}
5783
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005784void InstructionCodeGeneratorARM64::VisitParameterValue(
5785 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005786 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005787}
5788
5789void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5790 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005791 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005792 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005793}
5794
5795void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5796 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5797 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005798}
5799
5800void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005801 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005802 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005803 locations->SetInAt(i, Location::Any());
5804 }
5805 locations->SetOut(Location::Any());
5806}
5807
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005808void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005809 LOG(FATAL) << "Unreachable";
5810}
5811
Serban Constantinescu02164b32014-11-13 14:05:07 +00005812void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005813 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005814 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005815 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005816 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005817 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005818
5819 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005820 case DataType::Type::kInt32:
5821 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005822 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005823 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005824 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5825 break;
5826
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005827 case DataType::Type::kFloat32:
5828 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005829 InvokeRuntimeCallingConvention calling_convention;
5830 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5831 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5832 locations->SetOut(calling_convention.GetReturnLocation(type));
5833
5834 break;
5835 }
5836
Serban Constantinescu02164b32014-11-13 14:05:07 +00005837 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005838 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005839 }
5840}
5841
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005842void InstructionCodeGeneratorARM64::GenerateIntRemForPower2Denom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005843 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005844 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
5845 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
5846
5847 Register out = OutputRegister(instruction);
5848 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005849
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01005850 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01005851 // No need to adjust the result for non-negative dividends or the INT32_MIN/INT64_MIN dividends.
5852 // NOTE: The generated code for HRem correctly works for the INT32_MIN/INT64_MIN dividends.
5853 // INT*_MIN % imm must be 0 for any imm of power 2. 'and' works only with bits
5854 // 0..30 (Int32 case)/0..62 (Int64 case) of a dividend. For INT32_MIN/INT64_MIN they are zeros.
5855 // So 'and' always produces zero.
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005856 __ And(out, dividend, abs_imm - 1);
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01005857 } else {
5858 if (abs_imm == 2) {
5859 __ Cmp(dividend, 0);
5860 __ And(out, dividend, 1);
5861 __ Csneg(out, out, out, ge);
5862 } else {
5863 UseScratchRegisterScope temps(GetVIXLAssembler());
5864 Register temp = temps.AcquireSameSizeAs(out);
5865
5866 __ Negs(temp, dividend);
5867 __ And(out, dividend, abs_imm - 1);
5868 __ And(temp, temp, abs_imm - 1);
5869 __ Csneg(out, out, temp, mi);
5870 }
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005871 }
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005872}
5873
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005874void InstructionCodeGeneratorARM64::GenerateIntRemForConstDenom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005875 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005876
5877 if (imm == 0) {
5878 // Do not generate anything.
5879 // DivZeroCheck would prevent any code to be executed.
5880 return;
5881 }
5882
Evgeny Astigeevichf58dc652018-06-25 17:54:07 +01005883 if (IsPowerOfTwo(AbsOrMin(imm))) {
5884 // Cases imm == -1 or imm == 1 are handled in constant folding by
5885 // InstructionWithAbsorbingInputSimplifier.
5886 // If the cases have survided till code generation they are handled in
5887 // GenerateIntRemForPower2Denom becauses -1 and 1 are the power of 2 (2^0).
5888 // The correct code is generated for them, just more instructions.
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005889 GenerateIntRemForPower2Denom(instruction);
5890 } else {
5891 DCHECK(imm < -2 || imm > 2) << imm;
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01005892 GenerateDivRemWithAnyConstant(instruction, imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005893 }
5894}
5895
5896void InstructionCodeGeneratorARM64::GenerateIntRem(HRem* instruction) {
5897 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
5898 << instruction->GetResultType();
5899
5900 if (instruction->GetLocations()->InAt(1).IsConstant()) {
5901 GenerateIntRemForConstDenom(instruction);
5902 } else {
5903 Register out = OutputRegister(instruction);
5904 Register dividend = InputRegisterAt(instruction, 0);
5905 Register divisor = InputRegisterAt(instruction, 1);
5906 UseScratchRegisterScope temps(GetVIXLAssembler());
5907 Register temp = temps.AcquireSameSizeAs(out);
5908 __ Sdiv(temp, dividend, divisor);
5909 __ Msub(out, temp, divisor, dividend);
5910 }
5911}
5912
Serban Constantinescu02164b32014-11-13 14:05:07 +00005913void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005914 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005915
Serban Constantinescu02164b32014-11-13 14:05:07 +00005916 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005917 case DataType::Type::kInt32:
5918 case DataType::Type::kInt64: {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005919 GenerateIntRem(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005920 break;
5921 }
5922
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005923 case DataType::Type::kFloat32:
5924 case DataType::Type::kFloat64: {
5925 QuickEntrypointEnum entrypoint =
5926 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005927 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005928 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00005929 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5930 } else {
5931 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5932 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005933 break;
5934 }
5935
Serban Constantinescu02164b32014-11-13 14:05:07 +00005936 default:
5937 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005938 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005939 }
5940}
5941
Aart Bik1f8d51b2018-02-15 10:42:37 -08005942void LocationsBuilderARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005943 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005944}
5945
Aart Bik1f8d51b2018-02-15 10:42:37 -08005946void InstructionCodeGeneratorARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005947 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005948}
5949
5950void LocationsBuilderARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005951 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005952}
5953
5954void InstructionCodeGeneratorARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005955 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005956}
5957
Aart Bik3dad3412018-02-28 12:01:46 -08005958void LocationsBuilderARM64::VisitAbs(HAbs* abs) {
5959 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
5960 switch (abs->GetResultType()) {
5961 case DataType::Type::kInt32:
5962 case DataType::Type::kInt64:
5963 locations->SetInAt(0, Location::RequiresRegister());
5964 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5965 break;
5966 case DataType::Type::kFloat32:
5967 case DataType::Type::kFloat64:
5968 locations->SetInAt(0, Location::RequiresFpuRegister());
5969 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5970 break;
5971 default:
5972 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5973 }
5974}
5975
5976void InstructionCodeGeneratorARM64::VisitAbs(HAbs* abs) {
5977 switch (abs->GetResultType()) {
5978 case DataType::Type::kInt32:
5979 case DataType::Type::kInt64: {
5980 Register in_reg = InputRegisterAt(abs, 0);
5981 Register out_reg = OutputRegister(abs);
5982 __ Cmp(in_reg, Operand(0));
5983 __ Cneg(out_reg, in_reg, lt);
5984 break;
5985 }
5986 case DataType::Type::kFloat32:
5987 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01005988 VRegister in_reg = InputFPRegisterAt(abs, 0);
5989 VRegister out_reg = OutputFPRegister(abs);
Aart Bik3dad3412018-02-28 12:01:46 -08005990 __ Fabs(out_reg, in_reg);
5991 break;
5992 }
5993 default:
5994 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5995 }
5996}
5997
Igor Murashkind01745e2017-04-05 16:40:31 -07005998void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5999 constructor_fence->SetLocations(nullptr);
6000}
6001
6002void InstructionCodeGeneratorARM64::VisitConstructorFence(
6003 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
6004 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
6005}
6006
Calin Juravle27df7582015-04-17 19:12:31 +01006007void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6008 memory_barrier->SetLocations(nullptr);
6009}
6010
6011void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006012 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01006013}
6014
Alexandre Rames5319def2014-10-23 10:03:10 +01006015void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006016 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006017 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00006018 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01006019}
6020
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00006021void InstructionCodeGeneratorARM64::VisitReturn(HReturn* ret) {
6022 if (GetGraph()->IsCompilingOsr()) {
6023 // To simplify callers of an OSR method, we put the return value in both
6024 // floating point and core register.
6025 switch (ret->InputAt(0)->GetType()) {
6026 case DataType::Type::kFloat32:
6027 __ Fmov(w0, s0);
6028 break;
6029 case DataType::Type::kFloat64:
6030 __ Fmov(x0, d0);
6031 break;
6032 default:
6033 break;
6034 }
6035 }
Alexandre Rames5319def2014-10-23 10:03:10 +01006036 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01006037}
6038
6039void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
6040 instruction->SetLocations(nullptr);
6041}
6042
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006043void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01006044 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01006045}
6046
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006047void LocationsBuilderARM64::VisitRor(HRor* ror) {
6048 HandleBinaryOp(ror);
6049}
6050
6051void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
6052 HandleBinaryOp(ror);
6053}
6054
Serban Constantinescu02164b32014-11-13 14:05:07 +00006055void LocationsBuilderARM64::VisitShl(HShl* shl) {
6056 HandleShift(shl);
6057}
6058
6059void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
6060 HandleShift(shl);
6061}
6062
6063void LocationsBuilderARM64::VisitShr(HShr* shr) {
6064 HandleShift(shr);
6065}
6066
6067void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
6068 HandleShift(shr);
6069}
6070
Alexandre Rames5319def2014-10-23 10:03:10 +01006071void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006072 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006073}
6074
6075void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006076 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006077}
6078
Alexandre Rames67555f72014-11-18 10:55:16 +00006079void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006080 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00006081}
6082
6083void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01006084 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00006085}
6086
6087void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01006088 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006089}
6090
Alexandre Rames67555f72014-11-18 10:55:16 +00006091void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006092 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01006093}
6094
Vladimir Marko552a1342017-10-31 10:56:47 +00006095void LocationsBuilderARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6096 codegen_->CreateStringBuilderAppendLocations(instruction, LocationFrom(x0));
6097}
6098
6099void InstructionCodeGeneratorARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6100 __ Mov(w0, instruction->GetFormat()->GetValue());
6101 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
6102}
6103
Calin Juravlee460d1d2015-09-29 04:52:17 +01006104void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
6105 HUnresolvedInstanceFieldGet* instruction) {
6106 FieldAccessCallingConventionARM64 calling_convention;
6107 codegen_->CreateUnresolvedFieldLocationSummary(
6108 instruction, instruction->GetFieldType(), calling_convention);
6109}
6110
6111void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
6112 HUnresolvedInstanceFieldGet* instruction) {
6113 FieldAccessCallingConventionARM64 calling_convention;
6114 codegen_->GenerateUnresolvedFieldAccess(instruction,
6115 instruction->GetFieldType(),
6116 instruction->GetFieldIndex(),
6117 instruction->GetDexPc(),
6118 calling_convention);
6119}
6120
6121void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
6122 HUnresolvedInstanceFieldSet* instruction) {
6123 FieldAccessCallingConventionARM64 calling_convention;
6124 codegen_->CreateUnresolvedFieldLocationSummary(
6125 instruction, instruction->GetFieldType(), calling_convention);
6126}
6127
6128void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
6129 HUnresolvedInstanceFieldSet* instruction) {
6130 FieldAccessCallingConventionARM64 calling_convention;
6131 codegen_->GenerateUnresolvedFieldAccess(instruction,
6132 instruction->GetFieldType(),
6133 instruction->GetFieldIndex(),
6134 instruction->GetDexPc(),
6135 calling_convention);
6136}
6137
6138void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
6139 HUnresolvedStaticFieldGet* instruction) {
6140 FieldAccessCallingConventionARM64 calling_convention;
6141 codegen_->CreateUnresolvedFieldLocationSummary(
6142 instruction, instruction->GetFieldType(), calling_convention);
6143}
6144
6145void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
6146 HUnresolvedStaticFieldGet* instruction) {
6147 FieldAccessCallingConventionARM64 calling_convention;
6148 codegen_->GenerateUnresolvedFieldAccess(instruction,
6149 instruction->GetFieldType(),
6150 instruction->GetFieldIndex(),
6151 instruction->GetDexPc(),
6152 calling_convention);
6153}
6154
6155void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
6156 HUnresolvedStaticFieldSet* instruction) {
6157 FieldAccessCallingConventionARM64 calling_convention;
6158 codegen_->CreateUnresolvedFieldLocationSummary(
6159 instruction, instruction->GetFieldType(), calling_convention);
6160}
6161
6162void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
6163 HUnresolvedStaticFieldSet* instruction) {
6164 FieldAccessCallingConventionARM64 calling_convention;
6165 codegen_->GenerateUnresolvedFieldAccess(instruction,
6166 instruction->GetFieldType(),
6167 instruction->GetFieldIndex(),
6168 instruction->GetDexPc(),
6169 calling_convention);
6170}
6171
Alexandre Rames5319def2014-10-23 10:03:10 +01006172void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006173 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6174 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01006175 // In suspend check slow path, usually there are no caller-save registers at all.
6176 // If SIMD instructions are present, however, we force spilling all live SIMD
6177 // registers in full width (since the runtime only saves/restores lower part).
6178 locations->SetCustomSlowPathCallerSaves(
6179 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01006180}
6181
6182void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006183 HBasicBlock* block = instruction->GetBlock();
6184 if (block->GetLoopInformation() != nullptr) {
6185 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6186 // The back edge will generate the suspend check.
6187 return;
6188 }
6189 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6190 // The goto will generate the suspend check.
6191 return;
6192 }
6193 GenerateSuspendCheck(instruction, nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08006194 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01006195}
6196
Alexandre Rames67555f72014-11-18 10:55:16 +00006197void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006198 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6199 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00006200 InvokeRuntimeCallingConvention calling_convention;
6201 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
6202}
6203
6204void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00006205 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08006206 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00006207}
6208
6209void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
6210 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006211 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006212 DataType::Type input_type = conversion->GetInputType();
6213 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006214 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6215 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006216 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
6217 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006218 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6219 }
6220
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006221 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006222 locations->SetInAt(0, Location::RequiresFpuRegister());
6223 } else {
6224 locations->SetInAt(0, Location::RequiresRegister());
6225 }
6226
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006227 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006228 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6229 } else {
6230 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6231 }
6232}
6233
6234void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006235 DataType::Type result_type = conversion->GetResultType();
6236 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00006237
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006238 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6239 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006240
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006241 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
6242 int result_size = DataType::Size(result_type);
6243 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00006244 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006245 Register output = OutputRegister(conversion);
6246 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006247 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01006248 // 'int' values are used directly as W registers, discarding the top
6249 // bits, so we don't need to sign-extend and can just perform a move.
6250 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
6251 // top 32 bits of the target register. We theoretically could leave those
6252 // bits unchanged, but we would have to make sure that no code uses a
6253 // 32bit input value as a 64bit value assuming that the top 32 bits are
6254 // zero.
6255 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006256 } else if (DataType::IsUnsignedType(result_type) ||
6257 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
6258 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006259 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00006260 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006261 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006262 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006263 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006264 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6265 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006266 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006267 } else if (DataType::IsFloatingPointType(result_type) &&
6268 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006269 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
6270 } else {
6271 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6272 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006273 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00006274}
Alexandre Rames67555f72014-11-18 10:55:16 +00006275
Serban Constantinescu02164b32014-11-13 14:05:07 +00006276void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
6277 HandleShift(ushr);
6278}
6279
6280void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
6281 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00006282}
6283
6284void LocationsBuilderARM64::VisitXor(HXor* instruction) {
6285 HandleBinaryOp(instruction);
6286}
6287
6288void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
6289 HandleBinaryOp(instruction);
6290}
6291
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006292void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006293 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006294 LOG(FATAL) << "Unreachable";
6295}
6296
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006297void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006298 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006299 LOG(FATAL) << "Unreachable";
6300}
6301
Mark Mendellfe57faa2015-09-18 09:26:15 -04006302// Simple implementation of packed switch - generate cascaded compare/jumps.
6303void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6304 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006305 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006306 locations->SetInAt(0, Location::RequiresRegister());
6307}
6308
6309void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6310 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08006311 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006312 Register value_reg = InputRegisterAt(switch_instr, 0);
6313 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6314
Zheng Xu3927c8b2015-11-18 17:46:25 +08006315 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01006316 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08006317 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
6318 // make sure we don't emit it if the target may run out of range.
6319 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
6320 // ranges and emit the tables only as required.
6321 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04006322
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006323 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08006324 // Current instruction id is an upper bound of the number of HIRs in the graph.
6325 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
6326 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006327 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6328 Register temp = temps.AcquireW();
6329 __ Subs(temp, value_reg, Operand(lower_bound));
6330
Zheng Xu3927c8b2015-11-18 17:46:25 +08006331 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006332 // Jump to successors[0] if value == lower_bound.
6333 __ B(eq, codegen_->GetLabelOf(successors[0]));
6334 int32_t last_index = 0;
6335 for (; num_entries - last_index > 2; last_index += 2) {
6336 __ Subs(temp, temp, Operand(2));
6337 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6338 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
6339 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6340 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
6341 }
6342 if (num_entries - last_index == 2) {
6343 // The last missing case_value.
6344 __ Cmp(temp, Operand(1));
6345 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08006346 }
6347
6348 // And the default for any other value.
6349 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6350 __ B(codegen_->GetLabelOf(default_block));
6351 }
6352 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01006353 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08006354
6355 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6356
6357 // Below instructions should use at most one blocked register. Since there are two blocked
6358 // registers, we are free to block one.
6359 Register temp_w = temps.AcquireW();
6360 Register index;
6361 // Remove the bias.
6362 if (lower_bound != 0) {
6363 index = temp_w;
6364 __ Sub(index, value_reg, Operand(lower_bound));
6365 } else {
6366 index = value_reg;
6367 }
6368
6369 // Jump to default block if index is out of the range.
6370 __ Cmp(index, Operand(num_entries));
6371 __ B(hs, codegen_->GetLabelOf(default_block));
6372
6373 // In current VIXL implementation, it won't require any blocked registers to encode the
6374 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
6375 // register pressure.
6376 Register table_base = temps.AcquireX();
6377 // Load jump offset from the table.
6378 __ Adr(table_base, jump_table->GetTableStartLabel());
6379 Register jump_offset = temp_w;
6380 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
6381
6382 // Jump to target block by branching to table_base(pc related) + offset.
6383 Register target_address = table_base;
6384 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
6385 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006386 }
6387}
6388
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006389void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
6390 HInstruction* instruction,
6391 Location out,
6392 uint32_t offset,
6393 Location maybe_temp,
6394 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006395 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006396 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006397 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006398 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006399 if (kUseBakerReadBarrier) {
6400 // Load with fast path based Baker's read barrier.
6401 // /* HeapReference<Object> */ out = *(out + offset)
6402 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6403 out,
6404 out_reg,
6405 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006406 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006407 /* needs_null_check= */ false,
6408 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006409 } else {
6410 // Load with slow path based read barrier.
6411 // Save the value of `out` into `maybe_temp` before overwriting it
6412 // in the following move operation, as we will need it for the
6413 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006414 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00006415 __ Mov(temp_reg, out_reg);
6416 // /* HeapReference<Object> */ out = *(out + offset)
6417 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6418 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6419 }
6420 } else {
6421 // Plain load with no read barrier.
6422 // /* HeapReference<Object> */ out = *(out + offset)
6423 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6424 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6425 }
6426}
6427
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006428void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
6429 HInstruction* instruction,
6430 Location out,
6431 Location obj,
6432 uint32_t offset,
6433 Location maybe_temp,
6434 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006435 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006436 Register out_reg = RegisterFrom(out, type);
6437 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006438 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006439 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006440 if (kUseBakerReadBarrier) {
6441 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00006442 // /* HeapReference<Object> */ out = *(obj + offset)
6443 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6444 out,
6445 obj_reg,
6446 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006447 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006448 /* needs_null_check= */ false,
6449 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006450 } else {
6451 // Load with slow path based read barrier.
6452 // /* HeapReference<Object> */ out = *(obj + offset)
6453 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6454 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6455 }
6456 } else {
6457 // Plain load with no read barrier.
6458 // /* HeapReference<Object> */ out = *(obj + offset)
6459 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6460 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6461 }
6462}
6463
Vladimir Markoca1e0382018-04-11 09:58:41 +00006464void CodeGeneratorARM64::GenerateGcRootFieldLoad(
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006465 HInstruction* instruction,
6466 Location root,
6467 Register obj,
6468 uint32_t offset,
6469 vixl::aarch64::Label* fixup_label,
6470 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006471 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006472 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006473 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006474 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006475 if (kUseBakerReadBarrier) {
6476 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00006477 // Baker's read barrier are used.
Roland Levillain44015862016-01-22 11:47:17 +00006478
Vladimir Marko008e09f32018-08-06 15:42:43 +01006479 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6480 // the Marking Register) to decide whether we need to enter
6481 // the slow path to mark the GC root.
6482 //
6483 // We use shared thunks for the slow path; shared within the method
6484 // for JIT, across methods for AOT. That thunk checks the reference
6485 // and jumps to the entrypoint if needed.
6486 //
6487 // lr = &return_address;
6488 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6489 // if (mr) { // Thread::Current()->GetIsGcMarking()
6490 // goto gc_root_thunk<root_reg>(lr)
6491 // }
6492 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00006493
Vladimir Marko008e09f32018-08-06 15:42:43 +01006494 UseScratchRegisterScope temps(GetVIXLAssembler());
6495 DCHECK(temps.IsAvailable(ip0));
6496 DCHECK(temps.IsAvailable(ip1));
6497 temps.Exclude(ip0, ip1);
6498 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
Roland Levillain44015862016-01-22 11:47:17 +00006499
Vladimir Marko008e09f32018-08-06 15:42:43 +01006500 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6501 vixl::aarch64::Label return_address;
6502 __ adr(lr, &return_address);
6503 if (fixup_label != nullptr) {
6504 __ bind(fixup_label);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006505 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006506 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
Vladimir Marko94796f82018-08-08 15:15:33 +01006507 "GC root LDR must be 2 instructions (8B) before the return address label.");
Vladimir Marko008e09f32018-08-06 15:42:43 +01006508 __ ldr(root_reg, MemOperand(obj.X(), offset));
6509 EmitBakerReadBarrierCbnz(custom_data);
6510 __ bind(&return_address);
Roland Levillain44015862016-01-22 11:47:17 +00006511 } else {
6512 // GC root loaded through a slow path for read barriers other
6513 // than Baker's.
6514 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006515 if (fixup_label == nullptr) {
6516 __ Add(root_reg.X(), obj.X(), offset);
6517 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006518 EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006519 }
Roland Levillain44015862016-01-22 11:47:17 +00006520 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00006521 GenerateReadBarrierForRootSlow(instruction, root, root);
Roland Levillain44015862016-01-22 11:47:17 +00006522 }
6523 } else {
6524 // Plain GC root load with no read barrier.
6525 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006526 if (fixup_label == nullptr) {
6527 __ Ldr(root_reg, MemOperand(obj, offset));
6528 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006529 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006530 }
Roland Levillain44015862016-01-22 11:47:17 +00006531 // Note that GC roots are not affected by heap poisoning, thus we
6532 // do not have to unpoison `root_reg` here.
6533 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006534 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00006535}
6536
Vladimir Markoc8178f52020-11-24 10:38:16 +00006537void CodeGeneratorARM64::GenerateIntrinsicCasMoveWithBakerReadBarrier(
6538 vixl::aarch64::Register marked_old_value,
Vladimir Marko94796f82018-08-08 15:15:33 +01006539 vixl::aarch64::Register old_value) {
6540 DCHECK(kEmitCompilerReadBarrier);
6541 DCHECK(kUseBakerReadBarrier);
6542
6543 // Similar to the Baker RB path in GenerateGcRootFieldLoad(), with a MOV instead of LDR.
Vladimir Markoc8178f52020-11-24 10:38:16 +00006544 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(marked_old_value.GetCode());
Vladimir Marko94796f82018-08-08 15:15:33 +01006545
6546 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6547 vixl::aarch64::Label return_address;
6548 __ adr(lr, &return_address);
6549 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6550 "GC root LDR must be 2 instructions (8B) before the return address label.");
Vladimir Markoc8178f52020-11-24 10:38:16 +00006551 __ mov(marked_old_value, old_value);
Vladimir Marko94796f82018-08-08 15:15:33 +01006552 EmitBakerReadBarrierCbnz(custom_data);
6553 __ bind(&return_address);
6554}
6555
Roland Levillain44015862016-01-22 11:47:17 +00006556void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6557 Location ref,
Vladimir Marko248141f2018-08-10 10:40:07 +01006558 vixl::aarch64::Register obj,
6559 const vixl::aarch64::MemOperand& src,
Roland Levillain44015862016-01-22 11:47:17 +00006560 bool needs_null_check,
6561 bool use_load_acquire) {
6562 DCHECK(kEmitCompilerReadBarrier);
6563 DCHECK(kUseBakerReadBarrier);
6564
Vladimir Marko0ecac682018-08-07 10:40:38 +01006565 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6566 // Marking Register) to decide whether we need to enter the slow
6567 // path to mark the reference. Then, in the slow path, check the
6568 // gray bit in the lock word of the reference's holder (`obj`) to
6569 // decide whether to mark `ref` or not.
6570 //
6571 // We use shared thunks for the slow path; shared within the method
6572 // for JIT, across methods for AOT. That thunk checks the holder
6573 // and jumps to the entrypoint if needed. If the holder is not gray,
6574 // it creates a fake dependency and returns to the LDR instruction.
6575 //
6576 // lr = &gray_return_address;
6577 // if (mr) { // Thread::Current()->GetIsGcMarking()
6578 // goto field_thunk<holder_reg, base_reg, use_load_acquire>(lr)
6579 // }
6580 // not_gray_return_address:
6581 // // Original reference load. If the offset is too large to fit
6582 // // into LDR, we use an adjusted base register here.
6583 // HeapReference<mirror::Object> reference = *(obj+offset);
6584 // gray_return_address:
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006585
Vladimir Marko248141f2018-08-10 10:40:07 +01006586 DCHECK(src.GetAddrMode() == vixl::aarch64::Offset);
6587 DCHECK_ALIGNED(src.GetOffset(), sizeof(mirror::HeapReference<mirror::Object>));
6588
6589 UseScratchRegisterScope temps(GetVIXLAssembler());
6590 DCHECK(temps.IsAvailable(ip0));
6591 DCHECK(temps.IsAvailable(ip1));
6592 temps.Exclude(ip0, ip1);
6593 uint32_t custom_data = use_load_acquire
6594 ? EncodeBakerReadBarrierAcquireData(src.GetBaseRegister().GetCode(), obj.GetCode())
6595 : EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode());
6596
6597 {
6598 ExactAssemblyScope guard(GetVIXLAssembler(),
6599 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6600 vixl::aarch64::Label return_address;
6601 __ adr(lr, &return_address);
6602 EmitBakerReadBarrierCbnz(custom_data);
6603 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6604 "Field LDR must be 1 instruction (4B) before the return address label; "
6605 " 2 instructions (8B) for heap poisoning.");
6606 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
6607 if (use_load_acquire) {
6608 DCHECK_EQ(src.GetOffset(), 0);
6609 __ ldar(ref_reg, src);
6610 } else {
6611 __ ldr(ref_reg, src);
6612 }
6613 if (needs_null_check) {
6614 MaybeRecordImplicitNullCheck(instruction);
6615 }
6616 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6617 // macro instructions disallowed in ExactAssemblyScope.
6618 if (kPoisonHeapReferences) {
6619 __ neg(ref_reg, Operand(ref_reg));
6620 }
6621 __ bind(&return_address);
6622 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006623 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Vladimir Marko248141f2018-08-10 10:40:07 +01006624}
6625
6626void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6627 Location ref,
6628 Register obj,
6629 uint32_t offset,
6630 Location maybe_temp,
6631 bool needs_null_check,
6632 bool use_load_acquire) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006633 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6634 Register base = obj;
6635 if (use_load_acquire) {
6636 DCHECK(maybe_temp.IsRegister());
6637 base = WRegisterFrom(maybe_temp);
6638 __ Add(base, obj, offset);
6639 offset = 0u;
6640 } else if (offset >= kReferenceLoadMinFarOffset) {
6641 DCHECK(maybe_temp.IsRegister());
6642 base = WRegisterFrom(maybe_temp);
6643 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6644 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6645 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006646 }
Vladimir Marko248141f2018-08-10 10:40:07 +01006647 MemOperand src(base.X(), offset);
6648 GenerateFieldLoadWithBakerReadBarrier(
6649 instruction, ref, obj, src, needs_null_check, use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006650}
6651
Artem Serov0806f582018-10-11 20:14:20 +01006652void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HArrayGet* instruction,
6653 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006654 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006655 uint32_t data_offset,
6656 Location index,
Roland Levillain44015862016-01-22 11:47:17 +00006657 bool needs_null_check) {
6658 DCHECK(kEmitCompilerReadBarrier);
6659 DCHECK(kUseBakerReadBarrier);
6660
Vladimir Marko66d691d2017-04-07 17:53:39 +01006661 static_assert(
6662 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6663 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006664 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006665
Vladimir Marko008e09f32018-08-06 15:42:43 +01006666 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6667 // Marking Register) to decide whether we need to enter the slow
6668 // path to mark the reference. Then, in the slow path, check the
6669 // gray bit in the lock word of the reference's holder (`obj`) to
6670 // decide whether to mark `ref` or not.
6671 //
6672 // We use shared thunks for the slow path; shared within the method
6673 // for JIT, across methods for AOT. That thunk checks the holder
6674 // and jumps to the entrypoint if needed. If the holder is not gray,
6675 // it creates a fake dependency and returns to the LDR instruction.
6676 //
6677 // lr = &gray_return_address;
6678 // if (mr) { // Thread::Current()->GetIsGcMarking()
6679 // goto array_thunk<base_reg>(lr)
6680 // }
6681 // not_gray_return_address:
6682 // // Original reference load. If the offset is too large to fit
6683 // // into LDR, we use an adjusted base register here.
6684 // HeapReference<mirror::Object> reference = data[index];
6685 // gray_return_address:
Vladimir Marko66d691d2017-04-07 17:53:39 +01006686
Vladimir Marko008e09f32018-08-06 15:42:43 +01006687 DCHECK(index.IsValid());
6688 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
6689 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006690
Vladimir Marko008e09f32018-08-06 15:42:43 +01006691 UseScratchRegisterScope temps(GetVIXLAssembler());
6692 DCHECK(temps.IsAvailable(ip0));
6693 DCHECK(temps.IsAvailable(ip1));
6694 temps.Exclude(ip0, ip1);
Artem Serov0806f582018-10-11 20:14:20 +01006695
6696 Register temp;
6697 if (instruction->GetArray()->IsIntermediateAddress()) {
6698 // We do not need to compute the intermediate address from the array: the
6699 // input instruction has done it already. See the comment in
6700 // `TryExtractArrayAccessAddress()`.
6701 if (kIsDebugBuild) {
6702 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
6703 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
6704 }
6705 temp = obj;
6706 } else {
6707 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
6708 __ Add(temp.X(), obj.X(), Operand(data_offset));
6709 }
6710
Vladimir Marko008e09f32018-08-06 15:42:43 +01006711 uint32_t custom_data = EncodeBakerReadBarrierArrayData(temp.GetCode());
Vladimir Marko66d691d2017-04-07 17:53:39 +01006712
Vladimir Marko008e09f32018-08-06 15:42:43 +01006713 {
6714 ExactAssemblyScope guard(GetVIXLAssembler(),
6715 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6716 vixl::aarch64::Label return_address;
6717 __ adr(lr, &return_address);
6718 EmitBakerReadBarrierCbnz(custom_data);
6719 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6720 "Array LDR must be 1 instruction (4B) before the return address label; "
6721 " 2 instructions (8B) for heap poisoning.");
6722 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6723 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6724 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6725 // macro instructions disallowed in ExactAssemblyScope.
6726 if (kPoisonHeapReferences) {
6727 __ neg(ref_reg, Operand(ref_reg));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006728 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006729 __ bind(&return_address);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006730 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006731 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Roland Levillain44015862016-01-22 11:47:17 +00006732}
6733
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006734void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6735 // The following condition is a compile-time one, so it does not have a run-time cost.
6736 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
6737 // The following condition is a run-time one; it is executed after the
6738 // previous compile-time test, to avoid penalizing non-debug builds.
6739 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6740 UseScratchRegisterScope temps(GetVIXLAssembler());
6741 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6742 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6743 }
6744 }
6745}
6746
Vladimir Marko1bff99f2020-11-02 15:07:33 +00006747SlowPathCodeARM64* CodeGeneratorARM64::AddReadBarrierSlowPath(HInstruction* instruction,
6748 Location out,
6749 Location ref,
6750 Location obj,
6751 uint32_t offset,
6752 Location index) {
6753 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
6754 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6755 AddSlowPath(slow_path);
6756 return slow_path;
6757}
6758
Roland Levillain44015862016-01-22 11:47:17 +00006759void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6760 Location out,
6761 Location ref,
6762 Location obj,
6763 uint32_t offset,
6764 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006765 DCHECK(kEmitCompilerReadBarrier);
6766
Roland Levillain44015862016-01-22 11:47:17 +00006767 // Insert a slow path based read barrier *after* the reference load.
6768 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006769 // If heap poisoning is enabled, the unpoisoning of the loaded
6770 // reference will be carried out by the runtime within the slow
6771 // path.
6772 //
6773 // Note that `ref` currently does not get unpoisoned (when heap
6774 // poisoning is enabled), which is alright as the `ref` argument is
6775 // not used by the artReadBarrierSlow entry point.
6776 //
6777 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko1bff99f2020-11-02 15:07:33 +00006778 SlowPathCodeARM64* slow_path = AddReadBarrierSlowPath(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006779
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006780 __ B(slow_path->GetEntryLabel());
6781 __ Bind(slow_path->GetExitLabel());
6782}
6783
Roland Levillain44015862016-01-22 11:47:17 +00006784void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6785 Location out,
6786 Location ref,
6787 Location obj,
6788 uint32_t offset,
6789 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006790 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006791 // Baker's read barriers shall be handled by the fast path
6792 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6793 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006794 // If heap poisoning is enabled, unpoisoning will be taken care of
6795 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006796 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006797 } else if (kPoisonHeapReferences) {
6798 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6799 }
6800}
6801
Roland Levillain44015862016-01-22 11:47:17 +00006802void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6803 Location out,
6804 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006805 DCHECK(kEmitCompilerReadBarrier);
6806
Roland Levillain44015862016-01-22 11:47:17 +00006807 // Insert a slow path based read barrier *after* the GC root load.
6808 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006809 // Note that GC roots are not affected by heap poisoning, so we do
6810 // not need to do anything special for this here.
6811 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006812 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006813 AddSlowPath(slow_path);
6814
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006815 __ B(slow_path->GetEntryLabel());
6816 __ Bind(slow_path->GetExitLabel());
6817}
6818
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006819void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6820 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006821 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006822 locations->SetInAt(0, Location::RequiresRegister());
6823 locations->SetOut(Location::RequiresRegister());
6824}
6825
6826void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6827 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006828 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006829 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006830 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006831 __ Ldr(XRegisterFrom(locations->Out()),
6832 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006833 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006834 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006835 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006836 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6837 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006838 __ Ldr(XRegisterFrom(locations->Out()),
6839 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006840 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006841}
6842
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006843static void PatchJitRootUse(uint8_t* code,
6844 const uint8_t* roots_data,
6845 vixl::aarch64::Literal<uint32_t>* literal,
6846 uint64_t index_in_table) {
6847 uint32_t literal_offset = literal->GetOffset();
6848 uintptr_t address =
6849 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6850 uint8_t* data = code + literal_offset;
6851 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6852}
6853
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006854void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6855 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006856 const StringReference& string_reference = entry.first;
6857 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006858 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006859 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006860 }
6861 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006862 const TypeReference& type_reference = entry.first;
6863 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006864 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006865 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006866 }
6867}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006868
Artem Serov8ba4de12019-12-04 21:10:23 +00006869MemOperand InstructionCodeGeneratorARM64::VecNEONAddress(
Artem Serov1a719e42019-07-18 14:24:55 +01006870 HVecMemoryOperation* instruction,
6871 UseScratchRegisterScope* temps_scope,
6872 size_t size,
6873 bool is_string_char_at,
6874 /*out*/ Register* scratch) {
6875 LocationSummary* locations = instruction->GetLocations();
6876 Register base = InputRegisterAt(instruction, 0);
6877
6878 if (instruction->InputAt(1)->IsIntermediateAddressIndex()) {
6879 DCHECK(!is_string_char_at);
6880 return MemOperand(base.X(), InputRegisterAt(instruction, 1).X());
6881 }
6882
6883 Location index = locations->InAt(1);
6884 uint32_t offset = is_string_char_at
6885 ? mirror::String::ValueOffset().Uint32Value()
6886 : mirror::Array::DataOffset(size).Uint32Value();
6887 size_t shift = ComponentSizeShiftWidth(size);
6888
6889 // HIntermediateAddress optimization is only applied for scalar ArrayGet and ArraySet.
6890 DCHECK(!instruction->InputAt(0)->IsIntermediateAddress());
6891
6892 if (index.IsConstant()) {
6893 offset += Int64FromLocation(index) << shift;
6894 return HeapOperand(base, offset);
6895 } else {
6896 *scratch = temps_scope->AcquireSameSizeAs(base);
6897 __ Add(*scratch, base, Operand(WRegisterFrom(index), LSL, shift));
6898 return HeapOperand(*scratch, offset);
6899 }
6900}
6901
Artem Serov8ba4de12019-12-04 21:10:23 +00006902SVEMemOperand InstructionCodeGeneratorARM64::VecSVEAddress(
6903 HVecMemoryOperation* instruction,
6904 UseScratchRegisterScope* temps_scope,
6905 size_t size,
6906 bool is_string_char_at,
6907 /*out*/ Register* scratch) {
6908 LocationSummary* locations = instruction->GetLocations();
6909 Register base = InputRegisterAt(instruction, 0);
6910 Location index = locations->InAt(1);
6911
6912 // TODO: Support intermediate address sharing for SVE accesses.
6913 DCHECK(!instruction->InputAt(1)->IsIntermediateAddressIndex());
6914 DCHECK(!instruction->InputAt(0)->IsIntermediateAddress());
6915 DCHECK(!index.IsConstant());
6916
6917 uint32_t offset = is_string_char_at
6918 ? mirror::String::ValueOffset().Uint32Value()
6919 : mirror::Array::DataOffset(size).Uint32Value();
6920 size_t shift = ComponentSizeShiftWidth(size);
6921
6922 *scratch = temps_scope->AcquireSameSizeAs(base);
6923 __ Add(*scratch, base, offset);
6924 return SVEMemOperand(scratch->X(), XRegisterFrom(index), LSL, shift);
6925}
6926
Alexandre Rames67555f72014-11-18 10:55:16 +00006927#undef __
6928#undef QUICK_ENTRY_POINT
6929
Vladimir Markoca1e0382018-04-11 09:58:41 +00006930#define __ assembler.GetVIXLAssembler()->
6931
6932static void EmitGrayCheckAndFastPath(arm64::Arm64Assembler& assembler,
6933 vixl::aarch64::Register base_reg,
6934 vixl::aarch64::MemOperand& lock_word,
Vladimir Marko7a695052018-04-12 10:26:50 +01006935 vixl::aarch64::Label* slow_path,
6936 vixl::aarch64::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006937 // Load the lock word containing the rb_state.
6938 __ Ldr(ip0.W(), lock_word);
6939 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01006940 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Vladimir Markoca1e0382018-04-11 09:58:41 +00006941 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6942 __ Tbnz(ip0.W(), LockWord::kReadBarrierStateShift, slow_path);
6943 static_assert(
6944 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET,
6945 "Field and array LDR offsets must be the same to reuse the same code.");
Vladimir Marko7a695052018-04-12 10:26:50 +01006946 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
6947 if (throw_npe != nullptr) {
6948 __ Bind(throw_npe);
6949 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006950 // Adjust the return address back to the LDR (1 instruction; 2 for heap poisoning).
6951 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6952 "Field LDR must be 1 instruction (4B) before the return address label; "
6953 " 2 instructions (8B) for heap poisoning.");
6954 __ Add(lr, lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6955 // Introduce a dependency on the lock_word including rb_state,
6956 // to prevent load-load reordering, and without using
6957 // a memory barrier (which would be more expensive).
6958 __ Add(base_reg, base_reg, Operand(ip0, LSR, 32));
6959 __ Br(lr); // And return back to the function.
6960 // Note: The fake dependency is unnecessary for the slow path.
6961}
6962
6963// Load the read barrier introspection entrypoint in register `entrypoint`.
6964static void LoadReadBarrierMarkIntrospectionEntrypoint(arm64::Arm64Assembler& assembler,
6965 vixl::aarch64::Register entrypoint) {
6966 // entrypoint = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6967 DCHECK_EQ(ip0.GetCode(), 16u);
6968 const int32_t entry_point_offset =
6969 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6970 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
6971}
6972
6973void CodeGeneratorARM64::CompileBakerReadBarrierThunk(Arm64Assembler& assembler,
6974 uint32_t encoded_data,
6975 /*out*/ std::string* debug_name) {
6976 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
6977 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006978 case BakerReadBarrierKind::kField:
6979 case BakerReadBarrierKind::kAcquire: {
Artem Serova07de552020-11-01 22:42:43 +00006980 Register base_reg =
6981 vixl::aarch64::XRegister(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00006982 CheckValidReg(base_reg.GetCode());
Artem Serova07de552020-11-01 22:42:43 +00006983 Register holder_reg =
6984 vixl::aarch64::XRegister(BakerReadBarrierSecondRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00006985 CheckValidReg(holder_reg.GetCode());
6986 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6987 temps.Exclude(ip0, ip1);
Roland Levillain988c3912019-09-25 19:33:35 +01006988 // In the case of a field load (with relaxed semantic), if `base_reg` differs from
6989 // `holder_reg`, the offset was too large and we must have emitted (during the construction
6990 // of the HIR graph, see `art::HInstructionBuilder::BuildInstanceFieldAccess`) and preserved
6991 // (see `art::PrepareForRegisterAllocation::VisitNullCheck`) an explicit null check before
6992 // the load. Otherwise, for implicit null checks, we need to null-check the holder as we do
6993 // not necessarily do that check before going to the thunk.
6994 //
6995 // In the case of a field load with load-acquire semantics (where `base_reg` always differs
6996 // from `holder_reg`), we also need an explicit null check when implicit null checks are
6997 // allowed, as we do not emit one before going to the thunk.
Vladimir Marko7a695052018-04-12 10:26:50 +01006998 vixl::aarch64::Label throw_npe_label;
6999 vixl::aarch64::Label* throw_npe = nullptr;
Roland Levillain988c3912019-09-25 19:33:35 +01007000 if (GetCompilerOptions().GetImplicitNullChecks() &&
7001 (holder_reg.Is(base_reg) || (kind == BakerReadBarrierKind::kAcquire))) {
Vladimir Marko7a695052018-04-12 10:26:50 +01007002 throw_npe = &throw_npe_label;
7003 __ Cbz(holder_reg.W(), throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00007004 }
Vladimir Marko7a695052018-04-12 10:26:50 +01007005 // Check if the holder is gray and, if not, add fake dependency to the base register
7006 // and return to the LDR instruction to load the reference. Otherwise, use introspection
7007 // to load the reference and call the entrypoint that performs further checks on the
7008 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00007009 vixl::aarch64::Label slow_path;
7010 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
Vladimir Marko7a695052018-04-12 10:26:50 +01007011 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00007012 __ Bind(&slow_path);
Vladimir Marko0ecac682018-08-07 10:40:38 +01007013 if (kind == BakerReadBarrierKind::kField) {
7014 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
7015 __ Ldr(ip0.W(), ldr_address); // Load the LDR (immediate) unsigned offset.
7016 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
7017 __ Ubfx(ip0.W(), ip0.W(), 10, 12); // Extract the offset.
7018 __ Ldr(ip0.W(), MemOperand(base_reg, ip0, LSL, 2)); // Load the reference.
7019 } else {
7020 DCHECK(kind == BakerReadBarrierKind::kAcquire);
7021 DCHECK(!base_reg.Is(holder_reg));
7022 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
7023 __ Ldar(ip0.W(), MemOperand(base_reg));
7024 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00007025 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
7026 __ Br(ip1); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00007027 break;
7028 }
7029 case BakerReadBarrierKind::kArray: {
Artem Serova07de552020-11-01 22:42:43 +00007030 Register base_reg =
7031 vixl::aarch64::XRegister(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00007032 CheckValidReg(base_reg.GetCode());
7033 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7034 BakerReadBarrierSecondRegField::Decode(encoded_data));
7035 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
7036 temps.Exclude(ip0, ip1);
7037 vixl::aarch64::Label slow_path;
7038 int32_t data_offset =
7039 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
7040 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
7041 DCHECK_LT(lock_word.GetOffset(), 0);
7042 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path);
7043 __ Bind(&slow_path);
7044 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
7045 __ Ldr(ip0.W(), ldr_address); // Load the LDR (register) unsigned offset.
7046 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
7047 __ Ubfx(ip0, ip0, 16, 6); // Extract the index register, plus 32 (bit 21 is set).
7048 __ Bfi(ip1, ip0, 3, 6); // Insert ip0 to the entrypoint address to create
7049 // a switch case target based on the index register.
7050 __ Mov(ip0, base_reg); // Move the base register to ip0.
7051 __ Br(ip1); // Jump to the entrypoint's array switch case.
7052 break;
7053 }
7054 case BakerReadBarrierKind::kGcRoot: {
7055 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
7056 // and it does not have a forwarding address), call the correct introspection entrypoint;
7057 // otherwise return the reference (or the extracted forwarding address).
7058 // There is no gray bit check for GC roots.
Artem Serova07de552020-11-01 22:42:43 +00007059 Register root_reg =
7060 vixl::aarch64::WRegister(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00007061 CheckValidReg(root_reg.GetCode());
7062 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7063 BakerReadBarrierSecondRegField::Decode(encoded_data));
7064 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
7065 temps.Exclude(ip0, ip1);
7066 vixl::aarch64::Label return_label, not_marked, forwarding_address;
7067 __ Cbz(root_reg, &return_label);
7068 MemOperand lock_word(root_reg.X(), mirror::Object::MonitorOffset().Int32Value());
7069 __ Ldr(ip0.W(), lock_word);
7070 __ Tbz(ip0.W(), LockWord::kMarkBitStateShift, &not_marked);
7071 __ Bind(&return_label);
7072 __ Br(lr);
7073 __ Bind(&not_marked);
7074 __ Tst(ip0.W(), Operand(ip0.W(), LSL, 1));
7075 __ B(&forwarding_address, mi);
7076 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
7077 // Adjust the art_quick_read_barrier_mark_introspection address in IP1 to
7078 // art_quick_read_barrier_mark_introspection_gc_roots.
7079 __ Add(ip1, ip1, Operand(BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRYPOINT_OFFSET));
7080 __ Mov(ip0.W(), root_reg);
7081 __ Br(ip1);
7082 __ Bind(&forwarding_address);
7083 __ Lsl(root_reg, ip0.W(), LockWord::kForwardingAddressShift);
7084 __ Br(lr);
7085 break;
7086 }
7087 default:
7088 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
7089 UNREACHABLE();
7090 }
7091
Vladimir Marko966b46f2018-08-03 10:20:19 +00007092 // For JIT, the slow path is considered part of the compiled method,
Vladimir Markof91fc122020-05-13 09:21:00 +01007093 // so JIT should pass null as `debug_name`.
Vladimir Marko695348f2020-05-19 14:42:02 +01007094 DCHECK(!GetCompilerOptions().IsJitCompiler() || debug_name == nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00007095 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00007096 std::ostringstream oss;
7097 oss << "BakerReadBarrierThunk";
7098 switch (kind) {
7099 case BakerReadBarrierKind::kField:
7100 oss << "Field_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
7101 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
7102 break;
Vladimir Marko0ecac682018-08-07 10:40:38 +01007103 case BakerReadBarrierKind::kAcquire:
7104 oss << "Acquire_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
7105 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
7106 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +00007107 case BakerReadBarrierKind::kArray:
7108 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
7109 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7110 BakerReadBarrierSecondRegField::Decode(encoded_data));
7111 break;
7112 case BakerReadBarrierKind::kGcRoot:
7113 oss << "GcRoot_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
7114 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7115 BakerReadBarrierSecondRegField::Decode(encoded_data));
7116 break;
7117 }
7118 *debug_name = oss.str();
7119 }
7120}
7121
7122#undef __
7123
Alexandre Rames5319def2014-10-23 10:03:10 +01007124} // namespace arm64
7125} // namespace art