blob: 36040ca690cf4b4f631ea9c5b1e5dd46e028dfd3 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Vladimir Markof4f2daa2017-03-20 18:26:59 +000019#include "arch/arm64/asm_support_arm64.h"
Serban Constantinescu579885a2015-02-22 20:51:33 +000020#include "arch/arm64/instruction_set_features_arm64.h"
Vladimir Marko86c87522020-05-11 16:55:55 +010021#include "arch/arm64/jni_frame_arm64.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000022#include "art_method-inl.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070023#include "base/bit_utils.h"
24#include "base/bit_utils_iterator.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010025#include "class_table.h"
Zheng Xuc6667102015-05-15 16:08:45 +080026#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000027#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080029#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010030#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010031#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070032#include "heap_poisoning.h"
Nicolas Geoffray4313ccb2020-08-26 17:01:15 +010033#include "interpreter/mterp/nterp.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080034#include "intrinsics.h"
35#include "intrinsics_arm64.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010036#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070037#include "lock_word.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010038#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070039#include "mirror/class-inl.h"
Vladimir Marko2d98dc22020-10-01 11:21:37 +000040#include "mirror/var_handle.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000041#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010042#include "thread.h"
43#include "utils/arm64/assembler_arm64.h"
44#include "utils/assembler.h"
45#include "utils/stack_checks.h"
46
Scott Wakeling97c72b72016-06-24 16:19:36 +010047using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000048using vixl::ExactAssemblyScope;
49using vixl::CodeBufferCheckScope;
50using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010051
52#ifdef __
53#error "ARM64 Codegen VIXL macro-assembler macro already defined."
54#endif
55
Vladimir Marko0a516052019-10-14 13:00:44 +000056namespace art {
Alexandre Rames5319def2014-10-23 10:03:10 +010057
Roland Levillain22ccc3a2015-11-24 13:10:05 +000058template<class MirrorType>
59class GcRoot;
60
Alexandre Rames5319def2014-10-23 10:03:10 +010061namespace arm64 {
62
Alexandre Ramesbe919d92016-08-23 18:33:36 +010063using helpers::ARM64EncodableConstantOrRegister;
64using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080065using helpers::CPURegisterFrom;
66using helpers::DRegisterFrom;
67using helpers::FPRegisterFrom;
68using helpers::HeapOperand;
69using helpers::HeapOperandFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010070using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080071using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080072using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010073using helpers::InputRegisterAt;
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +010074using helpers::Int64FromLocation;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010075using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080076using helpers::LocationFrom;
77using helpers::OperandFromMemOperand;
78using helpers::OutputCPURegister;
79using helpers::OutputFPRegister;
80using helpers::OutputRegister;
81using helpers::RegisterFrom;
82using helpers::StackOperandFrom;
83using helpers::VIXLRegCodeFromART;
84using helpers::WRegisterFrom;
85using helpers::XRegisterFrom;
86
Vladimir Markof3e0ee22015-12-17 15:23:13 +000087// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080088// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
89// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000090static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010091
Vladimir Markof4f2daa2017-03-20 18:26:59 +000092// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
93// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
Vladimir Marko008e09f32018-08-06 15:42:43 +010094// For the Baker read barrier implementation using link-time generated thunks we need to split
Vladimir Markof4f2daa2017-03-20 18:26:59 +000095// the offset explicitly.
96constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
97
Alexandre Rames5319def2014-10-23 10:03:10 +010098inline Condition ARM64Condition(IfCondition cond) {
99 switch (cond) {
100 case kCondEQ: return eq;
101 case kCondNE: return ne;
102 case kCondLT: return lt;
103 case kCondLE: return le;
104 case kCondGT: return gt;
105 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700106 case kCondB: return lo;
107 case kCondBE: return ls;
108 case kCondA: return hi;
109 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100110 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000111 LOG(FATAL) << "Unreachable";
112 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100113}
114
Vladimir Markod6e069b2016-01-18 11:11:01 +0000115inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
116 // The ARM64 condition codes can express all the necessary branches, see the
117 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
118 // There is no dex instruction or HIR that would need the missing conditions
119 // "equal or unordered" or "not equal".
120 switch (cond) {
121 case kCondEQ: return eq;
122 case kCondNE: return ne /* unordered */;
123 case kCondLT: return gt_bias ? cc : lt /* unordered */;
124 case kCondLE: return gt_bias ? ls : le /* unordered */;
125 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
126 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
127 default:
128 LOG(FATAL) << "UNREACHABLE";
129 UNREACHABLE();
130 }
131}
132
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100133Location ARM64ReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000134 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
135 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
136 // but we use the exact registers for clarity.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100137 if (return_type == DataType::Type::kFloat32) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000138 return LocationFrom(s0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100139 } else if (return_type == DataType::Type::kFloat64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000140 return LocationFrom(d0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100141 } else if (return_type == DataType::Type::kInt64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000142 return LocationFrom(x0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100143 } else if (return_type == DataType::Type::kVoid) {
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100144 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000145 } else {
146 return LocationFrom(w0);
147 }
148}
149
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100150Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000151 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100152}
153
Vladimir Marko3232dbb2018-07-25 15:42:46 +0100154static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
155 InvokeRuntimeCallingConvention calling_convention;
156 RegisterSet caller_saves = RegisterSet::Empty();
157 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
158 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
159 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
160 DataType::Type::kReference).GetCode());
161 return caller_saves;
162}
163
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100164// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
165#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700166#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100167
Zheng Xuda403092015-04-24 17:35:39 +0800168// Calculate memory accessing operand for save/restore live registers.
169static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100170 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800171 int64_t spill_offset,
172 bool is_save) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800173 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
174 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100175 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800176 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100177 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800178 codegen->GetNumberOfFloatingPointRegisters()));
179
Vladimir Marko804b03f2016-09-14 16:26:36 +0100180 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serovc8150b52019-07-31 18:28:00 +0100181 const unsigned v_reg_size_in_bits = codegen->GetSlowPathFPWidth() * 8;
Artem Serov1a719e42019-07-18 14:24:55 +0100182 DCHECK_LE(codegen->GetSIMDRegisterWidth(), kQRegSizeInBytes);
Artem Serovc8150b52019-07-31 18:28:00 +0100183 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size_in_bits, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800184
185 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
186 UseScratchRegisterScope temps(masm);
187
188 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100189 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
190 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800191 int64_t reg_size = kXRegSizeInBytes;
192 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
193 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100194 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800195 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
196 // If the offset does not fit in the instruction's immediate field, use an alternate register
197 // to compute the base address(float point registers spill base address).
198 Register new_base = temps.AcquireSameSizeAs(base);
199 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
200 base = new_base;
201 spill_offset = -core_spill_size;
202 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
203 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
204 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
205 }
206
207 if (is_save) {
208 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
209 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
210 } else {
211 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
212 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
213 }
214}
215
216void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800217 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Andreas Gampe3db70682018-12-26 15:12:03 -0800218 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100219 for (uint32_t i : LowToHighBits(core_spills)) {
220 // If the register holds an object, update the stack mask.
221 if (locations->RegisterContainsObject(i)) {
222 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800223 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100224 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
225 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
226 saved_core_stack_offsets_[i] = stack_offset;
227 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800228 }
229
Artem Serovc8150b52019-07-31 18:28:00 +0100230 const size_t fp_reg_size = codegen->GetSlowPathFPWidth();
Andreas Gampe3db70682018-12-26 15:12:03 -0800231 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100232 for (uint32_t i : LowToHighBits(fp_spills)) {
233 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
234 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
235 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serov9df37b92019-07-23 16:41:54 +0100236 stack_offset += fp_reg_size;
Zheng Xuda403092015-04-24 17:35:39 +0800237 }
238
Vladimir Marko804b03f2016-09-14 16:26:36 +0100239 SaveRestoreLiveRegistersHelper(codegen,
240 locations,
Andreas Gampe3db70682018-12-26 15:12:03 -0800241 codegen->GetFirstRegisterSlotInSlowPath(), /* is_save= */ true);
Zheng Xuda403092015-04-24 17:35:39 +0800242}
243
244void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100245 SaveRestoreLiveRegistersHelper(codegen,
246 locations,
Andreas Gampe3db70682018-12-26 15:12:03 -0800247 codegen->GetFirstRegisterSlotInSlowPath(), /* is_save= */ false);
Zheng Xuda403092015-04-24 17:35:39 +0800248}
249
Alexandre Rames5319def2014-10-23 10:03:10 +0100250class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
251 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000252 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100253
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100254 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100255 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000256 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100257
Alexandre Rames5319def2014-10-23 10:03:10 +0100258 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000259 if (instruction_->CanThrowIntoCatchBlock()) {
260 // Live registers will be restored in the catch block if caught.
261 SaveLiveRegisters(codegen, instruction_->GetLocations());
262 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000263 // We're moving two locations to locations that could overlap, so we need a parallel
264 // move resolver.
265 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100266 codegen->EmitParallelMoves(locations->InAt(0),
267 LocationFrom(calling_convention.GetRegisterAt(0)),
268 DataType::Type::kInt32,
269 locations->InAt(1),
270 LocationFrom(calling_convention.GetRegisterAt(1)),
271 DataType::Type::kInt32);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000272 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
273 ? kQuickThrowStringBounds
274 : kQuickThrowArrayBounds;
275 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100276 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800277 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100278 }
279
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100280 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100281
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100282 const char* GetDescription() const override { return "BoundsCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100283
Alexandre Rames5319def2014-10-23 10:03:10 +0100284 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100285 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
286};
287
Alexandre Rames67555f72014-11-18 10:55:16 +0000288class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
289 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000290 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000291
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100292 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000293 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
294 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000295 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800296 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000297 }
298
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100299 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100300
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100301 const char* GetDescription() const override { return "DivZeroCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100302
Alexandre Rames67555f72014-11-18 10:55:16 +0000303 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000304 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
305};
306
307class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
308 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100309 LoadClassSlowPathARM64(HLoadClass* cls, HInstruction* at)
310 : SlowPathCodeARM64(at), cls_(cls) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000311 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100312 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Alexandre Rames67555f72014-11-18 10:55:16 +0000313 }
314
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100315 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000316 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000317 Location out = locations->Out();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100318 const uint32_t dex_pc = instruction_->GetDexPc();
319 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
320 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
Alexandre Rames67555f72014-11-18 10:55:16 +0000321
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100322 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames67555f72014-11-18 10:55:16 +0000323 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000324 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000325
Vladimir Markof3c52b42017-11-17 17:32:12 +0000326 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100327 if (must_resolve_type) {
328 DCHECK(IsSameDexFile(cls_->GetDexFile(), arm64_codegen->GetGraph()->GetDexFile()));
329 dex::TypeIndex type_index = cls_->GetTypeIndex();
330 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Vladimir Marko8f63f102020-09-28 12:10:28 +0100331 if (cls_->NeedsAccessCheck()) {
332 CheckEntrypointTypes<kQuickResolveTypeAndVerifyAccess, void*, uint32_t>();
333 arm64_codegen->InvokeRuntime(kQuickResolveTypeAndVerifyAccess, instruction_, dex_pc, this);
334 } else {
335 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
336 arm64_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
337 }
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100338 // If we also must_do_clinit, the resolved type is now in the correct register.
339 } else {
340 DCHECK(must_do_clinit);
341 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
342 arm64_codegen->MoveLocation(LocationFrom(calling_convention.GetRegisterAt(0)),
343 source,
344 cls_->GetType());
345 }
346 if (must_do_clinit) {
347 arm64_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
348 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800349 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000350
351 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000352 if (out.IsValid()) {
353 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100354 DataType::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000355 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000356 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000357 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000358 __ B(GetExitLabel());
359 }
360
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100361 const char* GetDescription() const override { return "LoadClassSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100362
Alexandre Rames67555f72014-11-18 10:55:16 +0000363 private:
364 // The class this slow path will load.
365 HLoadClass* const cls_;
366
Alexandre Rames67555f72014-11-18 10:55:16 +0000367 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
368};
369
Vladimir Markoaad75c62016-10-03 08:46:48 +0000370class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
371 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000372 explicit LoadStringSlowPathARM64(HLoadString* instruction)
373 : SlowPathCodeARM64(instruction) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000374
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100375 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000376 LocationSummary* locations = instruction_->GetLocations();
377 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
378 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
379
380 __ Bind(GetEntryLabel());
381 SaveLiveRegisters(codegen, locations);
382
Vladimir Markof3c52b42017-11-17 17:32:12 +0000383 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000384 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
385 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000386 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
387 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100388 DataType::Type type = instruction_->GetType();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000389 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
390
391 RestoreLiveRegisters(codegen, locations);
392
Vladimir Markoaad75c62016-10-03 08:46:48 +0000393 __ B(GetExitLabel());
394 }
395
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100396 const char* GetDescription() const override { return "LoadStringSlowPathARM64"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000397
398 private:
399 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
400};
401
Alexandre Rames5319def2014-10-23 10:03:10 +0100402class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
403 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000404 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100405
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100406 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000407 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100408 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000409 if (instruction_->CanThrowIntoCatchBlock()) {
410 // Live registers will be restored in the catch block if caught.
411 SaveLiveRegisters(codegen, instruction_->GetLocations());
412 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000413 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
414 instruction_,
415 instruction_->GetDexPc(),
416 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800417 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100418 }
419
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100420 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100421
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100422 const char* GetDescription() const override { return "NullCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100423
Alexandre Rames5319def2014-10-23 10:03:10 +0100424 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100425 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
426};
427
428class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
429 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100430 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000431 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100432
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100433 void EmitNativeCode(CodeGenerator* codegen) override {
Artem Serov7957d952017-04-04 15:44:09 +0100434 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000435 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100436 __ Bind(GetEntryLabel());
Artem Serov1a719e42019-07-18 14:24:55 +0100437 SaveLiveRegisters(codegen, locations); // Only saves live vector regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000438 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800439 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov1a719e42019-07-18 14:24:55 +0100440 RestoreLiveRegisters(codegen, locations); // Only restores live vector regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000441 if (successor_ == nullptr) {
442 __ B(GetReturnLabel());
443 } else {
444 __ B(arm64_codegen->GetLabelOf(successor_));
445 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100446 }
447
Scott Wakeling97c72b72016-06-24 16:19:36 +0100448 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100449 DCHECK(successor_ == nullptr);
450 return &return_label_;
451 }
452
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100453 HBasicBlock* GetSuccessor() const {
454 return successor_;
455 }
456
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100457 const char* GetDescription() const override { return "SuspendCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100458
Alexandre Rames5319def2014-10-23 10:03:10 +0100459 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100460 // If not null, the block to branch to after the suspend check.
461 HBasicBlock* const successor_;
462
463 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100464 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100465
466 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
467};
468
Alexandre Rames67555f72014-11-18 10:55:16 +0000469class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
470 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000471 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000472 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000473
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100474 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000475 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800476
Alexandre Rames3e69f162014-12-10 10:36:50 +0000477 DCHECK(instruction_->IsCheckCast()
478 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
479 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100480 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000481
Alexandre Rames67555f72014-11-18 10:55:16 +0000482 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000483
Vladimir Marko87584542017-12-12 17:47:52 +0000484 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000485 SaveLiveRegisters(codegen, locations);
486 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000487
488 // We're moving two locations to locations that could overlap, so we need a parallel
489 // move resolver.
490 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800491 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800492 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100493 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800494 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800495 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100496 DataType::Type::kReference);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000497 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000498 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800499 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100500 DataType::Type ret_type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000501 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
502 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
503 } else {
504 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800505 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
506 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000507 }
508
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000509 if (!is_fatal_) {
510 RestoreLiveRegisters(codegen, locations);
511 __ B(GetExitLabel());
512 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000513 }
514
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100515 const char* GetDescription() const override { return "TypeCheckSlowPathARM64"; }
516 bool IsFatal() const override { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100517
Alexandre Rames67555f72014-11-18 10:55:16 +0000518 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000519 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000520
Alexandre Rames67555f72014-11-18 10:55:16 +0000521 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
522};
523
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700524class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
525 public:
Aart Bik42249c32016-01-07 15:33:50 -0800526 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000527 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700528
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100529 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bik42249c32016-01-07 15:33:50 -0800530 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700531 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100532 LocationSummary* locations = instruction_->GetLocations();
533 SaveLiveRegisters(codegen, locations);
534 InvokeRuntimeCallingConvention calling_convention;
535 __ Mov(calling_convention.GetRegisterAt(0),
536 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000537 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100538 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700539 }
540
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100541 const char* GetDescription() const override { return "DeoptimizationSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100542
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700543 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700544 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
545};
546
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100547class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
548 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000549 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100550
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100551 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100552 LocationSummary* locations = instruction_->GetLocations();
553 __ Bind(GetEntryLabel());
554 SaveLiveRegisters(codegen, locations);
555
556 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100557 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100558 parallel_move.AddMove(
559 locations->InAt(0),
560 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100561 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100562 nullptr);
563 parallel_move.AddMove(
564 locations->InAt(1),
565 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100566 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100567 nullptr);
568 parallel_move.AddMove(
569 locations->InAt(2),
570 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100571 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100572 nullptr);
573 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
574
575 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000576 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100577 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
578 RestoreLiveRegisters(codegen, locations);
579 __ B(GetExitLabel());
580 }
581
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100582 const char* GetDescription() const override { return "ArraySetSlowPathARM64"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100583
584 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100585 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
586};
587
Zheng Xu3927c8b2015-11-18 17:46:25 +0800588void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
589 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000590 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800591
592 // We are about to use the assembler to place literals directly. Make sure we have enough
593 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000594 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
595 num_entries * sizeof(int32_t),
596 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800597
598 __ Bind(&table_start_);
599 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
600 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100601 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800602 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100603 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800604 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
605 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
606 Literal<int32_t> literal(jump_offset);
607 __ place(&literal);
608 }
609}
610
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000611// Slow path generating a read barrier for a heap reference.
612class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
613 public:
614 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
615 Location out,
616 Location ref,
617 Location obj,
618 uint32_t offset,
619 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000620 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000621 out_(out),
622 ref_(ref),
623 obj_(obj),
624 offset_(offset),
625 index_(index) {
626 DCHECK(kEmitCompilerReadBarrier);
627 // If `obj` is equal to `out` or `ref`, it means the initial object
628 // has been overwritten by (or after) the heap object reference load
629 // to be instrumented, e.g.:
630 //
631 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000632 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000633 //
634 // In that case, we have lost the information about the original
635 // object, and the emitted read barrier cannot work properly.
636 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
637 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
638 }
639
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100640 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000641 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
642 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100643 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000644 DCHECK(locations->CanCall());
645 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100646 DCHECK(instruction_->IsInstanceFieldGet() ||
647 instruction_->IsStaticFieldGet() ||
648 instruction_->IsArrayGet() ||
649 instruction_->IsInstanceOf() ||
650 instruction_->IsCheckCast() ||
Vladimir Markoa41ea272020-09-07 15:24:36 +0000651 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000652 << "Unexpected instruction in read barrier for heap reference slow path: "
653 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000654 // The read barrier instrumentation of object ArrayGet
655 // instructions does not support the HIntermediateAddress
656 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000657 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100658 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000659
660 __ Bind(GetEntryLabel());
661
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000662 SaveLiveRegisters(codegen, locations);
663
664 // We may have to change the index's value, but as `index_` is a
665 // constant member (like other "inputs" of this slow path),
666 // introduce a copy of it, `index`.
667 Location index = index_;
668 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100669 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000670 if (instruction_->IsArrayGet()) {
671 // Compute the actual memory offset and store it in `index`.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100672 Register index_reg = RegisterFrom(index_, DataType::Type::kInt32);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000673 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
674 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
675 // We are about to change the value of `index_reg` (see the
676 // calls to vixl::MacroAssembler::Lsl and
677 // vixl::MacroAssembler::Mov below), but it has
678 // not been saved by the previous call to
679 // art::SlowPathCode::SaveLiveRegisters, as it is a
680 // callee-save register --
681 // art::SlowPathCode::SaveLiveRegisters does not consider
682 // callee-save registers, as it has been designed with the
683 // assumption that callee-save registers are supposed to be
684 // handled by the called function. So, as a callee-save
685 // register, `index_reg` _would_ eventually be saved onto
686 // the stack, but it would be too late: we would have
687 // changed its value earlier. Therefore, we manually save
688 // it here into another freely available register,
689 // `free_reg`, chosen of course among the caller-save
690 // registers (as a callee-save `free_reg` register would
691 // exhibit the same problem).
692 //
693 // Note we could have requested a temporary register from
694 // the register allocator instead; but we prefer not to, as
695 // this is a slow path, and we know we can find a
696 // caller-save register that is available.
697 Register free_reg = FindAvailableCallerSaveRegister(codegen);
698 __ Mov(free_reg.W(), index_reg);
699 index_reg = free_reg;
700 index = LocationFrom(index_reg);
701 } else {
702 // The initial register stored in `index_` has already been
703 // saved in the call to art::SlowPathCode::SaveLiveRegisters
704 // (as it is not a callee-save register), so we can freely
705 // use it.
706 }
707 // Shifting the index value contained in `index_reg` by the scale
708 // factor (2) cannot overflow in practice, as the runtime is
709 // unable to allocate object arrays with a size larger than
710 // 2^26 - 1 (that is, 2^28 - 4 bytes).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100711 __ Lsl(index_reg, index_reg, DataType::SizeShift(type));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000712 static_assert(
713 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
714 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
715 __ Add(index_reg, index_reg, Operand(offset_));
716 } else {
Vladimir Markoa41ea272020-09-07 15:24:36 +0000717 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile/VarHandleGet
Roland Levillain3d312422016-06-23 13:53:42 +0100718 // intrinsics, `index_` is not shifted by a scale factor of 2
719 // (as in the case of ArrayGet), as it is actually an offset
720 // to an object field within an object.
721 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000722 DCHECK(instruction_->GetLocations()->Intrinsified());
Vladimir Marko2d98dc22020-10-01 11:21:37 +0000723 Intrinsics intrinsic = instruction_->AsInvoke()->GetIntrinsic();
724 DCHECK(intrinsic == Intrinsics::kUnsafeGetObject ||
725 intrinsic == Intrinsics::kUnsafeGetObjectVolatile ||
726 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
727 mirror::VarHandle::AccessModeTemplate::kGet)
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000728 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100729 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100730 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000731 }
732 }
733
734 // We're moving two or three locations to locations that could
735 // overlap, so we need a parallel move resolver.
736 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100737 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000738 parallel_move.AddMove(ref_,
739 LocationFrom(calling_convention.GetRegisterAt(0)),
740 type,
741 nullptr);
742 parallel_move.AddMove(obj_,
743 LocationFrom(calling_convention.GetRegisterAt(1)),
744 type,
745 nullptr);
746 if (index.IsValid()) {
747 parallel_move.AddMove(index,
748 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100749 DataType::Type::kInt32,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000750 nullptr);
751 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
752 } else {
753 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
754 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
755 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000756 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000757 instruction_,
758 instruction_->GetDexPc(),
759 this);
760 CheckEntrypointTypes<
761 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
762 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
763
764 RestoreLiveRegisters(codegen, locations);
765
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000766 __ B(GetExitLabel());
767 }
768
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100769 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000770
771 private:
772 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100773 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
774 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000775 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
776 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
777 return Register(VIXLRegCodeFromART(i), kXRegSize);
778 }
779 }
780 // We shall never fail to find a free caller-save register, as
781 // there are more than two core caller-save registers on ARM64
782 // (meaning it is possible to find one which is different from
783 // `ref` and `obj`).
784 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
785 LOG(FATAL) << "Could not find a free register";
786 UNREACHABLE();
787 }
788
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000789 const Location out_;
790 const Location ref_;
791 const Location obj_;
792 const uint32_t offset_;
793 // An additional location containing an index to an array.
794 // Only used for HArrayGet and the UnsafeGetObject &
795 // UnsafeGetObjectVolatile intrinsics.
796 const Location index_;
797
798 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
799};
800
801// Slow path generating a read barrier for a GC root.
802class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
803 public:
804 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000805 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000806 DCHECK(kEmitCompilerReadBarrier);
807 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000808
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100809 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000810 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100811 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000812 DCHECK(locations->CanCall());
813 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Vladimir Markoa41ea272020-09-07 15:24:36 +0000814 DCHECK(instruction_->IsLoadClass() ||
815 instruction_->IsLoadString() ||
816 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000817 << "Unexpected instruction in read barrier for GC root slow path: "
818 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000819
820 __ Bind(GetEntryLabel());
821 SaveLiveRegisters(codegen, locations);
822
823 InvokeRuntimeCallingConvention calling_convention;
824 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
825 // The argument of the ReadBarrierForRootSlow is not a managed
826 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
827 // thus we need a 64-bit move here, and we cannot use
828 //
829 // arm64_codegen->MoveLocation(
830 // LocationFrom(calling_convention.GetRegisterAt(0)),
831 // root_,
832 // type);
833 //
834 // which would emit a 32-bit move, as `type` is a (32-bit wide)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100835 // reference type (`DataType::Type::kReference`).
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000836 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000837 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000838 instruction_,
839 instruction_->GetDexPc(),
840 this);
841 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
842 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
843
844 RestoreLiveRegisters(codegen, locations);
845 __ B(GetExitLabel());
846 }
847
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100848 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000849
850 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000851 const Location out_;
852 const Location root_;
853
854 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
855};
856
Alexandre Rames5319def2014-10-23 10:03:10 +0100857#undef __
858
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100859Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100860 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100861 if (type == DataType::Type::kVoid) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100862 LOG(FATAL) << "Unreachable type " << type;
863 }
864
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100865 if (DataType::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100866 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
867 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100868 } else if (!DataType::IsFloatingPointType(type) &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000869 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000870 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
871 } else {
872 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100873 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
874 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100875 }
876
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000877 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100878 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100879 return next_location;
880}
881
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100882Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100883 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100884}
885
Vladimir Marko86c87522020-05-11 16:55:55 +0100886Location CriticalNativeCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
887 DCHECK_NE(type, DataType::Type::kReference);
888
889 Location location = Location::NoLocation();
890 if (DataType::IsFloatingPointType(type)) {
891 if (fpr_index_ < kParameterFPRegistersLength) {
892 location = LocationFrom(kParameterFPRegisters[fpr_index_]);
893 ++fpr_index_;
894 }
895 } else {
896 // Native ABI uses the same registers as managed, except that the method register x0
897 // is a normal argument.
898 if (gpr_index_ < 1u + kParameterCoreRegistersLength) {
899 location = LocationFrom(gpr_index_ == 0u ? x0 : kParameterCoreRegisters[gpr_index_ - 1u]);
900 ++gpr_index_;
901 }
902 }
903 if (location.IsInvalid()) {
904 if (DataType::Is64BitType(type)) {
905 location = Location::DoubleStackSlot(stack_offset_);
906 } else {
907 location = Location::StackSlot(stack_offset_);
908 }
909 stack_offset_ += kFramePointerSize;
910
911 if (for_register_allocation_) {
912 location = Location::Any();
913 }
914 }
915 return location;
916}
917
918Location CriticalNativeCallingConventionVisitorARM64::GetReturnLocation(DataType::Type type) const {
919 // We perform conversion to the managed ABI return register after the call if needed.
920 InvokeDexCallingConventionVisitorARM64 dex_calling_convention;
921 return dex_calling_convention.GetReturnLocation(type);
922}
923
924Location CriticalNativeCallingConventionVisitorARM64::GetMethodLocation() const {
925 // Pass the method in the hidden argument x15.
926 return Location::RegisterLocation(x15.GetCode());
927}
928
Serban Constantinescu579885a2015-02-22 20:51:33 +0000929CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100930 const CompilerOptions& compiler_options,
931 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100932 : CodeGenerator(graph,
933 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000934 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000935 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100936 callee_saved_core_registers.GetList(),
937 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100938 compiler_options,
939 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100940 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
941 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov1a719e42019-07-18 14:24:55 +0100942 location_builder_neon_(graph, this),
943 instruction_visitor_neon_(graph, this),
944 location_builder_sve_(graph, this),
945 instruction_visitor_sve_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100946 move_resolver_(graph->GetAllocator(), this),
Artem Serovaa6f4832018-11-21 18:57:54 +0000947 assembler_(graph->GetAllocator(),
948 compiler_options.GetInstructionSetFeatures()->AsArm64InstructionSetFeatures()),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000949 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100950 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000951 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100952 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko8f63f102020-09-28 12:10:28 +0100953 public_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
954 package_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000955 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100956 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +0100957 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100958 call_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100959 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100960 uint32_literals_(std::less<uint32_t>(),
961 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
962 uint64_literals_(std::less<uint64_t>(),
963 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000964 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100965 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000966 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +0000967 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
968 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
969 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000970 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000971 AddAllocatedRegister(LocationFrom(lr));
Artem Serov1a719e42019-07-18 14:24:55 +0100972
973 bool use_sve = ShouldUseSVE();
974 if (use_sve) {
975 location_builder_ = &location_builder_sve_;
976 instruction_visitor_ = &instruction_visitor_sve_;
977 } else {
978 location_builder_ = &location_builder_neon_;
979 instruction_visitor_ = &instruction_visitor_neon_;
980 }
981}
982
983bool CodeGeneratorARM64::ShouldUseSVE() const {
984 return kArm64AllowSVE && GetInstructionSetFeatures().HasSVE();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000985}
Alexandre Rames5319def2014-10-23 10:03:10 +0100986
Alexandre Rames67555f72014-11-18 10:55:16 +0000987#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100988
Zheng Xu3927c8b2015-11-18 17:46:25 +0800989void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100990 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800991 jump_table->EmitTable(this);
992 }
993}
994
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000995void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800996 EmitJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +0000997
998 // Emit JIT baker read barrier slow paths.
Vladimir Marko695348f2020-05-19 14:42:02 +0100999 DCHECK(GetCompilerOptions().IsJitCompiler() || jit_baker_read_barrier_slow_paths_.empty());
Vladimir Marko966b46f2018-08-03 10:20:19 +00001000 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
1001 uint32_t encoded_data = entry.first;
1002 vixl::aarch64::Label* slow_path_entry = &entry.second.label;
1003 __ Bind(slow_path_entry);
Andreas Gampe3db70682018-12-26 15:12:03 -08001004 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name= */ nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00001005 }
1006
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001007 // Ensure we emit the literal pool.
1008 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001009
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001010 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001011
1012 // Verify Baker read barrier linker patches.
1013 if (kIsDebugBuild) {
1014 ArrayRef<const uint8_t> code = allocator->GetMemory();
1015 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
1016 DCHECK(info.label.IsBound());
1017 uint32_t literal_offset = info.label.GetLocation();
1018 DCHECK_ALIGNED(literal_offset, 4u);
1019
1020 auto GetInsn = [&code](uint32_t offset) {
1021 DCHECK_ALIGNED(offset, 4u);
1022 return
1023 (static_cast<uint32_t>(code[offset + 0]) << 0) +
1024 (static_cast<uint32_t>(code[offset + 1]) << 8) +
1025 (static_cast<uint32_t>(code[offset + 2]) << 16)+
1026 (static_cast<uint32_t>(code[offset + 3]) << 24);
1027 };
1028
1029 const uint32_t encoded_data = info.custom_data;
1030 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
1031 // Check that the next instruction matches the expected LDR.
1032 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01001033 case BakerReadBarrierKind::kField:
1034 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00001035 DCHECK_GE(code.size() - literal_offset, 8u);
1036 uint32_t next_insn = GetInsn(literal_offset + 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001037 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1038 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko0ecac682018-08-07 10:40:38 +01001039 if (kind == BakerReadBarrierKind::kField) {
1040 // LDR (immediate) with correct base_reg.
1041 CHECK_EQ(next_insn & 0xffc003e0u, 0xb9400000u | (base_reg << 5));
1042 } else {
1043 DCHECK(kind == BakerReadBarrierKind::kAcquire);
1044 // LDAR with correct base_reg.
1045 CHECK_EQ(next_insn & 0xffffffe0u, 0x88dffc00u | (base_reg << 5));
1046 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001047 break;
1048 }
1049 case BakerReadBarrierKind::kArray: {
1050 DCHECK_GE(code.size() - literal_offset, 8u);
1051 uint32_t next_insn = GetInsn(literal_offset + 4u);
1052 // LDR (register) with the correct base_reg, size=10 (32-bit), option=011 (extend = LSL),
1053 // and S=1 (shift amount = 2 for 32-bit version), i.e. LDR Wt, [Xn, Xm, LSL #2].
1054 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1055 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1056 CHECK_EQ(next_insn & 0xffe0ffe0u, 0xb8607800u | (base_reg << 5));
1057 CheckValidReg((next_insn >> 16) & 0x1f); // Check index register
1058 break;
1059 }
1060 case BakerReadBarrierKind::kGcRoot: {
1061 DCHECK_GE(literal_offset, 4u);
1062 uint32_t prev_insn = GetInsn(literal_offset - 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001063 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko94796f82018-08-08 15:15:33 +01001064 // Usually LDR (immediate) with correct root_reg but
1065 // we may have a "MOV marked, old_value" for UnsafeCASObject.
1066 if ((prev_insn & 0xffe0ffff) != (0x2a0003e0 | root_reg)) { // MOV?
1067 CHECK_EQ(prev_insn & 0xffc0001fu, 0xb9400000u | root_reg); // LDR?
1068 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001069 break;
1070 }
1071 default:
1072 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
1073 UNREACHABLE();
1074 }
1075 }
1076 }
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001077}
1078
Zheng Xuad4450e2015-04-17 18:48:56 +08001079void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1080 // Note: There are 6 kinds of moves:
1081 // 1. constant -> GPR/FPR (non-cycle)
1082 // 2. constant -> stack (non-cycle)
1083 // 3. GPR/FPR -> GPR/FPR
1084 // 4. GPR/FPR -> stack
1085 // 5. stack -> GPR/FPR
1086 // 6. stack -> stack (non-cycle)
1087 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1088 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1089 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1090 // dependency.
1091 vixl_temps_.Open(GetVIXLAssembler());
1092}
1093
1094void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1095 vixl_temps_.Close();
1096}
1097
1098Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001099 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1100 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1101 || kind == Location::kSIMDStackSlot);
1102 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1103 ? Location::kFpuRegister
1104 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001105 Location scratch = GetScratchLocation(kind);
1106 if (!scratch.Equals(Location::NoLocation())) {
1107 return scratch;
1108 }
1109 // Allocate from VIXL temp registers.
1110 if (kind == Location::kRegister) {
1111 scratch = LocationFrom(vixl_temps_.AcquireX());
1112 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001113 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serov1a719e42019-07-18 14:24:55 +01001114 scratch = codegen_->GetGraph()->HasSIMD()
1115 ? codegen_->GetInstructionCodeGeneratorArm64()->AllocateSIMDScratchLocation(&vixl_temps_)
1116 : LocationFrom(vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001117 }
1118 AddScratchLocation(scratch);
1119 return scratch;
1120}
1121
1122void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1123 if (loc.IsRegister()) {
1124 vixl_temps_.Release(XRegisterFrom(loc));
1125 } else {
1126 DCHECK(loc.IsFpuRegister());
Artem Serov1a719e42019-07-18 14:24:55 +01001127 if (codegen_->GetGraph()->HasSIMD()) {
1128 codegen_->GetInstructionCodeGeneratorArm64()->FreeSIMDScratchLocation(loc, &vixl_temps_);
1129 } else {
1130 vixl_temps_.Release(DRegisterFrom(loc));
1131 }
Zheng Xuad4450e2015-04-17 18:48:56 +08001132 }
1133 RemoveScratchLocation(loc);
1134}
1135
Alexandre Rames3e69f162014-12-10 10:36:50 +00001136void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001137 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001138 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001139}
1140
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001141void CodeGeneratorARM64::MaybeIncrementHotness(bool is_frame_entry) {
1142 MacroAssembler* masm = GetVIXLAssembler();
1143 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1144 UseScratchRegisterScope temps(masm);
1145 Register counter = temps.AcquireX();
1146 Register method = is_frame_entry ? kArtMethodRegister : temps.AcquireX();
1147 if (!is_frame_entry) {
1148 __ Ldr(method, MemOperand(sp, 0));
1149 }
1150 __ Ldrh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
1151 __ Add(counter, counter, 1);
1152 // Subtract one if the counter would overflow.
1153 __ Sub(counter, counter, Operand(counter, LSR, 16));
1154 __ Strh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
1155 }
1156
1157 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffray095dc462020-08-17 16:40:28 +01001158 ScopedProfilingInfoUse spiu(
1159 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
1160 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001161 if (info != nullptr) {
Nicolas Geoffrayc1cd1332020-01-25 13:08:24 +00001162 uint64_t address = reinterpret_cast64<uint64_t>(info);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001163 vixl::aarch64::Label done;
1164 UseScratchRegisterScope temps(masm);
1165 Register temp = temps.AcquireX();
1166 Register counter = temps.AcquireW();
1167 __ Mov(temp, address);
1168 __ Ldrh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
1169 __ Add(counter, counter, 1);
Nicolas Geoffray4313ccb2020-08-26 17:01:15 +01001170 __ And(counter, counter, interpreter::kTieredHotnessMask);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001171 __ Strh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
Nicolas Geoffray4313ccb2020-08-26 17:01:15 +01001172 __ Cbnz(counter, &done);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001173 if (is_frame_entry) {
1174 if (HasEmptyFrame()) {
Vladimir Markodec78172020-06-19 15:31:23 +01001175 // The entrypoint expects the method at the bottom of the stack. We
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001176 // claim stack space necessary for alignment.
Vladimir Markodec78172020-06-19 15:31:23 +01001177 IncreaseFrame(kStackAlignment);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001178 __ Stp(kArtMethodRegister, lr, MemOperand(sp, 0));
1179 } else if (!RequiresCurrentMethod()) {
1180 __ Str(kArtMethodRegister, MemOperand(sp, 0));
1181 }
1182 } else {
1183 CHECK(RequiresCurrentMethod());
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001184 }
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001185 uint32_t entrypoint_offset =
1186 GetThreadOffset<kArm64PointerSize>(kQuickCompileOptimized).Int32Value();
1187 __ Ldr(lr, MemOperand(tr, entrypoint_offset));
1188 // Note: we don't record the call here (and therefore don't generate a stack
1189 // map), as the entrypoint should never be suspended.
1190 __ Blr(lr);
1191 if (HasEmptyFrame()) {
1192 CHECK(is_frame_entry);
1193 __ Ldr(lr, MemOperand(sp, 8));
Vladimir Markodec78172020-06-19 15:31:23 +01001194 DecreaseFrame(kStackAlignment);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001195 }
1196 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001197 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001198 }
1199}
1200
Alexandre Rames5319def2014-10-23 10:03:10 +01001201void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001202 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001203 __ Bind(&frame_entry_label_);
1204
Vladimir Marko33bff252017-11-01 14:35:42 +00001205 bool do_overflow_check =
1206 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001207 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001208 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001209 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001210 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001211 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001212 {
1213 // Ensure that between load and RecordPcInfo there are no pools emitted.
1214 ExactAssemblyScope eas(GetVIXLAssembler(),
1215 kInstructionSize,
1216 CodeBufferCheckScope::kExactSize);
1217 __ ldr(wzr, MemOperand(temp, 0));
1218 RecordPcInfo(nullptr, 0);
1219 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001220 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001221
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001222 if (!HasEmptyFrame()) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001223 // Stack layout:
1224 // sp[frame_size - 8] : lr.
1225 // ... : other preserved core registers.
1226 // ... : other preserved fp registers.
1227 // ... : reserved frame space.
1228 // sp[0] : current method.
Vladimir Marko1a225a72019-07-05 13:37:42 +01001229 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1230 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1231 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1232 DCHECK(!preserved_core_registers.IsEmpty());
1233 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1234 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001235
Vladimir Marko1a225a72019-07-05 13:37:42 +01001236 // Save the current method if we need it, or if using STP reduces code
1237 // size. Note that we do not do this in HCurrentMethod, as the
1238 // instruction might have been removed in the SSA graph.
1239 CPURegister lowest_spill;
1240 if (core_spills_offset == kXRegSizeInBytes) {
1241 // If there is no gap between the method and the lowest core spill, use
1242 // aligned STP pre-index to store both. Max difference is 512. We do
1243 // that to reduce code size even if we do not have to save the method.
1244 DCHECK_LE(frame_size, 512); // 32 core registers are only 256 bytes.
1245 lowest_spill = preserved_core_registers.PopLowestIndex();
1246 __ Stp(kArtMethodRegister, lowest_spill, MemOperand(sp, -frame_size, PreIndex));
1247 } else if (RequiresCurrentMethod()) {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001248 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001249 } else {
1250 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001251 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001252 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Vladimir Marko1a225a72019-07-05 13:37:42 +01001253 if (lowest_spill.IsValid()) {
1254 GetAssembler()->cfi().RelOffset(DWARFReg(lowest_spill), core_spills_offset);
1255 core_spills_offset += kXRegSizeInBytes;
1256 }
1257 GetAssembler()->SpillRegisters(preserved_core_registers, core_spills_offset);
1258 GetAssembler()->SpillRegisters(preserved_fp_registers, fp_spills_offset);
Mingyao Yang063fc772016-08-02 11:02:54 -07001259
1260 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1261 // Initialize should_deoptimize flag to 0.
1262 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1263 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1264 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001265 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001266 MaybeIncrementHotness(/* is_frame_entry= */ true);
Andreas Gampe3db70682018-12-26 15:12:03 -08001267 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001268}
1269
1270void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001271 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001272 if (!HasEmptyFrame()) {
Vladimir Marko1a225a72019-07-05 13:37:42 +01001273 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1274 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1275 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1276 DCHECK(!preserved_core_registers.IsEmpty());
1277 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1278 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
1279
1280 CPURegister lowest_spill;
1281 if (core_spills_offset == kXRegSizeInBytes) {
1282 // If there is no gap between the method and the lowest core spill, use
1283 // aligned LDP pre-index to pop both. Max difference is 504. We do
1284 // that to reduce code size even though the loaded method is unused.
1285 DCHECK_LE(frame_size, 504); // 32 core registers are only 256 bytes.
1286 lowest_spill = preserved_core_registers.PopLowestIndex();
1287 core_spills_offset += kXRegSizeInBytes;
1288 }
1289 GetAssembler()->UnspillRegisters(preserved_fp_registers, fp_spills_offset);
1290 GetAssembler()->UnspillRegisters(preserved_core_registers, core_spills_offset);
1291 if (lowest_spill.IsValid()) {
1292 __ Ldp(xzr, lowest_spill, MemOperand(sp, frame_size, PostIndex));
1293 GetAssembler()->cfi().Restore(DWARFReg(lowest_spill));
1294 } else {
1295 __ Drop(frame_size);
1296 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001297 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001298 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001299 __ Ret();
1300 GetAssembler()->cfi().RestoreState();
1301 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001302}
1303
Scott Wakeling97c72b72016-06-24 16:19:36 +01001304CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001305 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001306 return CPURegList(CPURegister::kRegister, kXRegSize,
1307 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001308}
1309
Scott Wakeling97c72b72016-06-24 16:19:36 +01001310CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001311 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1312 GetNumberOfFloatingPointRegisters()));
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001313 return CPURegList(CPURegister::kVRegister, kDRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001314 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001315}
1316
Alexandre Rames5319def2014-10-23 10:03:10 +01001317void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1318 __ Bind(GetLabelOf(block));
1319}
1320
Calin Juravle175dc732015-08-25 15:42:32 +01001321void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1322 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001323 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001324}
1325
Calin Juravlee460d1d2015-09-29 04:52:17 +01001326void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1327 if (location.IsRegister()) {
1328 locations->AddTemp(location);
1329 } else {
1330 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1331 }
1332}
1333
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001334void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001335 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001336 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001337 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001338 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001339 if (value_can_be_null) {
1340 __ Cbz(value, &done);
1341 }
Roland Levillainc73f0522018-08-14 15:16:50 +01001342 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07001343 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01001344 // Calculate the offset (in the card table) of the card corresponding to
1345 // `object`.
Alexandre Rames5319def2014-10-23 10:03:10 +01001346 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Roland Levillainc73f0522018-08-14 15:16:50 +01001347 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
1348 // `object`'s card.
1349 //
1350 // Register `card` contains the address of the card table. Note that the card
1351 // table's base is biased during its creation so that it always starts at an
1352 // address whose least-significant byte is equal to `kCardDirty` (see
1353 // art::gc::accounting::CardTable::Create). Therefore the STRB instruction
1354 // below writes the `kCardDirty` (byte) value into the `object`'s card
1355 // (located at `card + object >> kCardShift`).
1356 //
1357 // This dual use of the value in register `card` (1. to calculate the location
1358 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
1359 // (no need to explicitly load `kCardDirty` as an immediate value).
Serban Constantinescu02164b32014-11-13 14:05:07 +00001360 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001361 if (value_can_be_null) {
1362 __ Bind(&done);
1363 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001364}
1365
David Brazdil58282f42016-01-14 12:45:10 +00001366void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001367 // Blocked core registers:
1368 // lr : Runtime reserved.
1369 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001370 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001371 // ip1 : VIXL core temp.
1372 // ip0 : VIXL core temp.
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001373 // x18 : Platform register.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001374 //
1375 // Blocked fp registers:
1376 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001377 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1378 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001379 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001380 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001381 }
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001382 blocked_core_registers_[X18] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001383
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001384 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001385 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001386 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001387 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001388
David Brazdil58282f42016-01-14 12:45:10 +00001389 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001390 // Stubs do not save callee-save floating point registers. If the graph
1391 // is debuggable, we need to deal with these registers differently. For
1392 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001393 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1394 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001395 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001396 }
1397 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001398}
1399
Alexandre Rames3e69f162014-12-10 10:36:50 +00001400size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1401 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1402 __ Str(reg, MemOperand(sp, stack_index));
1403 return kArm64WordSize;
1404}
1405
1406size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1407 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1408 __ Ldr(reg, MemOperand(sp, stack_index));
1409 return kArm64WordSize;
1410}
1411
Artem Serov9df37b92019-07-23 16:41:54 +01001412size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1413 uint32_t reg_id ATTRIBUTE_UNUSED) {
1414 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1415 << "use SaveRestoreLiveRegistersHelper";
1416 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001417}
1418
Artem Serov9df37b92019-07-23 16:41:54 +01001419size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1420 uint32_t reg_id ATTRIBUTE_UNUSED) {
1421 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1422 << "use SaveRestoreLiveRegistersHelper";
1423 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001424}
1425
Alexandre Rames5319def2014-10-23 10:03:10 +01001426void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001427 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001428}
1429
1430void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001431 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001432}
1433
Vladimir Markoa0431112018-06-25 09:32:54 +01001434const Arm64InstructionSetFeatures& CodeGeneratorARM64::GetInstructionSetFeatures() const {
1435 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArm64InstructionSetFeatures();
1436}
1437
Alexandre Rames67555f72014-11-18 10:55:16 +00001438void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001439 if (constant->IsIntConstant()) {
1440 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1441 } else if (constant->IsLongConstant()) {
1442 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1443 } else if (constant->IsNullConstant()) {
1444 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001445 } else if (constant->IsFloatConstant()) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001446 __ Fmov(VRegister(destination), constant->AsFloatConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001447 } else {
1448 DCHECK(constant->IsDoubleConstant());
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001449 __ Fmov(VRegister(destination), constant->AsDoubleConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001450 }
1451}
1452
Alexandre Rames3e69f162014-12-10 10:36:50 +00001453
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001454static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001455 DCHECK(constant.IsConstant());
1456 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001457 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001458 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001459 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1460 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1461 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1462 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001463}
1464
Roland Levillain952b2352017-05-03 19:49:14 +01001465// Allocate a scratch register from the VIXL pool, querying first
1466// the floating-point register pool, and then the core register
1467// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001468// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1469// using a different allocation strategy.
1470static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1471 vixl::aarch64::UseScratchRegisterScope* temps,
1472 int size_in_bits) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001473 return masm->GetScratchVRegisterList()->IsEmpty()
Roland Levillain558dea12017-01-27 19:40:44 +00001474 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1475 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1476}
1477
Calin Juravlee460d1d2015-09-29 04:52:17 +01001478void CodeGeneratorARM64::MoveLocation(Location destination,
1479 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001480 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001481 if (source.Equals(destination)) {
1482 return;
1483 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001484
1485 // A valid move can always be inferred from the destination and source
1486 // locations. When moving from and to a register, the argument type can be
1487 // used to generate 32bit instead of 64bit moves. In debug mode we also
1488 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001489 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001490
1491 if (destination.IsRegister() || destination.IsFpuRegister()) {
1492 if (unspecified_type) {
1493 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1494 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001495 (src_cst != nullptr && (src_cst->IsIntConstant()
1496 || src_cst->IsFloatConstant()
1497 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001498 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001499 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001500 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001501 // If the source is a double stack slot or a 64bit constant, a 64bit
1502 // type is appropriate. Else the source is a register, and since the
1503 // type has not been specified, we chose a 64bit type to force a 64bit
1504 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001505 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001506 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001507 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001508 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1509 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001510 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001511 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1512 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1513 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001514 } else if (source.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001515 GetInstructionCodeGeneratorArm64()->LoadSIMDRegFromStack(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001516 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001517 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001518 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001519 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001520 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001521 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001522 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001523 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001524 DataType::Type source_type = DataType::Is64BitType(dst_type)
1525 ? DataType::Type::kInt64
1526 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001527 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1528 }
1529 } else {
1530 DCHECK(source.IsFpuRegister());
1531 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001532 DataType::Type source_type = DataType::Is64BitType(dst_type)
1533 ? DataType::Type::kFloat64
1534 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001535 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1536 } else {
1537 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001538 if (GetGraph()->HasSIMD()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001539 GetInstructionCodeGeneratorArm64()->MoveSIMDRegToSIMDReg(destination, source);
Artem Serovd4bccf12017-04-03 18:47:32 +01001540 } else {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001541 __ Fmov(VRegister(dst), FPRegisterFrom(source, dst_type));
Artem Serovd4bccf12017-04-03 18:47:32 +01001542 }
1543 }
1544 }
1545 } else if (destination.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001546 GetInstructionCodeGeneratorArm64()->MoveToSIMDStackSlot(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001547 } else { // The destination is not a register. It must be a stack slot.
1548 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1549 if (source.IsRegister() || source.IsFpuRegister()) {
1550 if (unspecified_type) {
1551 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001552 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001553 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001554 dst_type =
1555 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001556 }
1557 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001558 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1559 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001560 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001561 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001562 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1563 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001564 UseScratchRegisterScope temps(GetVIXLAssembler());
1565 HConstant* src_cst = source.GetConstant();
1566 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001567 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001568 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1569 ? Register(xzr)
1570 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001571 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001572 if (src_cst->IsIntConstant()) {
1573 temp = temps.AcquireW();
1574 } else if (src_cst->IsLongConstant()) {
1575 temp = temps.AcquireX();
1576 } else if (src_cst->IsFloatConstant()) {
1577 temp = temps.AcquireS();
1578 } else {
1579 DCHECK(src_cst->IsDoubleConstant());
1580 temp = temps.AcquireD();
1581 }
1582 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001583 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001584 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001585 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001586 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001587 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001588 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001589 // Use any scratch register (a core or a floating-point one)
1590 // from VIXL scratch register pools as a temporary.
1591 //
1592 // We used to only use the FP scratch register pool, but in some
1593 // rare cases the only register from this pool (D31) would
1594 // already be used (e.g. within a ParallelMove instruction, when
1595 // a move is blocked by a another move requiring a scratch FP
1596 // register, which would reserve D31). To prevent this issue, we
1597 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001598 //
1599 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001600 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001601 // use AcquireFPOrCoreCPURegisterOfSize instead of
1602 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1603 // allocates core scratch registers first.
1604 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1605 GetVIXLAssembler(),
1606 &temps,
1607 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001608 __ Ldr(temp, StackOperandFrom(source));
1609 __ Str(temp, StackOperandFrom(destination));
1610 }
1611 }
1612}
1613
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001614void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001615 CPURegister dst,
1616 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001617 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001618 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001619 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001620 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001621 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001622 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001623 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001624 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001625 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001626 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001627 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001628 case DataType::Type::kInt16:
1629 __ Ldrsh(Register(dst), src);
1630 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001631 case DataType::Type::kInt32:
1632 case DataType::Type::kReference:
1633 case DataType::Type::kInt64:
1634 case DataType::Type::kFloat32:
1635 case DataType::Type::kFloat64:
1636 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001637 __ Ldr(dst, src);
1638 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001639 case DataType::Type::kUint32:
1640 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001641 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001642 LOG(FATAL) << "Unreachable type " << type;
1643 }
1644}
1645
Calin Juravle77520bc2015-01-12 18:45:46 +00001646void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001647 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001648 const MemOperand& src,
1649 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001650 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001651 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001652 Register temp_base = temps.AcquireX();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001653 DataType::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001654
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001655 DCHECK(!src.IsPreIndex());
1656 DCHECK(!src.IsPostIndex());
1657
1658 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001659 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001660 {
1661 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1662 MemOperand base = MemOperand(temp_base);
1663 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001664 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001665 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001666 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001667 {
1668 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1669 __ ldarb(Register(dst), base);
1670 if (needs_null_check) {
1671 MaybeRecordImplicitNullCheck(instruction);
1672 }
1673 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001674 if (type == DataType::Type::kInt8) {
1675 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001676 }
1677 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001678 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001679 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001680 {
1681 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1682 __ ldarh(Register(dst), base);
1683 if (needs_null_check) {
1684 MaybeRecordImplicitNullCheck(instruction);
1685 }
1686 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001687 if (type == DataType::Type::kInt16) {
1688 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
1689 }
Artem Serov914d7a82017-02-07 14:33:49 +00001690 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001691 case DataType::Type::kInt32:
1692 case DataType::Type::kReference:
1693 case DataType::Type::kInt64:
1694 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001695 {
1696 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1697 __ ldar(Register(dst), base);
1698 if (needs_null_check) {
1699 MaybeRecordImplicitNullCheck(instruction);
1700 }
1701 }
1702 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001703 case DataType::Type::kFloat32:
1704 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00001705 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001706 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001707
Artem Serov914d7a82017-02-07 14:33:49 +00001708 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1709 {
1710 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1711 __ ldar(temp, base);
1712 if (needs_null_check) {
1713 MaybeRecordImplicitNullCheck(instruction);
1714 }
1715 }
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001716 __ Fmov(VRegister(dst), temp);
Artem Serov914d7a82017-02-07 14:33:49 +00001717 break;
Roland Levillain44015862016-01-22 11:47:17 +00001718 }
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:
Artem Serov914d7a82017-02-07 14:33:49 +00001722 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001723 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001724 }
1725}
1726
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001727void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001728 CPURegister src,
1729 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001730 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001731 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001732 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001733 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001734 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001735 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001736 case DataType::Type::kUint16:
1737 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001738 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001739 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001740 case DataType::Type::kInt32:
1741 case DataType::Type::kReference:
1742 case DataType::Type::kInt64:
1743 case DataType::Type::kFloat32:
1744 case DataType::Type::kFloat64:
1745 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001746 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001747 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001748 case DataType::Type::kUint32:
1749 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001750 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001751 LOG(FATAL) << "Unreachable type " << type;
1752 }
1753}
1754
Artem Serov914d7a82017-02-07 14:33:49 +00001755void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001756 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001757 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00001758 const MemOperand& dst,
1759 bool needs_null_check) {
1760 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001761 UseScratchRegisterScope temps(GetVIXLAssembler());
1762 Register temp_base = temps.AcquireX();
1763
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001764 DCHECK(!dst.IsPreIndex());
1765 DCHECK(!dst.IsPostIndex());
1766
1767 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001768 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001769 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001770 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00001771 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001772 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001773 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001774 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001775 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001776 {
1777 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1778 __ stlrb(Register(src), base);
1779 if (needs_null_check) {
1780 MaybeRecordImplicitNullCheck(instruction);
1781 }
1782 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001783 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001784 case DataType::Type::kUint16:
1785 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001786 {
1787 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1788 __ stlrh(Register(src), base);
1789 if (needs_null_check) {
1790 MaybeRecordImplicitNullCheck(instruction);
1791 }
1792 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001793 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001794 case DataType::Type::kInt32:
1795 case DataType::Type::kReference:
1796 case DataType::Type::kInt64:
1797 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001798 {
1799 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1800 __ stlr(Register(src), base);
1801 if (needs_null_check) {
1802 MaybeRecordImplicitNullCheck(instruction);
1803 }
1804 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001805 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001806 case DataType::Type::kFloat32:
1807 case DataType::Type::kFloat64: {
1808 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001809 Register temp_src;
1810 if (src.IsZero()) {
1811 // The zero register is used to avoid synthesizing zero constants.
1812 temp_src = Register(src);
1813 } else {
1814 DCHECK(src.IsFPRegister());
1815 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001816 __ Fmov(temp_src, VRegister(src));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001817 }
Artem Serov914d7a82017-02-07 14:33:49 +00001818 {
1819 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1820 __ stlr(temp_src, base);
1821 if (needs_null_check) {
1822 MaybeRecordImplicitNullCheck(instruction);
1823 }
1824 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001825 break;
1826 }
Aart Bik66c158e2018-01-31 12:55:04 -08001827 case DataType::Type::kUint32:
1828 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001829 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001830 LOG(FATAL) << "Unreachable type " << type;
1831 }
1832}
1833
Calin Juravle175dc732015-08-25 15:42:32 +01001834void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1835 HInstruction* instruction,
1836 uint32_t dex_pc,
1837 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001838 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00001839
Vladimir Markof6675082019-05-17 12:05:28 +01001840 ThreadOffset64 entrypoint_offset = GetThreadOffset<kArm64PointerSize>(entrypoint);
1841 // Reduce code size for AOT by using shared trampolines for slow path runtime calls across the
1842 // entire oat file. This adds an extra branch and we do not want to slow down the main path.
1843 // For JIT, thunk sharing is per-method, so the gains would be smaller or even negative.
Vladimir Marko695348f2020-05-19 14:42:02 +01001844 if (slow_path == nullptr || GetCompilerOptions().IsJitCompiler()) {
Vladimir Markof6675082019-05-17 12:05:28 +01001845 __ Ldr(lr, MemOperand(tr, entrypoint_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00001846 // Ensure the pc position is recorded immediately after the `blr` instruction.
1847 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1848 __ blr(lr);
1849 if (EntrypointRequiresStackMap(entrypoint)) {
1850 RecordPcInfo(instruction, dex_pc, slow_path);
1851 }
Vladimir Markof6675082019-05-17 12:05:28 +01001852 } else {
1853 // Ensure the pc position is recorded immediately after the `bl` instruction.
1854 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1855 EmitEntrypointThunkCall(entrypoint_offset);
1856 if (EntrypointRequiresStackMap(entrypoint)) {
1857 RecordPcInfo(instruction, dex_pc, slow_path);
1858 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001859 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001860}
1861
Roland Levillaindec8f632016-07-22 17:10:06 +01001862void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1863 HInstruction* instruction,
1864 SlowPathCode* slow_path) {
1865 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01001866 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1867 __ Blr(lr);
1868}
1869
Alexandre Rames67555f72014-11-18 10:55:16 +00001870void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001871 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001872 UseScratchRegisterScope temps(GetVIXLAssembler());
1873 Register temp = temps.AcquireW();
Vladimir Markodc682aa2018-01-04 18:42:57 +00001874 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001875 const size_t status_byte_offset =
1876 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1877 constexpr uint32_t shifted_visibly_initialized_value =
1878 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001879
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001880 // CMP (immediate) is limited to imm12 or imm12<<12, so we would need to materialize
1881 // the constant 0xf0000000 for comparison with the full 32-bit field. To reduce the code
1882 // size, load only the high byte of the field and compare with 0xf0.
1883 // Note: The same code size could be achieved with LDR+MNV(asr #24)+CBNZ but benchmarks
1884 // show that this pattern is slower (tested on little cores).
1885 __ Ldrb(temp, HeapOperand(class_reg, status_byte_offset));
1886 __ Cmp(temp, shifted_visibly_initialized_value);
1887 __ B(lo, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001888 __ Bind(slow_path->GetExitLabel());
1889}
Alexandre Rames5319def2014-10-23 10:03:10 +01001890
Vladimir Marko175e7862018-03-27 09:03:13 +00001891void InstructionCodeGeneratorARM64::GenerateBitstringTypeCheckCompare(
1892 HTypeCheckInstruction* check, vixl::aarch64::Register temp) {
1893 uint32_t path_to_root = check->GetBitstringPathToRoot();
1894 uint32_t mask = check->GetBitstringMask();
1895 DCHECK(IsPowerOfTwo(mask + 1));
1896 size_t mask_bits = WhichPowerOf2(mask + 1);
1897
1898 if (mask_bits == 16u) {
1899 // Load only the bitstring part of the status word.
1900 __ Ldrh(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1901 } else {
1902 // /* uint32_t */ temp = temp->status_
1903 __ Ldr(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1904 // Extract the bitstring bits.
1905 __ Ubfx(temp, temp, 0, mask_bits);
1906 }
1907 // Compare the bitstring bits to `path_to_root`.
1908 __ Cmp(temp, path_to_root);
1909}
1910
Roland Levillain44015862016-01-22 11:47:17 +00001911void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001912 BarrierType type = BarrierAll;
1913
1914 switch (kind) {
1915 case MemBarrierKind::kAnyAny:
1916 case MemBarrierKind::kAnyStore: {
1917 type = BarrierAll;
1918 break;
1919 }
1920 case MemBarrierKind::kLoadAny: {
1921 type = BarrierReads;
1922 break;
1923 }
1924 case MemBarrierKind::kStoreStore: {
1925 type = BarrierWrites;
1926 break;
1927 }
1928 default:
1929 LOG(FATAL) << "Unexpected memory barrier " << kind;
1930 }
1931 __ Dmb(InnerShareable, type);
1932}
1933
Serban Constantinescu02164b32014-11-13 14:05:07 +00001934void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1935 HBasicBlock* successor) {
1936 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001937 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1938 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001939 slow_path =
1940 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001941 instruction->SetSlowPath(slow_path);
1942 codegen_->AddSlowPath(slow_path);
1943 if (successor != nullptr) {
1944 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001945 }
1946 } else {
1947 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1948 }
1949
Serban Constantinescu02164b32014-11-13 14:05:07 +00001950 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1951 Register temp = temps.AcquireW();
1952
Andreas Gampe542451c2016-07-26 09:02:02 -07001953 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001954 if (successor == nullptr) {
1955 __ Cbnz(temp, slow_path->GetEntryLabel());
1956 __ Bind(slow_path->GetReturnLabel());
1957 } else {
1958 __ Cbz(temp, codegen_->GetLabelOf(successor));
1959 __ B(slow_path->GetEntryLabel());
1960 // slow_path will return to GetLabelOf(successor).
1961 }
1962}
1963
Alexandre Rames5319def2014-10-23 10:03:10 +01001964InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1965 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001966 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001967 assembler_(codegen->GetAssembler()),
1968 codegen_(codegen) {}
1969
Alexandre Rames67555f72014-11-18 10:55:16 +00001970void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001971 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001972 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001973 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001974 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001975 case DataType::Type::kInt32:
1976 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01001977 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001978 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001979 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001980 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001981
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001982 case DataType::Type::kFloat32:
1983 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001984 locations->SetInAt(0, Location::RequiresFpuRegister());
1985 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001986 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001987 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001988
Alexandre Rames5319def2014-10-23 10:03:10 +01001989 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001990 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001991 }
1992}
1993
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001994void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
1995 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001996 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1997
1998 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001999 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01002000 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002001 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2002 object_field_get_with_read_barrier
2003 ? LocationSummary::kCallOnSlowPath
2004 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002005 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002006 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko0ecac682018-08-07 10:40:38 +01002007 // We need a temporary register for the read barrier load in
2008 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2009 // only if the field is volatile or the offset is too big.
2010 if (field_info.IsVolatile() ||
2011 field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2012 locations->AddTemp(FixedTempLocation());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002013 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002014 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002015 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002016 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002017 locations->SetOut(Location::RequiresFpuRegister());
2018 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002019 // The output overlaps for an object field get when read barriers
2020 // are enabled: we do not want the load to overwrite the object's
2021 // location, as we need it to emit the read barrier.
2022 locations->SetOut(
2023 Location::RequiresRegister(),
2024 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002025 }
2026}
2027
2028void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2029 const FieldInfo& field_info) {
2030 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002031 LocationSummary* locations = instruction->GetLocations();
2032 Location base_loc = locations->InAt(0);
2033 Location out = locations->Out();
2034 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01002035 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
2036 DataType::Type load_type = instruction->GetType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002037 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002038
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002039 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01002040 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002041 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002042 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002043 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002044 Location maybe_temp =
2045 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002046 // Note that potential implicit null checks are handled in this
2047 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2048 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2049 instruction,
2050 out,
2051 base,
2052 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002053 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002054 /* needs_null_check= */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002055 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002056 } else {
2057 // General case.
2058 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002059 // Note that a potential implicit null check is handled in this
2060 // CodeGeneratorARM64::LoadAcquire call.
2061 // NB: LoadAcquire will record the pc info if needed.
2062 codegen_->LoadAcquire(
Andreas Gampe3db70682018-12-26 15:12:03 -08002063 instruction, OutputCPURegister(instruction), field, /* needs_null_check= */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002064 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002065 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2066 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01002067 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002068 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002069 }
Vladimir Marko61b92282017-10-11 13:23:17 +01002070 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002071 // If read barriers are enabled, emit read barriers other than
2072 // Baker's using a slow path (and also unpoison the loaded
2073 // reference, if heap poisoning is enabled).
2074 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2075 }
Roland Levillain4d027112015-07-01 15:41:14 +01002076 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002077}
2078
2079void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2080 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002081 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01002082 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002083 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2084 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002085 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002086 locations->SetInAt(1, Location::RequiresFpuRegister());
2087 } else {
2088 locations->SetInAt(1, Location::RequiresRegister());
2089 }
2090}
2091
2092void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002093 const FieldInfo& field_info,
2094 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002095 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2096
2097 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002098 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002099 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002100 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002101 DataType::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002102
Roland Levillain4d027112015-07-01 15:41:14 +01002103 {
2104 // We use a block to end the scratch scope before the write barrier, thus
2105 // freeing the temporary registers so they can be used in `MarkGCCard`.
2106 UseScratchRegisterScope temps(GetVIXLAssembler());
2107
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002108 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002109 DCHECK(value.IsW());
2110 Register temp = temps.AcquireW();
2111 __ Mov(temp, value.W());
2112 GetAssembler()->PoisonHeapReference(temp.W());
2113 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002114 }
Roland Levillain4d027112015-07-01 15:41:14 +01002115
2116 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002117 codegen_->StoreRelease(
Andreas Gampe3db70682018-12-26 15:12:03 -08002118 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check= */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002119 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002120 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2121 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002122 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2123 codegen_->MaybeRecordImplicitNullCheck(instruction);
2124 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002125 }
2126
2127 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002128 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002129 }
2130}
2131
Alexandre Rames67555f72014-11-18 10:55:16 +00002132void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002133 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002134
2135 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002136 case DataType::Type::kInt32:
2137 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002138 Register dst = OutputRegister(instr);
2139 Register lhs = InputRegisterAt(instr, 0);
2140 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002141 if (instr->IsAdd()) {
2142 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002143 } else if (instr->IsAnd()) {
2144 __ And(dst, lhs, rhs);
2145 } else if (instr->IsOr()) {
2146 __ Orr(dst, lhs, rhs);
2147 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002148 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002149 } else if (instr->IsRor()) {
2150 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002151 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002152 __ Ror(dst, lhs, shift);
2153 } else {
2154 // Ensure shift distance is in the same size register as the result. If
2155 // we are rotating a long and the shift comes in a w register originally,
2156 // we don't need to sxtw for use as an x since the shift distances are
2157 // all & reg_bits - 1.
2158 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2159 }
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002160 } else if (instr->IsMin() || instr->IsMax()) {
2161 __ Cmp(lhs, rhs);
2162 __ Csel(dst, lhs, rhs, instr->IsMin() ? lt : gt);
Alexandre Rames67555f72014-11-18 10:55:16 +00002163 } else {
2164 DCHECK(instr->IsXor());
2165 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002166 }
2167 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002168 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002169 case DataType::Type::kFloat32:
2170 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002171 VRegister dst = OutputFPRegister(instr);
2172 VRegister lhs = InputFPRegisterAt(instr, 0);
2173 VRegister rhs = InputFPRegisterAt(instr, 1);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002174 if (instr->IsAdd()) {
2175 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002176 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002177 __ Fsub(dst, lhs, rhs);
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002178 } else if (instr->IsMin()) {
2179 __ Fmin(dst, lhs, rhs);
2180 } else if (instr->IsMax()) {
2181 __ Fmax(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002182 } else {
2183 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002184 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002185 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002186 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002187 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002188 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002189 }
2190}
2191
Serban Constantinescu02164b32014-11-13 14:05:07 +00002192void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2193 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2194
Vladimir Markoca6fff82017-10-03 14:49:14 +01002195 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002196 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002197 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002198 case DataType::Type::kInt32:
2199 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002200 locations->SetInAt(0, Location::RequiresRegister());
2201 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002202 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002203 break;
2204 }
2205 default:
2206 LOG(FATAL) << "Unexpected shift type " << type;
2207 }
2208}
2209
2210void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2211 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2212
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002213 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002214 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002215 case DataType::Type::kInt32:
2216 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002217 Register dst = OutputRegister(instr);
2218 Register lhs = InputRegisterAt(instr, 0);
2219 Operand rhs = InputOperandAt(instr, 1);
2220 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002221 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002222 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002223 if (instr->IsShl()) {
2224 __ Lsl(dst, lhs, shift_value);
2225 } else if (instr->IsShr()) {
2226 __ Asr(dst, lhs, shift_value);
2227 } else {
2228 __ Lsr(dst, lhs, shift_value);
2229 }
2230 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002231 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002232
2233 if (instr->IsShl()) {
2234 __ Lsl(dst, lhs, rhs_reg);
2235 } else if (instr->IsShr()) {
2236 __ Asr(dst, lhs, rhs_reg);
2237 } else {
2238 __ Lsr(dst, lhs, rhs_reg);
2239 }
2240 }
2241 break;
2242 }
2243 default:
2244 LOG(FATAL) << "Unexpected shift operation type " << type;
2245 }
2246}
2247
Alexandre Rames5319def2014-10-23 10:03:10 +01002248void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002249 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002250}
2251
2252void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002253 HandleBinaryOp(instruction);
2254}
2255
2256void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2257 HandleBinaryOp(instruction);
2258}
2259
2260void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2261 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002262}
2263
Artem Serov7fc63502016-02-09 17:15:29 +00002264void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002265 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002266 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002267 locations->SetInAt(0, Location::RequiresRegister());
2268 // There is no immediate variant of negated bitwise instructions in AArch64.
2269 locations->SetInAt(1, Location::RequiresRegister());
2270 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2271}
2272
Artem Serov7fc63502016-02-09 17:15:29 +00002273void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002274 Register dst = OutputRegister(instr);
2275 Register lhs = InputRegisterAt(instr, 0);
2276 Register rhs = InputRegisterAt(instr, 1);
2277
2278 switch (instr->GetOpKind()) {
2279 case HInstruction::kAnd:
2280 __ Bic(dst, lhs, rhs);
2281 break;
2282 case HInstruction::kOr:
2283 __ Orn(dst, lhs, rhs);
2284 break;
2285 case HInstruction::kXor:
2286 __ Eon(dst, lhs, rhs);
2287 break;
2288 default:
2289 LOG(FATAL) << "Unreachable";
2290 }
2291}
2292
Anton Kirilov74234da2017-01-13 14:42:47 +00002293void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2294 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002295 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2296 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002297 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002298 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002299 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2300 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2301 } else {
2302 locations->SetInAt(0, Location::RequiresRegister());
2303 }
2304 locations->SetInAt(1, Location::RequiresRegister());
2305 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2306}
2307
Anton Kirilov74234da2017-01-13 14:42:47 +00002308void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2309 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002310 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002311 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002312 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002313 Register out = OutputRegister(instruction);
2314 Register left;
2315 if (kind != HInstruction::kNeg) {
2316 left = InputRegisterAt(instruction, 0);
2317 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002318 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002319 // shifter operand operation, the IR generating `right_reg` (input to the type
2320 // conversion) can have a different type from the current instruction's type,
2321 // so we manually indicate the type.
2322 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002323 Operand right_operand(0);
2324
Anton Kirilov74234da2017-01-13 14:42:47 +00002325 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2326 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002327 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2328 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002329 right_operand = Operand(right_reg,
2330 helpers::ShiftFromOpKind(op_kind),
2331 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002332 }
2333
2334 // Logical binary operations do not support extension operations in the
2335 // operand. Note that VIXL would still manage if it was passed by generating
2336 // the extension as a separate instruction.
2337 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2338 DCHECK(!right_operand.IsExtendedRegister() ||
2339 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2340 kind != HInstruction::kNeg));
2341 switch (kind) {
2342 case HInstruction::kAdd:
2343 __ Add(out, left, right_operand);
2344 break;
2345 case HInstruction::kAnd:
2346 __ And(out, left, right_operand);
2347 break;
2348 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002349 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002350 __ Neg(out, right_operand);
2351 break;
2352 case HInstruction::kOr:
2353 __ Orr(out, left, right_operand);
2354 break;
2355 case HInstruction::kSub:
2356 __ Sub(out, left, right_operand);
2357 break;
2358 case HInstruction::kXor:
2359 __ Eor(out, left, right_operand);
2360 break;
2361 default:
2362 LOG(FATAL) << "Unexpected operation kind: " << kind;
2363 UNREACHABLE();
2364 }
2365}
2366
Artem Serov328429f2016-07-06 16:23:04 +01002367void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002368 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002369 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002370 locations->SetInAt(0, Location::RequiresRegister());
2371 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002372 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002373}
2374
Roland Levillain19c54192016-11-04 13:44:09 +00002375void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002376 __ Add(OutputRegister(instruction),
2377 InputRegisterAt(instruction, 0),
2378 Operand(InputOperandAt(instruction, 1)));
2379}
2380
Artem Serove1811ed2017-04-27 16:50:47 +01002381void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2382 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002383 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002384
2385 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2386
2387 locations->SetInAt(0, Location::RequiresRegister());
2388 // For byte case we don't need to shift the index variable so we can encode the data offset into
2389 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2390 // data offset constant generation out of the loop and reduce the critical path length in the
2391 // loop.
2392 locations->SetInAt(1, shift->GetValue() == 0
2393 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2394 : Location::RequiresRegister());
2395 locations->SetInAt(2, Location::ConstantLocation(shift));
2396 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2397}
2398
2399void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2400 HIntermediateAddressIndex* instruction) {
2401 Register index_reg = InputRegisterAt(instruction, 0);
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002402 uint32_t shift = Int64FromLocation(instruction->GetLocations()->InAt(2));
Artem Serove1811ed2017-04-27 16:50:47 +01002403 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2404
2405 if (shift == 0) {
2406 __ Add(OutputRegister(instruction), index_reg, offset);
2407 } else {
2408 Register offset_reg = InputRegisterAt(instruction, 1);
2409 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2410 }
2411}
2412
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002413void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002414 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002415 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002416 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2417 if (instr->GetOpKind() == HInstruction::kSub &&
2418 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002419 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002420 // Don't allocate register for Mneg instruction.
2421 } else {
2422 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2423 Location::RequiresRegister());
2424 }
2425 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2426 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002427 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2428}
2429
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002430void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002431 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002432 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2433 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002434
2435 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2436 // This fixup should be carried out for all multiply-accumulate instructions:
2437 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002438 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002439 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2440 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002441 vixl::aarch64::Instruction* prev =
2442 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002443 if (prev->IsLoadOrStore()) {
2444 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002445 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002446 __ nop();
2447 }
2448 }
2449
2450 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002451 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002452 __ Madd(res, mul_left, mul_right, accumulator);
2453 } else {
2454 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002455 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002456 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002457 __ Mneg(res, mul_left, mul_right);
2458 } else {
2459 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2460 __ Msub(res, mul_left, mul_right, accumulator);
2461 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002462 }
2463}
2464
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002465void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002466 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002467 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002468 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002469 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2470 object_array_get_with_read_barrier
2471 ? LocationSummary::kCallOnSlowPath
2472 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002473 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002474 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002475 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002476 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002477 // We need a temporary register for the read barrier load in
2478 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2479 // only if the offset is too big.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002480 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2481 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002482 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002483 if (offset >= kReferenceLoadMinFarOffset) {
2484 locations->AddTemp(FixedTempLocation());
2485 }
Artem Serov0806f582018-10-11 20:14:20 +01002486 } else if (!instruction->GetArray()->IsIntermediateAddress()) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01002487 // We need a non-scratch temporary for the array data pointer in
Artem Serov0806f582018-10-11 20:14:20 +01002488 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier() for the case with no
2489 // intermediate address.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002490 locations->AddTemp(Location::RequiresRegister());
2491 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002492 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002493 locations->SetInAt(0, Location::RequiresRegister());
2494 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002495 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002496 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2497 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002498 // The output overlaps in the case of an object array get with
2499 // read barriers enabled: we do not want the move to overwrite the
2500 // array's location, as we need it to emit the read barrier.
2501 locations->SetOut(
2502 Location::RequiresRegister(),
2503 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002504 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002505}
2506
2507void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002508 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002509 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002510 LocationSummary* locations = instruction->GetLocations();
2511 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002512 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002513 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002514 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2515 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002516 MacroAssembler* masm = GetVIXLAssembler();
2517 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002518
Artem Serov0806f582018-10-11 20:14:20 +01002519 // The non-Baker read barrier instrumentation of object ArrayGet instructions
Roland Levillain19c54192016-11-04 13:44:09 +00002520 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002521 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002522 instruction->GetArray()->IsIntermediateAddress() &&
Artem Serov0806f582018-10-11 20:14:20 +01002523 kEmitCompilerReadBarrier &&
2524 !kUseBakerReadBarrier));
Roland Levillain19c54192016-11-04 13:44:09 +00002525
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002526 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002527 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002528 // Note that a potential implicit null check is handled in the
2529 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002530 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002531 if (index.IsConstant()) {
Artem Serov0806f582018-10-11 20:14:20 +01002532 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002533 // Array load with a constant index can be treated as a field load.
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002534 offset += Int64FromLocation(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002535 Location maybe_temp =
2536 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2537 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2538 out,
2539 obj.W(),
2540 offset,
2541 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002542 /* needs_null_check= */ false,
2543 /* use_load_acquire= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002544 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002545 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08002546 instruction, out, obj.W(), offset, index, /* needs_null_check= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002547 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002548 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002549 // General case.
2550 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002551 Register length;
2552 if (maybe_compressed_char_at) {
2553 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2554 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002555 {
2556 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2557 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2558
2559 if (instruction->GetArray()->IsIntermediateAddress()) {
2560 DCHECK_LT(count_offset, offset);
2561 int64_t adjusted_offset =
2562 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2563 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2564 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2565 } else {
2566 __ Ldr(length, HeapOperand(obj, count_offset));
2567 }
2568 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002569 }
jessicahandojo05765752016-09-09 19:01:32 -07002570 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002571 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002572 if (maybe_compressed_char_at) {
2573 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002574 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2575 "Expecting 0=compressed, 1=uncompressed");
2576 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002577 __ Ldrb(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002578 HeapOperand(obj, offset + Int64FromLocation(index)));
jessicahandojo05765752016-09-09 19:01:32 -07002579 __ B(&done);
2580 __ Bind(&uncompressed_load);
2581 __ Ldrh(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002582 HeapOperand(obj, offset + (Int64FromLocation(index) << 1)));
jessicahandojo05765752016-09-09 19:01:32 -07002583 __ Bind(&done);
2584 } else {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002585 offset += Int64FromLocation(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002586 source = HeapOperand(obj, offset);
2587 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002588 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002589 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002590 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002591 // We do not need to compute the intermediate address from the array: the
2592 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002593 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002594 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002595 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2596 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
Roland Levillain44015862016-01-22 11:47:17 +00002597 }
2598 temp = obj;
2599 } else {
2600 __ Add(temp, obj, offset);
2601 }
jessicahandojo05765752016-09-09 19:01:32 -07002602 if (maybe_compressed_char_at) {
2603 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002604 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2605 "Expecting 0=compressed, 1=uncompressed");
2606 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002607 __ Ldrb(Register(OutputCPURegister(instruction)),
2608 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2609 __ B(&done);
2610 __ Bind(&uncompressed_load);
2611 __ Ldrh(Register(OutputCPURegister(instruction)),
2612 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2613 __ Bind(&done);
2614 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002615 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002616 }
Roland Levillain44015862016-01-22 11:47:17 +00002617 }
jessicahandojo05765752016-09-09 19:01:32 -07002618 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002619 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2620 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002621 codegen_->Load(type, OutputCPURegister(instruction), source);
2622 codegen_->MaybeRecordImplicitNullCheck(instruction);
2623 }
Roland Levillain44015862016-01-22 11:47:17 +00002624
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002625 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002626 static_assert(
2627 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2628 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2629 Location obj_loc = locations->InAt(0);
2630 if (index.IsConstant()) {
2631 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2632 } else {
2633 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2634 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002635 }
Roland Levillain4d027112015-07-01 15:41:14 +01002636 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002637}
2638
Alexandre Rames5319def2014-10-23 10:03:10 +01002639void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002640 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002641 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002642 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002643}
2644
2645void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002646 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002647 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002648 {
2649 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2650 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2651 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2652 codegen_->MaybeRecordImplicitNullCheck(instruction);
2653 }
jessicahandojo05765752016-09-09 19:01:32 -07002654 // Mask out compression flag from String's array length.
2655 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002656 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002657 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002658}
2659
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002660void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002661 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002662
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002663 bool needs_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002664 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002665 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002666 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002667 locations->SetInAt(0, Location::RequiresRegister());
2668 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002669 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2670 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002671 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002672 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002673 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002674 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002675 }
2676}
2677
2678void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002679 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002680 LocationSummary* locations = instruction->GetLocations();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002681 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002682 bool needs_write_barrier =
2683 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002684
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002685 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002686 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002687 CPURegister source = value;
2688 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002689 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002690 MemOperand destination = HeapOperand(array);
2691 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002692
2693 if (!needs_write_barrier) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002694 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002695 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002696 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002697 destination = HeapOperand(array, offset);
2698 } else {
2699 UseScratchRegisterScope temps(masm);
2700 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002701 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002702 // We do not need to compute the intermediate address from the array: the
2703 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002704 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002705 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002706 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2707 DCHECK(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002708 }
2709 temp = array;
2710 } else {
2711 __ Add(temp, array, offset);
2712 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002713 destination = HeapOperand(temp,
2714 XRegisterFrom(index),
2715 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002716 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002717 }
Artem Serov914d7a82017-02-07 14:33:49 +00002718 {
2719 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2720 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2721 codegen_->Store(value_type, value, destination);
2722 codegen_->MaybeRecordImplicitNullCheck(instruction);
2723 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002724 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002725 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002726
2727 bool can_value_be_null = instruction->GetValueCanBeNull();
2728 vixl::aarch64::Label do_store;
2729 if (can_value_be_null) {
2730 __ Cbz(Register(value), &do_store);
2731 }
2732
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002733 SlowPathCodeARM64* slow_path = nullptr;
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002734 if (needs_type_check) {
2735 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
2736 codegen_->AddSlowPath(slow_path);
2737
2738 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2739 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2740 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2741
Alexandre Rames97833a02015-04-16 15:07:12 +01002742 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002743 Register temp = temps.AcquireSameSizeAs(array);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002744 Register temp2 = temps.AcquireSameSizeAs(array);
2745
2746 // Note that when Baker read barriers are enabled, the type
2747 // checks are performed without read barriers. This is fine,
2748 // even in the case where a class object is in the from-space
2749 // after the flip, as a comparison involving such a type would
2750 // not produce a false positive; it may of course produce a
2751 // false negative, in which case we would take the ArraySet
2752 // slow path.
2753
2754 // /* HeapReference<Class> */ temp = array->klass_
2755 {
2756 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2757 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2758 __ Ldr(temp, HeapOperand(array, class_offset));
2759 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames97833a02015-04-16 15:07:12 +01002760 }
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002761 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Alexandre Rames97833a02015-04-16 15:07:12 +01002762
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002763 // /* HeapReference<Class> */ temp = temp->component_type_
2764 __ Ldr(temp, HeapOperand(temp, component_offset));
2765 // /* HeapReference<Class> */ temp2 = value->klass_
2766 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2767 // If heap poisoning is enabled, no need to unpoison `temp`
2768 // nor `temp2`, as we are comparing two poisoned references.
2769 __ Cmp(temp, temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002770
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002771 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2772 vixl::aarch64::Label do_put;
2773 __ B(eq, &do_put);
2774 // If heap poisoning is enabled, the `temp` reference has
2775 // not been unpoisoned yet; unpoison it now.
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002776 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002777
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002778 // /* HeapReference<Class> */ temp = temp->super_class_
2779 __ Ldr(temp, HeapOperand(temp, super_offset));
2780 // If heap poisoning is enabled, no need to unpoison
2781 // `temp`, as we are comparing against null below.
2782 __ Cbnz(temp, slow_path->GetEntryLabel());
2783 __ Bind(&do_put);
Vladimir Markod1ef8732017-04-18 13:55:13 +01002784 } else {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002785 __ B(ne, slow_path->GetEntryLabel());
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002786 }
2787 }
2788
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002789 codegen_->MarkGCCard(array, value.W(), /* value_can_be_null= */ false);
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002790
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002791 if (can_value_be_null) {
2792 DCHECK(do_store.IsLinked());
2793 __ Bind(&do_store);
2794 }
2795
2796 UseScratchRegisterScope temps(masm);
2797 if (kPoisonHeapReferences) {
2798 Register temp_source = temps.AcquireSameSizeAs(array);
2799 DCHECK(value.IsW());
2800 __ Mov(temp_source, value.W());
2801 GetAssembler()->PoisonHeapReference(temp_source);
2802 source = temp_source;
2803 }
2804
2805 if (index.IsConstant()) {
2806 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
2807 destination = HeapOperand(array, offset);
2808 } else {
2809 Register temp_base = temps.AcquireSameSizeAs(array);
2810 __ Add(temp_base, array, offset);
2811 destination = HeapOperand(temp_base,
2812 XRegisterFrom(index),
2813 LSL,
2814 DataType::SizeShift(value_type));
2815 }
2816
2817 {
2818 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2819 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2820 __ Str(source, destination);
2821
2822 if (can_value_be_null || !needs_type_check) {
2823 codegen_->MaybeRecordImplicitNullCheck(instruction);
2824 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002825 }
2826
2827 if (slow_path != nullptr) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002828 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002829 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002830 }
2831}
2832
Alexandre Rames67555f72014-11-18 10:55:16 +00002833void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002834 RegisterSet caller_saves = RegisterSet::Empty();
2835 InvokeRuntimeCallingConvention calling_convention;
2836 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2837 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2838 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002839
2840 // If both index and length are constant, we can check the bounds statically and
2841 // generate code accordingly. We want to make sure we generate constant locations
2842 // in that case, regardless of whether they are encodable in the comparison or not.
2843 HInstruction* index = instruction->InputAt(0);
2844 HInstruction* length = instruction->InputAt(1);
2845 bool both_const = index->IsConstant() && length->IsConstant();
2846 locations->SetInAt(0, both_const
2847 ? Location::ConstantLocation(index->AsConstant())
2848 : ARM64EncodableConstantOrRegister(index, instruction));
2849 locations->SetInAt(1, both_const
2850 ? Location::ConstantLocation(length->AsConstant())
2851 : ARM64EncodableConstantOrRegister(length, instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002852}
2853
2854void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Georgia Kouvelibe530852019-01-17 10:46:41 +00002855 LocationSummary* locations = instruction->GetLocations();
2856 Location index_loc = locations->InAt(0);
2857 Location length_loc = locations->InAt(1);
2858
2859 int cmp_first_input = 0;
2860 int cmp_second_input = 1;
2861 Condition cond = hs;
2862
2863 if (index_loc.IsConstant()) {
2864 int64_t index = Int64FromLocation(index_loc);
2865 if (length_loc.IsConstant()) {
2866 int64_t length = Int64FromLocation(length_loc);
2867 if (index < 0 || index >= length) {
2868 BoundsCheckSlowPathARM64* slow_path =
2869 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
2870 codegen_->AddSlowPath(slow_path);
2871 __ B(slow_path->GetEntryLabel());
2872 } else {
2873 // BCE will remove the bounds check if we are guaranteed to pass.
2874 // However, some optimization after BCE may have generated this, and we should not
2875 // generate a bounds check if it is a valid range.
2876 }
2877 return;
2878 }
2879 // Only the index is constant: change the order of the operands and commute the condition
2880 // so we can use an immediate constant for the index (only the second input to a cmp
2881 // instruction can be an immediate).
2882 cmp_first_input = 1;
2883 cmp_second_input = 0;
2884 cond = ls;
2885 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002886 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002887 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002888 __ Cmp(InputRegisterAt(instruction, cmp_first_input),
2889 InputOperandAt(instruction, cmp_second_input));
Alexandre Rames67555f72014-11-18 10:55:16 +00002890 codegen_->AddSlowPath(slow_path);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002891 __ B(slow_path->GetEntryLabel(), cond);
Alexandre Rames67555f72014-11-18 10:55:16 +00002892}
2893
Alexandre Rames67555f72014-11-18 10:55:16 +00002894void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2895 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002896 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00002897 locations->SetInAt(0, Location::RequiresRegister());
2898 if (check->HasUses()) {
2899 locations->SetOut(Location::SameAsFirstInput());
2900 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01002901 // Rely on the type initialization to save everything we need.
2902 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Alexandre Rames67555f72014-11-18 10:55:16 +00002903}
2904
2905void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2906 // We assume the class is not null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01002907 SlowPathCodeARM64* slow_path =
2908 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(check->GetLoadClass(), check);
Alexandre Rames67555f72014-11-18 10:55:16 +00002909 codegen_->AddSlowPath(slow_path);
2910 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2911}
2912
Roland Levillain1a653882016-03-18 18:05:57 +00002913static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2914 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2915 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2916}
2917
2918void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002919 VRegister lhs_reg = InputFPRegisterAt(instruction, 0);
Roland Levillain1a653882016-03-18 18:05:57 +00002920 Location rhs_loc = instruction->GetLocations()->InAt(1);
2921 if (rhs_loc.IsConstant()) {
2922 // 0.0 is the only immediate that can be encoded directly in
2923 // an FCMP instruction.
2924 //
2925 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2926 // specify that in a floating-point comparison, positive zero
2927 // and negative zero are considered equal, so we can use the
2928 // literal 0.0 for both cases here.
2929 //
2930 // Note however that some methods (Float.equal, Float.compare,
2931 // Float.compareTo, Double.equal, Double.compare,
2932 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2933 // StrictMath.min) consider 0.0 to be (strictly) greater than
2934 // -0.0. So if we ever translate calls to these methods into a
2935 // HCompare instruction, we must handle the -0.0 case with
2936 // care here.
2937 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2938 __ Fcmp(lhs_reg, 0.0);
2939 } else {
2940 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2941 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002942}
2943
Serban Constantinescu02164b32014-11-13 14:05:07 +00002944void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002945 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002946 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002947 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002948 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002949 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002950 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002951 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002952 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002953 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002954 case DataType::Type::kInt32:
2955 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002956 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002957 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002958 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2959 break;
2960 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002961 case DataType::Type::kFloat32:
2962 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002963 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002964 locations->SetInAt(1,
2965 IsFloatingPointZeroConstant(compare->InputAt(1))
2966 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2967 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002968 locations->SetOut(Location::RequiresRegister());
2969 break;
2970 }
2971 default:
2972 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2973 }
2974}
2975
2976void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002977 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002978
2979 // 0 if: left == right
2980 // 1 if: left > right
2981 // -1 if: left < right
2982 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002983 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002984 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002985 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002986 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002987 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002988 case DataType::Type::kInt32:
2989 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002990 Register result = OutputRegister(compare);
2991 Register left = InputRegisterAt(compare, 0);
2992 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002993 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002994 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2995 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002996 break;
2997 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002998 case DataType::Type::kFloat32:
2999 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003000 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003001 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003002 __ Cset(result, ne);
3003 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003004 break;
3005 }
3006 default:
3007 LOG(FATAL) << "Unimplemented compare type " << in_type;
3008 }
3009}
3010
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003011void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003012 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003013
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003014 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003015 locations->SetInAt(0, Location::RequiresFpuRegister());
3016 locations->SetInAt(1,
3017 IsFloatingPointZeroConstant(instruction->InputAt(1))
3018 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3019 : Location::RequiresFpuRegister());
3020 } else {
3021 // Integer cases.
3022 locations->SetInAt(0, Location::RequiresRegister());
3023 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3024 }
3025
David Brazdilb3e773e2016-01-26 11:28:37 +00003026 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003027 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003028 }
3029}
3030
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003031void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003032 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003033 return;
3034 }
3035
3036 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003037 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003038 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003039
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003040 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003041 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003042 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003043 } else {
3044 // Integer cases.
3045 Register lhs = InputRegisterAt(instruction, 0);
3046 Operand rhs = InputOperandAt(instruction, 1);
3047 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003048 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003049 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003050}
3051
3052#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3053 M(Equal) \
3054 M(NotEqual) \
3055 M(LessThan) \
3056 M(LessThanOrEqual) \
3057 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003058 M(GreaterThanOrEqual) \
3059 M(Below) \
3060 M(BelowOrEqual) \
3061 M(Above) \
3062 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003063#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003064void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3065void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003066FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003067#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003068#undef FOR_EACH_CONDITION_INSTRUCTION
3069
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003070void InstructionCodeGeneratorARM64::GenerateIntDivForPower2Denom(HDiv* instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003071 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003072 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003073 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
3074
3075 Register out = OutputRegister(instruction);
3076 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003077
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003078 Register final_dividend;
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003079 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003080 // No need to adjust the result for non-negative dividends or the INT32_MIN/INT64_MIN dividends.
3081 // NOTE: The generated code for HDiv correctly works for the INT32_MIN/INT64_MIN dividends:
3082 // imm == 2
3083 // add out, dividend(0x80000000), dividend(0x80000000), lsr #31 => out = 0x80000001
3084 // asr out, out(0x80000001), #1 => out = 0xc0000000
3085 // This is the same as 'asr out, 0x80000000, #1'
3086 //
3087 // imm > 2
3088 // add temp, dividend(0x80000000), imm - 1 => temp = 0b10..01..1, where the number
3089 // of the rightmost 1s is ctz_imm.
3090 // cmp dividend(0x80000000), 0 => N = 1, V = 0 (lt is true)
3091 // csel out, temp(0b10..01..1), dividend(0x80000000), lt => out = 0b10..01..1
3092 // asr out, out(0b10..01..1), #ctz_imm => out = 0b1..10..0, where the number of the
3093 // leftmost 1s is ctz_imm + 1.
3094 // This is the same as 'asr out, dividend(0x80000000), #ctz_imm'.
3095 //
3096 // imm == INT32_MIN
3097 // add tmp, dividend(0x80000000), #0x7fffffff => tmp = -1
3098 // cmp dividend(0x80000000), 0 => N = 1, V = 0 (lt is true)
3099 // csel out, temp(-1), dividend(0x80000000), lt => out = -1
3100 // neg out, out(-1), asr #31 => out = 1
3101 // This is the same as 'neg out, dividend(0x80000000), asr #31'.
3102 final_dividend = dividend;
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003103 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003104 if (abs_imm == 2) {
3105 int bits = DataType::Size(instruction->GetResultType()) * kBitsPerByte;
3106 __ Add(out, dividend, Operand(dividend, LSR, bits - 1));
3107 } else {
3108 UseScratchRegisterScope temps(GetVIXLAssembler());
3109 Register temp = temps.AcquireSameSizeAs(out);
3110 __ Add(temp, dividend, abs_imm - 1);
3111 __ Cmp(dividend, 0);
3112 __ Csel(out, temp, dividend, lt);
3113 }
3114 final_dividend = out;
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003115 }
3116
Zheng Xuc6667102015-05-15 16:08:45 +08003117 int ctz_imm = CTZ(abs_imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003118 if (imm > 0) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003119 __ Asr(out, final_dividend, ctz_imm);
Zheng Xuc6667102015-05-15 16:08:45 +08003120 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003121 __ Neg(out, Operand(final_dividend, ASR, ctz_imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003122 }
3123}
3124
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003125// Return true if the magic number was modified by subtracting 2^32(Int32 div) or 2^64(Int64 div).
3126// So dividend needs to be added.
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003127static inline bool NeedToAddDividend(int64_t magic_number, int64_t divisor) {
3128 return divisor > 0 && magic_number < 0;
3129}
3130
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003131// Return true if the magic number was modified by adding 2^32(Int32 div) or 2^64(Int64 div).
3132// So dividend needs to be subtracted.
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003133static inline bool NeedToSubDividend(int64_t magic_number, int64_t divisor) {
3134 return divisor < 0 && magic_number > 0;
3135}
3136
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003137// Generate code which increments the value in register 'in' by 1 if the value is negative.
3138// It is done with 'add out, in, in, lsr #31 or #63'.
3139// If the value is a result of an operation setting the N flag, CINC MI can be used
3140// instead of ADD. 'use_cond_inc' controls this.
3141void InstructionCodeGeneratorARM64::GenerateIncrementNegativeByOne(
3142 Register out,
3143 Register in,
3144 bool use_cond_inc) {
3145 if (use_cond_inc) {
3146 __ Cinc(out, in, mi);
3147 } else {
3148 __ Add(out, in, Operand(in, LSR, in.GetSizeInBits() - 1));
3149 }
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003150}
3151
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003152// Helper to generate code producing the result of HRem with a constant divisor.
3153void InstructionCodeGeneratorARM64::GenerateResultRemWithAnyConstant(
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003154 Register out,
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003155 Register dividend,
3156 Register quotient,
3157 int64_t divisor,
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003158 UseScratchRegisterScope* temps_scope) {
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003159 Register temp_imm = temps_scope->AcquireSameSizeAs(out);
3160 __ Mov(temp_imm, divisor);
3161 __ Msub(out, quotient, temp_imm, dividend);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003162}
3163
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003164// Helper to generate code for HDiv/HRem instructions when a dividend is non-negative and
3165// a divisor is a positive constant, not power of 2.
3166void InstructionCodeGeneratorARM64::GenerateInt64UnsignedDivRemWithAnyPositiveConstant(
3167 HBinaryOperation* instruction) {
3168 DCHECK(instruction->IsDiv() || instruction->IsRem());
3169 DCHECK(instruction->GetResultType() == DataType::Type::kInt64);
3170
3171 LocationSummary* locations = instruction->GetLocations();
3172 Location second = locations->InAt(1);
3173 DCHECK(second.IsConstant());
3174
3175 Register out = OutputRegister(instruction);
3176 Register dividend = InputRegisterAt(instruction, 0);
3177 int64_t imm = Int64FromConstant(second.GetConstant());
3178 DCHECK_GT(imm, 0);
3179
3180 int64_t magic;
3181 int shift;
3182 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ true, &magic, &shift);
3183
3184 UseScratchRegisterScope temps(GetVIXLAssembler());
3185 Register temp = temps.AcquireSameSizeAs(out);
3186
3187 auto generate_unsigned_div_code = [this, magic, shift](Register out,
3188 Register dividend,
3189 Register temp) {
3190 // temp = get_high(dividend * magic)
3191 __ Mov(temp, magic);
3192 if (magic > 0 && shift == 0) {
3193 __ Smulh(out, dividend, temp);
3194 } else {
3195 __ Smulh(temp, dividend, temp);
3196 if (magic < 0) {
3197 // The negative magic means that the multiplier m is greater than INT64_MAX.
3198 // In such a case shift is never 0. See the proof in
3199 // InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant.
3200 __ Add(temp, temp, dividend);
3201 }
3202 DCHECK_NE(shift, 0);
3203 __ Lsr(out, temp, shift);
3204 }
3205 };
3206
3207 if (instruction->IsDiv()) {
3208 generate_unsigned_div_code(out, dividend, temp);
3209 } else {
3210 generate_unsigned_div_code(temp, dividend, temp);
3211 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3212 }
3213}
3214
3215// Helper to generate code for HDiv/HRem instructions for any dividend and a constant divisor
3216// (not power of 2).
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003217void InstructionCodeGeneratorARM64::GenerateInt64DivRemWithAnyConstant(
3218 HBinaryOperation* instruction) {
Zheng Xuc6667102015-05-15 16:08:45 +08003219 DCHECK(instruction->IsDiv() || instruction->IsRem());
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003220 DCHECK(instruction->GetResultType() == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003221
3222 LocationSummary* locations = instruction->GetLocations();
3223 Location second = locations->InAt(1);
3224 DCHECK(second.IsConstant());
3225
3226 Register out = OutputRegister(instruction);
3227 Register dividend = InputRegisterAt(instruction, 0);
3228 int64_t imm = Int64FromConstant(second.GetConstant());
3229
Zheng Xuc6667102015-05-15 16:08:45 +08003230 int64_t magic;
3231 int shift;
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003232 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ true, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08003233
3234 UseScratchRegisterScope temps(GetVIXLAssembler());
3235 Register temp = temps.AcquireSameSizeAs(out);
3236
3237 // temp = get_high(dividend * magic)
3238 __ Mov(temp, magic);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003239 __ Smulh(temp, dividend, temp);
3240
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003241 // The multiplication result might need some corrections to be finalized.
3242 // The last correction is to increment by 1, if the result is negative.
3243 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3244 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3245 // However if one of the corrections is ADD or SUB, the sign can be detected
3246 // with ADDS/SUBS. They set the N flag if the result is negative.
3247 // This allows to use CINC MI which has latency 1.
3248 bool use_cond_inc = false;
3249
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003250 // Some combinations of magic_number and the divisor require to correct the result.
3251 // Check whether the correction is needed.
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003252 if (NeedToAddDividend(magic, imm)) {
3253 __ Adds(temp, temp, dividend);
3254 use_cond_inc = true;
3255 } else if (NeedToSubDividend(magic, imm)) {
3256 __ Subs(temp, temp, dividend);
3257 use_cond_inc = true;
3258 }
3259
3260 if (shift != 0) {
3261 __ Asr(temp, temp, shift);
3262 }
3263
3264 if (instruction->IsRem()) {
3265 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3266 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3267 } else {
3268 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3269 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003270}
3271
3272void InstructionCodeGeneratorARM64::GenerateInt32DivRemWithAnyConstant(
3273 HBinaryOperation* instruction) {
3274 DCHECK(instruction->IsDiv() || instruction->IsRem());
3275 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
3276
3277 LocationSummary* locations = instruction->GetLocations();
3278 Location second = locations->InAt(1);
3279 DCHECK(second.IsConstant());
3280
3281 Register out = OutputRegister(instruction);
3282 Register dividend = InputRegisterAt(instruction, 0);
3283 int64_t imm = Int64FromConstant(second.GetConstant());
3284
3285 int64_t magic;
3286 int shift;
3287 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
3288 UseScratchRegisterScope temps(GetVIXLAssembler());
3289 Register temp = temps.AcquireSameSizeAs(out);
3290
3291 // temp = get_high(dividend * magic)
3292 __ Mov(temp, magic);
3293 __ Smull(temp.X(), dividend, temp);
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003294
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003295 // The multiplication result might need some corrections to be finalized.
3296 // The last correction is to increment by 1, if the result is negative.
3297 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3298 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3299 // However if one of the corrections is ADD or SUB, the sign can be detected
3300 // with ADDS/SUBS. They set the N flag if the result is negative.
3301 // This allows to use CINC MI which has latency 1.
3302 bool use_cond_inc = false;
3303
3304 // ADD/SUB correction is performed in the high 32 bits
3305 // as high 32 bits are ignored because type are kInt32.
3306 if (NeedToAddDividend(magic, imm)) {
3307 __ Adds(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3308 use_cond_inc = true;
3309 } else if (NeedToSubDividend(magic, imm)) {
3310 __ Subs(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3311 use_cond_inc = true;
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003312 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003313
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003314 // Extract the result from the high 32 bits and apply the final right shift.
3315 DCHECK_LT(shift, 32);
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003316 if (imm > 0 && HasNonNegativeInputAt(instruction, 0)) {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01003317 // No need to adjust the result for a non-negative dividend and a positive divisor.
3318 if (instruction->IsDiv()) {
3319 __ Lsr(out.X(), temp.X(), 32 + shift);
3320 } else {
3321 __ Lsr(temp.X(), temp.X(), 32 + shift);
3322 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3323 }
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003324 } else {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01003325 __ Asr(temp.X(), temp.X(), 32 + shift);
3326
3327 if (instruction->IsRem()) {
3328 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3329 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3330 } else {
3331 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3332 }
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003333 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003334}
3335
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003336void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction,
3337 int64_t divisor) {
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003338 DCHECK(instruction->IsDiv() || instruction->IsRem());
3339 if (instruction->GetResultType() == DataType::Type::kInt64) {
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003340 if (divisor > 0 && HasNonNegativeInputAt(instruction, 0)) {
3341 GenerateInt64UnsignedDivRemWithAnyPositiveConstant(instruction);
3342 } else {
3343 GenerateInt64DivRemWithAnyConstant(instruction);
3344 }
Zheng Xuc6667102015-05-15 16:08:45 +08003345 } else {
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003346 GenerateInt32DivRemWithAnyConstant(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003347 }
3348}
3349
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003350void InstructionCodeGeneratorARM64::GenerateIntDivForConstDenom(HDiv *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003351 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Zheng Xuc6667102015-05-15 16:08:45 +08003352
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003353 if (imm == 0) {
3354 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3355 return;
3356 }
Zheng Xuc6667102015-05-15 16:08:45 +08003357
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003358 if (IsPowerOfTwo(AbsOrMin(imm))) {
3359 GenerateIntDivForPower2Denom(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003360 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003361 // Cases imm == -1 or imm == 1 are handled by InstructionSimplifier.
3362 DCHECK(imm < -2 || imm > 2) << imm;
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003363 GenerateDivRemWithAnyConstant(instruction, imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003364 }
3365}
3366
3367void InstructionCodeGeneratorARM64::GenerateIntDiv(HDiv *instruction) {
3368 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
3369 << instruction->GetResultType();
3370
3371 if (instruction->GetLocations()->InAt(1).IsConstant()) {
3372 GenerateIntDivForConstDenom(instruction);
3373 } else {
3374 Register out = OutputRegister(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003375 Register dividend = InputRegisterAt(instruction, 0);
3376 Register divisor = InputRegisterAt(instruction, 1);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003377 __ Sdiv(out, dividend, divisor);
Zheng Xuc6667102015-05-15 16:08:45 +08003378 }
3379}
3380
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003381void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3382 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003383 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003384 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003385 case DataType::Type::kInt32:
3386 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003387 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003388 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003389 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3390 break;
3391
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003392 case DataType::Type::kFloat32:
3393 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003394 locations->SetInAt(0, Location::RequiresFpuRegister());
3395 locations->SetInAt(1, Location::RequiresFpuRegister());
3396 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3397 break;
3398
3399 default:
3400 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3401 }
3402}
3403
3404void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003405 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003406 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003407 case DataType::Type::kInt32:
3408 case DataType::Type::kInt64:
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003409 GenerateIntDiv(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003410 break;
3411
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003412 case DataType::Type::kFloat32:
3413 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003414 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3415 break;
3416
3417 default:
3418 LOG(FATAL) << "Unexpected div type " << type;
3419 }
3420}
3421
Alexandre Rames67555f72014-11-18 10:55:16 +00003422void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003423 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003424 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003425}
3426
3427void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3428 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003429 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003430 codegen_->AddSlowPath(slow_path);
3431 Location value = instruction->GetLocations()->InAt(0);
3432
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003433 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003434
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003435 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003436 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Elliott Hughesc1896c92018-11-29 11:33:18 -08003437 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003438 }
3439
Alexandre Rames67555f72014-11-18 10:55:16 +00003440 if (value.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003441 int64_t divisor = Int64FromLocation(value);
Alexandre Rames67555f72014-11-18 10:55:16 +00003442 if (divisor == 0) {
3443 __ B(slow_path->GetEntryLabel());
3444 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003445 // A division by a non-null constant is valid. We don't need to perform
3446 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003447 }
3448 } else {
3449 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3450 }
3451}
3452
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003453void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3454 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003455 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003456 locations->SetOut(Location::ConstantLocation(constant));
3457}
3458
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003459void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3460 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003461 // Will be generated at use site.
3462}
3463
Alexandre Rames5319def2014-10-23 10:03:10 +01003464void LocationsBuilderARM64::VisitExit(HExit* exit) {
3465 exit->SetLocations(nullptr);
3466}
3467
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003468void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003469}
3470
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003471void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3472 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003473 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003474 locations->SetOut(Location::ConstantLocation(constant));
3475}
3476
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003477void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003478 // Will be generated at use site.
3479}
3480
David Brazdilfc6a86a2015-06-26 10:33:45 +00003481void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003482 if (successor->IsExitBlock()) {
3483 DCHECK(got->GetPrevious()->AlwaysThrows());
3484 return; // no code needed
3485 }
3486
Serban Constantinescu02164b32014-11-13 14:05:07 +00003487 HBasicBlock* block = got->GetBlock();
3488 HInstruction* previous = got->GetPrevious();
3489 HLoopInformation* info = block->GetLoopInformation();
3490
David Brazdil46e2a392015-03-16 17:31:52 +00003491 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00003492 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003493 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3494 return;
3495 }
3496 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3497 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08003498 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003499 }
3500 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003501 __ B(codegen_->GetLabelOf(successor));
3502 }
3503}
3504
David Brazdilfc6a86a2015-06-26 10:33:45 +00003505void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3506 got->SetLocations(nullptr);
3507}
3508
3509void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3510 HandleGoto(got, got->GetSuccessor());
3511}
3512
3513void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3514 try_boundary->SetLocations(nullptr);
3515}
3516
3517void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3518 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3519 if (!successor->IsExitBlock()) {
3520 HandleGoto(try_boundary, successor);
3521 }
3522}
3523
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003524void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003525 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003526 vixl::aarch64::Label* true_target,
3527 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003528 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003529
David Brazdil0debae72015-11-12 18:37:00 +00003530 if (true_target == nullptr && false_target == nullptr) {
3531 // Nothing to do. The code always falls through.
3532 return;
3533 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003534 // Constant condition, statically compared against "true" (integer value 1).
3535 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003536 if (true_target != nullptr) {
3537 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003538 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003539 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003540 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003541 if (false_target != nullptr) {
3542 __ B(false_target);
3543 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003544 }
David Brazdil0debae72015-11-12 18:37:00 +00003545 return;
3546 }
3547
3548 // The following code generates these patterns:
3549 // (1) true_target == nullptr && false_target != nullptr
3550 // - opposite condition true => branch to false_target
3551 // (2) true_target != nullptr && false_target == nullptr
3552 // - condition true => branch to true_target
3553 // (3) true_target != nullptr && false_target != nullptr
3554 // - condition true => branch to true_target
3555 // - branch to false_target
3556 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003557 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003558 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003559 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003560 if (true_target == nullptr) {
3561 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3562 } else {
3563 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3564 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003565 } else {
3566 // The condition instruction has not been materialized, use its inputs as
3567 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003568 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003569
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003570 DataType::Type type = condition->InputAt(0)->GetType();
3571 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003572 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003573 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003574 IfCondition opposite_condition = condition->GetOppositeCondition();
3575 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003576 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003577 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003578 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003579 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003580 // Integer cases.
3581 Register lhs = InputRegisterAt(condition, 0);
3582 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003583
3584 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003585 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003586 if (true_target == nullptr) {
3587 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3588 non_fallthrough_target = false_target;
3589 } else {
3590 arm64_cond = ARM64Condition(condition->GetCondition());
3591 non_fallthrough_target = true_target;
3592 }
3593
Aart Bik086d27e2016-01-20 17:02:00 -08003594 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003595 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003596 switch (arm64_cond) {
3597 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003598 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003599 break;
3600 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003601 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003602 break;
3603 case lt:
3604 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003605 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003606 break;
3607 case ge:
3608 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003609 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003610 break;
3611 default:
3612 // Without the `static_cast` the compiler throws an error for
3613 // `-Werror=sign-promo`.
3614 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3615 }
3616 } else {
3617 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003618 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003619 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003620 }
3621 }
David Brazdil0debae72015-11-12 18:37:00 +00003622
3623 // If neither branch falls through (case 3), the conditional branch to `true_target`
3624 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3625 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003626 __ B(false_target);
3627 }
3628}
3629
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003630void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003631 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003632 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003633 locations->SetInAt(0, Location::RequiresRegister());
3634 }
3635}
3636
3637void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003638 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3639 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003640 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3641 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3642 true_target = nullptr;
3643 }
3644 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3645 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3646 false_target = nullptr;
3647 }
Andreas Gampe3db70682018-12-26 15:12:03 -08003648 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003649}
3650
3651void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003652 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003653 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003654 InvokeRuntimeCallingConvention calling_convention;
3655 RegisterSet caller_saves = RegisterSet::Empty();
3656 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3657 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003658 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003659 locations->SetInAt(0, Location::RequiresRegister());
3660 }
3661}
3662
3663void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003664 SlowPathCodeARM64* slow_path =
3665 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003666 GenerateTestAndBranch(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08003667 /* condition_input_index= */ 0,
David Brazdil0debae72015-11-12 18:37:00 +00003668 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08003669 /* false_target= */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003670}
3671
Mingyao Yang063fc772016-08-02 11:02:54 -07003672void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003673 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003674 LocationSummary(flag, LocationSummary::kNoCall);
3675 locations->SetOut(Location::RequiresRegister());
3676}
3677
3678void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3679 __ Ldr(OutputRegister(flag),
3680 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3681}
3682
David Brazdilc0b601b2016-02-08 14:20:45 +00003683static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3684 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003685 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003686}
3687
Alexandre Rames880f1192016-06-13 16:04:50 +01003688static inline Condition GetConditionForSelect(HCondition* condition) {
3689 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003690 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3691 : ARM64Condition(cond);
3692}
3693
David Brazdil74eb1b22015-12-14 11:44:01 +00003694void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003695 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003696 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003697 locations->SetInAt(0, Location::RequiresFpuRegister());
3698 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003699 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003700 } else {
3701 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3702 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3703 bool is_true_value_constant = cst_true_value != nullptr;
3704 bool is_false_value_constant = cst_false_value != nullptr;
3705 // Ask VIXL whether we should synthesize constants in registers.
3706 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3707 Operand true_op = is_true_value_constant ?
3708 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3709 Operand false_op = is_false_value_constant ?
3710 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3711 bool true_value_in_register = false;
3712 bool false_value_in_register = false;
3713 MacroAssembler::GetCselSynthesisInformation(
3714 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3715 true_value_in_register |= !is_true_value_constant;
3716 false_value_in_register |= !is_false_value_constant;
3717
3718 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3719 : Location::ConstantLocation(cst_true_value));
3720 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3721 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003722 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003723 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003724
David Brazdil74eb1b22015-12-14 11:44:01 +00003725 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3726 locations->SetInAt(2, Location::RequiresRegister());
3727 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003728}
3729
3730void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003731 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003732 Condition csel_cond;
3733
3734 if (IsBooleanValueOrMaterializedCondition(cond)) {
3735 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003736 // Use the condition flags set by the previous instruction.
3737 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003738 } else {
3739 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003740 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003741 }
3742 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003743 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003744 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003745 } else {
3746 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003747 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003748 }
3749
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003750 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003751 __ Fcsel(OutputFPRegister(select),
3752 InputFPRegisterAt(select, 1),
3753 InputFPRegisterAt(select, 0),
3754 csel_cond);
3755 } else {
3756 __ Csel(OutputRegister(select),
3757 InputOperandAt(select, 1),
3758 InputOperandAt(select, 0),
3759 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003760 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003761}
3762
David Srbecky0cf44932015-12-09 14:09:59 +00003763void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003764 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00003765}
3766
David Srbeckyd28f4a02016-03-14 17:14:24 +00003767void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3768 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003769}
3770
Vladimir Markodec78172020-06-19 15:31:23 +01003771void CodeGeneratorARM64::IncreaseFrame(size_t adjustment) {
3772 __ Claim(adjustment);
3773 GetAssembler()->cfi().AdjustCFAOffset(adjustment);
3774}
3775
3776void CodeGeneratorARM64::DecreaseFrame(size_t adjustment) {
3777 __ Drop(adjustment);
3778 GetAssembler()->cfi().AdjustCFAOffset(-adjustment);
3779}
3780
David Srbeckyc7098ff2016-02-09 14:30:11 +00003781void CodeGeneratorARM64::GenerateNop() {
3782 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003783}
3784
Alexandre Rames5319def2014-10-23 10:03:10 +01003785void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003786 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003787}
3788
3789void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003790 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003791}
3792
3793void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003794 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003795}
3796
3797void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003798 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003799}
3800
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003801// Temp is used for read barrier.
3802static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3803 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003804 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003805 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3806 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3807 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3808 return 1;
3809 }
3810 return 0;
3811}
3812
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003813// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003814// interface pointer, one for loading the current interface.
3815// The other checks have one temp for loading the object's class.
3816static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3817 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3818 return 3;
3819 }
3820 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003821}
3822
Alexandre Rames67555f72014-11-18 10:55:16 +00003823void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003824 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003825 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003826 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003827 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003828 case TypeCheckKind::kExactCheck:
3829 case TypeCheckKind::kAbstractClassCheck:
3830 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00003831 case TypeCheckKind::kArrayObjectCheck: {
3832 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
3833 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
3834 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003835 break;
Vladimir Marko87584542017-12-12 17:47:52 +00003836 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003837 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003838 case TypeCheckKind::kUnresolvedCheck:
3839 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003840 call_kind = LocationSummary::kCallOnSlowPath;
3841 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00003842 case TypeCheckKind::kBitstringCheck:
3843 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003844 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003845
Vladimir Markoca6fff82017-10-03 14:49:14 +01003846 LocationSummary* locations =
3847 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003848 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003849 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003850 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003851 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003852 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3853 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3854 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3855 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3856 } else {
3857 locations->SetInAt(1, Location::RequiresRegister());
3858 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003859 // The "out" register is used as a temporary, so it overlaps with the inputs.
3860 // Note that TypeCheckSlowPathARM64 uses this register too.
3861 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003862 // Add temps if necessary for read barriers.
3863 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003864}
3865
3866void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003867 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003868 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003869 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003870 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003871 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3872 ? Register()
3873 : InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003874 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003875 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003876 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3877 DCHECK_LE(num_temps, 1u);
3878 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003879 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3880 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3881 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3882 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003883
Scott Wakeling97c72b72016-06-24 16:19:36 +01003884 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003885 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003886
3887 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003888 // Avoid null check if we know `obj` is not null.
3889 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003890 __ Cbz(obj, &zero);
3891 }
3892
Roland Levillain44015862016-01-22 11:47:17 +00003893 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003894 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003895 ReadBarrierOption read_barrier_option =
3896 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003897 // /* HeapReference<Class> */ out = obj->klass_
3898 GenerateReferenceLoadTwoRegisters(instruction,
3899 out_loc,
3900 obj_loc,
3901 class_offset,
3902 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003903 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003904 __ Cmp(out, cls);
3905 __ Cset(out, eq);
3906 if (zero.IsLinked()) {
3907 __ B(&done);
3908 }
3909 break;
3910 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003911
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003912 case TypeCheckKind::kAbstractClassCheck: {
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 // If the class is abstract, we eagerly fetch the super class of the
3923 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003924 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003925 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003926 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003927 GenerateReferenceLoadOneRegister(instruction,
3928 out_loc,
3929 super_offset,
3930 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003931 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003932 // If `out` is null, we use it for the result, and jump to `done`.
3933 __ Cbz(out, &done);
3934 __ Cmp(out, cls);
3935 __ B(ne, &loop);
3936 __ Mov(out, 1);
3937 if (zero.IsLinked()) {
3938 __ B(&done);
3939 }
3940 break;
3941 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003942
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003943 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003944 ReadBarrierOption read_barrier_option =
3945 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003946 // /* HeapReference<Class> */ out = obj->klass_
3947 GenerateReferenceLoadTwoRegisters(instruction,
3948 out_loc,
3949 obj_loc,
3950 class_offset,
3951 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003952 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003953 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003954 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003955 __ Bind(&loop);
3956 __ Cmp(out, cls);
3957 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003958 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003959 GenerateReferenceLoadOneRegister(instruction,
3960 out_loc,
3961 super_offset,
3962 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003963 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003964 __ Cbnz(out, &loop);
3965 // If `out` is null, we use it for the result, and jump to `done`.
3966 __ B(&done);
3967 __ Bind(&success);
3968 __ Mov(out, 1);
3969 if (zero.IsLinked()) {
3970 __ B(&done);
3971 }
3972 break;
3973 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003974
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003975 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003976 ReadBarrierOption read_barrier_option =
3977 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003978 // /* HeapReference<Class> */ out = obj->klass_
3979 GenerateReferenceLoadTwoRegisters(instruction,
3980 out_loc,
3981 obj_loc,
3982 class_offset,
3983 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003984 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003985 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003986 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003987 __ Cmp(out, cls);
3988 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003989 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003990 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003991 GenerateReferenceLoadOneRegister(instruction,
3992 out_loc,
3993 component_offset,
3994 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003995 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003996 // If `out` is null, we use it for the result, and jump to `done`.
3997 __ Cbz(out, &done);
3998 __ Ldrh(out, HeapOperand(out, primitive_offset));
3999 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4000 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004001 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004002 __ Mov(out, 1);
4003 __ B(&done);
4004 break;
4005 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004006
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004007 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004008 // No read barrier since the slow path will retry upon failure.
4009 // /* HeapReference<Class> */ out = obj->klass_
4010 GenerateReferenceLoadTwoRegisters(instruction,
4011 out_loc,
4012 obj_loc,
4013 class_offset,
4014 maybe_temp_loc,
4015 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004016 __ Cmp(out, cls);
4017 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004018 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08004019 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004020 codegen_->AddSlowPath(slow_path);
4021 __ B(ne, slow_path->GetEntryLabel());
4022 __ Mov(out, 1);
4023 if (zero.IsLinked()) {
4024 __ B(&done);
4025 }
4026 break;
4027 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004028
Calin Juravle98893e12015-10-02 21:05:03 +01004029 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004030 case TypeCheckKind::kInterfaceCheck: {
4031 // Note that we indeed only call on slow path, but we always go
4032 // into the slow path for the unresolved and interface check
4033 // cases.
4034 //
4035 // We cannot directly call the InstanceofNonTrivial runtime
4036 // entry point without resorting to a type checking slow path
4037 // here (i.e. by calling InvokeRuntime directly), as it would
4038 // require to assign fixed registers for the inputs of this
4039 // HInstanceOf instruction (following the runtime calling
4040 // convention), which might be cluttered by the potential first
4041 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004042 //
4043 // TODO: Introduce a new runtime entry point taking the object
4044 // to test (instead of its class) as argument, and let it deal
4045 // with the read barrier issues. This will let us refactor this
4046 // case of the `switch` code as it was previously (with a direct
4047 // call to the runtime not using a type checking slow path).
4048 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004049 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004050 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08004051 instruction, /* is_fatal= */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004052 codegen_->AddSlowPath(slow_path);
4053 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004054 if (zero.IsLinked()) {
4055 __ B(&done);
4056 }
4057 break;
4058 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004059
4060 case TypeCheckKind::kBitstringCheck: {
4061 // /* HeapReference<Class> */ temp = obj->klass_
4062 GenerateReferenceLoadTwoRegisters(instruction,
4063 out_loc,
4064 obj_loc,
4065 class_offset,
4066 maybe_temp_loc,
4067 kWithoutReadBarrier);
4068
4069 GenerateBitstringTypeCheckCompare(instruction, out);
4070 __ Cset(out, eq);
4071 if (zero.IsLinked()) {
4072 __ B(&done);
4073 }
4074 break;
4075 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004076 }
4077
4078 if (zero.IsLinked()) {
4079 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004080 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004081 }
4082
4083 if (done.IsLinked()) {
4084 __ Bind(&done);
4085 }
4086
4087 if (slow_path != nullptr) {
4088 __ Bind(slow_path->GetExitLabel());
4089 }
4090}
4091
4092void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004093 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00004094 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004095 LocationSummary* locations =
4096 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004097 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00004098 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
4099 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
4100 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
4101 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
4102 } else {
4103 locations->SetInAt(1, Location::RequiresRegister());
4104 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004105 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4106 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004107}
4108
4109void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004110 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004111 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004112 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004113 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00004114 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
4115 ? Register()
4116 : InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004117 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4118 DCHECK_GE(num_temps, 1u);
4119 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004120 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004121 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4122 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004123 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004124 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4125 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4126 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4127 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4128 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4129 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4130 const uint32_t object_array_data_offset =
4131 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004132
Vladimir Marko87584542017-12-12 17:47:52 +00004133 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004134 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004135 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4136 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004137 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004138
Scott Wakeling97c72b72016-06-24 16:19:36 +01004139 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004140 // Avoid null check if we know obj is not null.
4141 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004142 __ Cbz(obj, &done);
4143 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004144
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004145 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004146 case TypeCheckKind::kExactCheck:
4147 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004148 // /* HeapReference<Class> */ temp = obj->klass_
4149 GenerateReferenceLoadTwoRegisters(instruction,
4150 temp_loc,
4151 obj_loc,
4152 class_offset,
4153 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004154 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004155
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004156 __ Cmp(temp, cls);
4157 // Jump to slow path for throwing the exception or doing a
4158 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004159 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004160 break;
4161 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004162
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004163 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004164 // /* HeapReference<Class> */ temp = obj->klass_
4165 GenerateReferenceLoadTwoRegisters(instruction,
4166 temp_loc,
4167 obj_loc,
4168 class_offset,
4169 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004170 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004171
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004172 // If the class is abstract, we eagerly fetch the super class of the
4173 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004174 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004175 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004176 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004177 GenerateReferenceLoadOneRegister(instruction,
4178 temp_loc,
4179 super_offset,
4180 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004181 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004182
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004183 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4184 // exception.
4185 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4186 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004187 __ Cmp(temp, cls);
4188 __ B(ne, &loop);
4189 break;
4190 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004191
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004192 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004193 // /* HeapReference<Class> */ temp = obj->klass_
4194 GenerateReferenceLoadTwoRegisters(instruction,
4195 temp_loc,
4196 obj_loc,
4197 class_offset,
4198 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004199 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004200
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004201 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004202 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004203 __ Bind(&loop);
4204 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004205 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004206
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004207 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004208 GenerateReferenceLoadOneRegister(instruction,
4209 temp_loc,
4210 super_offset,
4211 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004212 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004213
4214 // If the class reference currently in `temp` is not null, jump
4215 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004216 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004217 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004218 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004219 break;
4220 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004221
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004222 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004223 // /* HeapReference<Class> */ temp = obj->klass_
4224 GenerateReferenceLoadTwoRegisters(instruction,
4225 temp_loc,
4226 obj_loc,
4227 class_offset,
4228 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004229 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004230
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004231 // Do an exact check.
4232 __ Cmp(temp, cls);
4233 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004234
4235 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004236 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004237 GenerateReferenceLoadOneRegister(instruction,
4238 temp_loc,
4239 component_offset,
4240 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004241 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004242
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004243 // If the component type is null, jump to the slow path to throw the exception.
4244 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4245 // Otherwise, the object is indeed an array. Further check that this component type is not a
4246 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004247 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4248 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004249 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004250 break;
4251 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004252
Calin Juravle98893e12015-10-02 21:05:03 +01004253 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004254 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004255 //
4256 // We cannot directly call the CheckCast runtime entry point
4257 // without resorting to a type checking slow path here (i.e. by
4258 // calling InvokeRuntime directly), as it would require to
4259 // assign fixed registers for the inputs of this HInstanceOf
4260 // instruction (following the runtime calling convention), which
4261 // might be cluttered by the potential first read barrier
4262 // emission at the beginning of this method.
4263 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004264 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004265 case TypeCheckKind::kInterfaceCheck: {
4266 // /* HeapReference<Class> */ temp = obj->klass_
4267 GenerateReferenceLoadTwoRegisters(instruction,
4268 temp_loc,
4269 obj_loc,
4270 class_offset,
4271 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004272 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004273
4274 // /* HeapReference<Class> */ temp = temp->iftable_
4275 GenerateReferenceLoadTwoRegisters(instruction,
4276 temp_loc,
4277 temp_loc,
4278 iftable_offset,
4279 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004280 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004281 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004282 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004283 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004284 vixl::aarch64::Label start_loop;
4285 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004286 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004287 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4288 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004289 // Go to next interface.
4290 __ Add(temp, temp, 2 * kHeapReferenceSize);
4291 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004292 // Compare the classes and continue the loop if they do not match.
4293 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4294 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004295 break;
4296 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004297
4298 case TypeCheckKind::kBitstringCheck: {
4299 // /* HeapReference<Class> */ temp = obj->klass_
4300 GenerateReferenceLoadTwoRegisters(instruction,
4301 temp_loc,
4302 obj_loc,
4303 class_offset,
4304 maybe_temp2_loc,
4305 kWithoutReadBarrier);
4306
4307 GenerateBitstringTypeCheckCompare(instruction, temp);
4308 __ B(ne, type_check_slow_path->GetEntryLabel());
4309 break;
4310 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004311 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004312 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004313
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004314 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004315}
4316
Alexandre Rames5319def2014-10-23 10:03:10 +01004317void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004318 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004319 locations->SetOut(Location::ConstantLocation(constant));
4320}
4321
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004322void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004323 // Will be generated at use site.
4324}
4325
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004326void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004327 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004328 locations->SetOut(Location::ConstantLocation(constant));
4329}
4330
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004331void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004332 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004333}
4334
Calin Juravle175dc732015-08-25 15:42:32 +01004335void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4336 // The trampoline uses the same calling convention as dex calling conventions,
4337 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4338 // the method_idx.
4339 HandleInvoke(invoke);
4340}
4341
4342void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4343 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004344 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004345}
4346
Alexandre Rames5319def2014-10-23 10:03:10 +01004347void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004348 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004349 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004350}
4351
Alexandre Rames67555f72014-11-18 10:55:16 +00004352void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4353 HandleInvoke(invoke);
4354}
4355
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004356void CodeGeneratorARM64::MaybeGenerateInlineCacheCheck(HInstruction* instruction,
4357 Register klass) {
4358 DCHECK_EQ(klass.GetCode(), 0u);
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004359 // We know the destination of an intrinsic, so no need to record inline
4360 // caches.
4361 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00004362 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004363 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004364 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
Nicolas Geoffray095dc462020-08-17 16:40:28 +01004365 ScopedProfilingInfoUse spiu(
4366 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
4367 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00004368 if (info != nullptr) {
4369 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
4370 uint64_t address = reinterpret_cast64<uint64_t>(cache);
4371 vixl::aarch64::Label done;
4372 __ Mov(x8, address);
4373 __ Ldr(x9, MemOperand(x8, InlineCache::ClassesOffset().Int32Value()));
4374 // Fast path for a monomorphic cache.
4375 __ Cmp(klass, x9);
4376 __ B(eq, &done);
4377 InvokeRuntime(kQuickUpdateInlineCache, instruction, instruction->GetDexPc());
4378 __ Bind(&done);
4379 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004380 }
4381}
4382
Alexandre Rames67555f72014-11-18 10:55:16 +00004383void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4384 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004385 LocationSummary* locations = invoke->GetLocations();
4386 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004387 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004388 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004389 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004390
Artem Serov914d7a82017-02-07 14:33:49 +00004391 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004392 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004393 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004394 {
4395 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4396 // /* HeapReference<Class> */ temp = temp->klass_
4397 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4398 codegen_->MaybeRecordImplicitNullCheck(invoke);
4399 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004400 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004401 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004402 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004403 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004404 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004405 }
Artem Serov914d7a82017-02-07 14:33:49 +00004406
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004407 // Instead of simply (possibly) unpoisoning `temp` here, we should
4408 // emit a read barrier for the previous class reference load.
4409 // However this is not required in practice, as this is an
4410 // intermediate/temporary reference and because the current
4411 // concurrent copying collector keeps the from-space memory
4412 // intact/accessible until the end of the marking phase (the
4413 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004414 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004415
4416 // If we're compiling baseline, update the inline cache.
4417 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
4418
4419 // The register ip1 is required to be used for the hidden argument in
4420 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
4421 MacroAssembler* masm = GetVIXLAssembler();
4422 UseScratchRegisterScope scratch_scope(masm);
4423 scratch_scope.Exclude(ip1);
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004424 __ Mov(ip1, invoke->GetMethodReference().index);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004425
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004426 __ Ldr(temp,
4427 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4428 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004429 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004430 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004431 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004432 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004433 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004434
4435 {
4436 // Ensure the pc position is recorded immediately after the `blr` instruction.
4437 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4438
4439 // lr();
4440 __ blr(lr);
4441 DCHECK(!codegen_->IsLeafMethod());
4442 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4443 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004444
Andreas Gampe3db70682018-12-26 15:12:03 -08004445 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004446}
4447
4448void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004449 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004450 if (intrinsic.TryDispatch(invoke)) {
4451 return;
4452 }
4453
Alexandre Rames67555f72014-11-18 10:55:16 +00004454 HandleInvoke(invoke);
4455}
4456
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004457void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004458 // Explicit clinit checks triggered by static invokes must have been pruned by
4459 // art::PrepareForRegisterAllocation.
4460 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004461
Vladimir Markoca6fff82017-10-03 14:49:14 +01004462 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004463 if (intrinsic.TryDispatch(invoke)) {
4464 return;
4465 }
4466
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004467 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
Vladimir Marko86c87522020-05-11 16:55:55 +01004468 CriticalNativeCallingConventionVisitorARM64 calling_convention_visitor(
4469 /*for_register_allocation=*/ true);
4470 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4471 } else {
4472 HandleInvoke(invoke);
4473 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004474}
4475
Andreas Gampe878d58c2015-01-15 23:24:00 -08004476static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4477 if (invoke->GetLocations()->Intrinsified()) {
4478 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4479 intrinsic.Dispatch(invoke);
4480 return true;
4481 }
4482 return false;
4483}
4484
Vladimir Markodc151b22015-10-15 18:02:30 +01004485HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4486 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004487 ArtMethod* method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004488 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004489 return desired_dispatch_info;
4490}
4491
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004492void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4493 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004494 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004495 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4496 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004497 case MethodLoadKind::kStringInit: {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004498 uint32_t offset =
4499 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004500 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004501 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004502 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004503 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004504 case MethodLoadKind::kRecursive:
Vladimir Marko86c87522020-05-11 16:55:55 +01004505 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004506 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004507 case MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01004508 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01004509 // Add ADRP with its PC-relative method patch.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004510 vixl::aarch64::Label* adrp_label =
4511 NewBootImageMethodPatch(invoke->GetResolvedMethodReference());
Vladimir Marko65979462017-05-19 17:25:12 +01004512 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4513 // Add ADD with its PC-relative method patch.
4514 vixl::aarch64::Label* add_label =
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004515 NewBootImageMethodPatch(invoke->GetResolvedMethodReference(), adrp_label);
Vladimir Marko65979462017-05-19 17:25:12 +01004516 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4517 break;
4518 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004519 case MethodLoadKind::kBootImageRelRo: {
Vladimir Markob066d432018-01-03 13:14:37 +00004520 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004521 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00004522 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_offset);
4523 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4524 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
4525 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_offset, adrp_label);
4526 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
4527 EmitLdrOffsetPlaceholder(ldr_label, WRegisterFrom(temp), XRegisterFrom(temp));
4528 break;
4529 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004530 case MethodLoadKind::kBssEntry: {
Vladimir Markob066d432018-01-03 13:14:37 +00004531 // Add ADRP with its PC-relative .bss entry patch.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004532 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(invoke->GetMethodReference());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004533 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Markob066d432018-01-03 13:14:37 +00004534 // Add LDR with its PC-relative .bss entry patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004535 vixl::aarch64::Label* ldr_label =
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004536 NewMethodBssEntryPatch(invoke->GetMethodReference(), adrp_label);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01004537 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004538 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004539 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004540 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004541 case MethodLoadKind::kJitDirectAddress:
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004542 // Load method address from literal pool.
4543 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
4544 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004545 case MethodLoadKind::kRuntimeCall: {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004546 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4547 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004548 }
4549 }
4550
Vladimir Marko86c87522020-05-11 16:55:55 +01004551 auto call_code_pointer_member = [&](MemberOffset offset) {
4552 // LR = callee_method->member;
4553 __ Ldr(lr, MemOperand(XRegisterFrom(callee_method), offset.Int32Value()));
4554 {
4555 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4556 ExactAssemblyScope eas(GetVIXLAssembler(),
4557 kInstructionSize,
4558 CodeBufferCheckScope::kExactSize);
4559 // lr()
4560 __ blr(lr);
4561 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4562 }
4563 };
Vladimir Marko58155012015-08-19 12:49:41 +00004564 switch (invoke->GetCodePtrLocation()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004565 case CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004566 {
4567 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4568 ExactAssemblyScope eas(GetVIXLAssembler(),
4569 kInstructionSize,
4570 CodeBufferCheckScope::kExactSize);
4571 __ bl(&frame_entry_label_);
4572 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4573 }
Vladimir Marko58155012015-08-19 12:49:41 +00004574 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004575 case CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01004576 size_t out_frame_size =
4577 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorARM64,
4578 kAapcs64StackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01004579 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Marko86c87522020-05-11 16:55:55 +01004580 call_code_pointer_member(ArtMethod::EntryPointFromJniOffset(kArm64PointerSize));
4581 // Zero-/sign-extend the result when needed due to native and managed ABI mismatch.
4582 switch (invoke->GetType()) {
4583 case DataType::Type::kBool:
4584 __ Ubfx(w0, w0, 0, 8);
4585 break;
4586 case DataType::Type::kInt8:
4587 __ Sbfx(w0, w0, 0, 8);
4588 break;
4589 case DataType::Type::kUint16:
4590 __ Ubfx(w0, w0, 0, 16);
4591 break;
4592 case DataType::Type::kInt16:
4593 __ Sbfx(w0, w0, 0, 16);
4594 break;
4595 case DataType::Type::kInt32:
4596 case DataType::Type::kInt64:
4597 case DataType::Type::kFloat32:
4598 case DataType::Type::kFloat64:
4599 case DataType::Type::kVoid:
4600 break;
4601 default:
4602 DCHECK(false) << invoke->GetType();
4603 break;
4604 }
4605 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01004606 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01004607 }
4608 break;
4609 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004610 case CodePtrLocation::kCallArtMethod:
Vladimir Marko86c87522020-05-11 16:55:55 +01004611 call_code_pointer_member(ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize));
Vladimir Marko58155012015-08-19 12:49:41 +00004612 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004613 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004614
Andreas Gampe878d58c2015-01-15 23:24:00 -08004615 DCHECK(!IsLeafMethod());
4616}
4617
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004618void CodeGeneratorARM64::GenerateVirtualCall(
4619 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004620 // Use the calling convention instead of the location of the receiver, as
4621 // intrinsics may have put the receiver in a different register. In the intrinsics
4622 // slow path, the arguments have been moved to the right place, so here we are
4623 // guaranteed that the receiver is the first register of the calling convention.
4624 InvokeDexCallingConvention calling_convention;
4625 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004626 Register temp = XRegisterFrom(temp_in);
4627 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4628 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4629 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004630 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004631
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004632 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004633
4634 {
4635 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4636 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4637 // /* HeapReference<Class> */ temp = receiver->klass_
4638 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4639 MaybeRecordImplicitNullCheck(invoke);
4640 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004641 // Instead of simply (possibly) unpoisoning `temp` here, we should
4642 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004643 // intermediate/temporary reference and because the current
4644 // concurrent copying collector keeps the from-space memory
4645 // intact/accessible until the end of the marking phase (the
4646 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004647 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004648
4649 // If we're compiling baseline, update the inline cache.
4650 MaybeGenerateInlineCacheCheck(invoke, temp);
4651
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004652 // temp = temp->GetMethodAt(method_offset);
4653 __ Ldr(temp, MemOperand(temp, method_offset));
4654 // lr = temp->GetEntryPoint();
4655 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004656 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004657 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004658 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4659 // lr();
4660 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004661 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004662 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004663}
4664
Vladimir Marko9922f002020-06-08 15:05:15 +01004665void CodeGeneratorARM64::MoveFromReturnRegister(Location trg, DataType::Type type) {
4666 if (!trg.IsValid()) {
4667 DCHECK(type == DataType::Type::kVoid);
4668 return;
4669 }
4670
4671 DCHECK_NE(type, DataType::Type::kVoid);
4672
4673 if (DataType::IsIntegralType(type) || type == DataType::Type::kReference) {
4674 Register trg_reg = RegisterFrom(trg, type);
4675 Register res_reg = RegisterFrom(ARM64ReturnLocation(type), type);
4676 __ Mov(trg_reg, res_reg, kDiscardForSameWReg);
4677 } else {
4678 VRegister trg_reg = FPRegisterFrom(trg, type);
4679 VRegister res_reg = FPRegisterFrom(ARM64ReturnLocation(type), type);
4680 __ Fmov(trg_reg, res_reg);
4681 }
4682}
4683
Orion Hodsonac141392017-01-13 11:53:47 +00004684void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00004685 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
4686 if (intrinsic.TryDispatch(invoke)) {
4687 return;
4688 }
Orion Hodsonac141392017-01-13 11:53:47 +00004689 HandleInvoke(invoke);
4690}
4691
4692void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00004693 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4694 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
4695 return;
4696 }
Orion Hodsonac141392017-01-13 11:53:47 +00004697 codegen_->GenerateInvokePolymorphicCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004698 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004699}
4700
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004701void LocationsBuilderARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4702 HandleInvoke(invoke);
4703}
4704
4705void InstructionCodeGeneratorARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4706 codegen_->GenerateInvokeCustomCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004707 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004708}
4709
Vladimir Marko6fd16062018-06-26 11:02:04 +01004710vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageIntrinsicPatch(
4711 uint32_t intrinsic_data,
4712 vixl::aarch64::Label* adrp_label) {
4713 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004714 /* dex_file= */ nullptr, intrinsic_data, adrp_label, &boot_image_other_patches_);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004715}
4716
Vladimir Markob066d432018-01-03 13:14:37 +00004717vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageRelRoPatch(
4718 uint32_t boot_image_offset,
4719 vixl::aarch64::Label* adrp_label) {
4720 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004721 /* dex_file= */ nullptr, boot_image_offset, adrp_label, &boot_image_other_patches_);
Vladimir Markob066d432018-01-03 13:14:37 +00004722}
4723
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004724vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004725 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004726 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004727 return NewPcRelativePatch(
4728 target_method.dex_file, target_method.index, adrp_label, &boot_image_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004729}
4730
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004731vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4732 MethodReference target_method,
4733 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004734 return NewPcRelativePatch(
4735 target_method.dex_file, target_method.index, adrp_label, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004736}
4737
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004738vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageTypePatch(
Scott Wakeling97c72b72016-06-24 16:19:36 +01004739 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004740 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004741 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004742 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &boot_image_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004743}
4744
Vladimir Marko1998cd02017-01-13 13:02:58 +00004745vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
Vladimir Marko8f63f102020-09-28 12:10:28 +01004746 HLoadClass* load_class,
Vladimir Marko1998cd02017-01-13 13:02:58 +00004747 vixl::aarch64::Label* adrp_label) {
Vladimir Marko8f63f102020-09-28 12:10:28 +01004748 const DexFile& dex_file = load_class->GetDexFile();
4749 dex::TypeIndex type_index = load_class->GetTypeIndex();
4750 ArenaDeque<PcRelativePatchInfo>* patches = nullptr;
4751 switch (load_class->GetLoadKind()) {
4752 case HLoadClass::LoadKind::kBssEntry:
4753 patches = &type_bss_entry_patches_;
4754 break;
4755 case HLoadClass::LoadKind::kBssEntryPublic:
4756 patches = &public_type_bss_entry_patches_;
4757 break;
4758 case HLoadClass::LoadKind::kBssEntryPackage:
4759 patches = &package_type_bss_entry_patches_;
4760 break;
4761 default:
4762 LOG(FATAL) << "Unexpected load kind: " << load_class->GetLoadKind();
4763 UNREACHABLE();
4764 }
4765 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004766}
4767
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004768vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004769 const DexFile& dex_file,
4770 dex::StringIndex string_index,
4771 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004772 return NewPcRelativePatch(
4773 &dex_file, string_index.index_, adrp_label, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01004774}
4775
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004776vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4777 const DexFile& dex_file,
4778 dex::StringIndex string_index,
4779 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004780 return NewPcRelativePatch(&dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004781}
4782
Vladimir Markof6675082019-05-17 12:05:28 +01004783void CodeGeneratorARM64::EmitEntrypointThunkCall(ThreadOffset64 entrypoint_offset) {
4784 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01004785 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markof6675082019-05-17 12:05:28 +01004786 call_entrypoint_patches_.emplace_back(/*dex_file*/ nullptr, entrypoint_offset.Uint32Value());
4787 vixl::aarch64::Label* bl_label = &call_entrypoint_patches_.back().label;
4788 __ bind(bl_label);
4789 __ bl(static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4790}
4791
Vladimir Marko966b46f2018-08-03 10:20:19 +00004792void CodeGeneratorARM64::EmitBakerReadBarrierCbnz(uint32_t custom_data) {
Vladimir Marko94796f82018-08-08 15:15:33 +01004793 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01004794 if (GetCompilerOptions().IsJitCompiler()) {
Vladimir Marko966b46f2018-08-03 10:20:19 +00004795 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
4796 vixl::aarch64::Label* slow_path_entry = &it->second.label;
4797 __ cbnz(mr, slow_path_entry);
4798 } else {
4799 baker_read_barrier_patches_.emplace_back(custom_data);
4800 vixl::aarch64::Label* cbnz_label = &baker_read_barrier_patches_.back().label;
4801 __ bind(cbnz_label);
4802 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4803 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004804}
4805
Scott Wakeling97c72b72016-06-24 16:19:36 +01004806vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004807 const DexFile* dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004808 uint32_t offset_or_index,
4809 vixl::aarch64::Label* adrp_label,
4810 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004811 // Add a patch entry and return the label.
4812 patches->emplace_back(dex_file, offset_or_index);
4813 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004814 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004815 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4816 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4817 return label;
4818}
4819
Scott Wakeling97c72b72016-06-24 16:19:36 +01004820vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4821 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004822 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004823}
4824
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004825vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004826 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004827 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004828 return jit_string_patches_.GetOrCreate(
4829 StringReference(&dex_file, string_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004830 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004831}
4832
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004833vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004834 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004835 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004836 return jit_class_patches_.GetOrCreate(
4837 TypeReference(&dex_file, type_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004838 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004839}
4840
Vladimir Markoaad75c62016-10-03 08:46:48 +00004841void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4842 vixl::aarch64::Register reg) {
4843 DCHECK(reg.IsX());
4844 SingleEmissionCheckScope guard(GetVIXLAssembler());
4845 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004846 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004847}
4848
4849void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4850 vixl::aarch64::Register out,
4851 vixl::aarch64::Register base) {
4852 DCHECK(out.IsX());
4853 DCHECK(base.IsX());
4854 SingleEmissionCheckScope guard(GetVIXLAssembler());
4855 __ Bind(fixup_label);
4856 __ add(out, base, Operand(/* offset placeholder */ 0));
4857}
4858
4859void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4860 vixl::aarch64::Register out,
4861 vixl::aarch64::Register base) {
4862 DCHECK(base.IsX());
4863 SingleEmissionCheckScope guard(GetVIXLAssembler());
4864 __ Bind(fixup_label);
4865 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4866}
4867
Vladimir Markoeebb8212018-06-05 14:57:24 +01004868void CodeGeneratorARM64::LoadBootImageAddress(vixl::aarch64::Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01004869 uint32_t boot_image_reference) {
4870 if (GetCompilerOptions().IsBootImage()) {
4871 // Add ADRP with its PC-relative type patch.
4872 vixl::aarch64::Label* adrp_label = NewBootImageIntrinsicPatch(boot_image_reference);
4873 EmitAdrpPlaceholder(adrp_label, reg.X());
4874 // Add ADD with its PC-relative type patch.
4875 vixl::aarch64::Label* add_label = NewBootImageIntrinsicPatch(boot_image_reference, adrp_label);
4876 EmitAddPlaceholder(add_label, reg.X(), reg.X());
Vladimir Markoa2da9b92018-10-10 14:21:55 +01004877 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01004878 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004879 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004880 EmitAdrpPlaceholder(adrp_label, reg.X());
4881 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004882 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_reference, adrp_label);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004883 EmitLdrOffsetPlaceholder(ldr_label, reg.W(), reg.X());
4884 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01004885 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01004886 gc::Heap* heap = Runtime::Current()->GetHeap();
4887 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004888 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01004889 __ Ldr(reg.W(), DeduplicateBootImageAddressLiteral(reinterpret_cast<uintptr_t>(address)));
4890 }
4891}
4892
Vladimir Marko6fd16062018-06-26 11:02:04 +01004893void CodeGeneratorARM64::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
4894 uint32_t boot_image_offset) {
4895 DCHECK(invoke->IsStatic());
4896 InvokeRuntimeCallingConvention calling_convention;
4897 Register argument = calling_convention.GetRegisterAt(0);
4898 if (GetCompilerOptions().IsBootImage()) {
4899 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
4900 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004901 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01004902 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
4903 // Add ADRP with its PC-relative type patch.
4904 vixl::aarch64::Label* adrp_label = NewBootImageTypePatch(*target_method.dex_file, type_idx);
4905 EmitAdrpPlaceholder(adrp_label, argument.X());
4906 // Add ADD with its PC-relative type patch.
4907 vixl::aarch64::Label* add_label =
4908 NewBootImageTypePatch(*target_method.dex_file, type_idx, adrp_label);
4909 EmitAddPlaceholder(add_label, argument.X(), argument.X());
4910 } else {
4911 LoadBootImageAddress(argument, boot_image_offset);
4912 }
4913 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
4914 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4915}
4916
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004917template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004918inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4919 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004920 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004921 for (const PcRelativePatchInfo& info : infos) {
4922 linker_patches->push_back(Factory(info.label.GetLocation(),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004923 info.target_dex_file,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004924 info.pc_insn_label->GetLocation(),
4925 info.offset_or_index));
4926 }
4927}
4928
Vladimir Marko6fd16062018-06-26 11:02:04 +01004929template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
4930linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
4931 const DexFile* target_dex_file,
4932 uint32_t pc_insn_offset,
4933 uint32_t boot_image_offset) {
4934 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
4935 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00004936}
4937
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004938void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004939 DCHECK(linker_patches->empty());
4940 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004941 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004942 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004943 boot_image_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004944 type_bss_entry_patches_.size() +
Vladimir Marko8f63f102020-09-28 12:10:28 +01004945 public_type_bss_entry_patches_.size() +
4946 package_type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004947 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004948 string_bss_entry_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01004949 boot_image_other_patches_.size() +
Vladimir Markof6675082019-05-17 12:05:28 +01004950 call_entrypoint_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004951 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004952 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01004953 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004954 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004955 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004956 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004957 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004958 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004959 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01004960 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01004961 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004962 DCHECK(boot_image_type_patches_.empty());
4963 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01004964 }
4965 if (GetCompilerOptions().IsBootImage()) {
4966 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
4967 boot_image_other_patches_, linker_patches);
4968 } else {
4969 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
4970 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004971 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004972 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4973 method_bss_entry_patches_, linker_patches);
4974 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4975 type_bss_entry_patches_, linker_patches);
Vladimir Marko8f63f102020-09-28 12:10:28 +01004976 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PublicTypeBssEntryPatch>(
4977 public_type_bss_entry_patches_, linker_patches);
4978 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PackageTypeBssEntryPatch>(
4979 package_type_bss_entry_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004980 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4981 string_bss_entry_patches_, linker_patches);
Vladimir Markof6675082019-05-17 12:05:28 +01004982 for (const PatchInfo<vixl::aarch64::Label>& info : call_entrypoint_patches_) {
4983 DCHECK(info.target_dex_file == nullptr);
4984 linker_patches->push_back(linker::LinkerPatch::CallEntrypointPatch(
4985 info.label.GetLocation(), info.offset_or_index));
4986 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004987 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004988 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
4989 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004990 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004991 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004992}
4993
Vladimir Markoca1e0382018-04-11 09:58:41 +00004994bool CodeGeneratorARM64::NeedsThunkCode(const linker::LinkerPatch& patch) const {
Vladimir Markof6675082019-05-17 12:05:28 +01004995 return patch.GetType() == linker::LinkerPatch::Type::kCallEntrypoint ||
4996 patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
Vladimir Markoca1e0382018-04-11 09:58:41 +00004997 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
4998}
4999
5000void CodeGeneratorARM64::EmitThunkCode(const linker::LinkerPatch& patch,
5001 /*out*/ ArenaVector<uint8_t>* code,
5002 /*out*/ std::string* debug_name) {
5003 Arm64Assembler assembler(GetGraph()->GetAllocator());
5004 switch (patch.GetType()) {
5005 case linker::LinkerPatch::Type::kCallRelative: {
5006 // The thunk just uses the entry point in the ArtMethod. This works even for calls
5007 // to the generic JNI and interpreter trampolines.
5008 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
5009 kArm64PointerSize).Int32Value());
5010 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
5011 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
5012 *debug_name = "MethodCallThunk";
5013 }
5014 break;
5015 }
Vladimir Markof6675082019-05-17 12:05:28 +01005016 case linker::LinkerPatch::Type::kCallEntrypoint: {
5017 Offset offset(patch.EntrypointOffset());
5018 assembler.JumpTo(ManagedRegister(arm64::TR), offset, ManagedRegister(arm64::IP0));
5019 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
5020 *debug_name = "EntrypointCallThunk_" + std::to_string(offset.Uint32Value());
5021 }
5022 break;
5023 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00005024 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
5025 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
5026 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
5027 break;
5028 }
5029 default:
5030 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
5031 UNREACHABLE();
5032 }
5033
5034 // Ensure we emit the literal pool if any.
5035 assembler.FinalizeCode();
5036 code->resize(assembler.CodeSize());
5037 MemoryRegion code_region(code->data(), code->size());
5038 assembler.FinalizeInstructions(code_region);
5039}
5040
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005041vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
5042 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005043 value,
5044 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
5045}
5046
Scott Wakeling97c72b72016-06-24 16:19:36 +01005047vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005048 return uint64_literals_.GetOrCreate(
5049 value,
5050 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00005051}
5052
Andreas Gampe878d58c2015-01-15 23:24:00 -08005053void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005054 // Explicit clinit checks triggered by static invokes must have been pruned by
5055 // art::PrepareForRegisterAllocation.
5056 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01005057
Andreas Gampe878d58c2015-01-15 23:24:00 -08005058 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08005059 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08005060 return;
5061 }
5062
Vladimir Marko86c87522020-05-11 16:55:55 +01005063 LocationSummary* locations = invoke->GetLocations();
5064 codegen_->GenerateStaticOrDirectCall(
5065 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005066
Andreas Gampe3db70682018-12-26 15:12:03 -08005067 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005068}
5069
5070void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08005071 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08005072 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08005073 return;
5074 }
5075
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005076 {
5077 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
5078 // are no pools emitted.
5079 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
5080 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
5081 DCHECK(!codegen_->IsLeafMethod());
5082 }
5083
Andreas Gampe3db70682018-12-26 15:12:03 -08005084 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005085}
5086
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005087HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
5088 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005089 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005090 case HLoadClass::LoadKind::kInvalid:
5091 LOG(FATAL) << "UNREACHABLE";
5092 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005093 case HLoadClass::LoadKind::kReferrersClass:
5094 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005095 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005096 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005097 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8f63f102020-09-28 12:10:28 +01005098 case HLoadClass::LoadKind::kBssEntryPublic:
5099 case HLoadClass::LoadKind::kBssEntryPackage:
Vladimir Marko695348f2020-05-19 14:42:02 +01005100 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005101 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005102 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005103 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01005104 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005105 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005106 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005107 break;
5108 }
5109 return desired_class_load_kind;
5110}
5111
Alexandre Rames67555f72014-11-18 10:55:16 +00005112void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005113 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005114 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005115 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005116 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005117 cls,
5118 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005119 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00005120 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005121 return;
5122 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005123 DCHECK_EQ(cls->NeedsAccessCheck(),
5124 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
5125 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005126
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005127 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5128 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005129 ? LocationSummary::kCallOnSlowPath
5130 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005131 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005132 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005133 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005134 }
5135
Vladimir Marko41559982017-01-06 14:04:23 +00005136 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005137 locations->SetInAt(0, Location::RequiresRegister());
5138 }
5139 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005140 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
5141 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5142 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005143 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005144 } else {
5145 // For non-Baker read barrier we have a temp-clobbering call.
5146 }
5147 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005148}
5149
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005150// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5151// move.
5152void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005153 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005154 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005155 codegen_->GenerateLoadClassRuntimeCall(cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08005156 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01005157 return;
5158 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005159 DCHECK_EQ(cls->NeedsAccessCheck(),
5160 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
5161 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Calin Juravle580b6092015-10-06 17:35:58 +01005162
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005163 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01005164 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00005165
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005166 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5167 ? kWithoutReadBarrier
5168 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005169 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005170 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005171 case HLoadClass::LoadKind::kReferrersClass: {
5172 DCHECK(!cls->CanCallRuntime());
5173 DCHECK(!cls->MustGenerateClinitCheck());
5174 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5175 Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00005176 codegen_->GenerateGcRootFieldLoad(cls,
5177 out_loc,
5178 current_method,
5179 ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005180 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005181 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005182 break;
5183 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005184 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005185 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5186 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005187 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005188 // Add ADRP with its PC-relative type patch.
5189 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08005190 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005191 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005192 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005193 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005194 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005195 codegen_->NewBootImageTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005196 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005197 break;
5198 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005199 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005200 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005201 uint32_t boot_image_offset = codegen_->GetBootImageOffset(cls);
5202 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5203 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005204 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005205 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005206 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005207 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005208 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005209 break;
5210 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005211 case HLoadClass::LoadKind::kBssEntry:
5212 case HLoadClass::LoadKind::kBssEntryPublic:
5213 case HLoadClass::LoadKind::kBssEntryPackage: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005214 // Add ADRP with its PC-relative Class .bss entry patch.
Vladimir Markof3c52b42017-11-17 17:32:12 +00005215 vixl::aarch64::Register temp = XRegisterFrom(out_loc);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005216 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(cls);
Vladimir Markof3c52b42017-11-17 17:32:12 +00005217 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005218 // Add LDR with its PC-relative Class .bss entry patch.
Vladimir Marko8f63f102020-09-28 12:10:28 +01005219 vixl::aarch64::Label* ldr_label = codegen_->NewBssEntryTypePatch(cls, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005220 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005221 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005222 codegen_->GenerateGcRootFieldLoad(cls,
5223 out_loc,
5224 temp,
5225 /* offset placeholder */ 0u,
5226 ldr_label,
5227 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005228 generate_null_check = true;
5229 break;
5230 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005231 case HLoadClass::LoadKind::kJitBootImageAddress: {
5232 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5233 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
5234 DCHECK_NE(address, 0u);
5235 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5236 break;
5237 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005238 case HLoadClass::LoadKind::kJitTableAddress: {
5239 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5240 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005241 cls->GetClass()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005242 codegen_->GenerateGcRootFieldLoad(cls,
5243 out_loc,
5244 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005245 /* offset= */ 0,
5246 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005247 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005248 break;
5249 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005250 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005251 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005252 LOG(FATAL) << "UNREACHABLE";
5253 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005254 }
5255
Vladimir Markoea4c1262017-02-06 19:59:33 +00005256 bool do_clinit = cls->MustGenerateClinitCheck();
5257 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005258 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01005259 SlowPathCodeARM64* slow_path =
5260 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005261 codegen_->AddSlowPath(slow_path);
5262 if (generate_null_check) {
5263 __ Cbz(out, slow_path->GetEntryLabel());
5264 }
5265 if (cls->MustGenerateClinitCheck()) {
5266 GenerateClassInitializationCheck(slow_path, out);
5267 } else {
5268 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005269 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005270 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005271 }
5272}
5273
Orion Hodsondbaa5c72018-05-10 08:22:46 +01005274void LocationsBuilderARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5275 InvokeRuntimeCallingConvention calling_convention;
5276 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5277 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
5278}
5279
5280void InstructionCodeGeneratorARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5281 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
5282}
5283
Orion Hodson18259d72018-04-12 11:18:23 +01005284void LocationsBuilderARM64::VisitLoadMethodType(HLoadMethodType* load) {
5285 InvokeRuntimeCallingConvention calling_convention;
5286 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5287 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
5288}
5289
5290void InstructionCodeGeneratorARM64::VisitLoadMethodType(HLoadMethodType* load) {
5291 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
5292}
5293
David Brazdilcb1c0552015-08-04 16:22:25 +01005294static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005295 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005296}
5297
Alexandre Rames67555f72014-11-18 10:55:16 +00005298void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5299 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005300 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00005301 locations->SetOut(Location::RequiresRegister());
5302}
5303
5304void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005305 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5306}
5307
5308void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005309 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005310}
5311
5312void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5313 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005314}
5315
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005316HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5317 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005318 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005319 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005320 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005321 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01005322 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005323 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005324 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005325 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01005326 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005327 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005328 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005329 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005330 }
5331 return desired_string_load_kind;
5332}
5333
Alexandre Rames67555f72014-11-18 10:55:16 +00005334void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005335 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005336 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005337 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005338 InvokeRuntimeCallingConvention calling_convention;
5339 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5340 } else {
5341 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005342 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5343 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005344 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005345 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005346 } else {
5347 // For non-Baker read barrier we have a temp-clobbering call.
5348 }
5349 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005350 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005351}
5352
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005353// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5354// move.
5355void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005356 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005357 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005358
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005359 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005360 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005361 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5362 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005363 // Add ADRP with its PC-relative String patch.
5364 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005365 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005366 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005367 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005368 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005369 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005370 codegen_->NewBootImageStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005371 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005372 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005373 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005374 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005375 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005376 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5377 uint32_t boot_image_offset = codegen_->GetBootImageOffset(load);
5378 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005379 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005380 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005381 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005382 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005383 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
5384 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005385 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005386 case HLoadString::LoadKind::kBssEntry: {
5387 // Add ADRP with its PC-relative String .bss entry patch.
5388 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005389 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00005390 Register temp = XRegisterFrom(out_loc);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005391 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005392 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005393 // Add LDR with its PC-relative String .bss entry patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005394 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005395 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005396 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005397 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005398 codegen_->GenerateGcRootFieldLoad(load,
5399 out_loc,
5400 temp,
5401 /* offset placeholder */ 0u,
5402 ldr_label,
5403 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005404 SlowPathCodeARM64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005405 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005406 codegen_->AddSlowPath(slow_path);
5407 __ Cbz(out.X(), slow_path->GetEntryLabel());
5408 __ Bind(slow_path->GetExitLabel());
Andreas Gampe3db70682018-12-26 15:12:03 -08005409 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005410 return;
5411 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005412 case HLoadString::LoadKind::kJitBootImageAddress: {
5413 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
5414 DCHECK_NE(address, 0u);
5415 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5416 return;
5417 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005418 case HLoadString::LoadKind::kJitTableAddress: {
5419 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005420 load->GetStringIndex(),
5421 load->GetString()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005422 codegen_->GenerateGcRootFieldLoad(load,
5423 out_loc,
5424 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005425 /* offset= */ 0,
5426 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005427 kCompilerReadBarrierOption);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005428 return;
5429 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005430 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005431 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005432 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005433
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005434 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005435 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005436 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005437 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005438 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5439 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005440 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005441}
5442
Alexandre Rames5319def2014-10-23 10:03:10 +01005443void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005444 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01005445 locations->SetOut(Location::ConstantLocation(constant));
5446}
5447
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005448void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005449 // Will be generated at use site.
5450}
5451
Alexandre Rames67555f72014-11-18 10:55:16 +00005452void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005453 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5454 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005455 InvokeRuntimeCallingConvention calling_convention;
5456 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5457}
5458
5459void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005460 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005461 instruction,
5462 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005463 if (instruction->IsEnter()) {
5464 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5465 } else {
5466 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5467 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005468 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005469}
5470
Alexandre Rames42d641b2014-10-27 14:00:51 +00005471void LocationsBuilderARM64::VisitMul(HMul* mul) {
5472 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005473 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005474 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005475 case DataType::Type::kInt32:
5476 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005477 locations->SetInAt(0, Location::RequiresRegister());
5478 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005479 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005480 break;
5481
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005482 case DataType::Type::kFloat32:
5483 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005484 locations->SetInAt(0, Location::RequiresFpuRegister());
5485 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005486 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005487 break;
5488
5489 default:
5490 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5491 }
5492}
5493
5494void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5495 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005496 case DataType::Type::kInt32:
5497 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005498 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5499 break;
5500
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005501 case DataType::Type::kFloat32:
5502 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005503 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005504 break;
5505
5506 default:
5507 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5508 }
5509}
5510
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005511void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5512 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005513 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005514 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005515 case DataType::Type::kInt32:
5516 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005517 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005518 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005519 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005520
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005521 case DataType::Type::kFloat32:
5522 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005523 locations->SetInAt(0, Location::RequiresFpuRegister());
5524 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005525 break;
5526
5527 default:
5528 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5529 }
5530}
5531
5532void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5533 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005534 case DataType::Type::kInt32:
5535 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005536 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5537 break;
5538
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005539 case DataType::Type::kFloat32:
5540 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005541 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005542 break;
5543
5544 default:
5545 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5546 }
5547}
5548
5549void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005550 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5551 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005552 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005553 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005554 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5555 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005556}
5557
5558void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005559 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5560 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005561 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005562 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005563 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005564}
5565
Alexandre Rames5319def2014-10-23 10:03:10 +01005566void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005567 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5568 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005569 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07005570 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005571 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005572}
5573
5574void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005575 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5576 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005577 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005578}
5579
5580void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005581 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005582 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005583 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005584}
5585
5586void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005587 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005588 case DataType::Type::kInt32:
5589 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005590 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005591 break;
5592
5593 default:
5594 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5595 }
5596}
5597
David Brazdil66d126e2015-04-03 16:02:44 +01005598void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005599 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005600 locations->SetInAt(0, Location::RequiresRegister());
5601 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5602}
5603
5604void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005605 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005606}
5607
Alexandre Rames5319def2014-10-23 10:03:10 +01005608void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005609 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5610 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005611}
5612
Calin Juravle2ae48182016-03-16 14:05:09 +00005613void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5614 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005615 return;
5616 }
Artem Serov914d7a82017-02-07 14:33:49 +00005617 {
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01005618 // Ensure that between load and RecordPcInfo there are no pools emitted.
Artem Serov914d7a82017-02-07 14:33:49 +00005619 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5620 Location obj = instruction->GetLocations()->InAt(0);
5621 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5622 RecordPcInfo(instruction, instruction->GetDexPc());
5623 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005624}
5625
Calin Juravle2ae48182016-03-16 14:05:09 +00005626void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005627 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005628 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005629
5630 LocationSummary* locations = instruction->GetLocations();
5631 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005632
5633 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005634}
5635
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005636void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005637 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005638}
5639
Alexandre Rames67555f72014-11-18 10:55:16 +00005640void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5641 HandleBinaryOp(instruction);
5642}
5643
5644void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5645 HandleBinaryOp(instruction);
5646}
5647
Alexandre Rames3e69f162014-12-10 10:36:50 +00005648void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5649 LOG(FATAL) << "Unreachable";
5650}
5651
5652void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005653 if (instruction->GetNext()->IsSuspendCheck() &&
5654 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5655 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5656 // The back edge will generate the suspend check.
5657 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5658 }
5659
Alexandre Rames3e69f162014-12-10 10:36:50 +00005660 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5661}
5662
Alexandre Rames5319def2014-10-23 10:03:10 +01005663void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005664 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005665 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5666 if (location.IsStackSlot()) {
5667 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5668 } else if (location.IsDoubleStackSlot()) {
5669 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5670 }
5671 locations->SetOut(location);
5672}
5673
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005674void InstructionCodeGeneratorARM64::VisitParameterValue(
5675 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005676 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005677}
5678
5679void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5680 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005681 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005682 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005683}
5684
5685void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5686 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5687 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005688}
5689
5690void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005691 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005692 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005693 locations->SetInAt(i, Location::Any());
5694 }
5695 locations->SetOut(Location::Any());
5696}
5697
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005698void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005699 LOG(FATAL) << "Unreachable";
5700}
5701
Serban Constantinescu02164b32014-11-13 14:05:07 +00005702void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005703 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005704 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005705 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005706 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005707 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005708
5709 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005710 case DataType::Type::kInt32:
5711 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005712 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005713 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005714 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5715 break;
5716
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005717 case DataType::Type::kFloat32:
5718 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005719 InvokeRuntimeCallingConvention calling_convention;
5720 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5721 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5722 locations->SetOut(calling_convention.GetReturnLocation(type));
5723
5724 break;
5725 }
5726
Serban Constantinescu02164b32014-11-13 14:05:07 +00005727 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005728 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005729 }
5730}
5731
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005732void InstructionCodeGeneratorARM64::GenerateIntRemForPower2Denom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005733 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005734 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
5735 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
5736
5737 Register out = OutputRegister(instruction);
5738 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005739
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01005740 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01005741 // No need to adjust the result for non-negative dividends or the INT32_MIN/INT64_MIN dividends.
5742 // NOTE: The generated code for HRem correctly works for the INT32_MIN/INT64_MIN dividends.
5743 // INT*_MIN % imm must be 0 for any imm of power 2. 'and' works only with bits
5744 // 0..30 (Int32 case)/0..62 (Int64 case) of a dividend. For INT32_MIN/INT64_MIN they are zeros.
5745 // So 'and' always produces zero.
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005746 __ And(out, dividend, abs_imm - 1);
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01005747 } else {
5748 if (abs_imm == 2) {
5749 __ Cmp(dividend, 0);
5750 __ And(out, dividend, 1);
5751 __ Csneg(out, out, out, ge);
5752 } else {
5753 UseScratchRegisterScope temps(GetVIXLAssembler());
5754 Register temp = temps.AcquireSameSizeAs(out);
5755
5756 __ Negs(temp, dividend);
5757 __ And(out, dividend, abs_imm - 1);
5758 __ And(temp, temp, abs_imm - 1);
5759 __ Csneg(out, out, temp, mi);
5760 }
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005761 }
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005762}
5763
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005764void InstructionCodeGeneratorARM64::GenerateIntRemForConstDenom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005765 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005766
5767 if (imm == 0) {
5768 // Do not generate anything.
5769 // DivZeroCheck would prevent any code to be executed.
5770 return;
5771 }
5772
Evgeny Astigeevichf58dc652018-06-25 17:54:07 +01005773 if (IsPowerOfTwo(AbsOrMin(imm))) {
5774 // Cases imm == -1 or imm == 1 are handled in constant folding by
5775 // InstructionWithAbsorbingInputSimplifier.
5776 // If the cases have survided till code generation they are handled in
5777 // GenerateIntRemForPower2Denom becauses -1 and 1 are the power of 2 (2^0).
5778 // The correct code is generated for them, just more instructions.
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005779 GenerateIntRemForPower2Denom(instruction);
5780 } else {
5781 DCHECK(imm < -2 || imm > 2) << imm;
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01005782 GenerateDivRemWithAnyConstant(instruction, imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005783 }
5784}
5785
5786void InstructionCodeGeneratorARM64::GenerateIntRem(HRem* instruction) {
5787 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
5788 << instruction->GetResultType();
5789
5790 if (instruction->GetLocations()->InAt(1).IsConstant()) {
5791 GenerateIntRemForConstDenom(instruction);
5792 } else {
5793 Register out = OutputRegister(instruction);
5794 Register dividend = InputRegisterAt(instruction, 0);
5795 Register divisor = InputRegisterAt(instruction, 1);
5796 UseScratchRegisterScope temps(GetVIXLAssembler());
5797 Register temp = temps.AcquireSameSizeAs(out);
5798 __ Sdiv(temp, dividend, divisor);
5799 __ Msub(out, temp, divisor, dividend);
5800 }
5801}
5802
Serban Constantinescu02164b32014-11-13 14:05:07 +00005803void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005804 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005805
Serban Constantinescu02164b32014-11-13 14:05:07 +00005806 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005807 case DataType::Type::kInt32:
5808 case DataType::Type::kInt64: {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005809 GenerateIntRem(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005810 break;
5811 }
5812
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005813 case DataType::Type::kFloat32:
5814 case DataType::Type::kFloat64: {
5815 QuickEntrypointEnum entrypoint =
5816 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005817 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005818 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00005819 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5820 } else {
5821 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5822 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005823 break;
5824 }
5825
Serban Constantinescu02164b32014-11-13 14:05:07 +00005826 default:
5827 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005828 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005829 }
5830}
5831
Aart Bik1f8d51b2018-02-15 10:42:37 -08005832void LocationsBuilderARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005833 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005834}
5835
Aart Bik1f8d51b2018-02-15 10:42:37 -08005836void InstructionCodeGeneratorARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005837 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005838}
5839
5840void LocationsBuilderARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005841 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005842}
5843
5844void InstructionCodeGeneratorARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005845 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005846}
5847
Aart Bik3dad3412018-02-28 12:01:46 -08005848void LocationsBuilderARM64::VisitAbs(HAbs* abs) {
5849 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
5850 switch (abs->GetResultType()) {
5851 case DataType::Type::kInt32:
5852 case DataType::Type::kInt64:
5853 locations->SetInAt(0, Location::RequiresRegister());
5854 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5855 break;
5856 case DataType::Type::kFloat32:
5857 case DataType::Type::kFloat64:
5858 locations->SetInAt(0, Location::RequiresFpuRegister());
5859 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5860 break;
5861 default:
5862 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5863 }
5864}
5865
5866void InstructionCodeGeneratorARM64::VisitAbs(HAbs* abs) {
5867 switch (abs->GetResultType()) {
5868 case DataType::Type::kInt32:
5869 case DataType::Type::kInt64: {
5870 Register in_reg = InputRegisterAt(abs, 0);
5871 Register out_reg = OutputRegister(abs);
5872 __ Cmp(in_reg, Operand(0));
5873 __ Cneg(out_reg, in_reg, lt);
5874 break;
5875 }
5876 case DataType::Type::kFloat32:
5877 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01005878 VRegister in_reg = InputFPRegisterAt(abs, 0);
5879 VRegister out_reg = OutputFPRegister(abs);
Aart Bik3dad3412018-02-28 12:01:46 -08005880 __ Fabs(out_reg, in_reg);
5881 break;
5882 }
5883 default:
5884 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5885 }
5886}
5887
Igor Murashkind01745e2017-04-05 16:40:31 -07005888void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5889 constructor_fence->SetLocations(nullptr);
5890}
5891
5892void InstructionCodeGeneratorARM64::VisitConstructorFence(
5893 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5894 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5895}
5896
Calin Juravle27df7582015-04-17 19:12:31 +01005897void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5898 memory_barrier->SetLocations(nullptr);
5899}
5900
5901void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005902 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005903}
5904
Alexandre Rames5319def2014-10-23 10:03:10 +01005905void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005906 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005907 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005908 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005909}
5910
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00005911void InstructionCodeGeneratorARM64::VisitReturn(HReturn* ret) {
5912 if (GetGraph()->IsCompilingOsr()) {
5913 // To simplify callers of an OSR method, we put the return value in both
5914 // floating point and core register.
5915 switch (ret->InputAt(0)->GetType()) {
5916 case DataType::Type::kFloat32:
5917 __ Fmov(w0, s0);
5918 break;
5919 case DataType::Type::kFloat64:
5920 __ Fmov(x0, d0);
5921 break;
5922 default:
5923 break;
5924 }
5925 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005926 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005927}
5928
5929void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5930 instruction->SetLocations(nullptr);
5931}
5932
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005933void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005934 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005935}
5936
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005937void LocationsBuilderARM64::VisitRor(HRor* ror) {
5938 HandleBinaryOp(ror);
5939}
5940
5941void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5942 HandleBinaryOp(ror);
5943}
5944
Serban Constantinescu02164b32014-11-13 14:05:07 +00005945void LocationsBuilderARM64::VisitShl(HShl* shl) {
5946 HandleShift(shl);
5947}
5948
5949void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5950 HandleShift(shl);
5951}
5952
5953void LocationsBuilderARM64::VisitShr(HShr* shr) {
5954 HandleShift(shr);
5955}
5956
5957void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5958 HandleShift(shr);
5959}
5960
Alexandre Rames5319def2014-10-23 10:03:10 +01005961void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005962 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005963}
5964
5965void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005966 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005967}
5968
Alexandre Rames67555f72014-11-18 10:55:16 +00005969void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005970 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005971}
5972
5973void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005974 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005975}
5976
5977void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005978 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005979}
5980
Alexandre Rames67555f72014-11-18 10:55:16 +00005981void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005982 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005983}
5984
Vladimir Marko552a1342017-10-31 10:56:47 +00005985void LocationsBuilderARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5986 codegen_->CreateStringBuilderAppendLocations(instruction, LocationFrom(x0));
5987}
5988
5989void InstructionCodeGeneratorARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5990 __ Mov(w0, instruction->GetFormat()->GetValue());
5991 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
5992}
5993
Calin Juravlee460d1d2015-09-29 04:52:17 +01005994void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5995 HUnresolvedInstanceFieldGet* instruction) {
5996 FieldAccessCallingConventionARM64 calling_convention;
5997 codegen_->CreateUnresolvedFieldLocationSummary(
5998 instruction, instruction->GetFieldType(), calling_convention);
5999}
6000
6001void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
6002 HUnresolvedInstanceFieldGet* instruction) {
6003 FieldAccessCallingConventionARM64 calling_convention;
6004 codegen_->GenerateUnresolvedFieldAccess(instruction,
6005 instruction->GetFieldType(),
6006 instruction->GetFieldIndex(),
6007 instruction->GetDexPc(),
6008 calling_convention);
6009}
6010
6011void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
6012 HUnresolvedInstanceFieldSet* instruction) {
6013 FieldAccessCallingConventionARM64 calling_convention;
6014 codegen_->CreateUnresolvedFieldLocationSummary(
6015 instruction, instruction->GetFieldType(), calling_convention);
6016}
6017
6018void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
6019 HUnresolvedInstanceFieldSet* instruction) {
6020 FieldAccessCallingConventionARM64 calling_convention;
6021 codegen_->GenerateUnresolvedFieldAccess(instruction,
6022 instruction->GetFieldType(),
6023 instruction->GetFieldIndex(),
6024 instruction->GetDexPc(),
6025 calling_convention);
6026}
6027
6028void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
6029 HUnresolvedStaticFieldGet* instruction) {
6030 FieldAccessCallingConventionARM64 calling_convention;
6031 codegen_->CreateUnresolvedFieldLocationSummary(
6032 instruction, instruction->GetFieldType(), calling_convention);
6033}
6034
6035void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
6036 HUnresolvedStaticFieldGet* instruction) {
6037 FieldAccessCallingConventionARM64 calling_convention;
6038 codegen_->GenerateUnresolvedFieldAccess(instruction,
6039 instruction->GetFieldType(),
6040 instruction->GetFieldIndex(),
6041 instruction->GetDexPc(),
6042 calling_convention);
6043}
6044
6045void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
6046 HUnresolvedStaticFieldSet* instruction) {
6047 FieldAccessCallingConventionARM64 calling_convention;
6048 codegen_->CreateUnresolvedFieldLocationSummary(
6049 instruction, instruction->GetFieldType(), calling_convention);
6050}
6051
6052void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
6053 HUnresolvedStaticFieldSet* instruction) {
6054 FieldAccessCallingConventionARM64 calling_convention;
6055 codegen_->GenerateUnresolvedFieldAccess(instruction,
6056 instruction->GetFieldType(),
6057 instruction->GetFieldIndex(),
6058 instruction->GetDexPc(),
6059 calling_convention);
6060}
6061
Alexandre Rames5319def2014-10-23 10:03:10 +01006062void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006063 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6064 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01006065 // In suspend check slow path, usually there are no caller-save registers at all.
6066 // If SIMD instructions are present, however, we force spilling all live SIMD
6067 // registers in full width (since the runtime only saves/restores lower part).
6068 locations->SetCustomSlowPathCallerSaves(
6069 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01006070}
6071
6072void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006073 HBasicBlock* block = instruction->GetBlock();
6074 if (block->GetLoopInformation() != nullptr) {
6075 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6076 // The back edge will generate the suspend check.
6077 return;
6078 }
6079 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6080 // The goto will generate the suspend check.
6081 return;
6082 }
6083 GenerateSuspendCheck(instruction, nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08006084 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01006085}
6086
Alexandre Rames67555f72014-11-18 10:55:16 +00006087void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006088 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6089 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00006090 InvokeRuntimeCallingConvention calling_convention;
6091 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
6092}
6093
6094void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00006095 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08006096 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00006097}
6098
6099void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
6100 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006101 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006102 DataType::Type input_type = conversion->GetInputType();
6103 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006104 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6105 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006106 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
6107 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006108 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6109 }
6110
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006111 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006112 locations->SetInAt(0, Location::RequiresFpuRegister());
6113 } else {
6114 locations->SetInAt(0, Location::RequiresRegister());
6115 }
6116
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006117 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006118 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6119 } else {
6120 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6121 }
6122}
6123
6124void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006125 DataType::Type result_type = conversion->GetResultType();
6126 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00006127
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006128 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6129 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006130
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006131 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
6132 int result_size = DataType::Size(result_type);
6133 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00006134 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006135 Register output = OutputRegister(conversion);
6136 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006137 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01006138 // 'int' values are used directly as W registers, discarding the top
6139 // bits, so we don't need to sign-extend and can just perform a move.
6140 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
6141 // top 32 bits of the target register. We theoretically could leave those
6142 // bits unchanged, but we would have to make sure that no code uses a
6143 // 32bit input value as a 64bit value assuming that the top 32 bits are
6144 // zero.
6145 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006146 } else if (DataType::IsUnsignedType(result_type) ||
6147 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
6148 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006149 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00006150 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006151 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006152 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006153 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006154 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6155 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006156 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006157 } else if (DataType::IsFloatingPointType(result_type) &&
6158 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006159 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
6160 } else {
6161 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6162 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006163 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00006164}
Alexandre Rames67555f72014-11-18 10:55:16 +00006165
Serban Constantinescu02164b32014-11-13 14:05:07 +00006166void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
6167 HandleShift(ushr);
6168}
6169
6170void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
6171 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00006172}
6173
6174void LocationsBuilderARM64::VisitXor(HXor* instruction) {
6175 HandleBinaryOp(instruction);
6176}
6177
6178void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
6179 HandleBinaryOp(instruction);
6180}
6181
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006182void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006183 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006184 LOG(FATAL) << "Unreachable";
6185}
6186
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006187void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006188 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006189 LOG(FATAL) << "Unreachable";
6190}
6191
Mark Mendellfe57faa2015-09-18 09:26:15 -04006192// Simple implementation of packed switch - generate cascaded compare/jumps.
6193void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6194 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006195 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006196 locations->SetInAt(0, Location::RequiresRegister());
6197}
6198
6199void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6200 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08006201 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006202 Register value_reg = InputRegisterAt(switch_instr, 0);
6203 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6204
Zheng Xu3927c8b2015-11-18 17:46:25 +08006205 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01006206 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08006207 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
6208 // make sure we don't emit it if the target may run out of range.
6209 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
6210 // ranges and emit the tables only as required.
6211 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04006212
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006213 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08006214 // Current instruction id is an upper bound of the number of HIRs in the graph.
6215 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
6216 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006217 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6218 Register temp = temps.AcquireW();
6219 __ Subs(temp, value_reg, Operand(lower_bound));
6220
Zheng Xu3927c8b2015-11-18 17:46:25 +08006221 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006222 // Jump to successors[0] if value == lower_bound.
6223 __ B(eq, codegen_->GetLabelOf(successors[0]));
6224 int32_t last_index = 0;
6225 for (; num_entries - last_index > 2; last_index += 2) {
6226 __ Subs(temp, temp, Operand(2));
6227 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6228 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
6229 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6230 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
6231 }
6232 if (num_entries - last_index == 2) {
6233 // The last missing case_value.
6234 __ Cmp(temp, Operand(1));
6235 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08006236 }
6237
6238 // And the default for any other value.
6239 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6240 __ B(codegen_->GetLabelOf(default_block));
6241 }
6242 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01006243 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08006244
6245 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6246
6247 // Below instructions should use at most one blocked register. Since there are two blocked
6248 // registers, we are free to block one.
6249 Register temp_w = temps.AcquireW();
6250 Register index;
6251 // Remove the bias.
6252 if (lower_bound != 0) {
6253 index = temp_w;
6254 __ Sub(index, value_reg, Operand(lower_bound));
6255 } else {
6256 index = value_reg;
6257 }
6258
6259 // Jump to default block if index is out of the range.
6260 __ Cmp(index, Operand(num_entries));
6261 __ B(hs, codegen_->GetLabelOf(default_block));
6262
6263 // In current VIXL implementation, it won't require any blocked registers to encode the
6264 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
6265 // register pressure.
6266 Register table_base = temps.AcquireX();
6267 // Load jump offset from the table.
6268 __ Adr(table_base, jump_table->GetTableStartLabel());
6269 Register jump_offset = temp_w;
6270 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
6271
6272 // Jump to target block by branching to table_base(pc related) + offset.
6273 Register target_address = table_base;
6274 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
6275 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006276 }
6277}
6278
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006279void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
6280 HInstruction* instruction,
6281 Location out,
6282 uint32_t offset,
6283 Location maybe_temp,
6284 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006285 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006286 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006287 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006288 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006289 if (kUseBakerReadBarrier) {
6290 // Load with fast path based Baker's read barrier.
6291 // /* HeapReference<Object> */ out = *(out + offset)
6292 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6293 out,
6294 out_reg,
6295 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006296 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006297 /* needs_null_check= */ false,
6298 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006299 } else {
6300 // Load with slow path based read barrier.
6301 // Save the value of `out` into `maybe_temp` before overwriting it
6302 // in the following move operation, as we will need it for the
6303 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006304 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00006305 __ Mov(temp_reg, out_reg);
6306 // /* HeapReference<Object> */ out = *(out + offset)
6307 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6308 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6309 }
6310 } else {
6311 // Plain load with no read barrier.
6312 // /* HeapReference<Object> */ out = *(out + offset)
6313 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6314 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6315 }
6316}
6317
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006318void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
6319 HInstruction* instruction,
6320 Location out,
6321 Location obj,
6322 uint32_t offset,
6323 Location maybe_temp,
6324 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006325 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006326 Register out_reg = RegisterFrom(out, type);
6327 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006328 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006329 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006330 if (kUseBakerReadBarrier) {
6331 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00006332 // /* HeapReference<Object> */ out = *(obj + offset)
6333 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6334 out,
6335 obj_reg,
6336 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006337 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006338 /* needs_null_check= */ false,
6339 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006340 } else {
6341 // Load with slow path based read barrier.
6342 // /* HeapReference<Object> */ out = *(obj + offset)
6343 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6344 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6345 }
6346 } else {
6347 // Plain load with no read barrier.
6348 // /* HeapReference<Object> */ out = *(obj + offset)
6349 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6350 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6351 }
6352}
6353
Vladimir Markoca1e0382018-04-11 09:58:41 +00006354void CodeGeneratorARM64::GenerateGcRootFieldLoad(
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006355 HInstruction* instruction,
6356 Location root,
6357 Register obj,
6358 uint32_t offset,
6359 vixl::aarch64::Label* fixup_label,
6360 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006361 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006362 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006363 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006364 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006365 if (kUseBakerReadBarrier) {
6366 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00006367 // Baker's read barrier are used.
Roland Levillain44015862016-01-22 11:47:17 +00006368
Vladimir Marko008e09f32018-08-06 15:42:43 +01006369 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6370 // the Marking Register) to decide whether we need to enter
6371 // the slow path to mark the GC root.
6372 //
6373 // We use shared thunks for the slow path; shared within the method
6374 // for JIT, across methods for AOT. That thunk checks the reference
6375 // and jumps to the entrypoint if needed.
6376 //
6377 // lr = &return_address;
6378 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6379 // if (mr) { // Thread::Current()->GetIsGcMarking()
6380 // goto gc_root_thunk<root_reg>(lr)
6381 // }
6382 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00006383
Vladimir Marko008e09f32018-08-06 15:42:43 +01006384 UseScratchRegisterScope temps(GetVIXLAssembler());
6385 DCHECK(temps.IsAvailable(ip0));
6386 DCHECK(temps.IsAvailable(ip1));
6387 temps.Exclude(ip0, ip1);
6388 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
Roland Levillain44015862016-01-22 11:47:17 +00006389
Vladimir Marko008e09f32018-08-06 15:42:43 +01006390 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6391 vixl::aarch64::Label return_address;
6392 __ adr(lr, &return_address);
6393 if (fixup_label != nullptr) {
6394 __ bind(fixup_label);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006395 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006396 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
Vladimir Marko94796f82018-08-08 15:15:33 +01006397 "GC root LDR must be 2 instructions (8B) before the return address label.");
Vladimir Marko008e09f32018-08-06 15:42:43 +01006398 __ ldr(root_reg, MemOperand(obj.X(), offset));
6399 EmitBakerReadBarrierCbnz(custom_data);
6400 __ bind(&return_address);
Roland Levillain44015862016-01-22 11:47:17 +00006401 } else {
6402 // GC root loaded through a slow path for read barriers other
6403 // than Baker's.
6404 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006405 if (fixup_label == nullptr) {
6406 __ Add(root_reg.X(), obj.X(), offset);
6407 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006408 EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006409 }
Roland Levillain44015862016-01-22 11:47:17 +00006410 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00006411 GenerateReadBarrierForRootSlow(instruction, root, root);
Roland Levillain44015862016-01-22 11:47:17 +00006412 }
6413 } else {
6414 // Plain GC root load with no read barrier.
6415 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006416 if (fixup_label == nullptr) {
6417 __ Ldr(root_reg, MemOperand(obj, offset));
6418 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006419 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006420 }
Roland Levillain44015862016-01-22 11:47:17 +00006421 // Note that GC roots are not affected by heap poisoning, thus we
6422 // do not have to unpoison `root_reg` here.
6423 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006424 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00006425}
6426
Vladimir Marko94796f82018-08-08 15:15:33 +01006427void CodeGeneratorARM64::GenerateUnsafeCasOldValueMovWithBakerReadBarrier(
6428 vixl::aarch64::Register marked,
6429 vixl::aarch64::Register old_value) {
6430 DCHECK(kEmitCompilerReadBarrier);
6431 DCHECK(kUseBakerReadBarrier);
6432
6433 // Similar to the Baker RB path in GenerateGcRootFieldLoad(), with a MOV instead of LDR.
6434 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(marked.GetCode());
6435
6436 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6437 vixl::aarch64::Label return_address;
6438 __ adr(lr, &return_address);
6439 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6440 "GC root LDR must be 2 instructions (8B) before the return address label.");
6441 __ mov(marked, old_value);
6442 EmitBakerReadBarrierCbnz(custom_data);
6443 __ bind(&return_address);
6444}
6445
Roland Levillain44015862016-01-22 11:47:17 +00006446void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6447 Location ref,
Vladimir Marko248141f2018-08-10 10:40:07 +01006448 vixl::aarch64::Register obj,
6449 const vixl::aarch64::MemOperand& src,
Roland Levillain44015862016-01-22 11:47:17 +00006450 bool needs_null_check,
6451 bool use_load_acquire) {
6452 DCHECK(kEmitCompilerReadBarrier);
6453 DCHECK(kUseBakerReadBarrier);
6454
Vladimir Marko0ecac682018-08-07 10:40:38 +01006455 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6456 // Marking Register) to decide whether we need to enter the slow
6457 // path to mark the reference. Then, in the slow path, check the
6458 // gray bit in the lock word of the reference's holder (`obj`) to
6459 // decide whether to mark `ref` or not.
6460 //
6461 // We use shared thunks for the slow path; shared within the method
6462 // for JIT, across methods for AOT. That thunk checks the holder
6463 // and jumps to the entrypoint if needed. If the holder is not gray,
6464 // it creates a fake dependency and returns to the LDR instruction.
6465 //
6466 // lr = &gray_return_address;
6467 // if (mr) { // Thread::Current()->GetIsGcMarking()
6468 // goto field_thunk<holder_reg, base_reg, use_load_acquire>(lr)
6469 // }
6470 // not_gray_return_address:
6471 // // Original reference load. If the offset is too large to fit
6472 // // into LDR, we use an adjusted base register here.
6473 // HeapReference<mirror::Object> reference = *(obj+offset);
6474 // gray_return_address:
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006475
Vladimir Marko248141f2018-08-10 10:40:07 +01006476 DCHECK(src.GetAddrMode() == vixl::aarch64::Offset);
6477 DCHECK_ALIGNED(src.GetOffset(), sizeof(mirror::HeapReference<mirror::Object>));
6478
6479 UseScratchRegisterScope temps(GetVIXLAssembler());
6480 DCHECK(temps.IsAvailable(ip0));
6481 DCHECK(temps.IsAvailable(ip1));
6482 temps.Exclude(ip0, ip1);
6483 uint32_t custom_data = use_load_acquire
6484 ? EncodeBakerReadBarrierAcquireData(src.GetBaseRegister().GetCode(), obj.GetCode())
6485 : EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode());
6486
6487 {
6488 ExactAssemblyScope guard(GetVIXLAssembler(),
6489 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6490 vixl::aarch64::Label return_address;
6491 __ adr(lr, &return_address);
6492 EmitBakerReadBarrierCbnz(custom_data);
6493 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6494 "Field LDR must be 1 instruction (4B) before the return address label; "
6495 " 2 instructions (8B) for heap poisoning.");
6496 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
6497 if (use_load_acquire) {
6498 DCHECK_EQ(src.GetOffset(), 0);
6499 __ ldar(ref_reg, src);
6500 } else {
6501 __ ldr(ref_reg, src);
6502 }
6503 if (needs_null_check) {
6504 MaybeRecordImplicitNullCheck(instruction);
6505 }
6506 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6507 // macro instructions disallowed in ExactAssemblyScope.
6508 if (kPoisonHeapReferences) {
6509 __ neg(ref_reg, Operand(ref_reg));
6510 }
6511 __ bind(&return_address);
6512 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006513 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Vladimir Marko248141f2018-08-10 10:40:07 +01006514}
6515
6516void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6517 Location ref,
6518 Register obj,
6519 uint32_t offset,
6520 Location maybe_temp,
6521 bool needs_null_check,
6522 bool use_load_acquire) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006523 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6524 Register base = obj;
6525 if (use_load_acquire) {
6526 DCHECK(maybe_temp.IsRegister());
6527 base = WRegisterFrom(maybe_temp);
6528 __ Add(base, obj, offset);
6529 offset = 0u;
6530 } else if (offset >= kReferenceLoadMinFarOffset) {
6531 DCHECK(maybe_temp.IsRegister());
6532 base = WRegisterFrom(maybe_temp);
6533 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6534 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6535 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006536 }
Vladimir Marko248141f2018-08-10 10:40:07 +01006537 MemOperand src(base.X(), offset);
6538 GenerateFieldLoadWithBakerReadBarrier(
6539 instruction, ref, obj, src, needs_null_check, use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006540}
6541
Artem Serov0806f582018-10-11 20:14:20 +01006542void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HArrayGet* instruction,
6543 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006544 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006545 uint32_t data_offset,
6546 Location index,
Roland Levillain44015862016-01-22 11:47:17 +00006547 bool needs_null_check) {
6548 DCHECK(kEmitCompilerReadBarrier);
6549 DCHECK(kUseBakerReadBarrier);
6550
Vladimir Marko66d691d2017-04-07 17:53:39 +01006551 static_assert(
6552 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6553 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006554 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006555
Vladimir Marko008e09f32018-08-06 15:42:43 +01006556 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6557 // Marking Register) to decide whether we need to enter the slow
6558 // path to mark the reference. Then, in the slow path, check the
6559 // gray bit in the lock word of the reference's holder (`obj`) to
6560 // decide whether to mark `ref` or not.
6561 //
6562 // We use shared thunks for the slow path; shared within the method
6563 // for JIT, across methods for AOT. That thunk checks the holder
6564 // and jumps to the entrypoint if needed. If the holder is not gray,
6565 // it creates a fake dependency and returns to the LDR instruction.
6566 //
6567 // lr = &gray_return_address;
6568 // if (mr) { // Thread::Current()->GetIsGcMarking()
6569 // goto array_thunk<base_reg>(lr)
6570 // }
6571 // not_gray_return_address:
6572 // // Original reference load. If the offset is too large to fit
6573 // // into LDR, we use an adjusted base register here.
6574 // HeapReference<mirror::Object> reference = data[index];
6575 // gray_return_address:
Vladimir Marko66d691d2017-04-07 17:53:39 +01006576
Vladimir Marko008e09f32018-08-06 15:42:43 +01006577 DCHECK(index.IsValid());
6578 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
6579 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006580
Vladimir Marko008e09f32018-08-06 15:42:43 +01006581 UseScratchRegisterScope temps(GetVIXLAssembler());
6582 DCHECK(temps.IsAvailable(ip0));
6583 DCHECK(temps.IsAvailable(ip1));
6584 temps.Exclude(ip0, ip1);
Artem Serov0806f582018-10-11 20:14:20 +01006585
6586 Register temp;
6587 if (instruction->GetArray()->IsIntermediateAddress()) {
6588 // We do not need to compute the intermediate address from the array: the
6589 // input instruction has done it already. See the comment in
6590 // `TryExtractArrayAccessAddress()`.
6591 if (kIsDebugBuild) {
6592 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
6593 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
6594 }
6595 temp = obj;
6596 } else {
6597 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
6598 __ Add(temp.X(), obj.X(), Operand(data_offset));
6599 }
6600
Vladimir Marko008e09f32018-08-06 15:42:43 +01006601 uint32_t custom_data = EncodeBakerReadBarrierArrayData(temp.GetCode());
Vladimir Marko66d691d2017-04-07 17:53:39 +01006602
Vladimir Marko008e09f32018-08-06 15:42:43 +01006603 {
6604 ExactAssemblyScope guard(GetVIXLAssembler(),
6605 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6606 vixl::aarch64::Label return_address;
6607 __ adr(lr, &return_address);
6608 EmitBakerReadBarrierCbnz(custom_data);
6609 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6610 "Array LDR must be 1 instruction (4B) before the return address label; "
6611 " 2 instructions (8B) for heap poisoning.");
6612 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6613 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6614 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6615 // macro instructions disallowed in ExactAssemblyScope.
6616 if (kPoisonHeapReferences) {
6617 __ neg(ref_reg, Operand(ref_reg));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006618 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006619 __ bind(&return_address);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006620 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006621 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Roland Levillain44015862016-01-22 11:47:17 +00006622}
6623
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006624void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6625 // The following condition is a compile-time one, so it does not have a run-time cost.
6626 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
6627 // The following condition is a run-time one; it is executed after the
6628 // previous compile-time test, to avoid penalizing non-debug builds.
6629 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6630 UseScratchRegisterScope temps(GetVIXLAssembler());
6631 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6632 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6633 }
6634 }
6635}
6636
Roland Levillain44015862016-01-22 11:47:17 +00006637void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6638 Location out,
6639 Location ref,
6640 Location obj,
6641 uint32_t offset,
6642 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006643 DCHECK(kEmitCompilerReadBarrier);
6644
Roland Levillain44015862016-01-22 11:47:17 +00006645 // Insert a slow path based read barrier *after* the reference load.
6646 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006647 // If heap poisoning is enabled, the unpoisoning of the loaded
6648 // reference will be carried out by the runtime within the slow
6649 // path.
6650 //
6651 // Note that `ref` currently does not get unpoisoned (when heap
6652 // poisoning is enabled), which is alright as the `ref` argument is
6653 // not used by the artReadBarrierSlow entry point.
6654 //
6655 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006656 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006657 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6658 AddSlowPath(slow_path);
6659
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006660 __ B(slow_path->GetEntryLabel());
6661 __ Bind(slow_path->GetExitLabel());
6662}
6663
Roland Levillain44015862016-01-22 11:47:17 +00006664void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6665 Location out,
6666 Location ref,
6667 Location obj,
6668 uint32_t offset,
6669 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006670 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006671 // Baker's read barriers shall be handled by the fast path
6672 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6673 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006674 // If heap poisoning is enabled, unpoisoning will be taken care of
6675 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006676 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006677 } else if (kPoisonHeapReferences) {
6678 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6679 }
6680}
6681
Roland Levillain44015862016-01-22 11:47:17 +00006682void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6683 Location out,
6684 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006685 DCHECK(kEmitCompilerReadBarrier);
6686
Roland Levillain44015862016-01-22 11:47:17 +00006687 // Insert a slow path based read barrier *after* the GC root load.
6688 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006689 // Note that GC roots are not affected by heap poisoning, so we do
6690 // not need to do anything special for this here.
6691 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006692 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006693 AddSlowPath(slow_path);
6694
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006695 __ B(slow_path->GetEntryLabel());
6696 __ Bind(slow_path->GetExitLabel());
6697}
6698
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006699void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6700 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006701 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006702 locations->SetInAt(0, Location::RequiresRegister());
6703 locations->SetOut(Location::RequiresRegister());
6704}
6705
6706void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6707 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006708 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006709 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006710 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006711 __ Ldr(XRegisterFrom(locations->Out()),
6712 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006713 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006714 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006715 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006716 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6717 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006718 __ Ldr(XRegisterFrom(locations->Out()),
6719 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006720 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006721}
6722
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006723static void PatchJitRootUse(uint8_t* code,
6724 const uint8_t* roots_data,
6725 vixl::aarch64::Literal<uint32_t>* literal,
6726 uint64_t index_in_table) {
6727 uint32_t literal_offset = literal->GetOffset();
6728 uintptr_t address =
6729 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6730 uint8_t* data = code + literal_offset;
6731 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6732}
6733
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006734void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6735 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006736 const StringReference& string_reference = entry.first;
6737 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006738 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006739 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006740 }
6741 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006742 const TypeReference& type_reference = entry.first;
6743 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006744 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006745 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006746 }
6747}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006748
Artem Serov1a719e42019-07-18 14:24:55 +01006749MemOperand InstructionCodeGeneratorARM64::VecNeonAddress(
6750 HVecMemoryOperation* instruction,
6751 UseScratchRegisterScope* temps_scope,
6752 size_t size,
6753 bool is_string_char_at,
6754 /*out*/ Register* scratch) {
6755 LocationSummary* locations = instruction->GetLocations();
6756 Register base = InputRegisterAt(instruction, 0);
6757
6758 if (instruction->InputAt(1)->IsIntermediateAddressIndex()) {
6759 DCHECK(!is_string_char_at);
6760 return MemOperand(base.X(), InputRegisterAt(instruction, 1).X());
6761 }
6762
6763 Location index = locations->InAt(1);
6764 uint32_t offset = is_string_char_at
6765 ? mirror::String::ValueOffset().Uint32Value()
6766 : mirror::Array::DataOffset(size).Uint32Value();
6767 size_t shift = ComponentSizeShiftWidth(size);
6768
6769 // HIntermediateAddress optimization is only applied for scalar ArrayGet and ArraySet.
6770 DCHECK(!instruction->InputAt(0)->IsIntermediateAddress());
6771
6772 if (index.IsConstant()) {
6773 offset += Int64FromLocation(index) << shift;
6774 return HeapOperand(base, offset);
6775 } else {
6776 *scratch = temps_scope->AcquireSameSizeAs(base);
6777 __ Add(*scratch, base, Operand(WRegisterFrom(index), LSL, shift));
6778 return HeapOperand(*scratch, offset);
6779 }
6780}
6781
Alexandre Rames67555f72014-11-18 10:55:16 +00006782#undef __
6783#undef QUICK_ENTRY_POINT
6784
Vladimir Markoca1e0382018-04-11 09:58:41 +00006785#define __ assembler.GetVIXLAssembler()->
6786
6787static void EmitGrayCheckAndFastPath(arm64::Arm64Assembler& assembler,
6788 vixl::aarch64::Register base_reg,
6789 vixl::aarch64::MemOperand& lock_word,
Vladimir Marko7a695052018-04-12 10:26:50 +01006790 vixl::aarch64::Label* slow_path,
6791 vixl::aarch64::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006792 // Load the lock word containing the rb_state.
6793 __ Ldr(ip0.W(), lock_word);
6794 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01006795 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Vladimir Markoca1e0382018-04-11 09:58:41 +00006796 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6797 __ Tbnz(ip0.W(), LockWord::kReadBarrierStateShift, slow_path);
6798 static_assert(
6799 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET,
6800 "Field and array LDR offsets must be the same to reuse the same code.");
Vladimir Marko7a695052018-04-12 10:26:50 +01006801 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
6802 if (throw_npe != nullptr) {
6803 __ Bind(throw_npe);
6804 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006805 // Adjust the return address back to the LDR (1 instruction; 2 for heap poisoning).
6806 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6807 "Field LDR must be 1 instruction (4B) before the return address label; "
6808 " 2 instructions (8B) for heap poisoning.");
6809 __ Add(lr, lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6810 // Introduce a dependency on the lock_word including rb_state,
6811 // to prevent load-load reordering, and without using
6812 // a memory barrier (which would be more expensive).
6813 __ Add(base_reg, base_reg, Operand(ip0, LSR, 32));
6814 __ Br(lr); // And return back to the function.
6815 // Note: The fake dependency is unnecessary for the slow path.
6816}
6817
6818// Load the read barrier introspection entrypoint in register `entrypoint`.
6819static void LoadReadBarrierMarkIntrospectionEntrypoint(arm64::Arm64Assembler& assembler,
6820 vixl::aarch64::Register entrypoint) {
6821 // entrypoint = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6822 DCHECK_EQ(ip0.GetCode(), 16u);
6823 const int32_t entry_point_offset =
6824 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6825 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
6826}
6827
6828void CodeGeneratorARM64::CompileBakerReadBarrierThunk(Arm64Assembler& assembler,
6829 uint32_t encoded_data,
6830 /*out*/ std::string* debug_name) {
6831 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
6832 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006833 case BakerReadBarrierKind::kField:
6834 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006835 auto base_reg =
6836 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6837 CheckValidReg(base_reg.GetCode());
6838 auto holder_reg =
6839 Register::GetXRegFromCode(BakerReadBarrierSecondRegField::Decode(encoded_data));
6840 CheckValidReg(holder_reg.GetCode());
6841 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6842 temps.Exclude(ip0, ip1);
Roland Levillain988c3912019-09-25 19:33:35 +01006843 // In the case of a field load (with relaxed semantic), if `base_reg` differs from
6844 // `holder_reg`, the offset was too large and we must have emitted (during the construction
6845 // of the HIR graph, see `art::HInstructionBuilder::BuildInstanceFieldAccess`) and preserved
6846 // (see `art::PrepareForRegisterAllocation::VisitNullCheck`) an explicit null check before
6847 // the load. Otherwise, for implicit null checks, we need to null-check the holder as we do
6848 // not necessarily do that check before going to the thunk.
6849 //
6850 // In the case of a field load with load-acquire semantics (where `base_reg` always differs
6851 // from `holder_reg`), we also need an explicit null check when implicit null checks are
6852 // allowed, as we do not emit one before going to the thunk.
Vladimir Marko7a695052018-04-12 10:26:50 +01006853 vixl::aarch64::Label throw_npe_label;
6854 vixl::aarch64::Label* throw_npe = nullptr;
Roland Levillain988c3912019-09-25 19:33:35 +01006855 if (GetCompilerOptions().GetImplicitNullChecks() &&
6856 (holder_reg.Is(base_reg) || (kind == BakerReadBarrierKind::kAcquire))) {
Vladimir Marko7a695052018-04-12 10:26:50 +01006857 throw_npe = &throw_npe_label;
6858 __ Cbz(holder_reg.W(), throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006859 }
Vladimir Marko7a695052018-04-12 10:26:50 +01006860 // Check if the holder is gray and, if not, add fake dependency to the base register
6861 // and return to the LDR instruction to load the reference. Otherwise, use introspection
6862 // to load the reference and call the entrypoint that performs further checks on the
6863 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006864 vixl::aarch64::Label slow_path;
6865 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
Vladimir Marko7a695052018-04-12 10:26:50 +01006866 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006867 __ Bind(&slow_path);
Vladimir Marko0ecac682018-08-07 10:40:38 +01006868 if (kind == BakerReadBarrierKind::kField) {
6869 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6870 __ Ldr(ip0.W(), ldr_address); // Load the LDR (immediate) unsigned offset.
6871 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6872 __ Ubfx(ip0.W(), ip0.W(), 10, 12); // Extract the offset.
6873 __ Ldr(ip0.W(), MemOperand(base_reg, ip0, LSL, 2)); // Load the reference.
6874 } else {
6875 DCHECK(kind == BakerReadBarrierKind::kAcquire);
6876 DCHECK(!base_reg.Is(holder_reg));
6877 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6878 __ Ldar(ip0.W(), MemOperand(base_reg));
6879 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006880 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
6881 __ Br(ip1); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006882 break;
6883 }
6884 case BakerReadBarrierKind::kArray: {
6885 auto base_reg =
6886 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6887 CheckValidReg(base_reg.GetCode());
6888 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6889 BakerReadBarrierSecondRegField::Decode(encoded_data));
6890 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6891 temps.Exclude(ip0, ip1);
6892 vixl::aarch64::Label slow_path;
6893 int32_t data_offset =
6894 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
6895 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
6896 DCHECK_LT(lock_word.GetOffset(), 0);
6897 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path);
6898 __ Bind(&slow_path);
6899 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
6900 __ Ldr(ip0.W(), ldr_address); // Load the LDR (register) unsigned offset.
6901 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6902 __ Ubfx(ip0, ip0, 16, 6); // Extract the index register, plus 32 (bit 21 is set).
6903 __ Bfi(ip1, ip0, 3, 6); // Insert ip0 to the entrypoint address to create
6904 // a switch case target based on the index register.
6905 __ Mov(ip0, base_reg); // Move the base register to ip0.
6906 __ Br(ip1); // Jump to the entrypoint's array switch case.
6907 break;
6908 }
6909 case BakerReadBarrierKind::kGcRoot: {
6910 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
6911 // and it does not have a forwarding address), call the correct introspection entrypoint;
6912 // otherwise return the reference (or the extracted forwarding address).
6913 // There is no gray bit check for GC roots.
6914 auto root_reg =
6915 Register::GetWRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6916 CheckValidReg(root_reg.GetCode());
6917 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6918 BakerReadBarrierSecondRegField::Decode(encoded_data));
6919 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6920 temps.Exclude(ip0, ip1);
6921 vixl::aarch64::Label return_label, not_marked, forwarding_address;
6922 __ Cbz(root_reg, &return_label);
6923 MemOperand lock_word(root_reg.X(), mirror::Object::MonitorOffset().Int32Value());
6924 __ Ldr(ip0.W(), lock_word);
6925 __ Tbz(ip0.W(), LockWord::kMarkBitStateShift, &not_marked);
6926 __ Bind(&return_label);
6927 __ Br(lr);
6928 __ Bind(&not_marked);
6929 __ Tst(ip0.W(), Operand(ip0.W(), LSL, 1));
6930 __ B(&forwarding_address, mi);
6931 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6932 // Adjust the art_quick_read_barrier_mark_introspection address in IP1 to
6933 // art_quick_read_barrier_mark_introspection_gc_roots.
6934 __ Add(ip1, ip1, Operand(BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRYPOINT_OFFSET));
6935 __ Mov(ip0.W(), root_reg);
6936 __ Br(ip1);
6937 __ Bind(&forwarding_address);
6938 __ Lsl(root_reg, ip0.W(), LockWord::kForwardingAddressShift);
6939 __ Br(lr);
6940 break;
6941 }
6942 default:
6943 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
6944 UNREACHABLE();
6945 }
6946
Vladimir Marko966b46f2018-08-03 10:20:19 +00006947 // For JIT, the slow path is considered part of the compiled method,
Vladimir Markof91fc122020-05-13 09:21:00 +01006948 // so JIT should pass null as `debug_name`.
Vladimir Marko695348f2020-05-19 14:42:02 +01006949 DCHECK(!GetCompilerOptions().IsJitCompiler() || debug_name == nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00006950 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006951 std::ostringstream oss;
6952 oss << "BakerReadBarrierThunk";
6953 switch (kind) {
6954 case BakerReadBarrierKind::kField:
6955 oss << "Field_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
6956 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
6957 break;
Vladimir Marko0ecac682018-08-07 10:40:38 +01006958 case BakerReadBarrierKind::kAcquire:
6959 oss << "Acquire_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
6960 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
6961 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +00006962 case BakerReadBarrierKind::kArray:
6963 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
6964 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6965 BakerReadBarrierSecondRegField::Decode(encoded_data));
6966 break;
6967 case BakerReadBarrierKind::kGcRoot:
6968 oss << "GcRoot_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
6969 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6970 BakerReadBarrierSecondRegField::Decode(encoded_data));
6971 break;
6972 }
6973 *debug_name = oss.str();
6974 }
6975}
6976
6977#undef __
6978
Alexandre Rames5319def2014-10-23 10:03:10 +01006979} // namespace arm64
6980} // namespace art