blob: 8f4979f03dfa3998b4452ae742e7eec8aa0304bb [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 ||
Vladimir Markoe17530a2020-11-11 17:02:26 +0000726 intrinsic == Intrinsics::kUnsafeCASObject ||
Vladimir Marko2d98dc22020-10-01 11:21:37 +0000727 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
Vladimir Marko1bff99f2020-11-02 15:07:33 +0000728 mirror::VarHandle::AccessModeTemplate::kGet ||
729 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
730 mirror::VarHandle::AccessModeTemplate::kCompareAndSet ||
731 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
Vladimir Marko32c2eb82020-11-10 16:58:47 +0000732 mirror::VarHandle::AccessModeTemplate::kCompareAndExchange ||
733 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
734 mirror::VarHandle::AccessModeTemplate::kGetAndUpdate)
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000735 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100736 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100737 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000738 }
739 }
740
741 // We're moving two or three locations to locations that could
742 // overlap, so we need a parallel move resolver.
743 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100744 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000745 parallel_move.AddMove(ref_,
746 LocationFrom(calling_convention.GetRegisterAt(0)),
747 type,
748 nullptr);
749 parallel_move.AddMove(obj_,
750 LocationFrom(calling_convention.GetRegisterAt(1)),
751 type,
752 nullptr);
753 if (index.IsValid()) {
754 parallel_move.AddMove(index,
755 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100756 DataType::Type::kInt32,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000757 nullptr);
758 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
759 } else {
760 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
761 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
762 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000763 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000764 instruction_,
765 instruction_->GetDexPc(),
766 this);
767 CheckEntrypointTypes<
768 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
769 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
770
771 RestoreLiveRegisters(codegen, locations);
772
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000773 __ B(GetExitLabel());
774 }
775
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100776 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000777
778 private:
779 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100780 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
781 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000782 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
783 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
784 return Register(VIXLRegCodeFromART(i), kXRegSize);
785 }
786 }
787 // We shall never fail to find a free caller-save register, as
788 // there are more than two core caller-save registers on ARM64
789 // (meaning it is possible to find one which is different from
790 // `ref` and `obj`).
791 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
792 LOG(FATAL) << "Could not find a free register";
793 UNREACHABLE();
794 }
795
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000796 const Location out_;
797 const Location ref_;
798 const Location obj_;
799 const uint32_t offset_;
800 // An additional location containing an index to an array.
801 // Only used for HArrayGet and the UnsafeGetObject &
802 // UnsafeGetObjectVolatile intrinsics.
803 const Location index_;
804
805 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
806};
807
808// Slow path generating a read barrier for a GC root.
809class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
810 public:
811 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000812 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000813 DCHECK(kEmitCompilerReadBarrier);
814 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000815
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100816 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000817 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100818 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000819 DCHECK(locations->CanCall());
820 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Vladimir Markoa41ea272020-09-07 15:24:36 +0000821 DCHECK(instruction_->IsLoadClass() ||
822 instruction_->IsLoadString() ||
823 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000824 << "Unexpected instruction in read barrier for GC root slow path: "
825 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000826
827 __ Bind(GetEntryLabel());
828 SaveLiveRegisters(codegen, locations);
829
830 InvokeRuntimeCallingConvention calling_convention;
831 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
832 // The argument of the ReadBarrierForRootSlow is not a managed
833 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
834 // thus we need a 64-bit move here, and we cannot use
835 //
836 // arm64_codegen->MoveLocation(
837 // LocationFrom(calling_convention.GetRegisterAt(0)),
838 // root_,
839 // type);
840 //
841 // which would emit a 32-bit move, as `type` is a (32-bit wide)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100842 // reference type (`DataType::Type::kReference`).
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000843 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000844 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000845 instruction_,
846 instruction_->GetDexPc(),
847 this);
848 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
849 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
850
851 RestoreLiveRegisters(codegen, locations);
852 __ B(GetExitLabel());
853 }
854
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100855 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000856
857 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000858 const Location out_;
859 const Location root_;
860
861 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
862};
863
Alexandre Rames5319def2014-10-23 10:03:10 +0100864#undef __
865
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100866Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100867 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100868 if (type == DataType::Type::kVoid) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100869 LOG(FATAL) << "Unreachable type " << type;
870 }
871
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100872 if (DataType::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100873 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
874 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100875 } else if (!DataType::IsFloatingPointType(type) &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000876 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000877 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
878 } else {
879 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100880 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
881 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100882 }
883
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000884 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100885 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100886 return next_location;
887}
888
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100889Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100890 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100891}
892
Vladimir Marko86c87522020-05-11 16:55:55 +0100893Location CriticalNativeCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
894 DCHECK_NE(type, DataType::Type::kReference);
895
896 Location location = Location::NoLocation();
897 if (DataType::IsFloatingPointType(type)) {
898 if (fpr_index_ < kParameterFPRegistersLength) {
899 location = LocationFrom(kParameterFPRegisters[fpr_index_]);
900 ++fpr_index_;
901 }
902 } else {
903 // Native ABI uses the same registers as managed, except that the method register x0
904 // is a normal argument.
905 if (gpr_index_ < 1u + kParameterCoreRegistersLength) {
906 location = LocationFrom(gpr_index_ == 0u ? x0 : kParameterCoreRegisters[gpr_index_ - 1u]);
907 ++gpr_index_;
908 }
909 }
910 if (location.IsInvalid()) {
911 if (DataType::Is64BitType(type)) {
912 location = Location::DoubleStackSlot(stack_offset_);
913 } else {
914 location = Location::StackSlot(stack_offset_);
915 }
916 stack_offset_ += kFramePointerSize;
917
918 if (for_register_allocation_) {
919 location = Location::Any();
920 }
921 }
922 return location;
923}
924
925Location CriticalNativeCallingConventionVisitorARM64::GetReturnLocation(DataType::Type type) const {
926 // We perform conversion to the managed ABI return register after the call if needed.
927 InvokeDexCallingConventionVisitorARM64 dex_calling_convention;
928 return dex_calling_convention.GetReturnLocation(type);
929}
930
931Location CriticalNativeCallingConventionVisitorARM64::GetMethodLocation() const {
932 // Pass the method in the hidden argument x15.
933 return Location::RegisterLocation(x15.GetCode());
934}
935
Serban Constantinescu579885a2015-02-22 20:51:33 +0000936CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100937 const CompilerOptions& compiler_options,
938 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100939 : CodeGenerator(graph,
940 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000941 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000942 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100943 callee_saved_core_registers.GetList(),
944 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100945 compiler_options,
946 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100947 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
948 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov1a719e42019-07-18 14:24:55 +0100949 location_builder_neon_(graph, this),
950 instruction_visitor_neon_(graph, this),
951 location_builder_sve_(graph, this),
952 instruction_visitor_sve_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100953 move_resolver_(graph->GetAllocator(), this),
Artem Serovaa6f4832018-11-21 18:57:54 +0000954 assembler_(graph->GetAllocator(),
955 compiler_options.GetInstructionSetFeatures()->AsArm64InstructionSetFeatures()),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000956 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100957 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000958 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100959 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko8f63f102020-09-28 12:10:28 +0100960 public_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
961 package_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000962 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100963 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeb9eb002020-10-02 13:54:19 +0100964 boot_image_jni_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +0100965 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100966 call_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100967 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100968 uint32_literals_(std::less<uint32_t>(),
969 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
970 uint64_literals_(std::less<uint64_t>(),
971 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000972 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100973 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000974 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +0000975 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
976 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
977 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000978 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000979 AddAllocatedRegister(LocationFrom(lr));
Artem Serov1a719e42019-07-18 14:24:55 +0100980
981 bool use_sve = ShouldUseSVE();
982 if (use_sve) {
983 location_builder_ = &location_builder_sve_;
984 instruction_visitor_ = &instruction_visitor_sve_;
985 } else {
986 location_builder_ = &location_builder_neon_;
987 instruction_visitor_ = &instruction_visitor_neon_;
988 }
989}
990
991bool CodeGeneratorARM64::ShouldUseSVE() const {
992 return kArm64AllowSVE && GetInstructionSetFeatures().HasSVE();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000993}
Alexandre Rames5319def2014-10-23 10:03:10 +0100994
Alexandre Rames67555f72014-11-18 10:55:16 +0000995#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100996
Zheng Xu3927c8b2015-11-18 17:46:25 +0800997void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100998 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800999 jump_table->EmitTable(this);
1000 }
1001}
1002
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001003void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001004 EmitJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +00001005
1006 // Emit JIT baker read barrier slow paths.
Vladimir Marko695348f2020-05-19 14:42:02 +01001007 DCHECK(GetCompilerOptions().IsJitCompiler() || jit_baker_read_barrier_slow_paths_.empty());
Vladimir Marko966b46f2018-08-03 10:20:19 +00001008 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
1009 uint32_t encoded_data = entry.first;
1010 vixl::aarch64::Label* slow_path_entry = &entry.second.label;
1011 __ Bind(slow_path_entry);
Andreas Gampe3db70682018-12-26 15:12:03 -08001012 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name= */ nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00001013 }
1014
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001015 // Ensure we emit the literal pool.
1016 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001017
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001018 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001019
1020 // Verify Baker read barrier linker patches.
1021 if (kIsDebugBuild) {
1022 ArrayRef<const uint8_t> code = allocator->GetMemory();
1023 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
1024 DCHECK(info.label.IsBound());
1025 uint32_t literal_offset = info.label.GetLocation();
1026 DCHECK_ALIGNED(literal_offset, 4u);
1027
1028 auto GetInsn = [&code](uint32_t offset) {
1029 DCHECK_ALIGNED(offset, 4u);
1030 return
1031 (static_cast<uint32_t>(code[offset + 0]) << 0) +
1032 (static_cast<uint32_t>(code[offset + 1]) << 8) +
1033 (static_cast<uint32_t>(code[offset + 2]) << 16)+
1034 (static_cast<uint32_t>(code[offset + 3]) << 24);
1035 };
1036
1037 const uint32_t encoded_data = info.custom_data;
1038 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
1039 // Check that the next instruction matches the expected LDR.
1040 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01001041 case BakerReadBarrierKind::kField:
1042 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00001043 DCHECK_GE(code.size() - literal_offset, 8u);
1044 uint32_t next_insn = GetInsn(literal_offset + 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001045 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1046 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko0ecac682018-08-07 10:40:38 +01001047 if (kind == BakerReadBarrierKind::kField) {
1048 // LDR (immediate) with correct base_reg.
1049 CHECK_EQ(next_insn & 0xffc003e0u, 0xb9400000u | (base_reg << 5));
1050 } else {
1051 DCHECK(kind == BakerReadBarrierKind::kAcquire);
1052 // LDAR with correct base_reg.
1053 CHECK_EQ(next_insn & 0xffffffe0u, 0x88dffc00u | (base_reg << 5));
1054 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001055 break;
1056 }
1057 case BakerReadBarrierKind::kArray: {
1058 DCHECK_GE(code.size() - literal_offset, 8u);
1059 uint32_t next_insn = GetInsn(literal_offset + 4u);
1060 // LDR (register) with the correct base_reg, size=10 (32-bit), option=011 (extend = LSL),
1061 // and S=1 (shift amount = 2 for 32-bit version), i.e. LDR Wt, [Xn, Xm, LSL #2].
1062 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1063 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1064 CHECK_EQ(next_insn & 0xffe0ffe0u, 0xb8607800u | (base_reg << 5));
1065 CheckValidReg((next_insn >> 16) & 0x1f); // Check index register
1066 break;
1067 }
1068 case BakerReadBarrierKind::kGcRoot: {
1069 DCHECK_GE(literal_offset, 4u);
1070 uint32_t prev_insn = GetInsn(literal_offset - 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001071 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko94796f82018-08-08 15:15:33 +01001072 // Usually LDR (immediate) with correct root_reg but
Vladimir Markoc8178f52020-11-24 10:38:16 +00001073 // we may have a "MOV marked, old_value" for intrinsic CAS.
Vladimir Marko94796f82018-08-08 15:15:33 +01001074 if ((prev_insn & 0xffe0ffff) != (0x2a0003e0 | root_reg)) { // MOV?
1075 CHECK_EQ(prev_insn & 0xffc0001fu, 0xb9400000u | root_reg); // LDR?
1076 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001077 break;
1078 }
1079 default:
1080 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
1081 UNREACHABLE();
1082 }
1083 }
1084 }
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001085}
1086
Zheng Xuad4450e2015-04-17 18:48:56 +08001087void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1088 // Note: There are 6 kinds of moves:
1089 // 1. constant -> GPR/FPR (non-cycle)
1090 // 2. constant -> stack (non-cycle)
1091 // 3. GPR/FPR -> GPR/FPR
1092 // 4. GPR/FPR -> stack
1093 // 5. stack -> GPR/FPR
1094 // 6. stack -> stack (non-cycle)
1095 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1096 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1097 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1098 // dependency.
1099 vixl_temps_.Open(GetVIXLAssembler());
1100}
1101
1102void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1103 vixl_temps_.Close();
1104}
1105
1106Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001107 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1108 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1109 || kind == Location::kSIMDStackSlot);
1110 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1111 ? Location::kFpuRegister
1112 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001113 Location scratch = GetScratchLocation(kind);
1114 if (!scratch.Equals(Location::NoLocation())) {
1115 return scratch;
1116 }
1117 // Allocate from VIXL temp registers.
1118 if (kind == Location::kRegister) {
1119 scratch = LocationFrom(vixl_temps_.AcquireX());
1120 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001121 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serov1a719e42019-07-18 14:24:55 +01001122 scratch = codegen_->GetGraph()->HasSIMD()
1123 ? codegen_->GetInstructionCodeGeneratorArm64()->AllocateSIMDScratchLocation(&vixl_temps_)
1124 : LocationFrom(vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001125 }
1126 AddScratchLocation(scratch);
1127 return scratch;
1128}
1129
1130void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1131 if (loc.IsRegister()) {
1132 vixl_temps_.Release(XRegisterFrom(loc));
1133 } else {
1134 DCHECK(loc.IsFpuRegister());
Artem Serov1a719e42019-07-18 14:24:55 +01001135 if (codegen_->GetGraph()->HasSIMD()) {
1136 codegen_->GetInstructionCodeGeneratorArm64()->FreeSIMDScratchLocation(loc, &vixl_temps_);
1137 } else {
1138 vixl_temps_.Release(DRegisterFrom(loc));
1139 }
Zheng Xuad4450e2015-04-17 18:48:56 +08001140 }
1141 RemoveScratchLocation(loc);
1142}
1143
Alexandre Rames3e69f162014-12-10 10:36:50 +00001144void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001145 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001146 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001147}
1148
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001149void CodeGeneratorARM64::MaybeIncrementHotness(bool is_frame_entry) {
1150 MacroAssembler* masm = GetVIXLAssembler();
1151 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1152 UseScratchRegisterScope temps(masm);
1153 Register counter = temps.AcquireX();
1154 Register method = is_frame_entry ? kArtMethodRegister : temps.AcquireX();
1155 if (!is_frame_entry) {
1156 __ Ldr(method, MemOperand(sp, 0));
1157 }
1158 __ Ldrh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
1159 __ Add(counter, counter, 1);
1160 // Subtract one if the counter would overflow.
1161 __ Sub(counter, counter, Operand(counter, LSR, 16));
1162 __ Strh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
1163 }
1164
1165 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffray095dc462020-08-17 16:40:28 +01001166 ScopedProfilingInfoUse spiu(
1167 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
1168 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001169 if (info != nullptr) {
Nicolas Geoffrayc1cd1332020-01-25 13:08:24 +00001170 uint64_t address = reinterpret_cast64<uint64_t>(info);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001171 vixl::aarch64::Label done;
1172 UseScratchRegisterScope temps(masm);
1173 Register temp = temps.AcquireX();
1174 Register counter = temps.AcquireW();
1175 __ Mov(temp, address);
1176 __ Ldrh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
1177 __ Add(counter, counter, 1);
Nicolas Geoffray4313ccb2020-08-26 17:01:15 +01001178 __ And(counter, counter, interpreter::kTieredHotnessMask);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001179 __ Strh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
Nicolas Geoffray4313ccb2020-08-26 17:01:15 +01001180 __ Cbnz(counter, &done);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001181 if (is_frame_entry) {
1182 if (HasEmptyFrame()) {
Vladimir Markodec78172020-06-19 15:31:23 +01001183 // The entrypoint expects the method at the bottom of the stack. We
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001184 // claim stack space necessary for alignment.
Vladimir Markodec78172020-06-19 15:31:23 +01001185 IncreaseFrame(kStackAlignment);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001186 __ Stp(kArtMethodRegister, lr, MemOperand(sp, 0));
1187 } else if (!RequiresCurrentMethod()) {
1188 __ Str(kArtMethodRegister, MemOperand(sp, 0));
1189 }
1190 } else {
1191 CHECK(RequiresCurrentMethod());
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001192 }
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001193 uint32_t entrypoint_offset =
1194 GetThreadOffset<kArm64PointerSize>(kQuickCompileOptimized).Int32Value();
1195 __ Ldr(lr, MemOperand(tr, entrypoint_offset));
1196 // Note: we don't record the call here (and therefore don't generate a stack
1197 // map), as the entrypoint should never be suspended.
1198 __ Blr(lr);
1199 if (HasEmptyFrame()) {
1200 CHECK(is_frame_entry);
1201 __ Ldr(lr, MemOperand(sp, 8));
Vladimir Markodec78172020-06-19 15:31:23 +01001202 DecreaseFrame(kStackAlignment);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001203 }
1204 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001205 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001206 }
1207}
1208
Alexandre Rames5319def2014-10-23 10:03:10 +01001209void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001210 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001211 __ Bind(&frame_entry_label_);
1212
Vladimir Marko33bff252017-11-01 14:35:42 +00001213 bool do_overflow_check =
1214 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001215 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001216 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001217 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001218 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001219 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001220 {
1221 // Ensure that between load and RecordPcInfo there are no pools emitted.
1222 ExactAssemblyScope eas(GetVIXLAssembler(),
1223 kInstructionSize,
1224 CodeBufferCheckScope::kExactSize);
1225 __ ldr(wzr, MemOperand(temp, 0));
1226 RecordPcInfo(nullptr, 0);
1227 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001228 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001229
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001230 if (!HasEmptyFrame()) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001231 // Stack layout:
1232 // sp[frame_size - 8] : lr.
1233 // ... : other preserved core registers.
1234 // ... : other preserved fp registers.
1235 // ... : reserved frame space.
1236 // sp[0] : current method.
Vladimir Marko1a225a72019-07-05 13:37:42 +01001237 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1238 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1239 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1240 DCHECK(!preserved_core_registers.IsEmpty());
1241 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1242 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001243
Vladimir Marko1a225a72019-07-05 13:37:42 +01001244 // Save the current method if we need it, or if using STP reduces code
1245 // size. Note that we do not do this in HCurrentMethod, as the
1246 // instruction might have been removed in the SSA graph.
1247 CPURegister lowest_spill;
1248 if (core_spills_offset == kXRegSizeInBytes) {
1249 // If there is no gap between the method and the lowest core spill, use
1250 // aligned STP pre-index to store both. Max difference is 512. We do
1251 // that to reduce code size even if we do not have to save the method.
1252 DCHECK_LE(frame_size, 512); // 32 core registers are only 256 bytes.
1253 lowest_spill = preserved_core_registers.PopLowestIndex();
1254 __ Stp(kArtMethodRegister, lowest_spill, MemOperand(sp, -frame_size, PreIndex));
1255 } else if (RequiresCurrentMethod()) {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001256 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001257 } else {
1258 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001259 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001260 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Vladimir Marko1a225a72019-07-05 13:37:42 +01001261 if (lowest_spill.IsValid()) {
1262 GetAssembler()->cfi().RelOffset(DWARFReg(lowest_spill), core_spills_offset);
1263 core_spills_offset += kXRegSizeInBytes;
1264 }
1265 GetAssembler()->SpillRegisters(preserved_core_registers, core_spills_offset);
1266 GetAssembler()->SpillRegisters(preserved_fp_registers, fp_spills_offset);
Mingyao Yang063fc772016-08-02 11:02:54 -07001267
1268 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1269 // Initialize should_deoptimize flag to 0.
1270 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1271 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1272 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001273 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001274 MaybeIncrementHotness(/* is_frame_entry= */ true);
Andreas Gampe3db70682018-12-26 15:12:03 -08001275 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001276}
1277
1278void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001279 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001280 if (!HasEmptyFrame()) {
Vladimir Marko1a225a72019-07-05 13:37:42 +01001281 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1282 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1283 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1284 DCHECK(!preserved_core_registers.IsEmpty());
1285 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1286 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
1287
1288 CPURegister lowest_spill;
1289 if (core_spills_offset == kXRegSizeInBytes) {
1290 // If there is no gap between the method and the lowest core spill, use
1291 // aligned LDP pre-index to pop both. Max difference is 504. We do
1292 // that to reduce code size even though the loaded method is unused.
1293 DCHECK_LE(frame_size, 504); // 32 core registers are only 256 bytes.
1294 lowest_spill = preserved_core_registers.PopLowestIndex();
1295 core_spills_offset += kXRegSizeInBytes;
1296 }
1297 GetAssembler()->UnspillRegisters(preserved_fp_registers, fp_spills_offset);
1298 GetAssembler()->UnspillRegisters(preserved_core_registers, core_spills_offset);
1299 if (lowest_spill.IsValid()) {
1300 __ Ldp(xzr, lowest_spill, MemOperand(sp, frame_size, PostIndex));
1301 GetAssembler()->cfi().Restore(DWARFReg(lowest_spill));
1302 } else {
1303 __ Drop(frame_size);
1304 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001305 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001306 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001307 __ Ret();
1308 GetAssembler()->cfi().RestoreState();
1309 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001310}
1311
Scott Wakeling97c72b72016-06-24 16:19:36 +01001312CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001313 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001314 return CPURegList(CPURegister::kRegister, kXRegSize,
1315 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001316}
1317
Scott Wakeling97c72b72016-06-24 16:19:36 +01001318CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001319 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1320 GetNumberOfFloatingPointRegisters()));
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001321 return CPURegList(CPURegister::kVRegister, kDRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001322 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001323}
1324
Alexandre Rames5319def2014-10-23 10:03:10 +01001325void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1326 __ Bind(GetLabelOf(block));
1327}
1328
Calin Juravle175dc732015-08-25 15:42:32 +01001329void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1330 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001331 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001332}
1333
Calin Juravlee460d1d2015-09-29 04:52:17 +01001334void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1335 if (location.IsRegister()) {
1336 locations->AddTemp(location);
1337 } else {
1338 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1339 }
1340}
1341
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001342void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001343 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001344 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001345 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001346 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001347 if (value_can_be_null) {
1348 __ Cbz(value, &done);
1349 }
Roland Levillainc73f0522018-08-14 15:16:50 +01001350 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07001351 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01001352 // Calculate the offset (in the card table) of the card corresponding to
1353 // `object`.
Alexandre Rames5319def2014-10-23 10:03:10 +01001354 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Roland Levillainc73f0522018-08-14 15:16:50 +01001355 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
1356 // `object`'s card.
1357 //
1358 // Register `card` contains the address of the card table. Note that the card
1359 // table's base is biased during its creation so that it always starts at an
1360 // address whose least-significant byte is equal to `kCardDirty` (see
1361 // art::gc::accounting::CardTable::Create). Therefore the STRB instruction
1362 // below writes the `kCardDirty` (byte) value into the `object`'s card
1363 // (located at `card + object >> kCardShift`).
1364 //
1365 // This dual use of the value in register `card` (1. to calculate the location
1366 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
1367 // (no need to explicitly load `kCardDirty` as an immediate value).
Serban Constantinescu02164b32014-11-13 14:05:07 +00001368 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001369 if (value_can_be_null) {
1370 __ Bind(&done);
1371 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001372}
1373
David Brazdil58282f42016-01-14 12:45:10 +00001374void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001375 // Blocked core registers:
1376 // lr : Runtime reserved.
1377 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001378 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001379 // ip1 : VIXL core temp.
1380 // ip0 : VIXL core temp.
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001381 // x18 : Platform register.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001382 //
1383 // Blocked fp registers:
1384 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001385 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1386 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001387 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001388 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001389 }
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001390 blocked_core_registers_[X18] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001391
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001392 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001393 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001394 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001395 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001396
David Brazdil58282f42016-01-14 12:45:10 +00001397 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001398 // Stubs do not save callee-save floating point registers. If the graph
1399 // is debuggable, we need to deal with these registers differently. For
1400 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001401 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1402 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001403 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001404 }
1405 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001406}
1407
Alexandre Rames3e69f162014-12-10 10:36:50 +00001408size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1409 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1410 __ Str(reg, MemOperand(sp, stack_index));
1411 return kArm64WordSize;
1412}
1413
1414size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1415 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1416 __ Ldr(reg, MemOperand(sp, stack_index));
1417 return kArm64WordSize;
1418}
1419
Artem Serov9df37b92019-07-23 16:41:54 +01001420size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1421 uint32_t reg_id ATTRIBUTE_UNUSED) {
1422 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1423 << "use SaveRestoreLiveRegistersHelper";
1424 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001425}
1426
Artem Serov9df37b92019-07-23 16:41:54 +01001427size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1428 uint32_t reg_id ATTRIBUTE_UNUSED) {
1429 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1430 << "use SaveRestoreLiveRegistersHelper";
1431 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001432}
1433
Alexandre Rames5319def2014-10-23 10:03:10 +01001434void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001435 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001436}
1437
1438void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001439 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001440}
1441
Vladimir Markoa0431112018-06-25 09:32:54 +01001442const Arm64InstructionSetFeatures& CodeGeneratorARM64::GetInstructionSetFeatures() const {
1443 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArm64InstructionSetFeatures();
1444}
1445
Alexandre Rames67555f72014-11-18 10:55:16 +00001446void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001447 if (constant->IsIntConstant()) {
1448 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1449 } else if (constant->IsLongConstant()) {
1450 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1451 } else if (constant->IsNullConstant()) {
1452 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001453 } else if (constant->IsFloatConstant()) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001454 __ Fmov(VRegister(destination), constant->AsFloatConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001455 } else {
1456 DCHECK(constant->IsDoubleConstant());
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001457 __ Fmov(VRegister(destination), constant->AsDoubleConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001458 }
1459}
1460
Alexandre Rames3e69f162014-12-10 10:36:50 +00001461
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001462static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001463 DCHECK(constant.IsConstant());
1464 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001465 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001466 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001467 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1468 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1469 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1470 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001471}
1472
Roland Levillain952b2352017-05-03 19:49:14 +01001473// Allocate a scratch register from the VIXL pool, querying first
1474// the floating-point register pool, and then the core register
1475// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001476// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1477// using a different allocation strategy.
1478static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1479 vixl::aarch64::UseScratchRegisterScope* temps,
1480 int size_in_bits) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001481 return masm->GetScratchVRegisterList()->IsEmpty()
Roland Levillain558dea12017-01-27 19:40:44 +00001482 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1483 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1484}
1485
Calin Juravlee460d1d2015-09-29 04:52:17 +01001486void CodeGeneratorARM64::MoveLocation(Location destination,
1487 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001488 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001489 if (source.Equals(destination)) {
1490 return;
1491 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001492
1493 // A valid move can always be inferred from the destination and source
1494 // locations. When moving from and to a register, the argument type can be
1495 // used to generate 32bit instead of 64bit moves. In debug mode we also
1496 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001497 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001498
1499 if (destination.IsRegister() || destination.IsFpuRegister()) {
1500 if (unspecified_type) {
1501 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1502 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001503 (src_cst != nullptr && (src_cst->IsIntConstant()
1504 || src_cst->IsFloatConstant()
1505 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001506 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001507 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001508 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001509 // If the source is a double stack slot or a 64bit constant, a 64bit
1510 // type is appropriate. Else the source is a register, and since the
1511 // type has not been specified, we chose a 64bit type to force a 64bit
1512 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001513 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001514 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001515 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001516 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1517 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001518 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001519 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1520 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1521 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001522 } else if (source.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001523 GetInstructionCodeGeneratorArm64()->LoadSIMDRegFromStack(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001524 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001525 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001526 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001527 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001528 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001529 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001530 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001531 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001532 DataType::Type source_type = DataType::Is64BitType(dst_type)
1533 ? DataType::Type::kInt64
1534 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001535 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1536 }
1537 } else {
1538 DCHECK(source.IsFpuRegister());
1539 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001540 DataType::Type source_type = DataType::Is64BitType(dst_type)
1541 ? DataType::Type::kFloat64
1542 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001543 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1544 } else {
1545 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001546 if (GetGraph()->HasSIMD()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001547 GetInstructionCodeGeneratorArm64()->MoveSIMDRegToSIMDReg(destination, source);
Artem Serovd4bccf12017-04-03 18:47:32 +01001548 } else {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001549 __ Fmov(VRegister(dst), FPRegisterFrom(source, dst_type));
Artem Serovd4bccf12017-04-03 18:47:32 +01001550 }
1551 }
1552 }
1553 } else if (destination.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001554 GetInstructionCodeGeneratorArm64()->MoveToSIMDStackSlot(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001555 } else { // The destination is not a register. It must be a stack slot.
1556 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1557 if (source.IsRegister() || source.IsFpuRegister()) {
1558 if (unspecified_type) {
1559 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001560 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001561 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001562 dst_type =
1563 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001564 }
1565 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001566 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1567 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001568 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001569 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001570 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1571 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001572 UseScratchRegisterScope temps(GetVIXLAssembler());
1573 HConstant* src_cst = source.GetConstant();
1574 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001575 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001576 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1577 ? Register(xzr)
1578 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001579 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001580 if (src_cst->IsIntConstant()) {
1581 temp = temps.AcquireW();
1582 } else if (src_cst->IsLongConstant()) {
1583 temp = temps.AcquireX();
1584 } else if (src_cst->IsFloatConstant()) {
1585 temp = temps.AcquireS();
1586 } else {
1587 DCHECK(src_cst->IsDoubleConstant());
1588 temp = temps.AcquireD();
1589 }
1590 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001591 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001592 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001593 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001594 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001595 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001596 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001597 // Use any scratch register (a core or a floating-point one)
1598 // from VIXL scratch register pools as a temporary.
1599 //
1600 // We used to only use the FP scratch register pool, but in some
1601 // rare cases the only register from this pool (D31) would
1602 // already be used (e.g. within a ParallelMove instruction, when
1603 // a move is blocked by a another move requiring a scratch FP
1604 // register, which would reserve D31). To prevent this issue, we
1605 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001606 //
1607 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001608 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001609 // use AcquireFPOrCoreCPURegisterOfSize instead of
1610 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1611 // allocates core scratch registers first.
1612 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1613 GetVIXLAssembler(),
1614 &temps,
1615 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001616 __ Ldr(temp, StackOperandFrom(source));
1617 __ Str(temp, StackOperandFrom(destination));
1618 }
1619 }
1620}
1621
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001622void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001623 CPURegister dst,
1624 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001625 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001626 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001627 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001628 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001629 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001630 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001631 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001632 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001633 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001634 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001635 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001636 case DataType::Type::kInt16:
1637 __ Ldrsh(Register(dst), src);
1638 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001639 case DataType::Type::kInt32:
1640 case DataType::Type::kReference:
1641 case DataType::Type::kInt64:
1642 case DataType::Type::kFloat32:
1643 case DataType::Type::kFloat64:
1644 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001645 __ Ldr(dst, src);
1646 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001647 case DataType::Type::kUint32:
1648 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001649 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001650 LOG(FATAL) << "Unreachable type " << type;
1651 }
1652}
1653
Calin Juravle77520bc2015-01-12 18:45:46 +00001654void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001655 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001656 const MemOperand& src,
1657 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001658 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001659 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001660 Register temp_base = temps.AcquireX();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001661 DataType::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001662
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001663 DCHECK(!src.IsPreIndex());
1664 DCHECK(!src.IsPostIndex());
1665
1666 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001667 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001668 {
1669 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1670 MemOperand base = MemOperand(temp_base);
1671 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001672 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001673 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001674 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001675 {
1676 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1677 __ ldarb(Register(dst), base);
1678 if (needs_null_check) {
1679 MaybeRecordImplicitNullCheck(instruction);
1680 }
1681 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001682 if (type == DataType::Type::kInt8) {
1683 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001684 }
1685 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001686 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001687 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001688 {
1689 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1690 __ ldarh(Register(dst), base);
1691 if (needs_null_check) {
1692 MaybeRecordImplicitNullCheck(instruction);
1693 }
1694 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001695 if (type == DataType::Type::kInt16) {
1696 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
1697 }
Artem Serov914d7a82017-02-07 14:33:49 +00001698 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001699 case DataType::Type::kInt32:
1700 case DataType::Type::kReference:
1701 case DataType::Type::kInt64:
1702 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001703 {
1704 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1705 __ ldar(Register(dst), base);
1706 if (needs_null_check) {
1707 MaybeRecordImplicitNullCheck(instruction);
1708 }
1709 }
1710 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001711 case DataType::Type::kFloat32:
1712 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00001713 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001714 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001715
Artem Serov914d7a82017-02-07 14:33:49 +00001716 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1717 {
1718 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1719 __ ldar(temp, base);
1720 if (needs_null_check) {
1721 MaybeRecordImplicitNullCheck(instruction);
1722 }
1723 }
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001724 __ Fmov(VRegister(dst), temp);
Artem Serov914d7a82017-02-07 14:33:49 +00001725 break;
Roland Levillain44015862016-01-22 11:47:17 +00001726 }
Aart Bik66c158e2018-01-31 12:55:04 -08001727 case DataType::Type::kUint32:
1728 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001729 case DataType::Type::kVoid:
Artem Serov914d7a82017-02-07 14:33:49 +00001730 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001731 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001732 }
1733}
1734
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001735void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001736 CPURegister src,
1737 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001738 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001739 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001740 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001741 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001742 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001743 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001744 case DataType::Type::kUint16:
1745 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001746 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001747 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001748 case DataType::Type::kInt32:
1749 case DataType::Type::kReference:
1750 case DataType::Type::kInt64:
1751 case DataType::Type::kFloat32:
1752 case DataType::Type::kFloat64:
1753 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001754 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001755 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001756 case DataType::Type::kUint32:
1757 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001758 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001759 LOG(FATAL) << "Unreachable type " << type;
1760 }
1761}
1762
Artem Serov914d7a82017-02-07 14:33:49 +00001763void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001764 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001765 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00001766 const MemOperand& dst,
1767 bool needs_null_check) {
1768 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001769 UseScratchRegisterScope temps(GetVIXLAssembler());
1770 Register temp_base = temps.AcquireX();
1771
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001772 DCHECK(!dst.IsPreIndex());
1773 DCHECK(!dst.IsPostIndex());
1774
1775 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001776 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001777 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001778 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00001779 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001780 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001781 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001782 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001783 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001784 {
1785 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1786 __ stlrb(Register(src), base);
1787 if (needs_null_check) {
1788 MaybeRecordImplicitNullCheck(instruction);
1789 }
1790 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001791 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001792 case DataType::Type::kUint16:
1793 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001794 {
1795 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1796 __ stlrh(Register(src), base);
1797 if (needs_null_check) {
1798 MaybeRecordImplicitNullCheck(instruction);
1799 }
1800 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001801 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001802 case DataType::Type::kInt32:
1803 case DataType::Type::kReference:
1804 case DataType::Type::kInt64:
1805 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001806 {
1807 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1808 __ stlr(Register(src), base);
1809 if (needs_null_check) {
1810 MaybeRecordImplicitNullCheck(instruction);
1811 }
1812 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001813 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001814 case DataType::Type::kFloat32:
1815 case DataType::Type::kFloat64: {
1816 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001817 Register temp_src;
1818 if (src.IsZero()) {
1819 // The zero register is used to avoid synthesizing zero constants.
1820 temp_src = Register(src);
1821 } else {
1822 DCHECK(src.IsFPRegister());
1823 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001824 __ Fmov(temp_src, VRegister(src));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001825 }
Artem Serov914d7a82017-02-07 14:33:49 +00001826 {
1827 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1828 __ stlr(temp_src, base);
1829 if (needs_null_check) {
1830 MaybeRecordImplicitNullCheck(instruction);
1831 }
1832 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001833 break;
1834 }
Aart Bik66c158e2018-01-31 12:55:04 -08001835 case DataType::Type::kUint32:
1836 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001837 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001838 LOG(FATAL) << "Unreachable type " << type;
1839 }
1840}
1841
Calin Juravle175dc732015-08-25 15:42:32 +01001842void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1843 HInstruction* instruction,
1844 uint32_t dex_pc,
1845 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001846 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00001847
Vladimir Markof6675082019-05-17 12:05:28 +01001848 ThreadOffset64 entrypoint_offset = GetThreadOffset<kArm64PointerSize>(entrypoint);
1849 // Reduce code size for AOT by using shared trampolines for slow path runtime calls across the
1850 // entire oat file. This adds an extra branch and we do not want to slow down the main path.
1851 // For JIT, thunk sharing is per-method, so the gains would be smaller or even negative.
Vladimir Marko695348f2020-05-19 14:42:02 +01001852 if (slow_path == nullptr || GetCompilerOptions().IsJitCompiler()) {
Vladimir Markof6675082019-05-17 12:05:28 +01001853 __ Ldr(lr, MemOperand(tr, entrypoint_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00001854 // Ensure the pc position is recorded immediately after the `blr` instruction.
1855 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1856 __ blr(lr);
1857 if (EntrypointRequiresStackMap(entrypoint)) {
1858 RecordPcInfo(instruction, dex_pc, slow_path);
1859 }
Vladimir Markof6675082019-05-17 12:05:28 +01001860 } else {
1861 // Ensure the pc position is recorded immediately after the `bl` instruction.
1862 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1863 EmitEntrypointThunkCall(entrypoint_offset);
1864 if (EntrypointRequiresStackMap(entrypoint)) {
1865 RecordPcInfo(instruction, dex_pc, slow_path);
1866 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001867 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001868}
1869
Roland Levillaindec8f632016-07-22 17:10:06 +01001870void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1871 HInstruction* instruction,
1872 SlowPathCode* slow_path) {
1873 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01001874 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1875 __ Blr(lr);
1876}
1877
Alexandre Rames67555f72014-11-18 10:55:16 +00001878void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001879 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001880 UseScratchRegisterScope temps(GetVIXLAssembler());
1881 Register temp = temps.AcquireW();
Vladimir Markodc682aa2018-01-04 18:42:57 +00001882 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001883 const size_t status_byte_offset =
1884 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1885 constexpr uint32_t shifted_visibly_initialized_value =
1886 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001887
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001888 // CMP (immediate) is limited to imm12 or imm12<<12, so we would need to materialize
1889 // the constant 0xf0000000 for comparison with the full 32-bit field. To reduce the code
1890 // size, load only the high byte of the field and compare with 0xf0.
1891 // Note: The same code size could be achieved with LDR+MNV(asr #24)+CBNZ but benchmarks
1892 // show that this pattern is slower (tested on little cores).
1893 __ Ldrb(temp, HeapOperand(class_reg, status_byte_offset));
1894 __ Cmp(temp, shifted_visibly_initialized_value);
1895 __ B(lo, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001896 __ Bind(slow_path->GetExitLabel());
1897}
Alexandre Rames5319def2014-10-23 10:03:10 +01001898
Vladimir Marko175e7862018-03-27 09:03:13 +00001899void InstructionCodeGeneratorARM64::GenerateBitstringTypeCheckCompare(
1900 HTypeCheckInstruction* check, vixl::aarch64::Register temp) {
1901 uint32_t path_to_root = check->GetBitstringPathToRoot();
1902 uint32_t mask = check->GetBitstringMask();
1903 DCHECK(IsPowerOfTwo(mask + 1));
1904 size_t mask_bits = WhichPowerOf2(mask + 1);
1905
1906 if (mask_bits == 16u) {
1907 // Load only the bitstring part of the status word.
1908 __ Ldrh(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1909 } else {
1910 // /* uint32_t */ temp = temp->status_
1911 __ Ldr(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1912 // Extract the bitstring bits.
1913 __ Ubfx(temp, temp, 0, mask_bits);
1914 }
1915 // Compare the bitstring bits to `path_to_root`.
1916 __ Cmp(temp, path_to_root);
1917}
1918
Roland Levillain44015862016-01-22 11:47:17 +00001919void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001920 BarrierType type = BarrierAll;
1921
1922 switch (kind) {
1923 case MemBarrierKind::kAnyAny:
1924 case MemBarrierKind::kAnyStore: {
1925 type = BarrierAll;
1926 break;
1927 }
1928 case MemBarrierKind::kLoadAny: {
1929 type = BarrierReads;
1930 break;
1931 }
1932 case MemBarrierKind::kStoreStore: {
1933 type = BarrierWrites;
1934 break;
1935 }
1936 default:
1937 LOG(FATAL) << "Unexpected memory barrier " << kind;
1938 }
1939 __ Dmb(InnerShareable, type);
1940}
1941
Serban Constantinescu02164b32014-11-13 14:05:07 +00001942void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1943 HBasicBlock* successor) {
1944 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001945 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1946 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001947 slow_path =
1948 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001949 instruction->SetSlowPath(slow_path);
1950 codegen_->AddSlowPath(slow_path);
1951 if (successor != nullptr) {
1952 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001953 }
1954 } else {
1955 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1956 }
1957
Serban Constantinescu02164b32014-11-13 14:05:07 +00001958 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1959 Register temp = temps.AcquireW();
1960
Andreas Gampe542451c2016-07-26 09:02:02 -07001961 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001962 if (successor == nullptr) {
1963 __ Cbnz(temp, slow_path->GetEntryLabel());
1964 __ Bind(slow_path->GetReturnLabel());
1965 } else {
1966 __ Cbz(temp, codegen_->GetLabelOf(successor));
1967 __ B(slow_path->GetEntryLabel());
1968 // slow_path will return to GetLabelOf(successor).
1969 }
1970}
1971
Alexandre Rames5319def2014-10-23 10:03:10 +01001972InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1973 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001974 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001975 assembler_(codegen->GetAssembler()),
1976 codegen_(codegen) {}
1977
Alexandre Rames67555f72014-11-18 10:55:16 +00001978void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001979 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001980 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001981 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001982 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001983 case DataType::Type::kInt32:
1984 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01001985 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001986 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001987 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001988 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001989
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001990 case DataType::Type::kFloat32:
1991 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001992 locations->SetInAt(0, Location::RequiresFpuRegister());
1993 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001994 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001995 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001996
Alexandre Rames5319def2014-10-23 10:03:10 +01001997 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001998 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001999 }
2000}
2001
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002002void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2003 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002004 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2005
2006 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002007 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01002008 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002009 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2010 object_field_get_with_read_barrier
2011 ? LocationSummary::kCallOnSlowPath
2012 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002013 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002014 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko0ecac682018-08-07 10:40:38 +01002015 // We need a temporary register for the read barrier load in
2016 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2017 // only if the field is volatile or the offset is too big.
2018 if (field_info.IsVolatile() ||
2019 field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2020 locations->AddTemp(FixedTempLocation());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002021 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002022 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002023 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002024 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002025 locations->SetOut(Location::RequiresFpuRegister());
2026 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002027 // The output overlaps for an object field get when read barriers
2028 // are enabled: we do not want the load to overwrite the object's
2029 // location, as we need it to emit the read barrier.
2030 locations->SetOut(
2031 Location::RequiresRegister(),
2032 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002033 }
2034}
2035
2036void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2037 const FieldInfo& field_info) {
2038 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002039 LocationSummary* locations = instruction->GetLocations();
2040 Location base_loc = locations->InAt(0);
2041 Location out = locations->Out();
2042 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01002043 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
2044 DataType::Type load_type = instruction->GetType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002045 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002046
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002047 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01002048 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002049 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002050 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002051 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002052 Location maybe_temp =
2053 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002054 // Note that potential implicit null checks are handled in this
2055 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2056 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2057 instruction,
2058 out,
2059 base,
2060 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002061 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002062 /* needs_null_check= */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002063 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002064 } else {
2065 // General case.
2066 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002067 // Note that a potential implicit null check is handled in this
2068 // CodeGeneratorARM64::LoadAcquire call.
2069 // NB: LoadAcquire will record the pc info if needed.
2070 codegen_->LoadAcquire(
Andreas Gampe3db70682018-12-26 15:12:03 -08002071 instruction, OutputCPURegister(instruction), field, /* needs_null_check= */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002072 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002073 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2074 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01002075 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002076 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002077 }
Vladimir Marko61b92282017-10-11 13:23:17 +01002078 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002079 // If read barriers are enabled, emit read barriers other than
2080 // Baker's using a slow path (and also unpoison the loaded
2081 // reference, if heap poisoning is enabled).
2082 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2083 }
Roland Levillain4d027112015-07-01 15:41:14 +01002084 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002085}
2086
2087void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2088 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002089 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01002090 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002091 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2092 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002093 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002094 locations->SetInAt(1, Location::RequiresFpuRegister());
2095 } else {
2096 locations->SetInAt(1, Location::RequiresRegister());
2097 }
2098}
2099
2100void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002101 const FieldInfo& field_info,
2102 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002103 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2104
2105 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002106 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002107 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002108 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002109 DataType::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002110
Roland Levillain4d027112015-07-01 15:41:14 +01002111 {
2112 // We use a block to end the scratch scope before the write barrier, thus
2113 // freeing the temporary registers so they can be used in `MarkGCCard`.
2114 UseScratchRegisterScope temps(GetVIXLAssembler());
2115
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002116 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002117 DCHECK(value.IsW());
2118 Register temp = temps.AcquireW();
2119 __ Mov(temp, value.W());
2120 GetAssembler()->PoisonHeapReference(temp.W());
2121 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002122 }
Roland Levillain4d027112015-07-01 15:41:14 +01002123
2124 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002125 codegen_->StoreRelease(
Andreas Gampe3db70682018-12-26 15:12:03 -08002126 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check= */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002127 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002128 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2129 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002130 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2131 codegen_->MaybeRecordImplicitNullCheck(instruction);
2132 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002133 }
2134
2135 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002136 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002137 }
2138}
2139
Alexandre Rames67555f72014-11-18 10:55:16 +00002140void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002141 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002142
2143 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002144 case DataType::Type::kInt32:
2145 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002146 Register dst = OutputRegister(instr);
2147 Register lhs = InputRegisterAt(instr, 0);
2148 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002149 if (instr->IsAdd()) {
2150 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002151 } else if (instr->IsAnd()) {
2152 __ And(dst, lhs, rhs);
2153 } else if (instr->IsOr()) {
2154 __ Orr(dst, lhs, rhs);
2155 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002156 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002157 } else if (instr->IsRor()) {
2158 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002159 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002160 __ Ror(dst, lhs, shift);
2161 } else {
2162 // Ensure shift distance is in the same size register as the result. If
2163 // we are rotating a long and the shift comes in a w register originally,
2164 // we don't need to sxtw for use as an x since the shift distances are
2165 // all & reg_bits - 1.
2166 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2167 }
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002168 } else if (instr->IsMin() || instr->IsMax()) {
2169 __ Cmp(lhs, rhs);
2170 __ Csel(dst, lhs, rhs, instr->IsMin() ? lt : gt);
Alexandre Rames67555f72014-11-18 10:55:16 +00002171 } else {
2172 DCHECK(instr->IsXor());
2173 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002174 }
2175 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002176 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002177 case DataType::Type::kFloat32:
2178 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002179 VRegister dst = OutputFPRegister(instr);
2180 VRegister lhs = InputFPRegisterAt(instr, 0);
2181 VRegister rhs = InputFPRegisterAt(instr, 1);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002182 if (instr->IsAdd()) {
2183 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002184 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002185 __ Fsub(dst, lhs, rhs);
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002186 } else if (instr->IsMin()) {
2187 __ Fmin(dst, lhs, rhs);
2188 } else if (instr->IsMax()) {
2189 __ Fmax(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002190 } else {
2191 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002192 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002193 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002194 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002195 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002196 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002197 }
2198}
2199
Serban Constantinescu02164b32014-11-13 14:05:07 +00002200void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2201 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2202
Vladimir Markoca6fff82017-10-03 14:49:14 +01002203 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002204 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002205 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002206 case DataType::Type::kInt32:
2207 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002208 locations->SetInAt(0, Location::RequiresRegister());
2209 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002210 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002211 break;
2212 }
2213 default:
2214 LOG(FATAL) << "Unexpected shift type " << type;
2215 }
2216}
2217
2218void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2219 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2220
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002221 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002222 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002223 case DataType::Type::kInt32:
2224 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002225 Register dst = OutputRegister(instr);
2226 Register lhs = InputRegisterAt(instr, 0);
2227 Operand rhs = InputOperandAt(instr, 1);
2228 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002229 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002230 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002231 if (instr->IsShl()) {
2232 __ Lsl(dst, lhs, shift_value);
2233 } else if (instr->IsShr()) {
2234 __ Asr(dst, lhs, shift_value);
2235 } else {
2236 __ Lsr(dst, lhs, shift_value);
2237 }
2238 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002239 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002240
2241 if (instr->IsShl()) {
2242 __ Lsl(dst, lhs, rhs_reg);
2243 } else if (instr->IsShr()) {
2244 __ Asr(dst, lhs, rhs_reg);
2245 } else {
2246 __ Lsr(dst, lhs, rhs_reg);
2247 }
2248 }
2249 break;
2250 }
2251 default:
2252 LOG(FATAL) << "Unexpected shift operation type " << type;
2253 }
2254}
2255
Alexandre Rames5319def2014-10-23 10:03:10 +01002256void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002257 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002258}
2259
2260void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002261 HandleBinaryOp(instruction);
2262}
2263
2264void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2265 HandleBinaryOp(instruction);
2266}
2267
2268void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2269 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002270}
2271
Artem Serov7fc63502016-02-09 17:15:29 +00002272void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002273 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002274 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002275 locations->SetInAt(0, Location::RequiresRegister());
2276 // There is no immediate variant of negated bitwise instructions in AArch64.
2277 locations->SetInAt(1, Location::RequiresRegister());
2278 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2279}
2280
Artem Serov7fc63502016-02-09 17:15:29 +00002281void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002282 Register dst = OutputRegister(instr);
2283 Register lhs = InputRegisterAt(instr, 0);
2284 Register rhs = InputRegisterAt(instr, 1);
2285
2286 switch (instr->GetOpKind()) {
2287 case HInstruction::kAnd:
2288 __ Bic(dst, lhs, rhs);
2289 break;
2290 case HInstruction::kOr:
2291 __ Orn(dst, lhs, rhs);
2292 break;
2293 case HInstruction::kXor:
2294 __ Eon(dst, lhs, rhs);
2295 break;
2296 default:
2297 LOG(FATAL) << "Unreachable";
2298 }
2299}
2300
Anton Kirilov74234da2017-01-13 14:42:47 +00002301void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2302 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002303 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2304 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002305 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002306 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002307 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2308 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2309 } else {
2310 locations->SetInAt(0, Location::RequiresRegister());
2311 }
2312 locations->SetInAt(1, Location::RequiresRegister());
2313 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2314}
2315
Anton Kirilov74234da2017-01-13 14:42:47 +00002316void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2317 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002318 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002319 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002320 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002321 Register out = OutputRegister(instruction);
2322 Register left;
2323 if (kind != HInstruction::kNeg) {
2324 left = InputRegisterAt(instruction, 0);
2325 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002326 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002327 // shifter operand operation, the IR generating `right_reg` (input to the type
2328 // conversion) can have a different type from the current instruction's type,
2329 // so we manually indicate the type.
2330 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002331 Operand right_operand(0);
2332
Anton Kirilov74234da2017-01-13 14:42:47 +00002333 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2334 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002335 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2336 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002337 right_operand = Operand(right_reg,
2338 helpers::ShiftFromOpKind(op_kind),
2339 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002340 }
2341
2342 // Logical binary operations do not support extension operations in the
2343 // operand. Note that VIXL would still manage if it was passed by generating
2344 // the extension as a separate instruction.
2345 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2346 DCHECK(!right_operand.IsExtendedRegister() ||
2347 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2348 kind != HInstruction::kNeg));
2349 switch (kind) {
2350 case HInstruction::kAdd:
2351 __ Add(out, left, right_operand);
2352 break;
2353 case HInstruction::kAnd:
2354 __ And(out, left, right_operand);
2355 break;
2356 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002357 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002358 __ Neg(out, right_operand);
2359 break;
2360 case HInstruction::kOr:
2361 __ Orr(out, left, right_operand);
2362 break;
2363 case HInstruction::kSub:
2364 __ Sub(out, left, right_operand);
2365 break;
2366 case HInstruction::kXor:
2367 __ Eor(out, left, right_operand);
2368 break;
2369 default:
2370 LOG(FATAL) << "Unexpected operation kind: " << kind;
2371 UNREACHABLE();
2372 }
2373}
2374
Artem Serov328429f2016-07-06 16:23:04 +01002375void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002376 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002377 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002378 locations->SetInAt(0, Location::RequiresRegister());
2379 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002380 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002381}
2382
Roland Levillain19c54192016-11-04 13:44:09 +00002383void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002384 __ Add(OutputRegister(instruction),
2385 InputRegisterAt(instruction, 0),
2386 Operand(InputOperandAt(instruction, 1)));
2387}
2388
Artem Serove1811ed2017-04-27 16:50:47 +01002389void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2390 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002391 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002392
2393 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2394
2395 locations->SetInAt(0, Location::RequiresRegister());
2396 // For byte case we don't need to shift the index variable so we can encode the data offset into
2397 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2398 // data offset constant generation out of the loop and reduce the critical path length in the
2399 // loop.
2400 locations->SetInAt(1, shift->GetValue() == 0
2401 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2402 : Location::RequiresRegister());
2403 locations->SetInAt(2, Location::ConstantLocation(shift));
2404 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2405}
2406
2407void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2408 HIntermediateAddressIndex* instruction) {
2409 Register index_reg = InputRegisterAt(instruction, 0);
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002410 uint32_t shift = Int64FromLocation(instruction->GetLocations()->InAt(2));
Artem Serove1811ed2017-04-27 16:50:47 +01002411 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2412
2413 if (shift == 0) {
2414 __ Add(OutputRegister(instruction), index_reg, offset);
2415 } else {
2416 Register offset_reg = InputRegisterAt(instruction, 1);
2417 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2418 }
2419}
2420
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002421void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002422 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002423 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002424 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2425 if (instr->GetOpKind() == HInstruction::kSub &&
2426 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002427 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002428 // Don't allocate register for Mneg instruction.
2429 } else {
2430 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2431 Location::RequiresRegister());
2432 }
2433 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2434 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002435 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2436}
2437
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002438void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002439 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002440 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2441 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002442
2443 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2444 // This fixup should be carried out for all multiply-accumulate instructions:
2445 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002446 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002447 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2448 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002449 vixl::aarch64::Instruction* prev =
2450 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002451 if (prev->IsLoadOrStore()) {
2452 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002453 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002454 __ nop();
2455 }
2456 }
2457
2458 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002459 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002460 __ Madd(res, mul_left, mul_right, accumulator);
2461 } else {
2462 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002463 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002464 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002465 __ Mneg(res, mul_left, mul_right);
2466 } else {
2467 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2468 __ Msub(res, mul_left, mul_right, accumulator);
2469 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002470 }
2471}
2472
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002473void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002474 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002475 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002476 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002477 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2478 object_array_get_with_read_barrier
2479 ? LocationSummary::kCallOnSlowPath
2480 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002481 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002482 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002483 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002484 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002485 // We need a temporary register for the read barrier load in
2486 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2487 // only if the offset is too big.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002488 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2489 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002490 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002491 if (offset >= kReferenceLoadMinFarOffset) {
2492 locations->AddTemp(FixedTempLocation());
2493 }
Artem Serov0806f582018-10-11 20:14:20 +01002494 } else if (!instruction->GetArray()->IsIntermediateAddress()) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01002495 // We need a non-scratch temporary for the array data pointer in
Artem Serov0806f582018-10-11 20:14:20 +01002496 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier() for the case with no
2497 // intermediate address.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002498 locations->AddTemp(Location::RequiresRegister());
2499 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002500 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002501 locations->SetInAt(0, Location::RequiresRegister());
2502 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002503 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002504 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2505 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002506 // The output overlaps in the case of an object array get with
2507 // read barriers enabled: we do not want the move to overwrite the
2508 // array's location, as we need it to emit the read barrier.
2509 locations->SetOut(
2510 Location::RequiresRegister(),
2511 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002512 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002513}
2514
2515void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002516 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002517 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002518 LocationSummary* locations = instruction->GetLocations();
2519 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002520 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002521 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002522 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2523 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002524 MacroAssembler* masm = GetVIXLAssembler();
2525 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002526
Artem Serov0806f582018-10-11 20:14:20 +01002527 // The non-Baker read barrier instrumentation of object ArrayGet instructions
Roland Levillain19c54192016-11-04 13:44:09 +00002528 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002529 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002530 instruction->GetArray()->IsIntermediateAddress() &&
Artem Serov0806f582018-10-11 20:14:20 +01002531 kEmitCompilerReadBarrier &&
2532 !kUseBakerReadBarrier));
Roland Levillain19c54192016-11-04 13:44:09 +00002533
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002534 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002535 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002536 // Note that a potential implicit null check is handled in the
2537 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002538 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002539 if (index.IsConstant()) {
Artem Serov0806f582018-10-11 20:14:20 +01002540 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002541 // Array load with a constant index can be treated as a field load.
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002542 offset += Int64FromLocation(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002543 Location maybe_temp =
2544 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2545 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2546 out,
2547 obj.W(),
2548 offset,
2549 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002550 /* needs_null_check= */ false,
2551 /* use_load_acquire= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002552 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002553 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08002554 instruction, out, obj.W(), offset, index, /* needs_null_check= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002555 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002556 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002557 // General case.
2558 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002559 Register length;
2560 if (maybe_compressed_char_at) {
2561 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2562 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002563 {
2564 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2565 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2566
2567 if (instruction->GetArray()->IsIntermediateAddress()) {
2568 DCHECK_LT(count_offset, offset);
2569 int64_t adjusted_offset =
2570 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2571 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2572 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2573 } else {
2574 __ Ldr(length, HeapOperand(obj, count_offset));
2575 }
2576 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002577 }
jessicahandojo05765752016-09-09 19:01:32 -07002578 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002579 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002580 if (maybe_compressed_char_at) {
2581 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002582 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2583 "Expecting 0=compressed, 1=uncompressed");
2584 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002585 __ Ldrb(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002586 HeapOperand(obj, offset + Int64FromLocation(index)));
jessicahandojo05765752016-09-09 19:01:32 -07002587 __ B(&done);
2588 __ Bind(&uncompressed_load);
2589 __ Ldrh(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002590 HeapOperand(obj, offset + (Int64FromLocation(index) << 1)));
jessicahandojo05765752016-09-09 19:01:32 -07002591 __ Bind(&done);
2592 } else {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002593 offset += Int64FromLocation(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002594 source = HeapOperand(obj, offset);
2595 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002596 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002597 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002598 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002599 // We do not need to compute the intermediate address from the array: the
2600 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002601 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002602 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002603 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2604 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
Roland Levillain44015862016-01-22 11:47:17 +00002605 }
2606 temp = obj;
2607 } else {
2608 __ Add(temp, obj, offset);
2609 }
jessicahandojo05765752016-09-09 19:01:32 -07002610 if (maybe_compressed_char_at) {
2611 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002612 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2613 "Expecting 0=compressed, 1=uncompressed");
2614 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002615 __ Ldrb(Register(OutputCPURegister(instruction)),
2616 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2617 __ B(&done);
2618 __ Bind(&uncompressed_load);
2619 __ Ldrh(Register(OutputCPURegister(instruction)),
2620 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2621 __ Bind(&done);
2622 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002623 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002624 }
Roland Levillain44015862016-01-22 11:47:17 +00002625 }
jessicahandojo05765752016-09-09 19:01:32 -07002626 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002627 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2628 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002629 codegen_->Load(type, OutputCPURegister(instruction), source);
2630 codegen_->MaybeRecordImplicitNullCheck(instruction);
2631 }
Roland Levillain44015862016-01-22 11:47:17 +00002632
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002633 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002634 static_assert(
2635 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2636 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2637 Location obj_loc = locations->InAt(0);
2638 if (index.IsConstant()) {
2639 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2640 } else {
2641 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2642 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002643 }
Roland Levillain4d027112015-07-01 15:41:14 +01002644 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002645}
2646
Alexandre Rames5319def2014-10-23 10:03:10 +01002647void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002648 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002649 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002650 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002651}
2652
2653void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002654 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002655 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002656 {
2657 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2658 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2659 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2660 codegen_->MaybeRecordImplicitNullCheck(instruction);
2661 }
jessicahandojo05765752016-09-09 19:01:32 -07002662 // Mask out compression flag from String's array length.
2663 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002664 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002665 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002666}
2667
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002668void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002669 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002670
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002671 bool needs_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002672 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002673 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002674 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002675 locations->SetInAt(0, Location::RequiresRegister());
2676 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002677 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2678 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002679 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002680 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002681 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002682 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002683 }
2684}
2685
2686void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002687 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002688 LocationSummary* locations = instruction->GetLocations();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002689 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002690 bool needs_write_barrier =
2691 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002692
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002693 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002694 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002695 CPURegister source = value;
2696 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002697 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002698 MemOperand destination = HeapOperand(array);
2699 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002700
2701 if (!needs_write_barrier) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002702 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002703 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002704 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002705 destination = HeapOperand(array, offset);
2706 } else {
2707 UseScratchRegisterScope temps(masm);
2708 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002709 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002710 // We do not need to compute the intermediate address from the array: the
2711 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002712 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002713 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002714 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2715 DCHECK(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002716 }
2717 temp = array;
2718 } else {
2719 __ Add(temp, array, offset);
2720 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002721 destination = HeapOperand(temp,
2722 XRegisterFrom(index),
2723 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002724 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002725 }
Artem Serov914d7a82017-02-07 14:33:49 +00002726 {
2727 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2728 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2729 codegen_->Store(value_type, value, destination);
2730 codegen_->MaybeRecordImplicitNullCheck(instruction);
2731 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002732 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002733 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002734
2735 bool can_value_be_null = instruction->GetValueCanBeNull();
2736 vixl::aarch64::Label do_store;
2737 if (can_value_be_null) {
2738 __ Cbz(Register(value), &do_store);
2739 }
2740
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002741 SlowPathCodeARM64* slow_path = nullptr;
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002742 if (needs_type_check) {
2743 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
2744 codegen_->AddSlowPath(slow_path);
2745
2746 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2747 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2748 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2749
Alexandre Rames97833a02015-04-16 15:07:12 +01002750 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002751 Register temp = temps.AcquireSameSizeAs(array);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002752 Register temp2 = temps.AcquireSameSizeAs(array);
2753
2754 // Note that when Baker read barriers are enabled, the type
2755 // checks are performed without read barriers. This is fine,
2756 // even in the case where a class object is in the from-space
2757 // after the flip, as a comparison involving such a type would
2758 // not produce a false positive; it may of course produce a
2759 // false negative, in which case we would take the ArraySet
2760 // slow path.
2761
2762 // /* HeapReference<Class> */ temp = array->klass_
2763 {
2764 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2765 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2766 __ Ldr(temp, HeapOperand(array, class_offset));
2767 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames97833a02015-04-16 15:07:12 +01002768 }
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002769 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Alexandre Rames97833a02015-04-16 15:07:12 +01002770
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002771 // /* HeapReference<Class> */ temp = temp->component_type_
2772 __ Ldr(temp, HeapOperand(temp, component_offset));
2773 // /* HeapReference<Class> */ temp2 = value->klass_
2774 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2775 // If heap poisoning is enabled, no need to unpoison `temp`
2776 // nor `temp2`, as we are comparing two poisoned references.
2777 __ Cmp(temp, temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002778
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002779 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2780 vixl::aarch64::Label do_put;
2781 __ B(eq, &do_put);
2782 // If heap poisoning is enabled, the `temp` reference has
2783 // not been unpoisoned yet; unpoison it now.
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002784 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002785
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002786 // /* HeapReference<Class> */ temp = temp->super_class_
2787 __ Ldr(temp, HeapOperand(temp, super_offset));
2788 // If heap poisoning is enabled, no need to unpoison
2789 // `temp`, as we are comparing against null below.
2790 __ Cbnz(temp, slow_path->GetEntryLabel());
2791 __ Bind(&do_put);
Vladimir Markod1ef8732017-04-18 13:55:13 +01002792 } else {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002793 __ B(ne, slow_path->GetEntryLabel());
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002794 }
2795 }
2796
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002797 codegen_->MarkGCCard(array, value.W(), /* value_can_be_null= */ false);
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002798
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002799 if (can_value_be_null) {
2800 DCHECK(do_store.IsLinked());
2801 __ Bind(&do_store);
2802 }
2803
2804 UseScratchRegisterScope temps(masm);
2805 if (kPoisonHeapReferences) {
2806 Register temp_source = temps.AcquireSameSizeAs(array);
2807 DCHECK(value.IsW());
2808 __ Mov(temp_source, value.W());
2809 GetAssembler()->PoisonHeapReference(temp_source);
2810 source = temp_source;
2811 }
2812
2813 if (index.IsConstant()) {
2814 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
2815 destination = HeapOperand(array, offset);
2816 } else {
2817 Register temp_base = temps.AcquireSameSizeAs(array);
2818 __ Add(temp_base, array, offset);
2819 destination = HeapOperand(temp_base,
2820 XRegisterFrom(index),
2821 LSL,
2822 DataType::SizeShift(value_type));
2823 }
2824
2825 {
2826 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2827 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2828 __ Str(source, destination);
2829
2830 if (can_value_be_null || !needs_type_check) {
2831 codegen_->MaybeRecordImplicitNullCheck(instruction);
2832 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002833 }
2834
2835 if (slow_path != nullptr) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002836 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002837 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002838 }
2839}
2840
Alexandre Rames67555f72014-11-18 10:55:16 +00002841void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002842 RegisterSet caller_saves = RegisterSet::Empty();
2843 InvokeRuntimeCallingConvention calling_convention;
2844 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2845 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2846 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002847
2848 // If both index and length are constant, we can check the bounds statically and
2849 // generate code accordingly. We want to make sure we generate constant locations
2850 // in that case, regardless of whether they are encodable in the comparison or not.
2851 HInstruction* index = instruction->InputAt(0);
2852 HInstruction* length = instruction->InputAt(1);
2853 bool both_const = index->IsConstant() && length->IsConstant();
2854 locations->SetInAt(0, both_const
2855 ? Location::ConstantLocation(index->AsConstant())
2856 : ARM64EncodableConstantOrRegister(index, instruction));
2857 locations->SetInAt(1, both_const
2858 ? Location::ConstantLocation(length->AsConstant())
2859 : ARM64EncodableConstantOrRegister(length, instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002860}
2861
2862void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Georgia Kouvelibe530852019-01-17 10:46:41 +00002863 LocationSummary* locations = instruction->GetLocations();
2864 Location index_loc = locations->InAt(0);
2865 Location length_loc = locations->InAt(1);
2866
2867 int cmp_first_input = 0;
2868 int cmp_second_input = 1;
2869 Condition cond = hs;
2870
2871 if (index_loc.IsConstant()) {
2872 int64_t index = Int64FromLocation(index_loc);
2873 if (length_loc.IsConstant()) {
2874 int64_t length = Int64FromLocation(length_loc);
2875 if (index < 0 || index >= length) {
2876 BoundsCheckSlowPathARM64* slow_path =
2877 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
2878 codegen_->AddSlowPath(slow_path);
2879 __ B(slow_path->GetEntryLabel());
2880 } else {
2881 // BCE will remove the bounds check if we are guaranteed to pass.
2882 // However, some optimization after BCE may have generated this, and we should not
2883 // generate a bounds check if it is a valid range.
2884 }
2885 return;
2886 }
2887 // Only the index is constant: change the order of the operands and commute the condition
2888 // so we can use an immediate constant for the index (only the second input to a cmp
2889 // instruction can be an immediate).
2890 cmp_first_input = 1;
2891 cmp_second_input = 0;
2892 cond = ls;
2893 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002894 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002895 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002896 __ Cmp(InputRegisterAt(instruction, cmp_first_input),
2897 InputOperandAt(instruction, cmp_second_input));
Alexandre Rames67555f72014-11-18 10:55:16 +00002898 codegen_->AddSlowPath(slow_path);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002899 __ B(slow_path->GetEntryLabel(), cond);
Alexandre Rames67555f72014-11-18 10:55:16 +00002900}
2901
Alexandre Rames67555f72014-11-18 10:55:16 +00002902void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2903 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002904 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00002905 locations->SetInAt(0, Location::RequiresRegister());
2906 if (check->HasUses()) {
2907 locations->SetOut(Location::SameAsFirstInput());
2908 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01002909 // Rely on the type initialization to save everything we need.
2910 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Alexandre Rames67555f72014-11-18 10:55:16 +00002911}
2912
2913void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2914 // We assume the class is not null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01002915 SlowPathCodeARM64* slow_path =
2916 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(check->GetLoadClass(), check);
Alexandre Rames67555f72014-11-18 10:55:16 +00002917 codegen_->AddSlowPath(slow_path);
2918 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2919}
2920
Roland Levillain1a653882016-03-18 18:05:57 +00002921static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2922 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2923 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2924}
2925
2926void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002927 VRegister lhs_reg = InputFPRegisterAt(instruction, 0);
Roland Levillain1a653882016-03-18 18:05:57 +00002928 Location rhs_loc = instruction->GetLocations()->InAt(1);
2929 if (rhs_loc.IsConstant()) {
2930 // 0.0 is the only immediate that can be encoded directly in
2931 // an FCMP instruction.
2932 //
2933 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2934 // specify that in a floating-point comparison, positive zero
2935 // and negative zero are considered equal, so we can use the
2936 // literal 0.0 for both cases here.
2937 //
2938 // Note however that some methods (Float.equal, Float.compare,
2939 // Float.compareTo, Double.equal, Double.compare,
2940 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2941 // StrictMath.min) consider 0.0 to be (strictly) greater than
2942 // -0.0. So if we ever translate calls to these methods into a
2943 // HCompare instruction, we must handle the -0.0 case with
2944 // care here.
2945 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2946 __ Fcmp(lhs_reg, 0.0);
2947 } else {
2948 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2949 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002950}
2951
Serban Constantinescu02164b32014-11-13 14:05:07 +00002952void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002953 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002954 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002955 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002956 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002957 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002958 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002959 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002960 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002961 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002962 case DataType::Type::kInt32:
2963 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002964 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002965 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002966 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2967 break;
2968 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002969 case DataType::Type::kFloat32:
2970 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002971 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002972 locations->SetInAt(1,
2973 IsFloatingPointZeroConstant(compare->InputAt(1))
2974 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2975 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002976 locations->SetOut(Location::RequiresRegister());
2977 break;
2978 }
2979 default:
2980 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2981 }
2982}
2983
2984void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002985 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002986
2987 // 0 if: left == right
2988 // 1 if: left > right
2989 // -1 if: left < right
2990 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002991 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002992 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002993 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002994 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002995 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002996 case DataType::Type::kInt32:
2997 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002998 Register result = OutputRegister(compare);
2999 Register left = InputRegisterAt(compare, 0);
3000 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003001 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003002 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3003 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003004 break;
3005 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003006 case DataType::Type::kFloat32:
3007 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003008 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003009 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003010 __ Cset(result, ne);
3011 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003012 break;
3013 }
3014 default:
3015 LOG(FATAL) << "Unimplemented compare type " << in_type;
3016 }
3017}
3018
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003019void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003020 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003021
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003022 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003023 locations->SetInAt(0, Location::RequiresFpuRegister());
3024 locations->SetInAt(1,
3025 IsFloatingPointZeroConstant(instruction->InputAt(1))
3026 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3027 : Location::RequiresFpuRegister());
3028 } else {
3029 // Integer cases.
3030 locations->SetInAt(0, Location::RequiresRegister());
3031 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3032 }
3033
David Brazdilb3e773e2016-01-26 11:28:37 +00003034 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003035 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003036 }
3037}
3038
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003039void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003040 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003041 return;
3042 }
3043
3044 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003045 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003046 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003047
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003048 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003049 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003050 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003051 } else {
3052 // Integer cases.
3053 Register lhs = InputRegisterAt(instruction, 0);
3054 Operand rhs = InputOperandAt(instruction, 1);
3055 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003056 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003057 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003058}
3059
3060#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3061 M(Equal) \
3062 M(NotEqual) \
3063 M(LessThan) \
3064 M(LessThanOrEqual) \
3065 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003066 M(GreaterThanOrEqual) \
3067 M(Below) \
3068 M(BelowOrEqual) \
3069 M(Above) \
3070 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003071#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003072void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3073void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003074FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003075#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003076#undef FOR_EACH_CONDITION_INSTRUCTION
3077
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003078void InstructionCodeGeneratorARM64::GenerateIntDivForPower2Denom(HDiv* instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003079 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003080 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003081 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
3082
3083 Register out = OutputRegister(instruction);
3084 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003085
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003086 Register final_dividend;
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003087 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003088 // No need to adjust the result for non-negative dividends or the INT32_MIN/INT64_MIN dividends.
3089 // NOTE: The generated code for HDiv correctly works for the INT32_MIN/INT64_MIN dividends:
3090 // imm == 2
3091 // add out, dividend(0x80000000), dividend(0x80000000), lsr #31 => out = 0x80000001
3092 // asr out, out(0x80000001), #1 => out = 0xc0000000
3093 // This is the same as 'asr out, 0x80000000, #1'
3094 //
3095 // imm > 2
3096 // add temp, dividend(0x80000000), imm - 1 => temp = 0b10..01..1, where the number
3097 // of the rightmost 1s is ctz_imm.
3098 // cmp dividend(0x80000000), 0 => N = 1, V = 0 (lt is true)
3099 // csel out, temp(0b10..01..1), dividend(0x80000000), lt => out = 0b10..01..1
3100 // asr out, out(0b10..01..1), #ctz_imm => out = 0b1..10..0, where the number of the
3101 // leftmost 1s is ctz_imm + 1.
3102 // This is the same as 'asr out, dividend(0x80000000), #ctz_imm'.
3103 //
3104 // imm == INT32_MIN
3105 // add tmp, dividend(0x80000000), #0x7fffffff => tmp = -1
3106 // cmp dividend(0x80000000), 0 => N = 1, V = 0 (lt is true)
3107 // csel out, temp(-1), dividend(0x80000000), lt => out = -1
3108 // neg out, out(-1), asr #31 => out = 1
3109 // This is the same as 'neg out, dividend(0x80000000), asr #31'.
3110 final_dividend = dividend;
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003111 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003112 if (abs_imm == 2) {
3113 int bits = DataType::Size(instruction->GetResultType()) * kBitsPerByte;
3114 __ Add(out, dividend, Operand(dividend, LSR, bits - 1));
3115 } else {
3116 UseScratchRegisterScope temps(GetVIXLAssembler());
3117 Register temp = temps.AcquireSameSizeAs(out);
3118 __ Add(temp, dividend, abs_imm - 1);
3119 __ Cmp(dividend, 0);
3120 __ Csel(out, temp, dividend, lt);
3121 }
3122 final_dividend = out;
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003123 }
3124
Zheng Xuc6667102015-05-15 16:08:45 +08003125 int ctz_imm = CTZ(abs_imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003126 if (imm > 0) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003127 __ Asr(out, final_dividend, ctz_imm);
Zheng Xuc6667102015-05-15 16:08:45 +08003128 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003129 __ Neg(out, Operand(final_dividend, ASR, ctz_imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003130 }
3131}
3132
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003133// Return true if the magic number was modified by subtracting 2^32(Int32 div) or 2^64(Int64 div).
3134// So dividend needs to be added.
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003135static inline bool NeedToAddDividend(int64_t magic_number, int64_t divisor) {
3136 return divisor > 0 && magic_number < 0;
3137}
3138
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003139// Return true if the magic number was modified by adding 2^32(Int32 div) or 2^64(Int64 div).
3140// So dividend needs to be subtracted.
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003141static inline bool NeedToSubDividend(int64_t magic_number, int64_t divisor) {
3142 return divisor < 0 && magic_number > 0;
3143}
3144
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003145// Generate code which increments the value in register 'in' by 1 if the value is negative.
3146// It is done with 'add out, in, in, lsr #31 or #63'.
3147// If the value is a result of an operation setting the N flag, CINC MI can be used
3148// instead of ADD. 'use_cond_inc' controls this.
3149void InstructionCodeGeneratorARM64::GenerateIncrementNegativeByOne(
3150 Register out,
3151 Register in,
3152 bool use_cond_inc) {
3153 if (use_cond_inc) {
3154 __ Cinc(out, in, mi);
3155 } else {
3156 __ Add(out, in, Operand(in, LSR, in.GetSizeInBits() - 1));
3157 }
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003158}
3159
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003160// Helper to generate code producing the result of HRem with a constant divisor.
3161void InstructionCodeGeneratorARM64::GenerateResultRemWithAnyConstant(
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003162 Register out,
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003163 Register dividend,
3164 Register quotient,
3165 int64_t divisor,
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003166 UseScratchRegisterScope* temps_scope) {
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003167 Register temp_imm = temps_scope->AcquireSameSizeAs(out);
3168 __ Mov(temp_imm, divisor);
3169 __ Msub(out, quotient, temp_imm, dividend);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003170}
3171
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003172// Helper to generate code for HDiv/HRem instructions when a dividend is non-negative and
3173// a divisor is a positive constant, not power of 2.
3174void InstructionCodeGeneratorARM64::GenerateInt64UnsignedDivRemWithAnyPositiveConstant(
3175 HBinaryOperation* instruction) {
3176 DCHECK(instruction->IsDiv() || instruction->IsRem());
3177 DCHECK(instruction->GetResultType() == DataType::Type::kInt64);
3178
3179 LocationSummary* locations = instruction->GetLocations();
3180 Location second = locations->InAt(1);
3181 DCHECK(second.IsConstant());
3182
3183 Register out = OutputRegister(instruction);
3184 Register dividend = InputRegisterAt(instruction, 0);
3185 int64_t imm = Int64FromConstant(second.GetConstant());
3186 DCHECK_GT(imm, 0);
3187
3188 int64_t magic;
3189 int shift;
3190 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ true, &magic, &shift);
3191
3192 UseScratchRegisterScope temps(GetVIXLAssembler());
3193 Register temp = temps.AcquireSameSizeAs(out);
3194
3195 auto generate_unsigned_div_code = [this, magic, shift](Register out,
3196 Register dividend,
3197 Register temp) {
3198 // temp = get_high(dividend * magic)
3199 __ Mov(temp, magic);
3200 if (magic > 0 && shift == 0) {
3201 __ Smulh(out, dividend, temp);
3202 } else {
3203 __ Smulh(temp, dividend, temp);
3204 if (magic < 0) {
3205 // The negative magic means that the multiplier m is greater than INT64_MAX.
3206 // In such a case shift is never 0. See the proof in
3207 // InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant.
3208 __ Add(temp, temp, dividend);
3209 }
3210 DCHECK_NE(shift, 0);
3211 __ Lsr(out, temp, shift);
3212 }
3213 };
3214
3215 if (instruction->IsDiv()) {
3216 generate_unsigned_div_code(out, dividend, temp);
3217 } else {
3218 generate_unsigned_div_code(temp, dividend, temp);
3219 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3220 }
3221}
3222
3223// Helper to generate code for HDiv/HRem instructions for any dividend and a constant divisor
3224// (not power of 2).
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003225void InstructionCodeGeneratorARM64::GenerateInt64DivRemWithAnyConstant(
3226 HBinaryOperation* instruction) {
Zheng Xuc6667102015-05-15 16:08:45 +08003227 DCHECK(instruction->IsDiv() || instruction->IsRem());
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003228 DCHECK(instruction->GetResultType() == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003229
3230 LocationSummary* locations = instruction->GetLocations();
3231 Location second = locations->InAt(1);
3232 DCHECK(second.IsConstant());
3233
3234 Register out = OutputRegister(instruction);
3235 Register dividend = InputRegisterAt(instruction, 0);
3236 int64_t imm = Int64FromConstant(second.GetConstant());
3237
Zheng Xuc6667102015-05-15 16:08:45 +08003238 int64_t magic;
3239 int shift;
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003240 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ true, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08003241
3242 UseScratchRegisterScope temps(GetVIXLAssembler());
3243 Register temp = temps.AcquireSameSizeAs(out);
3244
3245 // temp = get_high(dividend * magic)
3246 __ Mov(temp, magic);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003247 __ Smulh(temp, dividend, temp);
3248
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003249 // The multiplication result might need some corrections to be finalized.
3250 // The last correction is to increment by 1, if the result is negative.
3251 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3252 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3253 // However if one of the corrections is ADD or SUB, the sign can be detected
3254 // with ADDS/SUBS. They set the N flag if the result is negative.
3255 // This allows to use CINC MI which has latency 1.
3256 bool use_cond_inc = false;
3257
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003258 // Some combinations of magic_number and the divisor require to correct the result.
3259 // Check whether the correction is needed.
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003260 if (NeedToAddDividend(magic, imm)) {
3261 __ Adds(temp, temp, dividend);
3262 use_cond_inc = true;
3263 } else if (NeedToSubDividend(magic, imm)) {
3264 __ Subs(temp, temp, dividend);
3265 use_cond_inc = true;
3266 }
3267
3268 if (shift != 0) {
3269 __ Asr(temp, temp, shift);
3270 }
3271
3272 if (instruction->IsRem()) {
3273 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3274 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3275 } else {
3276 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3277 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003278}
3279
3280void InstructionCodeGeneratorARM64::GenerateInt32DivRemWithAnyConstant(
3281 HBinaryOperation* instruction) {
3282 DCHECK(instruction->IsDiv() || instruction->IsRem());
3283 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
3284
3285 LocationSummary* locations = instruction->GetLocations();
3286 Location second = locations->InAt(1);
3287 DCHECK(second.IsConstant());
3288
3289 Register out = OutputRegister(instruction);
3290 Register dividend = InputRegisterAt(instruction, 0);
3291 int64_t imm = Int64FromConstant(second.GetConstant());
3292
3293 int64_t magic;
3294 int shift;
3295 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
3296 UseScratchRegisterScope temps(GetVIXLAssembler());
3297 Register temp = temps.AcquireSameSizeAs(out);
3298
3299 // temp = get_high(dividend * magic)
3300 __ Mov(temp, magic);
3301 __ Smull(temp.X(), dividend, temp);
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003302
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003303 // The multiplication result might need some corrections to be finalized.
3304 // The last correction is to increment by 1, if the result is negative.
3305 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3306 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3307 // However if one of the corrections is ADD or SUB, the sign can be detected
3308 // with ADDS/SUBS. They set the N flag if the result is negative.
3309 // This allows to use CINC MI which has latency 1.
3310 bool use_cond_inc = false;
3311
3312 // ADD/SUB correction is performed in the high 32 bits
3313 // as high 32 bits are ignored because type are kInt32.
3314 if (NeedToAddDividend(magic, imm)) {
3315 __ Adds(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3316 use_cond_inc = true;
3317 } else if (NeedToSubDividend(magic, imm)) {
3318 __ Subs(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3319 use_cond_inc = true;
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003320 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003321
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003322 // Extract the result from the high 32 bits and apply the final right shift.
3323 DCHECK_LT(shift, 32);
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003324 if (imm > 0 && HasNonNegativeInputAt(instruction, 0)) {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01003325 // No need to adjust the result for a non-negative dividend and a positive divisor.
3326 if (instruction->IsDiv()) {
3327 __ Lsr(out.X(), temp.X(), 32 + shift);
3328 } else {
3329 __ Lsr(temp.X(), temp.X(), 32 + shift);
3330 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3331 }
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003332 } else {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01003333 __ Asr(temp.X(), temp.X(), 32 + shift);
3334
3335 if (instruction->IsRem()) {
3336 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3337 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3338 } else {
3339 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3340 }
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003341 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003342}
3343
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003344void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction,
3345 int64_t divisor) {
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003346 DCHECK(instruction->IsDiv() || instruction->IsRem());
3347 if (instruction->GetResultType() == DataType::Type::kInt64) {
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003348 if (divisor > 0 && HasNonNegativeInputAt(instruction, 0)) {
3349 GenerateInt64UnsignedDivRemWithAnyPositiveConstant(instruction);
3350 } else {
3351 GenerateInt64DivRemWithAnyConstant(instruction);
3352 }
Zheng Xuc6667102015-05-15 16:08:45 +08003353 } else {
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003354 GenerateInt32DivRemWithAnyConstant(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003355 }
3356}
3357
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003358void InstructionCodeGeneratorARM64::GenerateIntDivForConstDenom(HDiv *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003359 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Zheng Xuc6667102015-05-15 16:08:45 +08003360
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003361 if (imm == 0) {
3362 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3363 return;
3364 }
Zheng Xuc6667102015-05-15 16:08:45 +08003365
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003366 if (IsPowerOfTwo(AbsOrMin(imm))) {
3367 GenerateIntDivForPower2Denom(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003368 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003369 // Cases imm == -1 or imm == 1 are handled by InstructionSimplifier.
3370 DCHECK(imm < -2 || imm > 2) << imm;
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003371 GenerateDivRemWithAnyConstant(instruction, imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003372 }
3373}
3374
3375void InstructionCodeGeneratorARM64::GenerateIntDiv(HDiv *instruction) {
3376 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
3377 << instruction->GetResultType();
3378
3379 if (instruction->GetLocations()->InAt(1).IsConstant()) {
3380 GenerateIntDivForConstDenom(instruction);
3381 } else {
3382 Register out = OutputRegister(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003383 Register dividend = InputRegisterAt(instruction, 0);
3384 Register divisor = InputRegisterAt(instruction, 1);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003385 __ Sdiv(out, dividend, divisor);
Zheng Xuc6667102015-05-15 16:08:45 +08003386 }
3387}
3388
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003389void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3390 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003391 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003392 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003393 case DataType::Type::kInt32:
3394 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003395 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003396 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003397 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3398 break;
3399
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003400 case DataType::Type::kFloat32:
3401 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003402 locations->SetInAt(0, Location::RequiresFpuRegister());
3403 locations->SetInAt(1, Location::RequiresFpuRegister());
3404 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3405 break;
3406
3407 default:
3408 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3409 }
3410}
3411
3412void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003413 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003414 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003415 case DataType::Type::kInt32:
3416 case DataType::Type::kInt64:
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003417 GenerateIntDiv(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003418 break;
3419
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003420 case DataType::Type::kFloat32:
3421 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003422 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3423 break;
3424
3425 default:
3426 LOG(FATAL) << "Unexpected div type " << type;
3427 }
3428}
3429
Alexandre Rames67555f72014-11-18 10:55:16 +00003430void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003431 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003432 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003433}
3434
3435void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3436 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003437 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003438 codegen_->AddSlowPath(slow_path);
3439 Location value = instruction->GetLocations()->InAt(0);
3440
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003441 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003442
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003443 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003444 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Elliott Hughesc1896c92018-11-29 11:33:18 -08003445 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003446 }
3447
Alexandre Rames67555f72014-11-18 10:55:16 +00003448 if (value.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003449 int64_t divisor = Int64FromLocation(value);
Alexandre Rames67555f72014-11-18 10:55:16 +00003450 if (divisor == 0) {
3451 __ B(slow_path->GetEntryLabel());
3452 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003453 // A division by a non-null constant is valid. We don't need to perform
3454 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003455 }
3456 } else {
3457 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3458 }
3459}
3460
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003461void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3462 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003463 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003464 locations->SetOut(Location::ConstantLocation(constant));
3465}
3466
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003467void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3468 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003469 // Will be generated at use site.
3470}
3471
Alexandre Rames5319def2014-10-23 10:03:10 +01003472void LocationsBuilderARM64::VisitExit(HExit* exit) {
3473 exit->SetLocations(nullptr);
3474}
3475
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003476void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003477}
3478
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003479void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3480 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003481 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003482 locations->SetOut(Location::ConstantLocation(constant));
3483}
3484
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003485void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003486 // Will be generated at use site.
3487}
3488
David Brazdilfc6a86a2015-06-26 10:33:45 +00003489void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003490 if (successor->IsExitBlock()) {
3491 DCHECK(got->GetPrevious()->AlwaysThrows());
3492 return; // no code needed
3493 }
3494
Serban Constantinescu02164b32014-11-13 14:05:07 +00003495 HBasicBlock* block = got->GetBlock();
3496 HInstruction* previous = got->GetPrevious();
3497 HLoopInformation* info = block->GetLoopInformation();
3498
David Brazdil46e2a392015-03-16 17:31:52 +00003499 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00003500 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003501 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3502 return;
3503 }
3504 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3505 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08003506 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003507 }
3508 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003509 __ B(codegen_->GetLabelOf(successor));
3510 }
3511}
3512
David Brazdilfc6a86a2015-06-26 10:33:45 +00003513void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3514 got->SetLocations(nullptr);
3515}
3516
3517void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3518 HandleGoto(got, got->GetSuccessor());
3519}
3520
3521void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3522 try_boundary->SetLocations(nullptr);
3523}
3524
3525void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3526 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3527 if (!successor->IsExitBlock()) {
3528 HandleGoto(try_boundary, successor);
3529 }
3530}
3531
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003532void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003533 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003534 vixl::aarch64::Label* true_target,
3535 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003536 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003537
David Brazdil0debae72015-11-12 18:37:00 +00003538 if (true_target == nullptr && false_target == nullptr) {
3539 // Nothing to do. The code always falls through.
3540 return;
3541 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003542 // Constant condition, statically compared against "true" (integer value 1).
3543 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003544 if (true_target != nullptr) {
3545 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003546 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003547 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003548 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003549 if (false_target != nullptr) {
3550 __ B(false_target);
3551 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003552 }
David Brazdil0debae72015-11-12 18:37:00 +00003553 return;
3554 }
3555
3556 // The following code generates these patterns:
3557 // (1) true_target == nullptr && false_target != nullptr
3558 // - opposite condition true => branch to false_target
3559 // (2) true_target != nullptr && false_target == nullptr
3560 // - condition true => branch to true_target
3561 // (3) true_target != nullptr && false_target != nullptr
3562 // - condition true => branch to true_target
3563 // - branch to false_target
3564 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003565 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003566 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003567 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003568 if (true_target == nullptr) {
3569 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3570 } else {
3571 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3572 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003573 } else {
3574 // The condition instruction has not been materialized, use its inputs as
3575 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003576 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003577
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003578 DataType::Type type = condition->InputAt(0)->GetType();
3579 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003580 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003581 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003582 IfCondition opposite_condition = condition->GetOppositeCondition();
3583 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003584 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003585 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003586 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003587 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003588 // Integer cases.
3589 Register lhs = InputRegisterAt(condition, 0);
3590 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003591
3592 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003593 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003594 if (true_target == nullptr) {
3595 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3596 non_fallthrough_target = false_target;
3597 } else {
3598 arm64_cond = ARM64Condition(condition->GetCondition());
3599 non_fallthrough_target = true_target;
3600 }
3601
Aart Bik086d27e2016-01-20 17:02:00 -08003602 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003603 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003604 switch (arm64_cond) {
3605 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003606 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003607 break;
3608 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003609 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003610 break;
3611 case lt:
3612 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003613 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003614 break;
3615 case ge:
3616 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003617 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003618 break;
3619 default:
3620 // Without the `static_cast` the compiler throws an error for
3621 // `-Werror=sign-promo`.
3622 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3623 }
3624 } else {
3625 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003626 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003627 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003628 }
3629 }
David Brazdil0debae72015-11-12 18:37:00 +00003630
3631 // If neither branch falls through (case 3), the conditional branch to `true_target`
3632 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3633 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003634 __ B(false_target);
3635 }
3636}
3637
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003638void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003639 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003640 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003641 locations->SetInAt(0, Location::RequiresRegister());
3642 }
3643}
3644
3645void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003646 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3647 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003648 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3649 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3650 true_target = nullptr;
3651 }
3652 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3653 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3654 false_target = nullptr;
3655 }
Andreas Gampe3db70682018-12-26 15:12:03 -08003656 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003657}
3658
3659void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003660 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003661 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003662 InvokeRuntimeCallingConvention calling_convention;
3663 RegisterSet caller_saves = RegisterSet::Empty();
3664 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3665 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003666 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003667 locations->SetInAt(0, Location::RequiresRegister());
3668 }
3669}
3670
3671void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003672 SlowPathCodeARM64* slow_path =
3673 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003674 GenerateTestAndBranch(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08003675 /* condition_input_index= */ 0,
David Brazdil0debae72015-11-12 18:37:00 +00003676 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08003677 /* false_target= */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003678}
3679
Mingyao Yang063fc772016-08-02 11:02:54 -07003680void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003681 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003682 LocationSummary(flag, LocationSummary::kNoCall);
3683 locations->SetOut(Location::RequiresRegister());
3684}
3685
3686void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3687 __ Ldr(OutputRegister(flag),
3688 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3689}
3690
David Brazdilc0b601b2016-02-08 14:20:45 +00003691static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3692 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003693 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003694}
3695
Alexandre Rames880f1192016-06-13 16:04:50 +01003696static inline Condition GetConditionForSelect(HCondition* condition) {
3697 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003698 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3699 : ARM64Condition(cond);
3700}
3701
David Brazdil74eb1b22015-12-14 11:44:01 +00003702void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003703 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003704 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003705 locations->SetInAt(0, Location::RequiresFpuRegister());
3706 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003707 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003708 } else {
3709 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3710 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3711 bool is_true_value_constant = cst_true_value != nullptr;
3712 bool is_false_value_constant = cst_false_value != nullptr;
3713 // Ask VIXL whether we should synthesize constants in registers.
3714 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3715 Operand true_op = is_true_value_constant ?
3716 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3717 Operand false_op = is_false_value_constant ?
3718 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3719 bool true_value_in_register = false;
3720 bool false_value_in_register = false;
3721 MacroAssembler::GetCselSynthesisInformation(
3722 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3723 true_value_in_register |= !is_true_value_constant;
3724 false_value_in_register |= !is_false_value_constant;
3725
3726 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3727 : Location::ConstantLocation(cst_true_value));
3728 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3729 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003730 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003731 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003732
David Brazdil74eb1b22015-12-14 11:44:01 +00003733 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3734 locations->SetInAt(2, Location::RequiresRegister());
3735 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003736}
3737
3738void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003739 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003740 Condition csel_cond;
3741
3742 if (IsBooleanValueOrMaterializedCondition(cond)) {
3743 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003744 // Use the condition flags set by the previous instruction.
3745 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003746 } else {
3747 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003748 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003749 }
3750 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003751 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003752 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003753 } else {
3754 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003755 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003756 }
3757
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003758 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003759 __ Fcsel(OutputFPRegister(select),
3760 InputFPRegisterAt(select, 1),
3761 InputFPRegisterAt(select, 0),
3762 csel_cond);
3763 } else {
3764 __ Csel(OutputRegister(select),
3765 InputOperandAt(select, 1),
3766 InputOperandAt(select, 0),
3767 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003768 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003769}
3770
David Srbecky0cf44932015-12-09 14:09:59 +00003771void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003772 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00003773}
3774
David Srbeckyd28f4a02016-03-14 17:14:24 +00003775void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3776 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003777}
3778
Vladimir Markodec78172020-06-19 15:31:23 +01003779void CodeGeneratorARM64::IncreaseFrame(size_t adjustment) {
3780 __ Claim(adjustment);
3781 GetAssembler()->cfi().AdjustCFAOffset(adjustment);
3782}
3783
3784void CodeGeneratorARM64::DecreaseFrame(size_t adjustment) {
3785 __ Drop(adjustment);
3786 GetAssembler()->cfi().AdjustCFAOffset(-adjustment);
3787}
3788
David Srbeckyc7098ff2016-02-09 14:30:11 +00003789void CodeGeneratorARM64::GenerateNop() {
3790 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003791}
3792
Alexandre Rames5319def2014-10-23 10:03:10 +01003793void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003794 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003795}
3796
3797void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003798 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003799}
3800
3801void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003802 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003803}
3804
3805void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003806 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003807}
3808
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003809// Temp is used for read barrier.
3810static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3811 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003812 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003813 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3814 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3815 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3816 return 1;
3817 }
3818 return 0;
3819}
3820
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003821// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003822// interface pointer, one for loading the current interface.
3823// The other checks have one temp for loading the object's class.
3824static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3825 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3826 return 3;
3827 }
3828 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003829}
3830
Alexandre Rames67555f72014-11-18 10:55:16 +00003831void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003832 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003833 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003834 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003835 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003836 case TypeCheckKind::kExactCheck:
3837 case TypeCheckKind::kAbstractClassCheck:
3838 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00003839 case TypeCheckKind::kArrayObjectCheck: {
3840 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
3841 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
3842 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003843 break;
Vladimir Marko87584542017-12-12 17:47:52 +00003844 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003845 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003846 case TypeCheckKind::kUnresolvedCheck:
3847 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003848 call_kind = LocationSummary::kCallOnSlowPath;
3849 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00003850 case TypeCheckKind::kBitstringCheck:
3851 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003852 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003853
Vladimir Markoca6fff82017-10-03 14:49:14 +01003854 LocationSummary* locations =
3855 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003856 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003857 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003858 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003859 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003860 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3861 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3862 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3863 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3864 } else {
3865 locations->SetInAt(1, Location::RequiresRegister());
3866 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003867 // The "out" register is used as a temporary, so it overlaps with the inputs.
3868 // Note that TypeCheckSlowPathARM64 uses this register too.
3869 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003870 // Add temps if necessary for read barriers.
3871 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003872}
3873
3874void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003875 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003876 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003877 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003878 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003879 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3880 ? Register()
3881 : InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003882 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003883 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003884 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3885 DCHECK_LE(num_temps, 1u);
3886 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003887 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3888 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3889 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3890 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003891
Scott Wakeling97c72b72016-06-24 16:19:36 +01003892 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003893 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003894
3895 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003896 // Avoid null check if we know `obj` is not null.
3897 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003898 __ Cbz(obj, &zero);
3899 }
3900
Roland Levillain44015862016-01-22 11:47:17 +00003901 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003902 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003903 ReadBarrierOption read_barrier_option =
3904 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003905 // /* HeapReference<Class> */ out = obj->klass_
3906 GenerateReferenceLoadTwoRegisters(instruction,
3907 out_loc,
3908 obj_loc,
3909 class_offset,
3910 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003911 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003912 __ Cmp(out, cls);
3913 __ Cset(out, eq);
3914 if (zero.IsLinked()) {
3915 __ B(&done);
3916 }
3917 break;
3918 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003919
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003920 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003921 ReadBarrierOption read_barrier_option =
3922 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003923 // /* HeapReference<Class> */ out = obj->klass_
3924 GenerateReferenceLoadTwoRegisters(instruction,
3925 out_loc,
3926 obj_loc,
3927 class_offset,
3928 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003929 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003930 // If the class is abstract, we eagerly fetch the super class of the
3931 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003932 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003933 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003934 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003935 GenerateReferenceLoadOneRegister(instruction,
3936 out_loc,
3937 super_offset,
3938 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003939 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003940 // If `out` is null, we use it for the result, and jump to `done`.
3941 __ Cbz(out, &done);
3942 __ Cmp(out, cls);
3943 __ B(ne, &loop);
3944 __ Mov(out, 1);
3945 if (zero.IsLinked()) {
3946 __ B(&done);
3947 }
3948 break;
3949 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003950
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003951 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003952 ReadBarrierOption read_barrier_option =
3953 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003954 // /* HeapReference<Class> */ out = obj->klass_
3955 GenerateReferenceLoadTwoRegisters(instruction,
3956 out_loc,
3957 obj_loc,
3958 class_offset,
3959 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003960 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003961 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003962 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003963 __ Bind(&loop);
3964 __ Cmp(out, cls);
3965 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003966 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003967 GenerateReferenceLoadOneRegister(instruction,
3968 out_loc,
3969 super_offset,
3970 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003971 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003972 __ Cbnz(out, &loop);
3973 // If `out` is null, we use it for the result, and jump to `done`.
3974 __ B(&done);
3975 __ Bind(&success);
3976 __ Mov(out, 1);
3977 if (zero.IsLinked()) {
3978 __ B(&done);
3979 }
3980 break;
3981 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003982
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003983 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003984 ReadBarrierOption read_barrier_option =
3985 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003986 // /* HeapReference<Class> */ out = obj->klass_
3987 GenerateReferenceLoadTwoRegisters(instruction,
3988 out_loc,
3989 obj_loc,
3990 class_offset,
3991 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003992 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003993 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003994 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003995 __ Cmp(out, cls);
3996 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003997 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003998 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003999 GenerateReferenceLoadOneRegister(instruction,
4000 out_loc,
4001 component_offset,
4002 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004003 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004004 // If `out` is null, we use it for the result, and jump to `done`.
4005 __ Cbz(out, &done);
4006 __ Ldrh(out, HeapOperand(out, primitive_offset));
4007 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4008 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004009 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004010 __ Mov(out, 1);
4011 __ B(&done);
4012 break;
4013 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004014
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004015 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004016 // No read barrier since the slow path will retry upon failure.
4017 // /* HeapReference<Class> */ out = obj->klass_
4018 GenerateReferenceLoadTwoRegisters(instruction,
4019 out_loc,
4020 obj_loc,
4021 class_offset,
4022 maybe_temp_loc,
4023 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004024 __ Cmp(out, cls);
4025 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004026 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08004027 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004028 codegen_->AddSlowPath(slow_path);
4029 __ B(ne, slow_path->GetEntryLabel());
4030 __ Mov(out, 1);
4031 if (zero.IsLinked()) {
4032 __ B(&done);
4033 }
4034 break;
4035 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004036
Calin Juravle98893e12015-10-02 21:05:03 +01004037 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004038 case TypeCheckKind::kInterfaceCheck: {
4039 // Note that we indeed only call on slow path, but we always go
4040 // into the slow path for the unresolved and interface check
4041 // cases.
4042 //
4043 // We cannot directly call the InstanceofNonTrivial runtime
4044 // entry point without resorting to a type checking slow path
4045 // here (i.e. by calling InvokeRuntime directly), as it would
4046 // require to assign fixed registers for the inputs of this
4047 // HInstanceOf instruction (following the runtime calling
4048 // convention), which might be cluttered by the potential first
4049 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004050 //
4051 // TODO: Introduce a new runtime entry point taking the object
4052 // to test (instead of its class) as argument, and let it deal
4053 // with the read barrier issues. This will let us refactor this
4054 // case of the `switch` code as it was previously (with a direct
4055 // call to the runtime not using a type checking slow path).
4056 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004057 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004058 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08004059 instruction, /* is_fatal= */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004060 codegen_->AddSlowPath(slow_path);
4061 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004062 if (zero.IsLinked()) {
4063 __ B(&done);
4064 }
4065 break;
4066 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004067
4068 case TypeCheckKind::kBitstringCheck: {
4069 // /* HeapReference<Class> */ temp = obj->klass_
4070 GenerateReferenceLoadTwoRegisters(instruction,
4071 out_loc,
4072 obj_loc,
4073 class_offset,
4074 maybe_temp_loc,
4075 kWithoutReadBarrier);
4076
4077 GenerateBitstringTypeCheckCompare(instruction, out);
4078 __ Cset(out, eq);
4079 if (zero.IsLinked()) {
4080 __ B(&done);
4081 }
4082 break;
4083 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004084 }
4085
4086 if (zero.IsLinked()) {
4087 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004088 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004089 }
4090
4091 if (done.IsLinked()) {
4092 __ Bind(&done);
4093 }
4094
4095 if (slow_path != nullptr) {
4096 __ Bind(slow_path->GetExitLabel());
4097 }
4098}
4099
4100void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004101 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00004102 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004103 LocationSummary* locations =
4104 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004105 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00004106 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
4107 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
4108 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
4109 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
4110 } else {
4111 locations->SetInAt(1, Location::RequiresRegister());
4112 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004113 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4114 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004115}
4116
4117void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004118 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004119 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004120 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004121 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00004122 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
4123 ? Register()
4124 : InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004125 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4126 DCHECK_GE(num_temps, 1u);
4127 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004128 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004129 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4130 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004131 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004132 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4133 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4134 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4135 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4136 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4137 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4138 const uint32_t object_array_data_offset =
4139 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004140
Vladimir Marko87584542017-12-12 17:47:52 +00004141 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004142 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004143 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4144 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004145 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004146
Scott Wakeling97c72b72016-06-24 16:19:36 +01004147 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004148 // Avoid null check if we know obj is not null.
4149 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004150 __ Cbz(obj, &done);
4151 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004152
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004153 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004154 case TypeCheckKind::kExactCheck:
4155 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004156 // /* HeapReference<Class> */ temp = obj->klass_
4157 GenerateReferenceLoadTwoRegisters(instruction,
4158 temp_loc,
4159 obj_loc,
4160 class_offset,
4161 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004162 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004163
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004164 __ Cmp(temp, cls);
4165 // Jump to slow path for throwing the exception or doing a
4166 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004167 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004168 break;
4169 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004170
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004171 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004172 // /* HeapReference<Class> */ temp = obj->klass_
4173 GenerateReferenceLoadTwoRegisters(instruction,
4174 temp_loc,
4175 obj_loc,
4176 class_offset,
4177 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004178 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004179
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004180 // If the class is abstract, we eagerly fetch the super class of the
4181 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004182 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004183 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004184 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004185 GenerateReferenceLoadOneRegister(instruction,
4186 temp_loc,
4187 super_offset,
4188 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004189 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004190
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004191 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4192 // exception.
4193 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4194 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004195 __ Cmp(temp, cls);
4196 __ B(ne, &loop);
4197 break;
4198 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004199
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004200 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004201 // /* HeapReference<Class> */ temp = obj->klass_
4202 GenerateReferenceLoadTwoRegisters(instruction,
4203 temp_loc,
4204 obj_loc,
4205 class_offset,
4206 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004207 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004208
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004209 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004210 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004211 __ Bind(&loop);
4212 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004213 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004214
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004215 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004216 GenerateReferenceLoadOneRegister(instruction,
4217 temp_loc,
4218 super_offset,
4219 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004220 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004221
4222 // If the class reference currently in `temp` is not null, jump
4223 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004224 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004225 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004226 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004227 break;
4228 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004229
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004230 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004231 // /* HeapReference<Class> */ temp = obj->klass_
4232 GenerateReferenceLoadTwoRegisters(instruction,
4233 temp_loc,
4234 obj_loc,
4235 class_offset,
4236 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004237 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004238
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004239 // Do an exact check.
4240 __ Cmp(temp, cls);
4241 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004242
4243 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004244 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004245 GenerateReferenceLoadOneRegister(instruction,
4246 temp_loc,
4247 component_offset,
4248 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004249 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004250
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004251 // If the component type is null, jump to the slow path to throw the exception.
4252 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4253 // Otherwise, the object is indeed an array. Further check that this component type is not a
4254 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004255 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4256 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004257 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004258 break;
4259 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004260
Calin Juravle98893e12015-10-02 21:05:03 +01004261 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004262 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004263 //
4264 // We cannot directly call the CheckCast runtime entry point
4265 // without resorting to a type checking slow path here (i.e. by
4266 // calling InvokeRuntime directly), as it would require to
4267 // assign fixed registers for the inputs of this HInstanceOf
4268 // instruction (following the runtime calling convention), which
4269 // might be cluttered by the potential first read barrier
4270 // emission at the beginning of this method.
4271 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004272 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004273 case TypeCheckKind::kInterfaceCheck: {
4274 // /* HeapReference<Class> */ temp = obj->klass_
4275 GenerateReferenceLoadTwoRegisters(instruction,
4276 temp_loc,
4277 obj_loc,
4278 class_offset,
4279 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004280 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004281
4282 // /* HeapReference<Class> */ temp = temp->iftable_
4283 GenerateReferenceLoadTwoRegisters(instruction,
4284 temp_loc,
4285 temp_loc,
4286 iftable_offset,
4287 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004288 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004289 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004290 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004291 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004292 vixl::aarch64::Label start_loop;
4293 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004294 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004295 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4296 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004297 // Go to next interface.
4298 __ Add(temp, temp, 2 * kHeapReferenceSize);
4299 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004300 // Compare the classes and continue the loop if they do not match.
4301 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4302 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004303 break;
4304 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004305
4306 case TypeCheckKind::kBitstringCheck: {
4307 // /* HeapReference<Class> */ temp = obj->klass_
4308 GenerateReferenceLoadTwoRegisters(instruction,
4309 temp_loc,
4310 obj_loc,
4311 class_offset,
4312 maybe_temp2_loc,
4313 kWithoutReadBarrier);
4314
4315 GenerateBitstringTypeCheckCompare(instruction, temp);
4316 __ B(ne, type_check_slow_path->GetEntryLabel());
4317 break;
4318 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004319 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004320 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004321
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004322 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004323}
4324
Alexandre Rames5319def2014-10-23 10:03:10 +01004325void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004326 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004327 locations->SetOut(Location::ConstantLocation(constant));
4328}
4329
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004330void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004331 // Will be generated at use site.
4332}
4333
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004334void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004335 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004336 locations->SetOut(Location::ConstantLocation(constant));
4337}
4338
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004339void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004340 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004341}
4342
Calin Juravle175dc732015-08-25 15:42:32 +01004343void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4344 // The trampoline uses the same calling convention as dex calling conventions,
4345 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4346 // the method_idx.
4347 HandleInvoke(invoke);
4348}
4349
4350void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4351 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004352 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004353}
4354
Alexandre Rames5319def2014-10-23 10:03:10 +01004355void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004356 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004357 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004358}
4359
Alexandre Rames67555f72014-11-18 10:55:16 +00004360void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4361 HandleInvoke(invoke);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004362 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
4363 // We cannot request ip1 as it's blocked by the register allocator.
4364 invoke->GetLocations()->SetInAt(invoke->GetNumberOfArguments() - 1, Location::Any());
4365 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004366}
4367
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004368void CodeGeneratorARM64::MaybeGenerateInlineCacheCheck(HInstruction* instruction,
4369 Register klass) {
4370 DCHECK_EQ(klass.GetCode(), 0u);
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004371 // We know the destination of an intrinsic, so no need to record inline
4372 // caches.
4373 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00004374 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004375 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004376 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
Nicolas Geoffray095dc462020-08-17 16:40:28 +01004377 ScopedProfilingInfoUse spiu(
4378 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
4379 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00004380 if (info != nullptr) {
4381 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
4382 uint64_t address = reinterpret_cast64<uint64_t>(cache);
4383 vixl::aarch64::Label done;
4384 __ Mov(x8, address);
4385 __ Ldr(x9, MemOperand(x8, InlineCache::ClassesOffset().Int32Value()));
4386 // Fast path for a monomorphic cache.
4387 __ Cmp(klass, x9);
4388 __ B(eq, &done);
4389 InvokeRuntime(kQuickUpdateInlineCache, instruction, instruction->GetDexPc());
4390 __ Bind(&done);
4391 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004392 }
4393}
4394
Alexandre Rames67555f72014-11-18 10:55:16 +00004395void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4396 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004397 LocationSummary* locations = invoke->GetLocations();
4398 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004399 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004400 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004401 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004402
Artem Serov914d7a82017-02-07 14:33:49 +00004403 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004404 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004405 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004406 {
4407 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4408 // /* HeapReference<Class> */ temp = temp->klass_
4409 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4410 codegen_->MaybeRecordImplicitNullCheck(invoke);
4411 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004412 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004413 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004414 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004415 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004416 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004417 }
Artem Serov914d7a82017-02-07 14:33:49 +00004418
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004419 // Instead of simply (possibly) unpoisoning `temp` here, we should
4420 // emit a read barrier for the previous class reference load.
4421 // However this is not required in practice, as this is an
4422 // intermediate/temporary reference and because the current
4423 // concurrent copying collector keeps the from-space memory
4424 // intact/accessible until the end of the marking phase (the
4425 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004426 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004427
4428 // If we're compiling baseline, update the inline cache.
4429 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
4430
4431 // The register ip1 is required to be used for the hidden argument in
4432 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
4433 MacroAssembler* masm = GetVIXLAssembler();
4434 UseScratchRegisterScope scratch_scope(masm);
4435 scratch_scope.Exclude(ip1);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004436 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
4437 Location interface_method = locations->InAt(invoke->GetNumberOfArguments() - 1);
4438 if (interface_method.IsStackSlot()) {
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00004439 __ Ldr(ip1, StackOperandFrom(interface_method));
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004440 } else {
4441 __ Mov(ip1, XRegisterFrom(interface_method));
4442 }
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00004443 // If the load kind is through a runtime call, we will pass the method we
4444 // fetch the IMT, which will either be a no-op if we don't hit the conflict
4445 // stub, or will make us always go through the trampoline when there is a
4446 // conflict.
4447 } else if (invoke->GetHiddenArgumentLoadKind() != MethodLoadKind::kRuntimeCall) {
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004448 codegen_->LoadMethod(
4449 invoke->GetHiddenArgumentLoadKind(), Location::RegisterLocation(ip1.GetCode()), invoke);
4450 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004451
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004452 __ Ldr(temp,
4453 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4454 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004455 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004456 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004457 __ Ldr(temp, MemOperand(temp, method_offset));
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00004458 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRuntimeCall) {
4459 // We pass the method from the IMT in case of a conflict. This will ensure
4460 // we go into the runtime to resolve the actual method.
4461 __ Mov(ip1, temp);
4462 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004463 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004464 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004465
4466 {
4467 // Ensure the pc position is recorded immediately after the `blr` instruction.
4468 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4469
4470 // lr();
4471 __ blr(lr);
4472 DCHECK(!codegen_->IsLeafMethod());
4473 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4474 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004475
Andreas Gampe3db70682018-12-26 15:12:03 -08004476 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004477}
4478
4479void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004480 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004481 if (intrinsic.TryDispatch(invoke)) {
4482 return;
4483 }
4484
Alexandre Rames67555f72014-11-18 10:55:16 +00004485 HandleInvoke(invoke);
4486}
4487
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004488void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004489 // Explicit clinit checks triggered by static invokes must have been pruned by
4490 // art::PrepareForRegisterAllocation.
4491 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004492
Vladimir Markoca6fff82017-10-03 14:49:14 +01004493 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004494 if (intrinsic.TryDispatch(invoke)) {
4495 return;
4496 }
4497
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004498 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
Vladimir Marko86c87522020-05-11 16:55:55 +01004499 CriticalNativeCallingConventionVisitorARM64 calling_convention_visitor(
4500 /*for_register_allocation=*/ true);
4501 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4502 } else {
4503 HandleInvoke(invoke);
4504 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004505}
4506
Andreas Gampe878d58c2015-01-15 23:24:00 -08004507static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4508 if (invoke->GetLocations()->Intrinsified()) {
4509 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4510 intrinsic.Dispatch(invoke);
4511 return true;
4512 }
4513 return false;
4514}
4515
Vladimir Markodc151b22015-10-15 18:02:30 +01004516HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4517 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004518 ArtMethod* method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004519 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004520 return desired_dispatch_info;
4521}
4522
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004523void CodeGeneratorARM64::LoadMethod(MethodLoadKind load_kind, Location temp, HInvoke* invoke) {
4524 switch (load_kind) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004525 case MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01004526 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01004527 // Add ADRP with its PC-relative method patch.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004528 vixl::aarch64::Label* adrp_label =
4529 NewBootImageMethodPatch(invoke->GetResolvedMethodReference());
Vladimir Marko65979462017-05-19 17:25:12 +01004530 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4531 // Add ADD with its PC-relative method patch.
4532 vixl::aarch64::Label* add_label =
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004533 NewBootImageMethodPatch(invoke->GetResolvedMethodReference(), adrp_label);
Vladimir Marko65979462017-05-19 17:25:12 +01004534 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4535 break;
4536 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004537 case MethodLoadKind::kBootImageRelRo: {
Vladimir Markob066d432018-01-03 13:14:37 +00004538 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004539 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00004540 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_offset);
4541 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4542 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
4543 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_offset, adrp_label);
4544 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
4545 EmitLdrOffsetPlaceholder(ldr_label, WRegisterFrom(temp), XRegisterFrom(temp));
4546 break;
4547 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004548 case MethodLoadKind::kBssEntry: {
Vladimir Markob066d432018-01-03 13:14:37 +00004549 // Add ADRP with its PC-relative .bss entry patch.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004550 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(invoke->GetMethodReference());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004551 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Markob066d432018-01-03 13:14:37 +00004552 // Add LDR with its PC-relative .bss entry patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004553 vixl::aarch64::Label* ldr_label =
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004554 NewMethodBssEntryPatch(invoke->GetMethodReference(), adrp_label);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01004555 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004556 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004557 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004558 }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004559 case MethodLoadKind::kJitDirectAddress: {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004560 // Load method address from literal pool.
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004561 __ Ldr(XRegisterFrom(temp),
4562 DeduplicateUint64Literal(reinterpret_cast<uint64_t>(invoke->GetResolvedMethod())));
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004563 break;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004564 }
4565 case MethodLoadKind::kRuntimeCall: {
4566 // Test situation, don't do anything.
4567 break;
4568 }
4569 default: {
4570 LOG(FATAL) << "Load kind should have already been handled " << load_kind;
4571 UNREACHABLE();
4572 }
4573 }
4574}
4575
4576void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4577 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
4578 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
4579 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4580 switch (invoke->GetMethodLoadKind()) {
4581 case MethodLoadKind::kStringInit: {
4582 uint32_t offset =
4583 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4584 // temp = thread->string_init_entrypoint
4585 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
4586 break;
4587 }
4588 case MethodLoadKind::kRecursive: {
4589 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
4590 break;
4591 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004592 case MethodLoadKind::kRuntimeCall: {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004593 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4594 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004595 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004596 case MethodLoadKind::kBootImageLinkTimePcRelative:
4597 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
4598 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
4599 // Do not materialize the method pointer, load directly the entrypoint.
4600 // Add ADRP with its PC-relative JNI entrypoint patch.
4601 vixl::aarch64::Label* adrp_label =
4602 NewBootImageJniEntrypointPatch(invoke->GetResolvedMethodReference());
4603 EmitAdrpPlaceholder(adrp_label, lr);
4604 // Add the LDR with its PC-relative method patch.
4605 vixl::aarch64::Label* add_label =
4606 NewBootImageJniEntrypointPatch(invoke->GetResolvedMethodReference(), adrp_label);
4607 EmitLdrOffsetPlaceholder(add_label, lr, lr);
4608 break;
4609 }
4610 FALLTHROUGH_INTENDED;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004611 default: {
4612 LoadMethod(invoke->GetMethodLoadKind(), temp, invoke);
4613 break;
4614 }
Vladimir Marko58155012015-08-19 12:49:41 +00004615 }
4616
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004617 auto call_lr = [&]() {
4618 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4619 ExactAssemblyScope eas(GetVIXLAssembler(),
4620 kInstructionSize,
4621 CodeBufferCheckScope::kExactSize);
4622 // lr()
4623 __ blr(lr);
4624 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko86c87522020-05-11 16:55:55 +01004625 };
Vladimir Marko58155012015-08-19 12:49:41 +00004626 switch (invoke->GetCodePtrLocation()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004627 case CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004628 {
4629 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4630 ExactAssemblyScope eas(GetVIXLAssembler(),
4631 kInstructionSize,
4632 CodeBufferCheckScope::kExactSize);
4633 __ bl(&frame_entry_label_);
4634 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4635 }
Vladimir Marko58155012015-08-19 12:49:41 +00004636 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004637 case CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01004638 size_t out_frame_size =
4639 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorARM64,
4640 kAapcs64StackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01004641 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004642 if (invoke->GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative) {
4643 call_lr();
4644 } else {
4645 // LR = callee_method->ptr_sized_fields_.data_; // EntryPointFromJni
4646 MemberOffset offset = ArtMethod::EntryPointFromJniOffset(kArm64PointerSize);
4647 __ Ldr(lr, MemOperand(XRegisterFrom(callee_method), offset.Int32Value()));
4648 // lr()
4649 call_lr();
4650 }
Vladimir Marko86c87522020-05-11 16:55:55 +01004651 // Zero-/sign-extend the result when needed due to native and managed ABI mismatch.
4652 switch (invoke->GetType()) {
4653 case DataType::Type::kBool:
4654 __ Ubfx(w0, w0, 0, 8);
4655 break;
4656 case DataType::Type::kInt8:
4657 __ Sbfx(w0, w0, 0, 8);
4658 break;
4659 case DataType::Type::kUint16:
4660 __ Ubfx(w0, w0, 0, 16);
4661 break;
4662 case DataType::Type::kInt16:
4663 __ Sbfx(w0, w0, 0, 16);
4664 break;
4665 case DataType::Type::kInt32:
4666 case DataType::Type::kInt64:
4667 case DataType::Type::kFloat32:
4668 case DataType::Type::kFloat64:
4669 case DataType::Type::kVoid:
4670 break;
4671 default:
4672 DCHECK(false) << invoke->GetType();
4673 break;
4674 }
4675 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01004676 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01004677 }
4678 break;
4679 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004680 case CodePtrLocation::kCallArtMethod: {
4681 // LR = callee_method->ptr_sized_fields_.entry_point_from_quick_compiled_code_;
4682 MemberOffset offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
4683 __ Ldr(lr, MemOperand(XRegisterFrom(callee_method), offset.Int32Value()));
4684 // lr()
4685 call_lr();
Vladimir Marko58155012015-08-19 12:49:41 +00004686 break;
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004687 }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004688 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004689
Andreas Gampe878d58c2015-01-15 23:24:00 -08004690 DCHECK(!IsLeafMethod());
4691}
4692
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004693void CodeGeneratorARM64::GenerateVirtualCall(
4694 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004695 // Use the calling convention instead of the location of the receiver, as
4696 // intrinsics may have put the receiver in a different register. In the intrinsics
4697 // slow path, the arguments have been moved to the right place, so here we are
4698 // guaranteed that the receiver is the first register of the calling convention.
4699 InvokeDexCallingConvention calling_convention;
4700 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004701 Register temp = XRegisterFrom(temp_in);
4702 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4703 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4704 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004705 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004706
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004707 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004708
4709 {
4710 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4711 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4712 // /* HeapReference<Class> */ temp = receiver->klass_
4713 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4714 MaybeRecordImplicitNullCheck(invoke);
4715 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004716 // Instead of simply (possibly) unpoisoning `temp` here, we should
4717 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004718 // intermediate/temporary reference and because the current
4719 // concurrent copying collector keeps the from-space memory
4720 // intact/accessible until the end of the marking phase (the
4721 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004722 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004723
4724 // If we're compiling baseline, update the inline cache.
4725 MaybeGenerateInlineCacheCheck(invoke, temp);
4726
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004727 // temp = temp->GetMethodAt(method_offset);
4728 __ Ldr(temp, MemOperand(temp, method_offset));
4729 // lr = temp->GetEntryPoint();
4730 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004731 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004732 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004733 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4734 // lr();
4735 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004736 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004737 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004738}
4739
Vladimir Marko9922f002020-06-08 15:05:15 +01004740void CodeGeneratorARM64::MoveFromReturnRegister(Location trg, DataType::Type type) {
4741 if (!trg.IsValid()) {
4742 DCHECK(type == DataType::Type::kVoid);
4743 return;
4744 }
4745
4746 DCHECK_NE(type, DataType::Type::kVoid);
4747
4748 if (DataType::IsIntegralType(type) || type == DataType::Type::kReference) {
4749 Register trg_reg = RegisterFrom(trg, type);
4750 Register res_reg = RegisterFrom(ARM64ReturnLocation(type), type);
4751 __ Mov(trg_reg, res_reg, kDiscardForSameWReg);
4752 } else {
4753 VRegister trg_reg = FPRegisterFrom(trg, type);
4754 VRegister res_reg = FPRegisterFrom(ARM64ReturnLocation(type), type);
4755 __ Fmov(trg_reg, res_reg);
4756 }
4757}
4758
Orion Hodsonac141392017-01-13 11:53:47 +00004759void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00004760 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
4761 if (intrinsic.TryDispatch(invoke)) {
4762 return;
4763 }
Orion Hodsonac141392017-01-13 11:53:47 +00004764 HandleInvoke(invoke);
4765}
4766
4767void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00004768 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4769 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
4770 return;
4771 }
Orion Hodsonac141392017-01-13 11:53:47 +00004772 codegen_->GenerateInvokePolymorphicCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004773 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004774}
4775
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004776void LocationsBuilderARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4777 HandleInvoke(invoke);
4778}
4779
4780void InstructionCodeGeneratorARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4781 codegen_->GenerateInvokeCustomCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004782 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004783}
4784
Vladimir Marko6fd16062018-06-26 11:02:04 +01004785vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageIntrinsicPatch(
4786 uint32_t intrinsic_data,
4787 vixl::aarch64::Label* adrp_label) {
4788 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004789 /* dex_file= */ nullptr, intrinsic_data, adrp_label, &boot_image_other_patches_);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004790}
4791
Vladimir Markob066d432018-01-03 13:14:37 +00004792vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageRelRoPatch(
4793 uint32_t boot_image_offset,
4794 vixl::aarch64::Label* adrp_label) {
4795 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004796 /* dex_file= */ nullptr, boot_image_offset, adrp_label, &boot_image_other_patches_);
Vladimir Markob066d432018-01-03 13:14:37 +00004797}
4798
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004799vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004800 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004801 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004802 return NewPcRelativePatch(
4803 target_method.dex_file, target_method.index, adrp_label, &boot_image_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004804}
4805
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004806vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4807 MethodReference target_method,
4808 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004809 return NewPcRelativePatch(
4810 target_method.dex_file, target_method.index, adrp_label, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004811}
4812
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004813vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageTypePatch(
Scott Wakeling97c72b72016-06-24 16:19:36 +01004814 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004815 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004816 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004817 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &boot_image_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004818}
4819
Vladimir Marko1998cd02017-01-13 13:02:58 +00004820vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
Vladimir Marko8f63f102020-09-28 12:10:28 +01004821 HLoadClass* load_class,
Vladimir Marko1998cd02017-01-13 13:02:58 +00004822 vixl::aarch64::Label* adrp_label) {
Vladimir Marko8f63f102020-09-28 12:10:28 +01004823 const DexFile& dex_file = load_class->GetDexFile();
4824 dex::TypeIndex type_index = load_class->GetTypeIndex();
4825 ArenaDeque<PcRelativePatchInfo>* patches = nullptr;
4826 switch (load_class->GetLoadKind()) {
4827 case HLoadClass::LoadKind::kBssEntry:
4828 patches = &type_bss_entry_patches_;
4829 break;
4830 case HLoadClass::LoadKind::kBssEntryPublic:
4831 patches = &public_type_bss_entry_patches_;
4832 break;
4833 case HLoadClass::LoadKind::kBssEntryPackage:
4834 patches = &package_type_bss_entry_patches_;
4835 break;
4836 default:
4837 LOG(FATAL) << "Unexpected load kind: " << load_class->GetLoadKind();
4838 UNREACHABLE();
4839 }
4840 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004841}
4842
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004843vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004844 const DexFile& dex_file,
4845 dex::StringIndex string_index,
4846 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004847 return NewPcRelativePatch(
4848 &dex_file, string_index.index_, adrp_label, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01004849}
4850
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004851vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4852 const DexFile& dex_file,
4853 dex::StringIndex string_index,
4854 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004855 return NewPcRelativePatch(&dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004856}
4857
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004858vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageJniEntrypointPatch(
4859 MethodReference target_method,
4860 vixl::aarch64::Label* adrp_label) {
4861 return NewPcRelativePatch(
4862 target_method.dex_file, target_method.index, adrp_label, &boot_image_jni_entrypoint_patches_);
4863}
4864
Vladimir Markof6675082019-05-17 12:05:28 +01004865void CodeGeneratorARM64::EmitEntrypointThunkCall(ThreadOffset64 entrypoint_offset) {
4866 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01004867 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markof6675082019-05-17 12:05:28 +01004868 call_entrypoint_patches_.emplace_back(/*dex_file*/ nullptr, entrypoint_offset.Uint32Value());
4869 vixl::aarch64::Label* bl_label = &call_entrypoint_patches_.back().label;
4870 __ bind(bl_label);
4871 __ bl(static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4872}
4873
Vladimir Marko966b46f2018-08-03 10:20:19 +00004874void CodeGeneratorARM64::EmitBakerReadBarrierCbnz(uint32_t custom_data) {
Vladimir Marko94796f82018-08-08 15:15:33 +01004875 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01004876 if (GetCompilerOptions().IsJitCompiler()) {
Vladimir Marko966b46f2018-08-03 10:20:19 +00004877 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
4878 vixl::aarch64::Label* slow_path_entry = &it->second.label;
4879 __ cbnz(mr, slow_path_entry);
4880 } else {
4881 baker_read_barrier_patches_.emplace_back(custom_data);
4882 vixl::aarch64::Label* cbnz_label = &baker_read_barrier_patches_.back().label;
4883 __ bind(cbnz_label);
4884 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4885 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004886}
4887
Scott Wakeling97c72b72016-06-24 16:19:36 +01004888vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004889 const DexFile* dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004890 uint32_t offset_or_index,
4891 vixl::aarch64::Label* adrp_label,
4892 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004893 // Add a patch entry and return the label.
4894 patches->emplace_back(dex_file, offset_or_index);
4895 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004896 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004897 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4898 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4899 return label;
4900}
4901
Scott Wakeling97c72b72016-06-24 16:19:36 +01004902vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4903 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004904 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004905}
4906
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004907vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004908 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004909 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004910 return jit_string_patches_.GetOrCreate(
4911 StringReference(&dex_file, string_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004912 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004913}
4914
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004915vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004916 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004917 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004918 return jit_class_patches_.GetOrCreate(
4919 TypeReference(&dex_file, type_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004920 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004921}
4922
Vladimir Markoaad75c62016-10-03 08:46:48 +00004923void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4924 vixl::aarch64::Register reg) {
4925 DCHECK(reg.IsX());
4926 SingleEmissionCheckScope guard(GetVIXLAssembler());
4927 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004928 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004929}
4930
4931void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4932 vixl::aarch64::Register out,
4933 vixl::aarch64::Register base) {
4934 DCHECK(out.IsX());
4935 DCHECK(base.IsX());
4936 SingleEmissionCheckScope guard(GetVIXLAssembler());
4937 __ Bind(fixup_label);
4938 __ add(out, base, Operand(/* offset placeholder */ 0));
4939}
4940
4941void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4942 vixl::aarch64::Register out,
4943 vixl::aarch64::Register base) {
4944 DCHECK(base.IsX());
4945 SingleEmissionCheckScope guard(GetVIXLAssembler());
4946 __ Bind(fixup_label);
4947 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4948}
4949
Vladimir Markoeebb8212018-06-05 14:57:24 +01004950void CodeGeneratorARM64::LoadBootImageAddress(vixl::aarch64::Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01004951 uint32_t boot_image_reference) {
4952 if (GetCompilerOptions().IsBootImage()) {
4953 // Add ADRP with its PC-relative type patch.
4954 vixl::aarch64::Label* adrp_label = NewBootImageIntrinsicPatch(boot_image_reference);
4955 EmitAdrpPlaceholder(adrp_label, reg.X());
4956 // Add ADD with its PC-relative type patch.
4957 vixl::aarch64::Label* add_label = NewBootImageIntrinsicPatch(boot_image_reference, adrp_label);
4958 EmitAddPlaceholder(add_label, reg.X(), reg.X());
Vladimir Markoa2da9b92018-10-10 14:21:55 +01004959 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01004960 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004961 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004962 EmitAdrpPlaceholder(adrp_label, reg.X());
4963 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004964 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_reference, adrp_label);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004965 EmitLdrOffsetPlaceholder(ldr_label, reg.W(), reg.X());
4966 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01004967 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01004968 gc::Heap* heap = Runtime::Current()->GetHeap();
4969 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004970 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01004971 __ Ldr(reg.W(), DeduplicateBootImageAddressLiteral(reinterpret_cast<uintptr_t>(address)));
4972 }
4973}
4974
Vladimir Markode91ca92020-10-27 13:41:40 +00004975void CodeGeneratorARM64::LoadIntrinsicDeclaringClass(vixl::aarch64::Register reg, HInvoke* invoke) {
4976 DCHECK_NE(invoke->GetIntrinsic(), Intrinsics::kNone);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004977 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01004978 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004979 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01004980 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
4981 // Add ADRP with its PC-relative type patch.
4982 vixl::aarch64::Label* adrp_label = NewBootImageTypePatch(*target_method.dex_file, type_idx);
Vladimir Markode91ca92020-10-27 13:41:40 +00004983 EmitAdrpPlaceholder(adrp_label, reg.X());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004984 // Add ADD with its PC-relative type patch.
4985 vixl::aarch64::Label* add_label =
4986 NewBootImageTypePatch(*target_method.dex_file, type_idx, adrp_label);
Vladimir Markode91ca92020-10-27 13:41:40 +00004987 EmitAddPlaceholder(add_label, reg.X(), reg.X());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004988 } else {
Vladimir Markode91ca92020-10-27 13:41:40 +00004989 uint32_t boot_image_offset = GetBootImageOffsetOfIntrinsicDeclaringClass(invoke);
4990 LoadBootImageAddress(reg, boot_image_offset);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004991 }
Vladimir Marko6fd16062018-06-26 11:02:04 +01004992}
4993
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004994template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004995inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4996 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004997 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004998 for (const PcRelativePatchInfo& info : infos) {
4999 linker_patches->push_back(Factory(info.label.GetLocation(),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005000 info.target_dex_file,
Vladimir Markoaad75c62016-10-03 08:46:48 +00005001 info.pc_insn_label->GetLocation(),
5002 info.offset_or_index));
5003 }
5004}
5005
Vladimir Marko6fd16062018-06-26 11:02:04 +01005006template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5007linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5008 const DexFile* target_dex_file,
5009 uint32_t pc_insn_offset,
5010 uint32_t boot_image_offset) {
5011 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5012 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005013}
5014
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005015void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005016 DCHECK(linker_patches->empty());
5017 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005018 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005019 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005020 boot_image_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005021 type_bss_entry_patches_.size() +
Vladimir Marko8f63f102020-09-28 12:10:28 +01005022 public_type_bss_entry_patches_.size() +
5023 package_type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005024 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005025 string_bss_entry_patches_.size() +
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005026 boot_image_jni_entrypoint_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005027 boot_image_other_patches_.size() +
Vladimir Markof6675082019-05-17 12:05:28 +01005028 call_entrypoint_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005029 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00005030 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005031 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005032 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005033 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005034 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005035 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005036 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005037 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01005038 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005039 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005040 DCHECK(boot_image_type_patches_.empty());
5041 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005042 }
5043 if (GetCompilerOptions().IsBootImage()) {
5044 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5045 boot_image_other_patches_, linker_patches);
5046 } else {
5047 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5048 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005049 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005050 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5051 method_bss_entry_patches_, linker_patches);
5052 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5053 type_bss_entry_patches_, linker_patches);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005054 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PublicTypeBssEntryPatch>(
5055 public_type_bss_entry_patches_, linker_patches);
5056 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PackageTypeBssEntryPatch>(
5057 package_type_bss_entry_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005058 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5059 string_bss_entry_patches_, linker_patches);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005060 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeJniEntrypointPatch>(
5061 boot_image_jni_entrypoint_patches_, linker_patches);
Vladimir Markof6675082019-05-17 12:05:28 +01005062 for (const PatchInfo<vixl::aarch64::Label>& info : call_entrypoint_patches_) {
5063 DCHECK(info.target_dex_file == nullptr);
5064 linker_patches->push_back(linker::LinkerPatch::CallEntrypointPatch(
5065 info.label.GetLocation(), info.offset_or_index));
5066 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005067 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005068 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
5069 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005070 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00005071 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005072}
5073
Vladimir Markoca1e0382018-04-11 09:58:41 +00005074bool CodeGeneratorARM64::NeedsThunkCode(const linker::LinkerPatch& patch) const {
Vladimir Markof6675082019-05-17 12:05:28 +01005075 return patch.GetType() == linker::LinkerPatch::Type::kCallEntrypoint ||
5076 patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
Vladimir Markoca1e0382018-04-11 09:58:41 +00005077 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
5078}
5079
5080void CodeGeneratorARM64::EmitThunkCode(const linker::LinkerPatch& patch,
5081 /*out*/ ArenaVector<uint8_t>* code,
5082 /*out*/ std::string* debug_name) {
5083 Arm64Assembler assembler(GetGraph()->GetAllocator());
5084 switch (patch.GetType()) {
5085 case linker::LinkerPatch::Type::kCallRelative: {
5086 // The thunk just uses the entry point in the ArtMethod. This works even for calls
5087 // to the generic JNI and interpreter trampolines.
5088 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
5089 kArm64PointerSize).Int32Value());
5090 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
5091 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
5092 *debug_name = "MethodCallThunk";
5093 }
5094 break;
5095 }
Vladimir Markof6675082019-05-17 12:05:28 +01005096 case linker::LinkerPatch::Type::kCallEntrypoint: {
5097 Offset offset(patch.EntrypointOffset());
5098 assembler.JumpTo(ManagedRegister(arm64::TR), offset, ManagedRegister(arm64::IP0));
5099 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
5100 *debug_name = "EntrypointCallThunk_" + std::to_string(offset.Uint32Value());
5101 }
5102 break;
5103 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00005104 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
5105 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
5106 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
5107 break;
5108 }
5109 default:
5110 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
5111 UNREACHABLE();
5112 }
5113
5114 // Ensure we emit the literal pool if any.
5115 assembler.FinalizeCode();
5116 code->resize(assembler.CodeSize());
5117 MemoryRegion code_region(code->data(), code->size());
5118 assembler.FinalizeInstructions(code_region);
5119}
5120
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005121vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
5122 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005123 value,
5124 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
5125}
5126
Scott Wakeling97c72b72016-06-24 16:19:36 +01005127vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005128 return uint64_literals_.GetOrCreate(
5129 value,
5130 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00005131}
5132
Andreas Gampe878d58c2015-01-15 23:24:00 -08005133void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005134 // Explicit clinit checks triggered by static invokes must have been pruned by
5135 // art::PrepareForRegisterAllocation.
5136 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01005137
Andreas Gampe878d58c2015-01-15 23:24:00 -08005138 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08005139 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08005140 return;
5141 }
5142
Vladimir Marko86c87522020-05-11 16:55:55 +01005143 LocationSummary* locations = invoke->GetLocations();
5144 codegen_->GenerateStaticOrDirectCall(
5145 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005146
Andreas Gampe3db70682018-12-26 15:12:03 -08005147 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005148}
5149
5150void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08005151 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08005152 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08005153 return;
5154 }
5155
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005156 {
5157 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
5158 // are no pools emitted.
5159 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
5160 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
5161 DCHECK(!codegen_->IsLeafMethod());
5162 }
5163
Andreas Gampe3db70682018-12-26 15:12:03 -08005164 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005165}
5166
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005167HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
5168 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005169 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005170 case HLoadClass::LoadKind::kInvalid:
5171 LOG(FATAL) << "UNREACHABLE";
5172 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005173 case HLoadClass::LoadKind::kReferrersClass:
5174 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005175 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005176 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005177 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8f63f102020-09-28 12:10:28 +01005178 case HLoadClass::LoadKind::kBssEntryPublic:
5179 case HLoadClass::LoadKind::kBssEntryPackage:
Vladimir Marko695348f2020-05-19 14:42:02 +01005180 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005181 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005182 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005183 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01005184 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005185 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005186 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005187 break;
5188 }
5189 return desired_class_load_kind;
5190}
5191
Alexandre Rames67555f72014-11-18 10:55:16 +00005192void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005193 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005194 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005195 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005196 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005197 cls,
5198 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005199 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00005200 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005201 return;
5202 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005203 DCHECK_EQ(cls->NeedsAccessCheck(),
5204 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
5205 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005206
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005207 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5208 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005209 ? LocationSummary::kCallOnSlowPath
5210 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005211 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005212 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005213 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005214 }
5215
Vladimir Marko41559982017-01-06 14:04:23 +00005216 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005217 locations->SetInAt(0, Location::RequiresRegister());
5218 }
5219 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005220 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
5221 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5222 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005223 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005224 } else {
5225 // For non-Baker read barrier we have a temp-clobbering call.
5226 }
5227 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005228}
5229
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005230// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5231// move.
5232void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005233 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005234 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005235 codegen_->GenerateLoadClassRuntimeCall(cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08005236 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01005237 return;
5238 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005239 DCHECK_EQ(cls->NeedsAccessCheck(),
5240 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
5241 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Calin Juravle580b6092015-10-06 17:35:58 +01005242
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005243 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01005244 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00005245
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005246 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5247 ? kWithoutReadBarrier
5248 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005249 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005250 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005251 case HLoadClass::LoadKind::kReferrersClass: {
5252 DCHECK(!cls->CanCallRuntime());
5253 DCHECK(!cls->MustGenerateClinitCheck());
5254 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5255 Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00005256 codegen_->GenerateGcRootFieldLoad(cls,
5257 out_loc,
5258 current_method,
5259 ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005260 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005261 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005262 break;
5263 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005264 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005265 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5266 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005267 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005268 // Add ADRP with its PC-relative type patch.
5269 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08005270 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005271 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005272 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005273 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005274 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005275 codegen_->NewBootImageTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005276 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005277 break;
5278 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005279 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005280 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markode91ca92020-10-27 13:41:40 +00005281 uint32_t boot_image_offset = CodeGenerator::GetBootImageOffset(cls);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005282 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5283 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005284 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005285 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005286 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005287 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005288 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005289 break;
5290 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005291 case HLoadClass::LoadKind::kBssEntry:
5292 case HLoadClass::LoadKind::kBssEntryPublic:
5293 case HLoadClass::LoadKind::kBssEntryPackage: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005294 // Add ADRP with its PC-relative Class .bss entry patch.
Vladimir Markof3c52b42017-11-17 17:32:12 +00005295 vixl::aarch64::Register temp = XRegisterFrom(out_loc);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005296 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(cls);
Vladimir Markof3c52b42017-11-17 17:32:12 +00005297 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005298 // Add LDR with its PC-relative Class .bss entry patch.
Vladimir Marko8f63f102020-09-28 12:10:28 +01005299 vixl::aarch64::Label* ldr_label = codegen_->NewBssEntryTypePatch(cls, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005300 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005301 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005302 codegen_->GenerateGcRootFieldLoad(cls,
5303 out_loc,
5304 temp,
5305 /* offset placeholder */ 0u,
5306 ldr_label,
5307 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005308 generate_null_check = true;
5309 break;
5310 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005311 case HLoadClass::LoadKind::kJitBootImageAddress: {
5312 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5313 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
5314 DCHECK_NE(address, 0u);
5315 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5316 break;
5317 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005318 case HLoadClass::LoadKind::kJitTableAddress: {
5319 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5320 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005321 cls->GetClass()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005322 codegen_->GenerateGcRootFieldLoad(cls,
5323 out_loc,
5324 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005325 /* offset= */ 0,
5326 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005327 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005328 break;
5329 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005330 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005331 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005332 LOG(FATAL) << "UNREACHABLE";
5333 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005334 }
5335
Vladimir Markoea4c1262017-02-06 19:59:33 +00005336 bool do_clinit = cls->MustGenerateClinitCheck();
5337 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005338 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01005339 SlowPathCodeARM64* slow_path =
5340 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005341 codegen_->AddSlowPath(slow_path);
5342 if (generate_null_check) {
5343 __ Cbz(out, slow_path->GetEntryLabel());
5344 }
5345 if (cls->MustGenerateClinitCheck()) {
5346 GenerateClassInitializationCheck(slow_path, out);
5347 } else {
5348 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005349 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005350 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005351 }
5352}
5353
Orion Hodsondbaa5c72018-05-10 08:22:46 +01005354void LocationsBuilderARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5355 InvokeRuntimeCallingConvention calling_convention;
5356 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5357 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
5358}
5359
5360void InstructionCodeGeneratorARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5361 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
5362}
5363
Orion Hodson18259d72018-04-12 11:18:23 +01005364void LocationsBuilderARM64::VisitLoadMethodType(HLoadMethodType* load) {
5365 InvokeRuntimeCallingConvention calling_convention;
5366 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5367 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
5368}
5369
5370void InstructionCodeGeneratorARM64::VisitLoadMethodType(HLoadMethodType* load) {
5371 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
5372}
5373
David Brazdilcb1c0552015-08-04 16:22:25 +01005374static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005375 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005376}
5377
Alexandre Rames67555f72014-11-18 10:55:16 +00005378void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5379 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005380 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00005381 locations->SetOut(Location::RequiresRegister());
5382}
5383
5384void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005385 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5386}
5387
5388void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005389 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005390}
5391
5392void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5393 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005394}
5395
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005396HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5397 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005398 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005399 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005400 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005401 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01005402 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005403 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005404 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005405 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01005406 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005407 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005408 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005409 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005410 }
5411 return desired_string_load_kind;
5412}
5413
Alexandre Rames67555f72014-11-18 10:55:16 +00005414void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005415 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005416 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005417 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005418 InvokeRuntimeCallingConvention calling_convention;
5419 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5420 } else {
5421 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005422 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5423 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005424 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005425 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005426 } else {
5427 // For non-Baker read barrier we have a temp-clobbering call.
5428 }
5429 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005430 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005431}
5432
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005433// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5434// move.
5435void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005436 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005437 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005438
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005439 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005440 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005441 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5442 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005443 // Add ADRP with its PC-relative String patch.
5444 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005445 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005446 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005447 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005448 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005449 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005450 codegen_->NewBootImageStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005451 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005452 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005453 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005454 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005455 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005456 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markode91ca92020-10-27 13:41:40 +00005457 uint32_t boot_image_offset = CodeGenerator::GetBootImageOffset(load);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005458 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005459 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005460 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005461 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005462 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005463 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
5464 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005465 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005466 case HLoadString::LoadKind::kBssEntry: {
5467 // Add ADRP with its PC-relative String .bss entry patch.
5468 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005469 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00005470 Register temp = XRegisterFrom(out_loc);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005471 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005472 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005473 // Add LDR with its PC-relative String .bss entry patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005474 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005475 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005476 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005477 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005478 codegen_->GenerateGcRootFieldLoad(load,
5479 out_loc,
5480 temp,
5481 /* offset placeholder */ 0u,
5482 ldr_label,
5483 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005484 SlowPathCodeARM64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005485 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005486 codegen_->AddSlowPath(slow_path);
5487 __ Cbz(out.X(), slow_path->GetEntryLabel());
5488 __ Bind(slow_path->GetExitLabel());
Andreas Gampe3db70682018-12-26 15:12:03 -08005489 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005490 return;
5491 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005492 case HLoadString::LoadKind::kJitBootImageAddress: {
5493 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
5494 DCHECK_NE(address, 0u);
5495 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5496 return;
5497 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005498 case HLoadString::LoadKind::kJitTableAddress: {
5499 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005500 load->GetStringIndex(),
5501 load->GetString()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005502 codegen_->GenerateGcRootFieldLoad(load,
5503 out_loc,
5504 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005505 /* offset= */ 0,
5506 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005507 kCompilerReadBarrierOption);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005508 return;
5509 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005510 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005511 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005512 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005513
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005514 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005515 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005516 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005517 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005518 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5519 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005520 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005521}
5522
Alexandre Rames5319def2014-10-23 10:03:10 +01005523void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005524 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01005525 locations->SetOut(Location::ConstantLocation(constant));
5526}
5527
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005528void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005529 // Will be generated at use site.
5530}
5531
Alexandre Rames67555f72014-11-18 10:55:16 +00005532void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005533 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5534 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005535 InvokeRuntimeCallingConvention calling_convention;
5536 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5537}
5538
5539void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005540 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005541 instruction,
5542 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005543 if (instruction->IsEnter()) {
5544 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5545 } else {
5546 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5547 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005548 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005549}
5550
Alexandre Rames42d641b2014-10-27 14:00:51 +00005551void LocationsBuilderARM64::VisitMul(HMul* mul) {
5552 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005553 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005554 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005555 case DataType::Type::kInt32:
5556 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005557 locations->SetInAt(0, Location::RequiresRegister());
5558 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005559 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005560 break;
5561
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005562 case DataType::Type::kFloat32:
5563 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005564 locations->SetInAt(0, Location::RequiresFpuRegister());
5565 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005566 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005567 break;
5568
5569 default:
5570 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5571 }
5572}
5573
5574void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5575 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005576 case DataType::Type::kInt32:
5577 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005578 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5579 break;
5580
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005581 case DataType::Type::kFloat32:
5582 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005583 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005584 break;
5585
5586 default:
5587 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5588 }
5589}
5590
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005591void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5592 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005593 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005594 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005595 case DataType::Type::kInt32:
5596 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005597 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005598 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005599 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005600
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005601 case DataType::Type::kFloat32:
5602 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005603 locations->SetInAt(0, Location::RequiresFpuRegister());
5604 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005605 break;
5606
5607 default:
5608 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5609 }
5610}
5611
5612void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5613 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005614 case DataType::Type::kInt32:
5615 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005616 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5617 break;
5618
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005619 case DataType::Type::kFloat32:
5620 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005621 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005622 break;
5623
5624 default:
5625 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5626 }
5627}
5628
5629void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005630 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5631 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005632 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005633 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005634 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5635 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005636}
5637
5638void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005639 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5640 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005641 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005642 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005643 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005644}
5645
Alexandre Rames5319def2014-10-23 10:03:10 +01005646void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005647 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5648 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005649 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07005650 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005651 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005652}
5653
5654void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005655 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5656 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005657 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005658}
5659
5660void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005661 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005662 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005663 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005664}
5665
5666void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005667 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005668 case DataType::Type::kInt32:
5669 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005670 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005671 break;
5672
5673 default:
5674 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5675 }
5676}
5677
David Brazdil66d126e2015-04-03 16:02:44 +01005678void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005679 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005680 locations->SetInAt(0, Location::RequiresRegister());
5681 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5682}
5683
5684void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005685 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005686}
5687
Alexandre Rames5319def2014-10-23 10:03:10 +01005688void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005689 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5690 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005691}
5692
Calin Juravle2ae48182016-03-16 14:05:09 +00005693void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5694 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005695 return;
5696 }
Artem Serov914d7a82017-02-07 14:33:49 +00005697 {
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01005698 // Ensure that between load and RecordPcInfo there are no pools emitted.
Artem Serov914d7a82017-02-07 14:33:49 +00005699 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5700 Location obj = instruction->GetLocations()->InAt(0);
5701 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5702 RecordPcInfo(instruction, instruction->GetDexPc());
5703 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005704}
5705
Calin Juravle2ae48182016-03-16 14:05:09 +00005706void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005707 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005708 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005709
5710 LocationSummary* locations = instruction->GetLocations();
5711 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005712
5713 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005714}
5715
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005716void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005717 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005718}
5719
Alexandre Rames67555f72014-11-18 10:55:16 +00005720void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5721 HandleBinaryOp(instruction);
5722}
5723
5724void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5725 HandleBinaryOp(instruction);
5726}
5727
Alexandre Rames3e69f162014-12-10 10:36:50 +00005728void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5729 LOG(FATAL) << "Unreachable";
5730}
5731
5732void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005733 if (instruction->GetNext()->IsSuspendCheck() &&
5734 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5735 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5736 // The back edge will generate the suspend check.
5737 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5738 }
5739
Alexandre Rames3e69f162014-12-10 10:36:50 +00005740 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5741}
5742
Alexandre Rames5319def2014-10-23 10:03:10 +01005743void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005744 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005745 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5746 if (location.IsStackSlot()) {
5747 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5748 } else if (location.IsDoubleStackSlot()) {
5749 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5750 }
5751 locations->SetOut(location);
5752}
5753
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005754void InstructionCodeGeneratorARM64::VisitParameterValue(
5755 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005756 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005757}
5758
5759void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5760 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005761 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005762 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005763}
5764
5765void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5766 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5767 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005768}
5769
5770void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005771 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005772 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005773 locations->SetInAt(i, Location::Any());
5774 }
5775 locations->SetOut(Location::Any());
5776}
5777
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005778void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005779 LOG(FATAL) << "Unreachable";
5780}
5781
Serban Constantinescu02164b32014-11-13 14:05:07 +00005782void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005783 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005784 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005785 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005786 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005787 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005788
5789 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005790 case DataType::Type::kInt32:
5791 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005792 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005793 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005794 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5795 break;
5796
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005797 case DataType::Type::kFloat32:
5798 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005799 InvokeRuntimeCallingConvention calling_convention;
5800 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5801 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5802 locations->SetOut(calling_convention.GetReturnLocation(type));
5803
5804 break;
5805 }
5806
Serban Constantinescu02164b32014-11-13 14:05:07 +00005807 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005808 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005809 }
5810}
5811
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005812void InstructionCodeGeneratorARM64::GenerateIntRemForPower2Denom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005813 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005814 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
5815 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
5816
5817 Register out = OutputRegister(instruction);
5818 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005819
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01005820 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01005821 // No need to adjust the result for non-negative dividends or the INT32_MIN/INT64_MIN dividends.
5822 // NOTE: The generated code for HRem correctly works for the INT32_MIN/INT64_MIN dividends.
5823 // INT*_MIN % imm must be 0 for any imm of power 2. 'and' works only with bits
5824 // 0..30 (Int32 case)/0..62 (Int64 case) of a dividend. For INT32_MIN/INT64_MIN they are zeros.
5825 // So 'and' always produces zero.
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005826 __ And(out, dividend, abs_imm - 1);
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01005827 } else {
5828 if (abs_imm == 2) {
5829 __ Cmp(dividend, 0);
5830 __ And(out, dividend, 1);
5831 __ Csneg(out, out, out, ge);
5832 } else {
5833 UseScratchRegisterScope temps(GetVIXLAssembler());
5834 Register temp = temps.AcquireSameSizeAs(out);
5835
5836 __ Negs(temp, dividend);
5837 __ And(out, dividend, abs_imm - 1);
5838 __ And(temp, temp, abs_imm - 1);
5839 __ Csneg(out, out, temp, mi);
5840 }
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005841 }
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005842}
5843
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005844void InstructionCodeGeneratorARM64::GenerateIntRemForConstDenom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005845 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005846
5847 if (imm == 0) {
5848 // Do not generate anything.
5849 // DivZeroCheck would prevent any code to be executed.
5850 return;
5851 }
5852
Evgeny Astigeevichf58dc652018-06-25 17:54:07 +01005853 if (IsPowerOfTwo(AbsOrMin(imm))) {
5854 // Cases imm == -1 or imm == 1 are handled in constant folding by
5855 // InstructionWithAbsorbingInputSimplifier.
5856 // If the cases have survided till code generation they are handled in
5857 // GenerateIntRemForPower2Denom becauses -1 and 1 are the power of 2 (2^0).
5858 // The correct code is generated for them, just more instructions.
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005859 GenerateIntRemForPower2Denom(instruction);
5860 } else {
5861 DCHECK(imm < -2 || imm > 2) << imm;
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01005862 GenerateDivRemWithAnyConstant(instruction, imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005863 }
5864}
5865
5866void InstructionCodeGeneratorARM64::GenerateIntRem(HRem* instruction) {
5867 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
5868 << instruction->GetResultType();
5869
5870 if (instruction->GetLocations()->InAt(1).IsConstant()) {
5871 GenerateIntRemForConstDenom(instruction);
5872 } else {
5873 Register out = OutputRegister(instruction);
5874 Register dividend = InputRegisterAt(instruction, 0);
5875 Register divisor = InputRegisterAt(instruction, 1);
5876 UseScratchRegisterScope temps(GetVIXLAssembler());
5877 Register temp = temps.AcquireSameSizeAs(out);
5878 __ Sdiv(temp, dividend, divisor);
5879 __ Msub(out, temp, divisor, dividend);
5880 }
5881}
5882
Serban Constantinescu02164b32014-11-13 14:05:07 +00005883void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005884 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005885
Serban Constantinescu02164b32014-11-13 14:05:07 +00005886 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005887 case DataType::Type::kInt32:
5888 case DataType::Type::kInt64: {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005889 GenerateIntRem(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005890 break;
5891 }
5892
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005893 case DataType::Type::kFloat32:
5894 case DataType::Type::kFloat64: {
5895 QuickEntrypointEnum entrypoint =
5896 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005897 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005898 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00005899 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5900 } else {
5901 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5902 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005903 break;
5904 }
5905
Serban Constantinescu02164b32014-11-13 14:05:07 +00005906 default:
5907 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005908 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005909 }
5910}
5911
Aart Bik1f8d51b2018-02-15 10:42:37 -08005912void LocationsBuilderARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005913 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005914}
5915
Aart Bik1f8d51b2018-02-15 10:42:37 -08005916void InstructionCodeGeneratorARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005917 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005918}
5919
5920void LocationsBuilderARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005921 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005922}
5923
5924void InstructionCodeGeneratorARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005925 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005926}
5927
Aart Bik3dad3412018-02-28 12:01:46 -08005928void LocationsBuilderARM64::VisitAbs(HAbs* abs) {
5929 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
5930 switch (abs->GetResultType()) {
5931 case DataType::Type::kInt32:
5932 case DataType::Type::kInt64:
5933 locations->SetInAt(0, Location::RequiresRegister());
5934 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5935 break;
5936 case DataType::Type::kFloat32:
5937 case DataType::Type::kFloat64:
5938 locations->SetInAt(0, Location::RequiresFpuRegister());
5939 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5940 break;
5941 default:
5942 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5943 }
5944}
5945
5946void InstructionCodeGeneratorARM64::VisitAbs(HAbs* abs) {
5947 switch (abs->GetResultType()) {
5948 case DataType::Type::kInt32:
5949 case DataType::Type::kInt64: {
5950 Register in_reg = InputRegisterAt(abs, 0);
5951 Register out_reg = OutputRegister(abs);
5952 __ Cmp(in_reg, Operand(0));
5953 __ Cneg(out_reg, in_reg, lt);
5954 break;
5955 }
5956 case DataType::Type::kFloat32:
5957 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01005958 VRegister in_reg = InputFPRegisterAt(abs, 0);
5959 VRegister out_reg = OutputFPRegister(abs);
Aart Bik3dad3412018-02-28 12:01:46 -08005960 __ Fabs(out_reg, in_reg);
5961 break;
5962 }
5963 default:
5964 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5965 }
5966}
5967
Igor Murashkind01745e2017-04-05 16:40:31 -07005968void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5969 constructor_fence->SetLocations(nullptr);
5970}
5971
5972void InstructionCodeGeneratorARM64::VisitConstructorFence(
5973 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5974 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5975}
5976
Calin Juravle27df7582015-04-17 19:12:31 +01005977void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5978 memory_barrier->SetLocations(nullptr);
5979}
5980
5981void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005982 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005983}
5984
Alexandre Rames5319def2014-10-23 10:03:10 +01005985void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005986 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005987 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005988 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005989}
5990
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00005991void InstructionCodeGeneratorARM64::VisitReturn(HReturn* ret) {
5992 if (GetGraph()->IsCompilingOsr()) {
5993 // To simplify callers of an OSR method, we put the return value in both
5994 // floating point and core register.
5995 switch (ret->InputAt(0)->GetType()) {
5996 case DataType::Type::kFloat32:
5997 __ Fmov(w0, s0);
5998 break;
5999 case DataType::Type::kFloat64:
6000 __ Fmov(x0, d0);
6001 break;
6002 default:
6003 break;
6004 }
6005 }
Alexandre Rames5319def2014-10-23 10:03:10 +01006006 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01006007}
6008
6009void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
6010 instruction->SetLocations(nullptr);
6011}
6012
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006013void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01006014 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01006015}
6016
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006017void LocationsBuilderARM64::VisitRor(HRor* ror) {
6018 HandleBinaryOp(ror);
6019}
6020
6021void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
6022 HandleBinaryOp(ror);
6023}
6024
Serban Constantinescu02164b32014-11-13 14:05:07 +00006025void LocationsBuilderARM64::VisitShl(HShl* shl) {
6026 HandleShift(shl);
6027}
6028
6029void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
6030 HandleShift(shl);
6031}
6032
6033void LocationsBuilderARM64::VisitShr(HShr* shr) {
6034 HandleShift(shr);
6035}
6036
6037void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
6038 HandleShift(shr);
6039}
6040
Alexandre Rames5319def2014-10-23 10:03:10 +01006041void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006042 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006043}
6044
6045void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006046 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006047}
6048
Alexandre Rames67555f72014-11-18 10:55:16 +00006049void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006050 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00006051}
6052
6053void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01006054 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00006055}
6056
6057void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01006058 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006059}
6060
Alexandre Rames67555f72014-11-18 10:55:16 +00006061void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006062 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01006063}
6064
Vladimir Marko552a1342017-10-31 10:56:47 +00006065void LocationsBuilderARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6066 codegen_->CreateStringBuilderAppendLocations(instruction, LocationFrom(x0));
6067}
6068
6069void InstructionCodeGeneratorARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6070 __ Mov(w0, instruction->GetFormat()->GetValue());
6071 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
6072}
6073
Calin Juravlee460d1d2015-09-29 04:52:17 +01006074void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
6075 HUnresolvedInstanceFieldGet* instruction) {
6076 FieldAccessCallingConventionARM64 calling_convention;
6077 codegen_->CreateUnresolvedFieldLocationSummary(
6078 instruction, instruction->GetFieldType(), calling_convention);
6079}
6080
6081void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
6082 HUnresolvedInstanceFieldGet* instruction) {
6083 FieldAccessCallingConventionARM64 calling_convention;
6084 codegen_->GenerateUnresolvedFieldAccess(instruction,
6085 instruction->GetFieldType(),
6086 instruction->GetFieldIndex(),
6087 instruction->GetDexPc(),
6088 calling_convention);
6089}
6090
6091void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
6092 HUnresolvedInstanceFieldSet* instruction) {
6093 FieldAccessCallingConventionARM64 calling_convention;
6094 codegen_->CreateUnresolvedFieldLocationSummary(
6095 instruction, instruction->GetFieldType(), calling_convention);
6096}
6097
6098void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
6099 HUnresolvedInstanceFieldSet* instruction) {
6100 FieldAccessCallingConventionARM64 calling_convention;
6101 codegen_->GenerateUnresolvedFieldAccess(instruction,
6102 instruction->GetFieldType(),
6103 instruction->GetFieldIndex(),
6104 instruction->GetDexPc(),
6105 calling_convention);
6106}
6107
6108void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
6109 HUnresolvedStaticFieldGet* instruction) {
6110 FieldAccessCallingConventionARM64 calling_convention;
6111 codegen_->CreateUnresolvedFieldLocationSummary(
6112 instruction, instruction->GetFieldType(), calling_convention);
6113}
6114
6115void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
6116 HUnresolvedStaticFieldGet* instruction) {
6117 FieldAccessCallingConventionARM64 calling_convention;
6118 codegen_->GenerateUnresolvedFieldAccess(instruction,
6119 instruction->GetFieldType(),
6120 instruction->GetFieldIndex(),
6121 instruction->GetDexPc(),
6122 calling_convention);
6123}
6124
6125void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
6126 HUnresolvedStaticFieldSet* instruction) {
6127 FieldAccessCallingConventionARM64 calling_convention;
6128 codegen_->CreateUnresolvedFieldLocationSummary(
6129 instruction, instruction->GetFieldType(), calling_convention);
6130}
6131
6132void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
6133 HUnresolvedStaticFieldSet* instruction) {
6134 FieldAccessCallingConventionARM64 calling_convention;
6135 codegen_->GenerateUnresolvedFieldAccess(instruction,
6136 instruction->GetFieldType(),
6137 instruction->GetFieldIndex(),
6138 instruction->GetDexPc(),
6139 calling_convention);
6140}
6141
Alexandre Rames5319def2014-10-23 10:03:10 +01006142void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006143 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6144 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01006145 // In suspend check slow path, usually there are no caller-save registers at all.
6146 // If SIMD instructions are present, however, we force spilling all live SIMD
6147 // registers in full width (since the runtime only saves/restores lower part).
6148 locations->SetCustomSlowPathCallerSaves(
6149 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01006150}
6151
6152void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006153 HBasicBlock* block = instruction->GetBlock();
6154 if (block->GetLoopInformation() != nullptr) {
6155 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6156 // The back edge will generate the suspend check.
6157 return;
6158 }
6159 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6160 // The goto will generate the suspend check.
6161 return;
6162 }
6163 GenerateSuspendCheck(instruction, nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08006164 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01006165}
6166
Alexandre Rames67555f72014-11-18 10:55:16 +00006167void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006168 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6169 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00006170 InvokeRuntimeCallingConvention calling_convention;
6171 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
6172}
6173
6174void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00006175 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08006176 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00006177}
6178
6179void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
6180 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006181 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006182 DataType::Type input_type = conversion->GetInputType();
6183 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006184 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6185 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006186 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
6187 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006188 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6189 }
6190
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006191 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006192 locations->SetInAt(0, Location::RequiresFpuRegister());
6193 } else {
6194 locations->SetInAt(0, Location::RequiresRegister());
6195 }
6196
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006197 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006198 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6199 } else {
6200 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6201 }
6202}
6203
6204void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006205 DataType::Type result_type = conversion->GetResultType();
6206 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00006207
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006208 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6209 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006210
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006211 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
6212 int result_size = DataType::Size(result_type);
6213 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00006214 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006215 Register output = OutputRegister(conversion);
6216 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006217 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01006218 // 'int' values are used directly as W registers, discarding the top
6219 // bits, so we don't need to sign-extend and can just perform a move.
6220 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
6221 // top 32 bits of the target register. We theoretically could leave those
6222 // bits unchanged, but we would have to make sure that no code uses a
6223 // 32bit input value as a 64bit value assuming that the top 32 bits are
6224 // zero.
6225 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006226 } else if (DataType::IsUnsignedType(result_type) ||
6227 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
6228 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006229 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00006230 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006231 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006232 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006233 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006234 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6235 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006236 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006237 } else if (DataType::IsFloatingPointType(result_type) &&
6238 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006239 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
6240 } else {
6241 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6242 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006243 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00006244}
Alexandre Rames67555f72014-11-18 10:55:16 +00006245
Serban Constantinescu02164b32014-11-13 14:05:07 +00006246void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
6247 HandleShift(ushr);
6248}
6249
6250void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
6251 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00006252}
6253
6254void LocationsBuilderARM64::VisitXor(HXor* instruction) {
6255 HandleBinaryOp(instruction);
6256}
6257
6258void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
6259 HandleBinaryOp(instruction);
6260}
6261
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006262void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006263 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006264 LOG(FATAL) << "Unreachable";
6265}
6266
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006267void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006268 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006269 LOG(FATAL) << "Unreachable";
6270}
6271
Mark Mendellfe57faa2015-09-18 09:26:15 -04006272// Simple implementation of packed switch - generate cascaded compare/jumps.
6273void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6274 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006275 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006276 locations->SetInAt(0, Location::RequiresRegister());
6277}
6278
6279void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6280 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08006281 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006282 Register value_reg = InputRegisterAt(switch_instr, 0);
6283 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6284
Zheng Xu3927c8b2015-11-18 17:46:25 +08006285 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01006286 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08006287 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
6288 // make sure we don't emit it if the target may run out of range.
6289 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
6290 // ranges and emit the tables only as required.
6291 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04006292
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006293 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08006294 // Current instruction id is an upper bound of the number of HIRs in the graph.
6295 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
6296 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006297 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6298 Register temp = temps.AcquireW();
6299 __ Subs(temp, value_reg, Operand(lower_bound));
6300
Zheng Xu3927c8b2015-11-18 17:46:25 +08006301 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006302 // Jump to successors[0] if value == lower_bound.
6303 __ B(eq, codegen_->GetLabelOf(successors[0]));
6304 int32_t last_index = 0;
6305 for (; num_entries - last_index > 2; last_index += 2) {
6306 __ Subs(temp, temp, Operand(2));
6307 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6308 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
6309 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6310 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
6311 }
6312 if (num_entries - last_index == 2) {
6313 // The last missing case_value.
6314 __ Cmp(temp, Operand(1));
6315 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08006316 }
6317
6318 // And the default for any other value.
6319 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6320 __ B(codegen_->GetLabelOf(default_block));
6321 }
6322 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01006323 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08006324
6325 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6326
6327 // Below instructions should use at most one blocked register. Since there are two blocked
6328 // registers, we are free to block one.
6329 Register temp_w = temps.AcquireW();
6330 Register index;
6331 // Remove the bias.
6332 if (lower_bound != 0) {
6333 index = temp_w;
6334 __ Sub(index, value_reg, Operand(lower_bound));
6335 } else {
6336 index = value_reg;
6337 }
6338
6339 // Jump to default block if index is out of the range.
6340 __ Cmp(index, Operand(num_entries));
6341 __ B(hs, codegen_->GetLabelOf(default_block));
6342
6343 // In current VIXL implementation, it won't require any blocked registers to encode the
6344 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
6345 // register pressure.
6346 Register table_base = temps.AcquireX();
6347 // Load jump offset from the table.
6348 __ Adr(table_base, jump_table->GetTableStartLabel());
6349 Register jump_offset = temp_w;
6350 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
6351
6352 // Jump to target block by branching to table_base(pc related) + offset.
6353 Register target_address = table_base;
6354 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
6355 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006356 }
6357}
6358
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006359void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
6360 HInstruction* instruction,
6361 Location out,
6362 uint32_t offset,
6363 Location maybe_temp,
6364 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006365 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006366 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006367 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006368 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006369 if (kUseBakerReadBarrier) {
6370 // Load with fast path based Baker's read barrier.
6371 // /* HeapReference<Object> */ out = *(out + offset)
6372 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6373 out,
6374 out_reg,
6375 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006376 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006377 /* needs_null_check= */ false,
6378 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006379 } else {
6380 // Load with slow path based read barrier.
6381 // Save the value of `out` into `maybe_temp` before overwriting it
6382 // in the following move operation, as we will need it for the
6383 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006384 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00006385 __ Mov(temp_reg, out_reg);
6386 // /* HeapReference<Object> */ out = *(out + offset)
6387 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6388 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6389 }
6390 } else {
6391 // Plain load with no read barrier.
6392 // /* HeapReference<Object> */ out = *(out + offset)
6393 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6394 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6395 }
6396}
6397
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006398void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
6399 HInstruction* instruction,
6400 Location out,
6401 Location obj,
6402 uint32_t offset,
6403 Location maybe_temp,
6404 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006405 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006406 Register out_reg = RegisterFrom(out, type);
6407 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006408 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006409 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006410 if (kUseBakerReadBarrier) {
6411 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00006412 // /* HeapReference<Object> */ out = *(obj + offset)
6413 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6414 out,
6415 obj_reg,
6416 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006417 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006418 /* needs_null_check= */ false,
6419 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006420 } else {
6421 // Load with slow path based read barrier.
6422 // /* HeapReference<Object> */ out = *(obj + offset)
6423 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6424 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6425 }
6426 } else {
6427 // Plain load with no read barrier.
6428 // /* HeapReference<Object> */ out = *(obj + offset)
6429 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6430 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6431 }
6432}
6433
Vladimir Markoca1e0382018-04-11 09:58:41 +00006434void CodeGeneratorARM64::GenerateGcRootFieldLoad(
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006435 HInstruction* instruction,
6436 Location root,
6437 Register obj,
6438 uint32_t offset,
6439 vixl::aarch64::Label* fixup_label,
6440 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006441 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006442 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006443 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006444 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006445 if (kUseBakerReadBarrier) {
6446 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00006447 // Baker's read barrier are used.
Roland Levillain44015862016-01-22 11:47:17 +00006448
Vladimir Marko008e09f32018-08-06 15:42:43 +01006449 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6450 // the Marking Register) to decide whether we need to enter
6451 // the slow path to mark the GC root.
6452 //
6453 // We use shared thunks for the slow path; shared within the method
6454 // for JIT, across methods for AOT. That thunk checks the reference
6455 // and jumps to the entrypoint if needed.
6456 //
6457 // lr = &return_address;
6458 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6459 // if (mr) { // Thread::Current()->GetIsGcMarking()
6460 // goto gc_root_thunk<root_reg>(lr)
6461 // }
6462 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00006463
Vladimir Marko008e09f32018-08-06 15:42:43 +01006464 UseScratchRegisterScope temps(GetVIXLAssembler());
6465 DCHECK(temps.IsAvailable(ip0));
6466 DCHECK(temps.IsAvailable(ip1));
6467 temps.Exclude(ip0, ip1);
6468 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
Roland Levillain44015862016-01-22 11:47:17 +00006469
Vladimir Marko008e09f32018-08-06 15:42:43 +01006470 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6471 vixl::aarch64::Label return_address;
6472 __ adr(lr, &return_address);
6473 if (fixup_label != nullptr) {
6474 __ bind(fixup_label);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006475 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006476 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
Vladimir Marko94796f82018-08-08 15:15:33 +01006477 "GC root LDR must be 2 instructions (8B) before the return address label.");
Vladimir Marko008e09f32018-08-06 15:42:43 +01006478 __ ldr(root_reg, MemOperand(obj.X(), offset));
6479 EmitBakerReadBarrierCbnz(custom_data);
6480 __ bind(&return_address);
Roland Levillain44015862016-01-22 11:47:17 +00006481 } else {
6482 // GC root loaded through a slow path for read barriers other
6483 // than Baker's.
6484 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006485 if (fixup_label == nullptr) {
6486 __ Add(root_reg.X(), obj.X(), offset);
6487 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006488 EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006489 }
Roland Levillain44015862016-01-22 11:47:17 +00006490 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00006491 GenerateReadBarrierForRootSlow(instruction, root, root);
Roland Levillain44015862016-01-22 11:47:17 +00006492 }
6493 } else {
6494 // Plain GC root load with no read barrier.
6495 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006496 if (fixup_label == nullptr) {
6497 __ Ldr(root_reg, MemOperand(obj, offset));
6498 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006499 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006500 }
Roland Levillain44015862016-01-22 11:47:17 +00006501 // Note that GC roots are not affected by heap poisoning, thus we
6502 // do not have to unpoison `root_reg` here.
6503 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006504 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00006505}
6506
Vladimir Markoc8178f52020-11-24 10:38:16 +00006507void CodeGeneratorARM64::GenerateIntrinsicCasMoveWithBakerReadBarrier(
6508 vixl::aarch64::Register marked_old_value,
Vladimir Marko94796f82018-08-08 15:15:33 +01006509 vixl::aarch64::Register old_value) {
6510 DCHECK(kEmitCompilerReadBarrier);
6511 DCHECK(kUseBakerReadBarrier);
6512
6513 // Similar to the Baker RB path in GenerateGcRootFieldLoad(), with a MOV instead of LDR.
Vladimir Markoc8178f52020-11-24 10:38:16 +00006514 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(marked_old_value.GetCode());
Vladimir Marko94796f82018-08-08 15:15:33 +01006515
6516 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6517 vixl::aarch64::Label return_address;
6518 __ adr(lr, &return_address);
6519 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6520 "GC root LDR must be 2 instructions (8B) before the return address label.");
Vladimir Markoc8178f52020-11-24 10:38:16 +00006521 __ mov(marked_old_value, old_value);
Vladimir Marko94796f82018-08-08 15:15:33 +01006522 EmitBakerReadBarrierCbnz(custom_data);
6523 __ bind(&return_address);
6524}
6525
Roland Levillain44015862016-01-22 11:47:17 +00006526void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6527 Location ref,
Vladimir Marko248141f2018-08-10 10:40:07 +01006528 vixl::aarch64::Register obj,
6529 const vixl::aarch64::MemOperand& src,
Roland Levillain44015862016-01-22 11:47:17 +00006530 bool needs_null_check,
6531 bool use_load_acquire) {
6532 DCHECK(kEmitCompilerReadBarrier);
6533 DCHECK(kUseBakerReadBarrier);
6534
Vladimir Marko0ecac682018-08-07 10:40:38 +01006535 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6536 // Marking Register) to decide whether we need to enter the slow
6537 // path to mark the reference. Then, in the slow path, check the
6538 // gray bit in the lock word of the reference's holder (`obj`) to
6539 // decide whether to mark `ref` or not.
6540 //
6541 // We use shared thunks for the slow path; shared within the method
6542 // for JIT, across methods for AOT. That thunk checks the holder
6543 // and jumps to the entrypoint if needed. If the holder is not gray,
6544 // it creates a fake dependency and returns to the LDR instruction.
6545 //
6546 // lr = &gray_return_address;
6547 // if (mr) { // Thread::Current()->GetIsGcMarking()
6548 // goto field_thunk<holder_reg, base_reg, use_load_acquire>(lr)
6549 // }
6550 // not_gray_return_address:
6551 // // Original reference load. If the offset is too large to fit
6552 // // into LDR, we use an adjusted base register here.
6553 // HeapReference<mirror::Object> reference = *(obj+offset);
6554 // gray_return_address:
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006555
Vladimir Marko248141f2018-08-10 10:40:07 +01006556 DCHECK(src.GetAddrMode() == vixl::aarch64::Offset);
6557 DCHECK_ALIGNED(src.GetOffset(), sizeof(mirror::HeapReference<mirror::Object>));
6558
6559 UseScratchRegisterScope temps(GetVIXLAssembler());
6560 DCHECK(temps.IsAvailable(ip0));
6561 DCHECK(temps.IsAvailable(ip1));
6562 temps.Exclude(ip0, ip1);
6563 uint32_t custom_data = use_load_acquire
6564 ? EncodeBakerReadBarrierAcquireData(src.GetBaseRegister().GetCode(), obj.GetCode())
6565 : EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode());
6566
6567 {
6568 ExactAssemblyScope guard(GetVIXLAssembler(),
6569 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6570 vixl::aarch64::Label return_address;
6571 __ adr(lr, &return_address);
6572 EmitBakerReadBarrierCbnz(custom_data);
6573 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6574 "Field LDR must be 1 instruction (4B) before the return address label; "
6575 " 2 instructions (8B) for heap poisoning.");
6576 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
6577 if (use_load_acquire) {
6578 DCHECK_EQ(src.GetOffset(), 0);
6579 __ ldar(ref_reg, src);
6580 } else {
6581 __ ldr(ref_reg, src);
6582 }
6583 if (needs_null_check) {
6584 MaybeRecordImplicitNullCheck(instruction);
6585 }
6586 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6587 // macro instructions disallowed in ExactAssemblyScope.
6588 if (kPoisonHeapReferences) {
6589 __ neg(ref_reg, Operand(ref_reg));
6590 }
6591 __ bind(&return_address);
6592 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006593 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Vladimir Marko248141f2018-08-10 10:40:07 +01006594}
6595
6596void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6597 Location ref,
6598 Register obj,
6599 uint32_t offset,
6600 Location maybe_temp,
6601 bool needs_null_check,
6602 bool use_load_acquire) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006603 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6604 Register base = obj;
6605 if (use_load_acquire) {
6606 DCHECK(maybe_temp.IsRegister());
6607 base = WRegisterFrom(maybe_temp);
6608 __ Add(base, obj, offset);
6609 offset = 0u;
6610 } else if (offset >= kReferenceLoadMinFarOffset) {
6611 DCHECK(maybe_temp.IsRegister());
6612 base = WRegisterFrom(maybe_temp);
6613 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6614 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6615 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006616 }
Vladimir Marko248141f2018-08-10 10:40:07 +01006617 MemOperand src(base.X(), offset);
6618 GenerateFieldLoadWithBakerReadBarrier(
6619 instruction, ref, obj, src, needs_null_check, use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006620}
6621
Artem Serov0806f582018-10-11 20:14:20 +01006622void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HArrayGet* instruction,
6623 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006624 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006625 uint32_t data_offset,
6626 Location index,
Roland Levillain44015862016-01-22 11:47:17 +00006627 bool needs_null_check) {
6628 DCHECK(kEmitCompilerReadBarrier);
6629 DCHECK(kUseBakerReadBarrier);
6630
Vladimir Marko66d691d2017-04-07 17:53:39 +01006631 static_assert(
6632 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6633 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006634 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006635
Vladimir Marko008e09f32018-08-06 15:42:43 +01006636 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6637 // Marking Register) to decide whether we need to enter the slow
6638 // path to mark the reference. Then, in the slow path, check the
6639 // gray bit in the lock word of the reference's holder (`obj`) to
6640 // decide whether to mark `ref` or not.
6641 //
6642 // We use shared thunks for the slow path; shared within the method
6643 // for JIT, across methods for AOT. That thunk checks the holder
6644 // and jumps to the entrypoint if needed. If the holder is not gray,
6645 // it creates a fake dependency and returns to the LDR instruction.
6646 //
6647 // lr = &gray_return_address;
6648 // if (mr) { // Thread::Current()->GetIsGcMarking()
6649 // goto array_thunk<base_reg>(lr)
6650 // }
6651 // not_gray_return_address:
6652 // // Original reference load. If the offset is too large to fit
6653 // // into LDR, we use an adjusted base register here.
6654 // HeapReference<mirror::Object> reference = data[index];
6655 // gray_return_address:
Vladimir Marko66d691d2017-04-07 17:53:39 +01006656
Vladimir Marko008e09f32018-08-06 15:42:43 +01006657 DCHECK(index.IsValid());
6658 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
6659 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006660
Vladimir Marko008e09f32018-08-06 15:42:43 +01006661 UseScratchRegisterScope temps(GetVIXLAssembler());
6662 DCHECK(temps.IsAvailable(ip0));
6663 DCHECK(temps.IsAvailable(ip1));
6664 temps.Exclude(ip0, ip1);
Artem Serov0806f582018-10-11 20:14:20 +01006665
6666 Register temp;
6667 if (instruction->GetArray()->IsIntermediateAddress()) {
6668 // We do not need to compute the intermediate address from the array: the
6669 // input instruction has done it already. See the comment in
6670 // `TryExtractArrayAccessAddress()`.
6671 if (kIsDebugBuild) {
6672 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
6673 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
6674 }
6675 temp = obj;
6676 } else {
6677 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
6678 __ Add(temp.X(), obj.X(), Operand(data_offset));
6679 }
6680
Vladimir Marko008e09f32018-08-06 15:42:43 +01006681 uint32_t custom_data = EncodeBakerReadBarrierArrayData(temp.GetCode());
Vladimir Marko66d691d2017-04-07 17:53:39 +01006682
Vladimir Marko008e09f32018-08-06 15:42:43 +01006683 {
6684 ExactAssemblyScope guard(GetVIXLAssembler(),
6685 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6686 vixl::aarch64::Label return_address;
6687 __ adr(lr, &return_address);
6688 EmitBakerReadBarrierCbnz(custom_data);
6689 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6690 "Array LDR must be 1 instruction (4B) before the return address label; "
6691 " 2 instructions (8B) for heap poisoning.");
6692 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6693 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6694 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6695 // macro instructions disallowed in ExactAssemblyScope.
6696 if (kPoisonHeapReferences) {
6697 __ neg(ref_reg, Operand(ref_reg));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006698 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006699 __ bind(&return_address);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006700 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006701 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Roland Levillain44015862016-01-22 11:47:17 +00006702}
6703
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006704void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6705 // The following condition is a compile-time one, so it does not have a run-time cost.
6706 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
6707 // The following condition is a run-time one; it is executed after the
6708 // previous compile-time test, to avoid penalizing non-debug builds.
6709 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6710 UseScratchRegisterScope temps(GetVIXLAssembler());
6711 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6712 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6713 }
6714 }
6715}
6716
Vladimir Marko1bff99f2020-11-02 15:07:33 +00006717SlowPathCodeARM64* CodeGeneratorARM64::AddReadBarrierSlowPath(HInstruction* instruction,
6718 Location out,
6719 Location ref,
6720 Location obj,
6721 uint32_t offset,
6722 Location index) {
6723 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
6724 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6725 AddSlowPath(slow_path);
6726 return slow_path;
6727}
6728
Roland Levillain44015862016-01-22 11:47:17 +00006729void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6730 Location out,
6731 Location ref,
6732 Location obj,
6733 uint32_t offset,
6734 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006735 DCHECK(kEmitCompilerReadBarrier);
6736
Roland Levillain44015862016-01-22 11:47:17 +00006737 // Insert a slow path based read barrier *after* the reference load.
6738 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006739 // If heap poisoning is enabled, the unpoisoning of the loaded
6740 // reference will be carried out by the runtime within the slow
6741 // path.
6742 //
6743 // Note that `ref` currently does not get unpoisoned (when heap
6744 // poisoning is enabled), which is alright as the `ref` argument is
6745 // not used by the artReadBarrierSlow entry point.
6746 //
6747 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko1bff99f2020-11-02 15:07:33 +00006748 SlowPathCodeARM64* slow_path = AddReadBarrierSlowPath(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006749
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006750 __ B(slow_path->GetEntryLabel());
6751 __ Bind(slow_path->GetExitLabel());
6752}
6753
Roland Levillain44015862016-01-22 11:47:17 +00006754void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6755 Location out,
6756 Location ref,
6757 Location obj,
6758 uint32_t offset,
6759 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006760 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006761 // Baker's read barriers shall be handled by the fast path
6762 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6763 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006764 // If heap poisoning is enabled, unpoisoning will be taken care of
6765 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006766 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006767 } else if (kPoisonHeapReferences) {
6768 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6769 }
6770}
6771
Roland Levillain44015862016-01-22 11:47:17 +00006772void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6773 Location out,
6774 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006775 DCHECK(kEmitCompilerReadBarrier);
6776
Roland Levillain44015862016-01-22 11:47:17 +00006777 // Insert a slow path based read barrier *after* the GC root load.
6778 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006779 // Note that GC roots are not affected by heap poisoning, so we do
6780 // not need to do anything special for this here.
6781 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006782 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006783 AddSlowPath(slow_path);
6784
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006785 __ B(slow_path->GetEntryLabel());
6786 __ Bind(slow_path->GetExitLabel());
6787}
6788
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006789void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6790 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006791 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006792 locations->SetInAt(0, Location::RequiresRegister());
6793 locations->SetOut(Location::RequiresRegister());
6794}
6795
6796void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6797 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006798 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006799 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006800 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006801 __ Ldr(XRegisterFrom(locations->Out()),
6802 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006803 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006804 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006805 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006806 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6807 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006808 __ Ldr(XRegisterFrom(locations->Out()),
6809 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006810 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006811}
6812
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006813static void PatchJitRootUse(uint8_t* code,
6814 const uint8_t* roots_data,
6815 vixl::aarch64::Literal<uint32_t>* literal,
6816 uint64_t index_in_table) {
6817 uint32_t literal_offset = literal->GetOffset();
6818 uintptr_t address =
6819 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6820 uint8_t* data = code + literal_offset;
6821 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6822}
6823
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006824void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6825 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006826 const StringReference& string_reference = entry.first;
6827 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006828 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006829 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006830 }
6831 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006832 const TypeReference& type_reference = entry.first;
6833 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006834 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006835 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006836 }
6837}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006838
Artem Serov1a719e42019-07-18 14:24:55 +01006839MemOperand InstructionCodeGeneratorARM64::VecNeonAddress(
6840 HVecMemoryOperation* instruction,
6841 UseScratchRegisterScope* temps_scope,
6842 size_t size,
6843 bool is_string_char_at,
6844 /*out*/ Register* scratch) {
6845 LocationSummary* locations = instruction->GetLocations();
6846 Register base = InputRegisterAt(instruction, 0);
6847
6848 if (instruction->InputAt(1)->IsIntermediateAddressIndex()) {
6849 DCHECK(!is_string_char_at);
6850 return MemOperand(base.X(), InputRegisterAt(instruction, 1).X());
6851 }
6852
6853 Location index = locations->InAt(1);
6854 uint32_t offset = is_string_char_at
6855 ? mirror::String::ValueOffset().Uint32Value()
6856 : mirror::Array::DataOffset(size).Uint32Value();
6857 size_t shift = ComponentSizeShiftWidth(size);
6858
6859 // HIntermediateAddress optimization is only applied for scalar ArrayGet and ArraySet.
6860 DCHECK(!instruction->InputAt(0)->IsIntermediateAddress());
6861
6862 if (index.IsConstant()) {
6863 offset += Int64FromLocation(index) << shift;
6864 return HeapOperand(base, offset);
6865 } else {
6866 *scratch = temps_scope->AcquireSameSizeAs(base);
6867 __ Add(*scratch, base, Operand(WRegisterFrom(index), LSL, shift));
6868 return HeapOperand(*scratch, offset);
6869 }
6870}
6871
Alexandre Rames67555f72014-11-18 10:55:16 +00006872#undef __
6873#undef QUICK_ENTRY_POINT
6874
Vladimir Markoca1e0382018-04-11 09:58:41 +00006875#define __ assembler.GetVIXLAssembler()->
6876
6877static void EmitGrayCheckAndFastPath(arm64::Arm64Assembler& assembler,
6878 vixl::aarch64::Register base_reg,
6879 vixl::aarch64::MemOperand& lock_word,
Vladimir Marko7a695052018-04-12 10:26:50 +01006880 vixl::aarch64::Label* slow_path,
6881 vixl::aarch64::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006882 // Load the lock word containing the rb_state.
6883 __ Ldr(ip0.W(), lock_word);
6884 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01006885 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Vladimir Markoca1e0382018-04-11 09:58:41 +00006886 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6887 __ Tbnz(ip0.W(), LockWord::kReadBarrierStateShift, slow_path);
6888 static_assert(
6889 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET,
6890 "Field and array LDR offsets must be the same to reuse the same code.");
Vladimir Marko7a695052018-04-12 10:26:50 +01006891 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
6892 if (throw_npe != nullptr) {
6893 __ Bind(throw_npe);
6894 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006895 // Adjust the return address back to the LDR (1 instruction; 2 for heap poisoning).
6896 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6897 "Field LDR must be 1 instruction (4B) before the return address label; "
6898 " 2 instructions (8B) for heap poisoning.");
6899 __ Add(lr, lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6900 // Introduce a dependency on the lock_word including rb_state,
6901 // to prevent load-load reordering, and without using
6902 // a memory barrier (which would be more expensive).
6903 __ Add(base_reg, base_reg, Operand(ip0, LSR, 32));
6904 __ Br(lr); // And return back to the function.
6905 // Note: The fake dependency is unnecessary for the slow path.
6906}
6907
6908// Load the read barrier introspection entrypoint in register `entrypoint`.
6909static void LoadReadBarrierMarkIntrospectionEntrypoint(arm64::Arm64Assembler& assembler,
6910 vixl::aarch64::Register entrypoint) {
6911 // entrypoint = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6912 DCHECK_EQ(ip0.GetCode(), 16u);
6913 const int32_t entry_point_offset =
6914 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6915 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
6916}
6917
6918void CodeGeneratorARM64::CompileBakerReadBarrierThunk(Arm64Assembler& assembler,
6919 uint32_t encoded_data,
6920 /*out*/ std::string* debug_name) {
6921 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
6922 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006923 case BakerReadBarrierKind::kField:
6924 case BakerReadBarrierKind::kAcquire: {
Artem Serova07de552020-11-01 22:42:43 +00006925 Register base_reg =
6926 vixl::aarch64::XRegister(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00006927 CheckValidReg(base_reg.GetCode());
Artem Serova07de552020-11-01 22:42:43 +00006928 Register holder_reg =
6929 vixl::aarch64::XRegister(BakerReadBarrierSecondRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00006930 CheckValidReg(holder_reg.GetCode());
6931 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6932 temps.Exclude(ip0, ip1);
Roland Levillain988c3912019-09-25 19:33:35 +01006933 // In the case of a field load (with relaxed semantic), if `base_reg` differs from
6934 // `holder_reg`, the offset was too large and we must have emitted (during the construction
6935 // of the HIR graph, see `art::HInstructionBuilder::BuildInstanceFieldAccess`) and preserved
6936 // (see `art::PrepareForRegisterAllocation::VisitNullCheck`) an explicit null check before
6937 // the load. Otherwise, for implicit null checks, we need to null-check the holder as we do
6938 // not necessarily do that check before going to the thunk.
6939 //
6940 // In the case of a field load with load-acquire semantics (where `base_reg` always differs
6941 // from `holder_reg`), we also need an explicit null check when implicit null checks are
6942 // allowed, as we do not emit one before going to the thunk.
Vladimir Marko7a695052018-04-12 10:26:50 +01006943 vixl::aarch64::Label throw_npe_label;
6944 vixl::aarch64::Label* throw_npe = nullptr;
Roland Levillain988c3912019-09-25 19:33:35 +01006945 if (GetCompilerOptions().GetImplicitNullChecks() &&
6946 (holder_reg.Is(base_reg) || (kind == BakerReadBarrierKind::kAcquire))) {
Vladimir Marko7a695052018-04-12 10:26:50 +01006947 throw_npe = &throw_npe_label;
6948 __ Cbz(holder_reg.W(), throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006949 }
Vladimir Marko7a695052018-04-12 10:26:50 +01006950 // Check if the holder is gray and, if not, add fake dependency to the base register
6951 // and return to the LDR instruction to load the reference. Otherwise, use introspection
6952 // to load the reference and call the entrypoint that performs further checks on the
6953 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006954 vixl::aarch64::Label slow_path;
6955 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
Vladimir Marko7a695052018-04-12 10:26:50 +01006956 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006957 __ Bind(&slow_path);
Vladimir Marko0ecac682018-08-07 10:40:38 +01006958 if (kind == BakerReadBarrierKind::kField) {
6959 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6960 __ Ldr(ip0.W(), ldr_address); // Load the LDR (immediate) unsigned offset.
6961 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6962 __ Ubfx(ip0.W(), ip0.W(), 10, 12); // Extract the offset.
6963 __ Ldr(ip0.W(), MemOperand(base_reg, ip0, LSL, 2)); // Load the reference.
6964 } else {
6965 DCHECK(kind == BakerReadBarrierKind::kAcquire);
6966 DCHECK(!base_reg.Is(holder_reg));
6967 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6968 __ Ldar(ip0.W(), MemOperand(base_reg));
6969 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006970 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
6971 __ Br(ip1); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006972 break;
6973 }
6974 case BakerReadBarrierKind::kArray: {
Artem Serova07de552020-11-01 22:42:43 +00006975 Register base_reg =
6976 vixl::aarch64::XRegister(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00006977 CheckValidReg(base_reg.GetCode());
6978 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6979 BakerReadBarrierSecondRegField::Decode(encoded_data));
6980 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6981 temps.Exclude(ip0, ip1);
6982 vixl::aarch64::Label slow_path;
6983 int32_t data_offset =
6984 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
6985 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
6986 DCHECK_LT(lock_word.GetOffset(), 0);
6987 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path);
6988 __ Bind(&slow_path);
6989 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
6990 __ Ldr(ip0.W(), ldr_address); // Load the LDR (register) unsigned offset.
6991 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6992 __ Ubfx(ip0, ip0, 16, 6); // Extract the index register, plus 32 (bit 21 is set).
6993 __ Bfi(ip1, ip0, 3, 6); // Insert ip0 to the entrypoint address to create
6994 // a switch case target based on the index register.
6995 __ Mov(ip0, base_reg); // Move the base register to ip0.
6996 __ Br(ip1); // Jump to the entrypoint's array switch case.
6997 break;
6998 }
6999 case BakerReadBarrierKind::kGcRoot: {
7000 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
7001 // and it does not have a forwarding address), call the correct introspection entrypoint;
7002 // otherwise return the reference (or the extracted forwarding address).
7003 // There is no gray bit check for GC roots.
Artem Serova07de552020-11-01 22:42:43 +00007004 Register root_reg =
7005 vixl::aarch64::WRegister(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00007006 CheckValidReg(root_reg.GetCode());
7007 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7008 BakerReadBarrierSecondRegField::Decode(encoded_data));
7009 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
7010 temps.Exclude(ip0, ip1);
7011 vixl::aarch64::Label return_label, not_marked, forwarding_address;
7012 __ Cbz(root_reg, &return_label);
7013 MemOperand lock_word(root_reg.X(), mirror::Object::MonitorOffset().Int32Value());
7014 __ Ldr(ip0.W(), lock_word);
7015 __ Tbz(ip0.W(), LockWord::kMarkBitStateShift, &not_marked);
7016 __ Bind(&return_label);
7017 __ Br(lr);
7018 __ Bind(&not_marked);
7019 __ Tst(ip0.W(), Operand(ip0.W(), LSL, 1));
7020 __ B(&forwarding_address, mi);
7021 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
7022 // Adjust the art_quick_read_barrier_mark_introspection address in IP1 to
7023 // art_quick_read_barrier_mark_introspection_gc_roots.
7024 __ Add(ip1, ip1, Operand(BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRYPOINT_OFFSET));
7025 __ Mov(ip0.W(), root_reg);
7026 __ Br(ip1);
7027 __ Bind(&forwarding_address);
7028 __ Lsl(root_reg, ip0.W(), LockWord::kForwardingAddressShift);
7029 __ Br(lr);
7030 break;
7031 }
7032 default:
7033 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
7034 UNREACHABLE();
7035 }
7036
Vladimir Marko966b46f2018-08-03 10:20:19 +00007037 // For JIT, the slow path is considered part of the compiled method,
Vladimir Markof91fc122020-05-13 09:21:00 +01007038 // so JIT should pass null as `debug_name`.
Vladimir Marko695348f2020-05-19 14:42:02 +01007039 DCHECK(!GetCompilerOptions().IsJitCompiler() || debug_name == nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00007040 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00007041 std::ostringstream oss;
7042 oss << "BakerReadBarrierThunk";
7043 switch (kind) {
7044 case BakerReadBarrierKind::kField:
7045 oss << "Field_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
7046 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
7047 break;
Vladimir Marko0ecac682018-08-07 10:40:38 +01007048 case BakerReadBarrierKind::kAcquire:
7049 oss << "Acquire_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
7050 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
7051 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +00007052 case BakerReadBarrierKind::kArray:
7053 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
7054 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7055 BakerReadBarrierSecondRegField::Decode(encoded_data));
7056 break;
7057 case BakerReadBarrierKind::kGcRoot:
7058 oss << "GcRoot_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
7059 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7060 BakerReadBarrierSecondRegField::Decode(encoded_data));
7061 break;
7062 }
7063 *debug_name = oss.str();
7064 }
7065}
7066
7067#undef __
7068
Alexandre Rames5319def2014-10-23 10:03:10 +01007069} // namespace arm64
7070} // namespace art