blob: 7becf230317ace38c5da76a503afddc0aa3400f8 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Vladimir Markof4f2daa2017-03-20 18:26:59 +000019#include "arch/arm64/asm_support_arm64.h"
Serban Constantinescu579885a2015-02-22 20:51:33 +000020#include "arch/arm64/instruction_set_features_arm64.h"
Vladimir Marko86c87522020-05-11 16:55:55 +010021#include "arch/arm64/jni_frame_arm64.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000022#include "art_method-inl.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070023#include "base/bit_utils.h"
24#include "base/bit_utils_iterator.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010025#include "class_table.h"
Zheng Xuc6667102015-05-15 16:08:45 +080026#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000027#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080029#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010030#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010031#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070032#include "heap_poisoning.h"
Nicolas Geoffray4313ccb2020-08-26 17:01:15 +010033#include "interpreter/mterp/nterp.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080034#include "intrinsics.h"
35#include "intrinsics_arm64.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010036#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070037#include "lock_word.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010038#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070039#include "mirror/class-inl.h"
Vladimir Marko2d98dc22020-10-01 11:21:37 +000040#include "mirror/var_handle.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000041#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010042#include "thread.h"
43#include "utils/arm64/assembler_arm64.h"
44#include "utils/assembler.h"
45#include "utils/stack_checks.h"
46
Scott Wakeling97c72b72016-06-24 16:19:36 +010047using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000048using vixl::ExactAssemblyScope;
49using vixl::CodeBufferCheckScope;
50using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010051
52#ifdef __
53#error "ARM64 Codegen VIXL macro-assembler macro already defined."
54#endif
55
Vladimir Marko0a516052019-10-14 13:00:44 +000056namespace art {
Alexandre Rames5319def2014-10-23 10:03:10 +010057
Roland Levillain22ccc3a2015-11-24 13:10:05 +000058template<class MirrorType>
59class GcRoot;
60
Alexandre Rames5319def2014-10-23 10:03:10 +010061namespace arm64 {
62
Alexandre Ramesbe919d92016-08-23 18:33:36 +010063using helpers::ARM64EncodableConstantOrRegister;
64using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080065using helpers::CPURegisterFrom;
66using helpers::DRegisterFrom;
67using helpers::FPRegisterFrom;
68using helpers::HeapOperand;
69using helpers::HeapOperandFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010070using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080071using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080072using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010073using helpers::InputRegisterAt;
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +010074using helpers::Int64FromLocation;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010075using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080076using helpers::LocationFrom;
77using helpers::OperandFromMemOperand;
78using helpers::OutputCPURegister;
79using helpers::OutputFPRegister;
80using helpers::OutputRegister;
81using helpers::RegisterFrom;
82using helpers::StackOperandFrom;
83using helpers::VIXLRegCodeFromART;
84using helpers::WRegisterFrom;
85using helpers::XRegisterFrom;
86
Vladimir Markof3e0ee22015-12-17 15:23:13 +000087// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080088// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
89// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000090static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010091
Vladimir Markof4f2daa2017-03-20 18:26:59 +000092// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
93// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
Vladimir Marko008e09f32018-08-06 15:42:43 +010094// For the Baker read barrier implementation using link-time generated thunks we need to split
Vladimir Markof4f2daa2017-03-20 18:26:59 +000095// the offset explicitly.
96constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
97
Alexandre Rames5319def2014-10-23 10:03:10 +010098inline Condition ARM64Condition(IfCondition cond) {
99 switch (cond) {
100 case kCondEQ: return eq;
101 case kCondNE: return ne;
102 case kCondLT: return lt;
103 case kCondLE: return le;
104 case kCondGT: return gt;
105 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700106 case kCondB: return lo;
107 case kCondBE: return ls;
108 case kCondA: return hi;
109 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100110 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000111 LOG(FATAL) << "Unreachable";
112 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100113}
114
Vladimir Markod6e069b2016-01-18 11:11:01 +0000115inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
116 // The ARM64 condition codes can express all the necessary branches, see the
117 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
118 // There is no dex instruction or HIR that would need the missing conditions
119 // "equal or unordered" or "not equal".
120 switch (cond) {
121 case kCondEQ: return eq;
122 case kCondNE: return ne /* unordered */;
123 case kCondLT: return gt_bias ? cc : lt /* unordered */;
124 case kCondLE: return gt_bias ? ls : le /* unordered */;
125 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
126 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
127 default:
128 LOG(FATAL) << "UNREACHABLE";
129 UNREACHABLE();
130 }
131}
132
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100133Location ARM64ReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000134 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
135 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
136 // but we use the exact registers for clarity.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100137 if (return_type == DataType::Type::kFloat32) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000138 return LocationFrom(s0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100139 } else if (return_type == DataType::Type::kFloat64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000140 return LocationFrom(d0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100141 } else if (return_type == DataType::Type::kInt64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000142 return LocationFrom(x0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100143 } else if (return_type == DataType::Type::kVoid) {
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100144 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000145 } else {
146 return LocationFrom(w0);
147 }
148}
149
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100150Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000151 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100152}
153
Vladimir Marko3232dbb2018-07-25 15:42:46 +0100154static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
155 InvokeRuntimeCallingConvention calling_convention;
156 RegisterSet caller_saves = RegisterSet::Empty();
157 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
158 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
159 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
160 DataType::Type::kReference).GetCode());
161 return caller_saves;
162}
163
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100164// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
165#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700166#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100167
Zheng Xuda403092015-04-24 17:35:39 +0800168// Calculate memory accessing operand for save/restore live registers.
169static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100170 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800171 int64_t spill_offset,
172 bool is_save) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800173 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
174 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100175 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800176 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100177 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800178 codegen->GetNumberOfFloatingPointRegisters()));
179
Vladimir Marko804b03f2016-09-14 16:26:36 +0100180 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serovc8150b52019-07-31 18:28:00 +0100181 const unsigned v_reg_size_in_bits = codegen->GetSlowPathFPWidth() * 8;
Artem Serov1a719e42019-07-18 14:24:55 +0100182 DCHECK_LE(codegen->GetSIMDRegisterWidth(), kQRegSizeInBytes);
Artem Serovc8150b52019-07-31 18:28:00 +0100183 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size_in_bits, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800184
185 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
186 UseScratchRegisterScope temps(masm);
187
188 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100189 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
190 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800191 int64_t reg_size = kXRegSizeInBytes;
192 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
193 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100194 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800195 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
196 // If the offset does not fit in the instruction's immediate field, use an alternate register
197 // to compute the base address(float point registers spill base address).
198 Register new_base = temps.AcquireSameSizeAs(base);
199 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
200 base = new_base;
201 spill_offset = -core_spill_size;
202 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
203 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
204 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
205 }
206
207 if (is_save) {
208 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
209 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
210 } else {
211 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
212 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
213 }
214}
215
216void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800217 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Andreas Gampe3db70682018-12-26 15:12:03 -0800218 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100219 for (uint32_t i : LowToHighBits(core_spills)) {
220 // If the register holds an object, update the stack mask.
221 if (locations->RegisterContainsObject(i)) {
222 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800223 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100224 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
225 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
226 saved_core_stack_offsets_[i] = stack_offset;
227 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800228 }
229
Artem Serovc8150b52019-07-31 18:28:00 +0100230 const size_t fp_reg_size = codegen->GetSlowPathFPWidth();
Andreas Gampe3db70682018-12-26 15:12:03 -0800231 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100232 for (uint32_t i : LowToHighBits(fp_spills)) {
233 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
234 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
235 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serov9df37b92019-07-23 16:41:54 +0100236 stack_offset += fp_reg_size;
Zheng Xuda403092015-04-24 17:35:39 +0800237 }
238
Vladimir Marko804b03f2016-09-14 16:26:36 +0100239 SaveRestoreLiveRegistersHelper(codegen,
240 locations,
Andreas Gampe3db70682018-12-26 15:12:03 -0800241 codegen->GetFirstRegisterSlotInSlowPath(), /* is_save= */ true);
Zheng Xuda403092015-04-24 17:35:39 +0800242}
243
244void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100245 SaveRestoreLiveRegistersHelper(codegen,
246 locations,
Andreas Gampe3db70682018-12-26 15:12:03 -0800247 codegen->GetFirstRegisterSlotInSlowPath(), /* is_save= */ false);
Zheng Xuda403092015-04-24 17:35:39 +0800248}
249
Alexandre Rames5319def2014-10-23 10:03:10 +0100250class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
251 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000252 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100253
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100254 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100255 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000256 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100257
Alexandre Rames5319def2014-10-23 10:03:10 +0100258 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000259 if (instruction_->CanThrowIntoCatchBlock()) {
260 // Live registers will be restored in the catch block if caught.
261 SaveLiveRegisters(codegen, instruction_->GetLocations());
262 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000263 // We're moving two locations to locations that could overlap, so we need a parallel
264 // move resolver.
265 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100266 codegen->EmitParallelMoves(locations->InAt(0),
267 LocationFrom(calling_convention.GetRegisterAt(0)),
268 DataType::Type::kInt32,
269 locations->InAt(1),
270 LocationFrom(calling_convention.GetRegisterAt(1)),
271 DataType::Type::kInt32);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000272 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
273 ? kQuickThrowStringBounds
274 : kQuickThrowArrayBounds;
275 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100276 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800277 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100278 }
279
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100280 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100281
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100282 const char* GetDescription() const override { return "BoundsCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100283
Alexandre Rames5319def2014-10-23 10:03:10 +0100284 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100285 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
286};
287
Alexandre Rames67555f72014-11-18 10:55:16 +0000288class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
289 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000290 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000291
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100292 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000293 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
294 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000295 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800296 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000297 }
298
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100299 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100300
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100301 const char* GetDescription() const override { return "DivZeroCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100302
Alexandre Rames67555f72014-11-18 10:55:16 +0000303 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000304 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
305};
306
307class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
308 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100309 LoadClassSlowPathARM64(HLoadClass* cls, HInstruction* at)
310 : SlowPathCodeARM64(at), cls_(cls) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000311 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100312 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Alexandre Rames67555f72014-11-18 10:55:16 +0000313 }
314
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100315 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000316 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000317 Location out = locations->Out();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100318 const uint32_t dex_pc = instruction_->GetDexPc();
319 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
320 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
Alexandre Rames67555f72014-11-18 10:55:16 +0000321
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100322 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames67555f72014-11-18 10:55:16 +0000323 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000324 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000325
Vladimir Markof3c52b42017-11-17 17:32:12 +0000326 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100327 if (must_resolve_type) {
328 DCHECK(IsSameDexFile(cls_->GetDexFile(), arm64_codegen->GetGraph()->GetDexFile()));
329 dex::TypeIndex type_index = cls_->GetTypeIndex();
330 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Vladimir Marko8f63f102020-09-28 12:10:28 +0100331 if (cls_->NeedsAccessCheck()) {
332 CheckEntrypointTypes<kQuickResolveTypeAndVerifyAccess, void*, uint32_t>();
333 arm64_codegen->InvokeRuntime(kQuickResolveTypeAndVerifyAccess, instruction_, dex_pc, this);
334 } else {
335 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
336 arm64_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
337 }
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100338 // If we also must_do_clinit, the resolved type is now in the correct register.
339 } else {
340 DCHECK(must_do_clinit);
341 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
342 arm64_codegen->MoveLocation(LocationFrom(calling_convention.GetRegisterAt(0)),
343 source,
344 cls_->GetType());
345 }
346 if (must_do_clinit) {
347 arm64_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
348 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800349 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000350
351 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000352 if (out.IsValid()) {
353 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100354 DataType::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000355 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000356 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000357 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000358 __ B(GetExitLabel());
359 }
360
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100361 const char* GetDescription() const override { return "LoadClassSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100362
Alexandre Rames67555f72014-11-18 10:55:16 +0000363 private:
364 // The class this slow path will load.
365 HLoadClass* const cls_;
366
Alexandre Rames67555f72014-11-18 10:55:16 +0000367 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
368};
369
Vladimir Markoaad75c62016-10-03 08:46:48 +0000370class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
371 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000372 explicit LoadStringSlowPathARM64(HLoadString* instruction)
373 : SlowPathCodeARM64(instruction) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000374
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100375 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000376 LocationSummary* locations = instruction_->GetLocations();
377 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
378 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
379
380 __ Bind(GetEntryLabel());
381 SaveLiveRegisters(codegen, locations);
382
Vladimir Markof3c52b42017-11-17 17:32:12 +0000383 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000384 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
385 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000386 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
387 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100388 DataType::Type type = instruction_->GetType();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000389 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
390
391 RestoreLiveRegisters(codegen, locations);
392
Vladimir Markoaad75c62016-10-03 08:46:48 +0000393 __ B(GetExitLabel());
394 }
395
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100396 const char* GetDescription() const override { return "LoadStringSlowPathARM64"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000397
398 private:
399 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
400};
401
Alexandre Rames5319def2014-10-23 10:03:10 +0100402class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
403 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000404 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100405
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100406 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000407 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100408 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000409 if (instruction_->CanThrowIntoCatchBlock()) {
410 // Live registers will be restored in the catch block if caught.
411 SaveLiveRegisters(codegen, instruction_->GetLocations());
412 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000413 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
414 instruction_,
415 instruction_->GetDexPc(),
416 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800417 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100418 }
419
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100420 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100421
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100422 const char* GetDescription() const override { return "NullCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100423
Alexandre Rames5319def2014-10-23 10:03:10 +0100424 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100425 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
426};
427
428class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
429 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100430 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000431 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100432
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100433 void EmitNativeCode(CodeGenerator* codegen) override {
Artem Serov7957d952017-04-04 15:44:09 +0100434 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000435 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100436 __ Bind(GetEntryLabel());
Artem Serov1a719e42019-07-18 14:24:55 +0100437 SaveLiveRegisters(codegen, locations); // Only saves live vector regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000438 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800439 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov1a719e42019-07-18 14:24:55 +0100440 RestoreLiveRegisters(codegen, locations); // Only restores live vector regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000441 if (successor_ == nullptr) {
442 __ B(GetReturnLabel());
443 } else {
444 __ B(arm64_codegen->GetLabelOf(successor_));
445 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100446 }
447
Scott Wakeling97c72b72016-06-24 16:19:36 +0100448 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100449 DCHECK(successor_ == nullptr);
450 return &return_label_;
451 }
452
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100453 HBasicBlock* GetSuccessor() const {
454 return successor_;
455 }
456
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100457 const char* GetDescription() const override { return "SuspendCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100458
Alexandre Rames5319def2014-10-23 10:03:10 +0100459 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100460 // If not null, the block to branch to after the suspend check.
461 HBasicBlock* const successor_;
462
463 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100464 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100465
466 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
467};
468
Alexandre Rames67555f72014-11-18 10:55:16 +0000469class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
470 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000471 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000472 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000473
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100474 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000475 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800476
Alexandre Rames3e69f162014-12-10 10:36:50 +0000477 DCHECK(instruction_->IsCheckCast()
478 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
479 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100480 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000481
Alexandre Rames67555f72014-11-18 10:55:16 +0000482 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000483
Vladimir Marko87584542017-12-12 17:47:52 +0000484 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000485 SaveLiveRegisters(codegen, locations);
486 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000487
488 // We're moving two locations to locations that could overlap, so we need a parallel
489 // move resolver.
490 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800491 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800492 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100493 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800494 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800495 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100496 DataType::Type::kReference);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000497 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000498 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800499 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100500 DataType::Type ret_type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000501 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
502 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
503 } else {
504 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800505 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
506 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000507 }
508
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000509 if (!is_fatal_) {
510 RestoreLiveRegisters(codegen, locations);
511 __ B(GetExitLabel());
512 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000513 }
514
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100515 const char* GetDescription() const override { return "TypeCheckSlowPathARM64"; }
516 bool IsFatal() const override { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100517
Alexandre Rames67555f72014-11-18 10:55:16 +0000518 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000519 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000520
Alexandre Rames67555f72014-11-18 10:55:16 +0000521 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
522};
523
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700524class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
525 public:
Aart Bik42249c32016-01-07 15:33:50 -0800526 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000527 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700528
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100529 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bik42249c32016-01-07 15:33:50 -0800530 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700531 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100532 LocationSummary* locations = instruction_->GetLocations();
533 SaveLiveRegisters(codegen, locations);
534 InvokeRuntimeCallingConvention calling_convention;
535 __ Mov(calling_convention.GetRegisterAt(0),
536 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000537 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100538 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700539 }
540
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100541 const char* GetDescription() const override { return "DeoptimizationSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100542
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700543 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700544 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
545};
546
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100547class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
548 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000549 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100550
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100551 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100552 LocationSummary* locations = instruction_->GetLocations();
553 __ Bind(GetEntryLabel());
554 SaveLiveRegisters(codegen, locations);
555
556 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100557 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100558 parallel_move.AddMove(
559 locations->InAt(0),
560 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100561 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100562 nullptr);
563 parallel_move.AddMove(
564 locations->InAt(1),
565 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100566 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100567 nullptr);
568 parallel_move.AddMove(
569 locations->InAt(2),
570 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100571 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100572 nullptr);
573 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
574
575 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000576 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100577 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
578 RestoreLiveRegisters(codegen, locations);
579 __ B(GetExitLabel());
580 }
581
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100582 const char* GetDescription() const override { return "ArraySetSlowPathARM64"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100583
584 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100585 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
586};
587
Zheng Xu3927c8b2015-11-18 17:46:25 +0800588void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
589 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000590 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800591
592 // We are about to use the assembler to place literals directly. Make sure we have enough
593 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000594 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
595 num_entries * sizeof(int32_t),
596 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800597
598 __ Bind(&table_start_);
599 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
600 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100601 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800602 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100603 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800604 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
605 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
606 Literal<int32_t> literal(jump_offset);
607 __ place(&literal);
608 }
609}
610
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000611// Slow path generating a read barrier for a heap reference.
612class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
613 public:
614 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
615 Location out,
616 Location ref,
617 Location obj,
618 uint32_t offset,
619 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000620 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000621 out_(out),
622 ref_(ref),
623 obj_(obj),
624 offset_(offset),
625 index_(index) {
626 DCHECK(kEmitCompilerReadBarrier);
627 // If `obj` is equal to `out` or `ref`, it means the initial object
628 // has been overwritten by (or after) the heap object reference load
629 // to be instrumented, e.g.:
630 //
631 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000632 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000633 //
634 // In that case, we have lost the information about the original
635 // object, and the emitted read barrier cannot work properly.
636 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
637 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
638 }
639
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100640 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000641 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
642 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100643 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000644 DCHECK(locations->CanCall());
645 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100646 DCHECK(instruction_->IsInstanceFieldGet() ||
647 instruction_->IsStaticFieldGet() ||
648 instruction_->IsArrayGet() ||
649 instruction_->IsInstanceOf() ||
650 instruction_->IsCheckCast() ||
Vladimir Markoa41ea272020-09-07 15:24:36 +0000651 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000652 << "Unexpected instruction in read barrier for heap reference slow path: "
653 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000654 // The read barrier instrumentation of object ArrayGet
655 // instructions does not support the HIntermediateAddress
656 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000657 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100658 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000659
660 __ Bind(GetEntryLabel());
661
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000662 SaveLiveRegisters(codegen, locations);
663
664 // We may have to change the index's value, but as `index_` is a
665 // constant member (like other "inputs" of this slow path),
666 // introduce a copy of it, `index`.
667 Location index = index_;
668 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100669 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000670 if (instruction_->IsArrayGet()) {
671 // Compute the actual memory offset and store it in `index`.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100672 Register index_reg = RegisterFrom(index_, DataType::Type::kInt32);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000673 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
674 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
675 // We are about to change the value of `index_reg` (see the
676 // calls to vixl::MacroAssembler::Lsl and
677 // vixl::MacroAssembler::Mov below), but it has
678 // not been saved by the previous call to
679 // art::SlowPathCode::SaveLiveRegisters, as it is a
680 // callee-save register --
681 // art::SlowPathCode::SaveLiveRegisters does not consider
682 // callee-save registers, as it has been designed with the
683 // assumption that callee-save registers are supposed to be
684 // handled by the called function. So, as a callee-save
685 // register, `index_reg` _would_ eventually be saved onto
686 // the stack, but it would be too late: we would have
687 // changed its value earlier. Therefore, we manually save
688 // it here into another freely available register,
689 // `free_reg`, chosen of course among the caller-save
690 // registers (as a callee-save `free_reg` register would
691 // exhibit the same problem).
692 //
693 // Note we could have requested a temporary register from
694 // the register allocator instead; but we prefer not to, as
695 // this is a slow path, and we know we can find a
696 // caller-save register that is available.
697 Register free_reg = FindAvailableCallerSaveRegister(codegen);
698 __ Mov(free_reg.W(), index_reg);
699 index_reg = free_reg;
700 index = LocationFrom(index_reg);
701 } else {
702 // The initial register stored in `index_` has already been
703 // saved in the call to art::SlowPathCode::SaveLiveRegisters
704 // (as it is not a callee-save register), so we can freely
705 // use it.
706 }
707 // Shifting the index value contained in `index_reg` by the scale
708 // factor (2) cannot overflow in practice, as the runtime is
709 // unable to allocate object arrays with a size larger than
710 // 2^26 - 1 (that is, 2^28 - 4 bytes).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100711 __ Lsl(index_reg, index_reg, DataType::SizeShift(type));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000712 static_assert(
713 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
714 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
715 __ Add(index_reg, index_reg, Operand(offset_));
716 } else {
Vladimir Markoa41ea272020-09-07 15:24:36 +0000717 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile/VarHandleGet
Roland Levillain3d312422016-06-23 13:53:42 +0100718 // intrinsics, `index_` is not shifted by a scale factor of 2
719 // (as in the case of ArrayGet), as it is actually an offset
720 // to an object field within an object.
721 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000722 DCHECK(instruction_->GetLocations()->Intrinsified());
Vladimir Marko2d98dc22020-10-01 11:21:37 +0000723 Intrinsics intrinsic = instruction_->AsInvoke()->GetIntrinsic();
724 DCHECK(intrinsic == Intrinsics::kUnsafeGetObject ||
725 intrinsic == Intrinsics::kUnsafeGetObjectVolatile ||
726 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
Vladimir Marko1bff99f2020-11-02 15:07:33 +0000727 mirror::VarHandle::AccessModeTemplate::kGet ||
728 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
729 mirror::VarHandle::AccessModeTemplate::kCompareAndSet ||
730 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
731 mirror::VarHandle::AccessModeTemplate::kCompareAndExchange)
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000732 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100733 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100734 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000735 }
736 }
737
738 // We're moving two or three locations to locations that could
739 // overlap, so we need a parallel move resolver.
740 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100741 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000742 parallel_move.AddMove(ref_,
743 LocationFrom(calling_convention.GetRegisterAt(0)),
744 type,
745 nullptr);
746 parallel_move.AddMove(obj_,
747 LocationFrom(calling_convention.GetRegisterAt(1)),
748 type,
749 nullptr);
750 if (index.IsValid()) {
751 parallel_move.AddMove(index,
752 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100753 DataType::Type::kInt32,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000754 nullptr);
755 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
756 } else {
757 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
758 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
759 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000760 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000761 instruction_,
762 instruction_->GetDexPc(),
763 this);
764 CheckEntrypointTypes<
765 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
766 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
767
768 RestoreLiveRegisters(codegen, locations);
769
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000770 __ B(GetExitLabel());
771 }
772
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100773 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000774
775 private:
776 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100777 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
778 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000779 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
780 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
781 return Register(VIXLRegCodeFromART(i), kXRegSize);
782 }
783 }
784 // We shall never fail to find a free caller-save register, as
785 // there are more than two core caller-save registers on ARM64
786 // (meaning it is possible to find one which is different from
787 // `ref` and `obj`).
788 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
789 LOG(FATAL) << "Could not find a free register";
790 UNREACHABLE();
791 }
792
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000793 const Location out_;
794 const Location ref_;
795 const Location obj_;
796 const uint32_t offset_;
797 // An additional location containing an index to an array.
798 // Only used for HArrayGet and the UnsafeGetObject &
799 // UnsafeGetObjectVolatile intrinsics.
800 const Location index_;
801
802 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
803};
804
805// Slow path generating a read barrier for a GC root.
806class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
807 public:
808 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000809 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000810 DCHECK(kEmitCompilerReadBarrier);
811 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000812
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100813 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000814 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100815 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000816 DCHECK(locations->CanCall());
817 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Vladimir Markoa41ea272020-09-07 15:24:36 +0000818 DCHECK(instruction_->IsLoadClass() ||
819 instruction_->IsLoadString() ||
820 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000821 << "Unexpected instruction in read barrier for GC root slow path: "
822 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000823
824 __ Bind(GetEntryLabel());
825 SaveLiveRegisters(codegen, locations);
826
827 InvokeRuntimeCallingConvention calling_convention;
828 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
829 // The argument of the ReadBarrierForRootSlow is not a managed
830 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
831 // thus we need a 64-bit move here, and we cannot use
832 //
833 // arm64_codegen->MoveLocation(
834 // LocationFrom(calling_convention.GetRegisterAt(0)),
835 // root_,
836 // type);
837 //
838 // which would emit a 32-bit move, as `type` is a (32-bit wide)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100839 // reference type (`DataType::Type::kReference`).
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000840 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000841 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000842 instruction_,
843 instruction_->GetDexPc(),
844 this);
845 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
846 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
847
848 RestoreLiveRegisters(codegen, locations);
849 __ B(GetExitLabel());
850 }
851
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100852 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000853
854 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000855 const Location out_;
856 const Location root_;
857
858 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
859};
860
Alexandre Rames5319def2014-10-23 10:03:10 +0100861#undef __
862
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100863Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100864 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100865 if (type == DataType::Type::kVoid) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100866 LOG(FATAL) << "Unreachable type " << type;
867 }
868
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100869 if (DataType::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100870 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
871 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100872 } else if (!DataType::IsFloatingPointType(type) &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000873 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000874 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
875 } else {
876 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100877 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
878 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100879 }
880
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000881 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100882 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100883 return next_location;
884}
885
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100886Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100887 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100888}
889
Vladimir Marko86c87522020-05-11 16:55:55 +0100890Location CriticalNativeCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
891 DCHECK_NE(type, DataType::Type::kReference);
892
893 Location location = Location::NoLocation();
894 if (DataType::IsFloatingPointType(type)) {
895 if (fpr_index_ < kParameterFPRegistersLength) {
896 location = LocationFrom(kParameterFPRegisters[fpr_index_]);
897 ++fpr_index_;
898 }
899 } else {
900 // Native ABI uses the same registers as managed, except that the method register x0
901 // is a normal argument.
902 if (gpr_index_ < 1u + kParameterCoreRegistersLength) {
903 location = LocationFrom(gpr_index_ == 0u ? x0 : kParameterCoreRegisters[gpr_index_ - 1u]);
904 ++gpr_index_;
905 }
906 }
907 if (location.IsInvalid()) {
908 if (DataType::Is64BitType(type)) {
909 location = Location::DoubleStackSlot(stack_offset_);
910 } else {
911 location = Location::StackSlot(stack_offset_);
912 }
913 stack_offset_ += kFramePointerSize;
914
915 if (for_register_allocation_) {
916 location = Location::Any();
917 }
918 }
919 return location;
920}
921
922Location CriticalNativeCallingConventionVisitorARM64::GetReturnLocation(DataType::Type type) const {
923 // We perform conversion to the managed ABI return register after the call if needed.
924 InvokeDexCallingConventionVisitorARM64 dex_calling_convention;
925 return dex_calling_convention.GetReturnLocation(type);
926}
927
928Location CriticalNativeCallingConventionVisitorARM64::GetMethodLocation() const {
929 // Pass the method in the hidden argument x15.
930 return Location::RegisterLocation(x15.GetCode());
931}
932
Serban Constantinescu579885a2015-02-22 20:51:33 +0000933CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100934 const CompilerOptions& compiler_options,
935 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100936 : CodeGenerator(graph,
937 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000938 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000939 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100940 callee_saved_core_registers.GetList(),
941 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100942 compiler_options,
943 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100944 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
945 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov1a719e42019-07-18 14:24:55 +0100946 location_builder_neon_(graph, this),
947 instruction_visitor_neon_(graph, this),
948 location_builder_sve_(graph, this),
949 instruction_visitor_sve_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100950 move_resolver_(graph->GetAllocator(), this),
Artem Serovaa6f4832018-11-21 18:57:54 +0000951 assembler_(graph->GetAllocator(),
952 compiler_options.GetInstructionSetFeatures()->AsArm64InstructionSetFeatures()),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000953 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100954 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000955 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100956 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko8f63f102020-09-28 12:10:28 +0100957 public_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
958 package_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000959 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100960 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeb9eb002020-10-02 13:54:19 +0100961 boot_image_jni_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +0100962 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100963 call_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100964 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100965 uint32_literals_(std::less<uint32_t>(),
966 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
967 uint64_literals_(std::less<uint64_t>(),
968 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000969 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100970 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000971 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +0000972 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
973 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
974 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000975 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000976 AddAllocatedRegister(LocationFrom(lr));
Artem Serov1a719e42019-07-18 14:24:55 +0100977
978 bool use_sve = ShouldUseSVE();
979 if (use_sve) {
980 location_builder_ = &location_builder_sve_;
981 instruction_visitor_ = &instruction_visitor_sve_;
982 } else {
983 location_builder_ = &location_builder_neon_;
984 instruction_visitor_ = &instruction_visitor_neon_;
985 }
986}
987
988bool CodeGeneratorARM64::ShouldUseSVE() const {
989 return kArm64AllowSVE && GetInstructionSetFeatures().HasSVE();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000990}
Alexandre Rames5319def2014-10-23 10:03:10 +0100991
Alexandre Rames67555f72014-11-18 10:55:16 +0000992#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100993
Zheng Xu3927c8b2015-11-18 17:46:25 +0800994void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100995 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800996 jump_table->EmitTable(this);
997 }
998}
999
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001000void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001001 EmitJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +00001002
1003 // Emit JIT baker read barrier slow paths.
Vladimir Marko695348f2020-05-19 14:42:02 +01001004 DCHECK(GetCompilerOptions().IsJitCompiler() || jit_baker_read_barrier_slow_paths_.empty());
Vladimir Marko966b46f2018-08-03 10:20:19 +00001005 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
1006 uint32_t encoded_data = entry.first;
1007 vixl::aarch64::Label* slow_path_entry = &entry.second.label;
1008 __ Bind(slow_path_entry);
Andreas Gampe3db70682018-12-26 15:12:03 -08001009 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name= */ nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00001010 }
1011
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001012 // Ensure we emit the literal pool.
1013 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001014
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001015 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001016
1017 // Verify Baker read barrier linker patches.
1018 if (kIsDebugBuild) {
1019 ArrayRef<const uint8_t> code = allocator->GetMemory();
1020 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
1021 DCHECK(info.label.IsBound());
1022 uint32_t literal_offset = info.label.GetLocation();
1023 DCHECK_ALIGNED(literal_offset, 4u);
1024
1025 auto GetInsn = [&code](uint32_t offset) {
1026 DCHECK_ALIGNED(offset, 4u);
1027 return
1028 (static_cast<uint32_t>(code[offset + 0]) << 0) +
1029 (static_cast<uint32_t>(code[offset + 1]) << 8) +
1030 (static_cast<uint32_t>(code[offset + 2]) << 16)+
1031 (static_cast<uint32_t>(code[offset + 3]) << 24);
1032 };
1033
1034 const uint32_t encoded_data = info.custom_data;
1035 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
1036 // Check that the next instruction matches the expected LDR.
1037 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01001038 case BakerReadBarrierKind::kField:
1039 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00001040 DCHECK_GE(code.size() - literal_offset, 8u);
1041 uint32_t next_insn = GetInsn(literal_offset + 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001042 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1043 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko0ecac682018-08-07 10:40:38 +01001044 if (kind == BakerReadBarrierKind::kField) {
1045 // LDR (immediate) with correct base_reg.
1046 CHECK_EQ(next_insn & 0xffc003e0u, 0xb9400000u | (base_reg << 5));
1047 } else {
1048 DCHECK(kind == BakerReadBarrierKind::kAcquire);
1049 // LDAR with correct base_reg.
1050 CHECK_EQ(next_insn & 0xffffffe0u, 0x88dffc00u | (base_reg << 5));
1051 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001052 break;
1053 }
1054 case BakerReadBarrierKind::kArray: {
1055 DCHECK_GE(code.size() - literal_offset, 8u);
1056 uint32_t next_insn = GetInsn(literal_offset + 4u);
1057 // LDR (register) with the correct base_reg, size=10 (32-bit), option=011 (extend = LSL),
1058 // and S=1 (shift amount = 2 for 32-bit version), i.e. LDR Wt, [Xn, Xm, LSL #2].
1059 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1060 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1061 CHECK_EQ(next_insn & 0xffe0ffe0u, 0xb8607800u | (base_reg << 5));
1062 CheckValidReg((next_insn >> 16) & 0x1f); // Check index register
1063 break;
1064 }
1065 case BakerReadBarrierKind::kGcRoot: {
1066 DCHECK_GE(literal_offset, 4u);
1067 uint32_t prev_insn = GetInsn(literal_offset - 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001068 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko94796f82018-08-08 15:15:33 +01001069 // Usually LDR (immediate) with correct root_reg but
1070 // we may have a "MOV marked, old_value" for UnsafeCASObject.
1071 if ((prev_insn & 0xffe0ffff) != (0x2a0003e0 | root_reg)) { // MOV?
1072 CHECK_EQ(prev_insn & 0xffc0001fu, 0xb9400000u | root_reg); // LDR?
1073 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001074 break;
1075 }
1076 default:
1077 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
1078 UNREACHABLE();
1079 }
1080 }
1081 }
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001082}
1083
Zheng Xuad4450e2015-04-17 18:48:56 +08001084void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1085 // Note: There are 6 kinds of moves:
1086 // 1. constant -> GPR/FPR (non-cycle)
1087 // 2. constant -> stack (non-cycle)
1088 // 3. GPR/FPR -> GPR/FPR
1089 // 4. GPR/FPR -> stack
1090 // 5. stack -> GPR/FPR
1091 // 6. stack -> stack (non-cycle)
1092 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1093 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1094 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1095 // dependency.
1096 vixl_temps_.Open(GetVIXLAssembler());
1097}
1098
1099void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1100 vixl_temps_.Close();
1101}
1102
1103Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001104 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1105 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1106 || kind == Location::kSIMDStackSlot);
1107 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1108 ? Location::kFpuRegister
1109 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001110 Location scratch = GetScratchLocation(kind);
1111 if (!scratch.Equals(Location::NoLocation())) {
1112 return scratch;
1113 }
1114 // Allocate from VIXL temp registers.
1115 if (kind == Location::kRegister) {
1116 scratch = LocationFrom(vixl_temps_.AcquireX());
1117 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001118 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serov1a719e42019-07-18 14:24:55 +01001119 scratch = codegen_->GetGraph()->HasSIMD()
1120 ? codegen_->GetInstructionCodeGeneratorArm64()->AllocateSIMDScratchLocation(&vixl_temps_)
1121 : LocationFrom(vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001122 }
1123 AddScratchLocation(scratch);
1124 return scratch;
1125}
1126
1127void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1128 if (loc.IsRegister()) {
1129 vixl_temps_.Release(XRegisterFrom(loc));
1130 } else {
1131 DCHECK(loc.IsFpuRegister());
Artem Serov1a719e42019-07-18 14:24:55 +01001132 if (codegen_->GetGraph()->HasSIMD()) {
1133 codegen_->GetInstructionCodeGeneratorArm64()->FreeSIMDScratchLocation(loc, &vixl_temps_);
1134 } else {
1135 vixl_temps_.Release(DRegisterFrom(loc));
1136 }
Zheng Xuad4450e2015-04-17 18:48:56 +08001137 }
1138 RemoveScratchLocation(loc);
1139}
1140
Alexandre Rames3e69f162014-12-10 10:36:50 +00001141void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001142 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001143 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001144}
1145
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001146void CodeGeneratorARM64::MaybeIncrementHotness(bool is_frame_entry) {
1147 MacroAssembler* masm = GetVIXLAssembler();
1148 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1149 UseScratchRegisterScope temps(masm);
1150 Register counter = temps.AcquireX();
1151 Register method = is_frame_entry ? kArtMethodRegister : temps.AcquireX();
1152 if (!is_frame_entry) {
1153 __ Ldr(method, MemOperand(sp, 0));
1154 }
1155 __ Ldrh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
1156 __ Add(counter, counter, 1);
1157 // Subtract one if the counter would overflow.
1158 __ Sub(counter, counter, Operand(counter, LSR, 16));
1159 __ Strh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
1160 }
1161
1162 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffray095dc462020-08-17 16:40:28 +01001163 ScopedProfilingInfoUse spiu(
1164 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
1165 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001166 if (info != nullptr) {
Nicolas Geoffrayc1cd1332020-01-25 13:08:24 +00001167 uint64_t address = reinterpret_cast64<uint64_t>(info);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001168 vixl::aarch64::Label done;
1169 UseScratchRegisterScope temps(masm);
1170 Register temp = temps.AcquireX();
1171 Register counter = temps.AcquireW();
1172 __ Mov(temp, address);
1173 __ Ldrh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
1174 __ Add(counter, counter, 1);
Nicolas Geoffray4313ccb2020-08-26 17:01:15 +01001175 __ And(counter, counter, interpreter::kTieredHotnessMask);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001176 __ Strh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
Nicolas Geoffray4313ccb2020-08-26 17:01:15 +01001177 __ Cbnz(counter, &done);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001178 if (is_frame_entry) {
1179 if (HasEmptyFrame()) {
Vladimir Markodec78172020-06-19 15:31:23 +01001180 // The entrypoint expects the method at the bottom of the stack. We
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001181 // claim stack space necessary for alignment.
Vladimir Markodec78172020-06-19 15:31:23 +01001182 IncreaseFrame(kStackAlignment);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001183 __ Stp(kArtMethodRegister, lr, MemOperand(sp, 0));
1184 } else if (!RequiresCurrentMethod()) {
1185 __ Str(kArtMethodRegister, MemOperand(sp, 0));
1186 }
1187 } else {
1188 CHECK(RequiresCurrentMethod());
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001189 }
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001190 uint32_t entrypoint_offset =
1191 GetThreadOffset<kArm64PointerSize>(kQuickCompileOptimized).Int32Value();
1192 __ Ldr(lr, MemOperand(tr, entrypoint_offset));
1193 // Note: we don't record the call here (and therefore don't generate a stack
1194 // map), as the entrypoint should never be suspended.
1195 __ Blr(lr);
1196 if (HasEmptyFrame()) {
1197 CHECK(is_frame_entry);
1198 __ Ldr(lr, MemOperand(sp, 8));
Vladimir Markodec78172020-06-19 15:31:23 +01001199 DecreaseFrame(kStackAlignment);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001200 }
1201 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001202 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001203 }
1204}
1205
Alexandre Rames5319def2014-10-23 10:03:10 +01001206void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001207 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001208 __ Bind(&frame_entry_label_);
1209
Vladimir Marko33bff252017-11-01 14:35:42 +00001210 bool do_overflow_check =
1211 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001212 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001213 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001214 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001215 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001216 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001217 {
1218 // Ensure that between load and RecordPcInfo there are no pools emitted.
1219 ExactAssemblyScope eas(GetVIXLAssembler(),
1220 kInstructionSize,
1221 CodeBufferCheckScope::kExactSize);
1222 __ ldr(wzr, MemOperand(temp, 0));
1223 RecordPcInfo(nullptr, 0);
1224 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001225 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001226
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001227 if (!HasEmptyFrame()) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001228 // Stack layout:
1229 // sp[frame_size - 8] : lr.
1230 // ... : other preserved core registers.
1231 // ... : other preserved fp registers.
1232 // ... : reserved frame space.
1233 // sp[0] : current method.
Vladimir Marko1a225a72019-07-05 13:37:42 +01001234 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1235 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1236 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1237 DCHECK(!preserved_core_registers.IsEmpty());
1238 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1239 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001240
Vladimir Marko1a225a72019-07-05 13:37:42 +01001241 // Save the current method if we need it, or if using STP reduces code
1242 // size. Note that we do not do this in HCurrentMethod, as the
1243 // instruction might have been removed in the SSA graph.
1244 CPURegister lowest_spill;
1245 if (core_spills_offset == kXRegSizeInBytes) {
1246 // If there is no gap between the method and the lowest core spill, use
1247 // aligned STP pre-index to store both. Max difference is 512. We do
1248 // that to reduce code size even if we do not have to save the method.
1249 DCHECK_LE(frame_size, 512); // 32 core registers are only 256 bytes.
1250 lowest_spill = preserved_core_registers.PopLowestIndex();
1251 __ Stp(kArtMethodRegister, lowest_spill, MemOperand(sp, -frame_size, PreIndex));
1252 } else if (RequiresCurrentMethod()) {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001253 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001254 } else {
1255 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001256 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001257 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Vladimir Marko1a225a72019-07-05 13:37:42 +01001258 if (lowest_spill.IsValid()) {
1259 GetAssembler()->cfi().RelOffset(DWARFReg(lowest_spill), core_spills_offset);
1260 core_spills_offset += kXRegSizeInBytes;
1261 }
1262 GetAssembler()->SpillRegisters(preserved_core_registers, core_spills_offset);
1263 GetAssembler()->SpillRegisters(preserved_fp_registers, fp_spills_offset);
Mingyao Yang063fc772016-08-02 11:02:54 -07001264
1265 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1266 // Initialize should_deoptimize flag to 0.
1267 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1268 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1269 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001270 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001271 MaybeIncrementHotness(/* is_frame_entry= */ true);
Andreas Gampe3db70682018-12-26 15:12:03 -08001272 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001273}
1274
1275void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001276 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001277 if (!HasEmptyFrame()) {
Vladimir Marko1a225a72019-07-05 13:37:42 +01001278 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1279 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1280 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1281 DCHECK(!preserved_core_registers.IsEmpty());
1282 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1283 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
1284
1285 CPURegister lowest_spill;
1286 if (core_spills_offset == kXRegSizeInBytes) {
1287 // If there is no gap between the method and the lowest core spill, use
1288 // aligned LDP pre-index to pop both. Max difference is 504. We do
1289 // that to reduce code size even though the loaded method is unused.
1290 DCHECK_LE(frame_size, 504); // 32 core registers are only 256 bytes.
1291 lowest_spill = preserved_core_registers.PopLowestIndex();
1292 core_spills_offset += kXRegSizeInBytes;
1293 }
1294 GetAssembler()->UnspillRegisters(preserved_fp_registers, fp_spills_offset);
1295 GetAssembler()->UnspillRegisters(preserved_core_registers, core_spills_offset);
1296 if (lowest_spill.IsValid()) {
1297 __ Ldp(xzr, lowest_spill, MemOperand(sp, frame_size, PostIndex));
1298 GetAssembler()->cfi().Restore(DWARFReg(lowest_spill));
1299 } else {
1300 __ Drop(frame_size);
1301 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001302 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001303 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001304 __ Ret();
1305 GetAssembler()->cfi().RestoreState();
1306 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001307}
1308
Scott Wakeling97c72b72016-06-24 16:19:36 +01001309CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001310 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001311 return CPURegList(CPURegister::kRegister, kXRegSize,
1312 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001313}
1314
Scott Wakeling97c72b72016-06-24 16:19:36 +01001315CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001316 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1317 GetNumberOfFloatingPointRegisters()));
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001318 return CPURegList(CPURegister::kVRegister, kDRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001319 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001320}
1321
Alexandre Rames5319def2014-10-23 10:03:10 +01001322void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1323 __ Bind(GetLabelOf(block));
1324}
1325
Calin Juravle175dc732015-08-25 15:42:32 +01001326void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1327 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001328 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001329}
1330
Calin Juravlee460d1d2015-09-29 04:52:17 +01001331void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1332 if (location.IsRegister()) {
1333 locations->AddTemp(location);
1334 } else {
1335 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1336 }
1337}
1338
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001339void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001340 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001341 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001342 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001343 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001344 if (value_can_be_null) {
1345 __ Cbz(value, &done);
1346 }
Roland Levillainc73f0522018-08-14 15:16:50 +01001347 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07001348 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01001349 // Calculate the offset (in the card table) of the card corresponding to
1350 // `object`.
Alexandre Rames5319def2014-10-23 10:03:10 +01001351 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Roland Levillainc73f0522018-08-14 15:16:50 +01001352 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
1353 // `object`'s card.
1354 //
1355 // Register `card` contains the address of the card table. Note that the card
1356 // table's base is biased during its creation so that it always starts at an
1357 // address whose least-significant byte is equal to `kCardDirty` (see
1358 // art::gc::accounting::CardTable::Create). Therefore the STRB instruction
1359 // below writes the `kCardDirty` (byte) value into the `object`'s card
1360 // (located at `card + object >> kCardShift`).
1361 //
1362 // This dual use of the value in register `card` (1. to calculate the location
1363 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
1364 // (no need to explicitly load `kCardDirty` as an immediate value).
Serban Constantinescu02164b32014-11-13 14:05:07 +00001365 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001366 if (value_can_be_null) {
1367 __ Bind(&done);
1368 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001369}
1370
David Brazdil58282f42016-01-14 12:45:10 +00001371void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001372 // Blocked core registers:
1373 // lr : Runtime reserved.
1374 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001375 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001376 // ip1 : VIXL core temp.
1377 // ip0 : VIXL core temp.
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001378 // x18 : Platform register.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001379 //
1380 // Blocked fp registers:
1381 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001382 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1383 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001384 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001385 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001386 }
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001387 blocked_core_registers_[X18] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001388
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001389 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001390 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001391 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001392 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001393
David Brazdil58282f42016-01-14 12:45:10 +00001394 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001395 // Stubs do not save callee-save floating point registers. If the graph
1396 // is debuggable, we need to deal with these registers differently. For
1397 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001398 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1399 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001400 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001401 }
1402 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001403}
1404
Alexandre Rames3e69f162014-12-10 10:36:50 +00001405size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1406 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1407 __ Str(reg, MemOperand(sp, stack_index));
1408 return kArm64WordSize;
1409}
1410
1411size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1412 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1413 __ Ldr(reg, MemOperand(sp, stack_index));
1414 return kArm64WordSize;
1415}
1416
Artem Serov9df37b92019-07-23 16:41:54 +01001417size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1418 uint32_t reg_id ATTRIBUTE_UNUSED) {
1419 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1420 << "use SaveRestoreLiveRegistersHelper";
1421 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001422}
1423
Artem Serov9df37b92019-07-23 16:41:54 +01001424size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1425 uint32_t reg_id ATTRIBUTE_UNUSED) {
1426 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1427 << "use SaveRestoreLiveRegistersHelper";
1428 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001429}
1430
Alexandre Rames5319def2014-10-23 10:03:10 +01001431void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001432 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001433}
1434
1435void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001436 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001437}
1438
Vladimir Markoa0431112018-06-25 09:32:54 +01001439const Arm64InstructionSetFeatures& CodeGeneratorARM64::GetInstructionSetFeatures() const {
1440 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArm64InstructionSetFeatures();
1441}
1442
Alexandre Rames67555f72014-11-18 10:55:16 +00001443void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001444 if (constant->IsIntConstant()) {
1445 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1446 } else if (constant->IsLongConstant()) {
1447 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1448 } else if (constant->IsNullConstant()) {
1449 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001450 } else if (constant->IsFloatConstant()) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001451 __ Fmov(VRegister(destination), constant->AsFloatConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001452 } else {
1453 DCHECK(constant->IsDoubleConstant());
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001454 __ Fmov(VRegister(destination), constant->AsDoubleConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001455 }
1456}
1457
Alexandre Rames3e69f162014-12-10 10:36:50 +00001458
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001459static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001460 DCHECK(constant.IsConstant());
1461 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001462 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001463 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001464 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1465 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1466 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1467 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001468}
1469
Roland Levillain952b2352017-05-03 19:49:14 +01001470// Allocate a scratch register from the VIXL pool, querying first
1471// the floating-point register pool, and then the core register
1472// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001473// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1474// using a different allocation strategy.
1475static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1476 vixl::aarch64::UseScratchRegisterScope* temps,
1477 int size_in_bits) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001478 return masm->GetScratchVRegisterList()->IsEmpty()
Roland Levillain558dea12017-01-27 19:40:44 +00001479 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1480 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1481}
1482
Calin Juravlee460d1d2015-09-29 04:52:17 +01001483void CodeGeneratorARM64::MoveLocation(Location destination,
1484 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001485 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001486 if (source.Equals(destination)) {
1487 return;
1488 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001489
1490 // A valid move can always be inferred from the destination and source
1491 // locations. When moving from and to a register, the argument type can be
1492 // used to generate 32bit instead of 64bit moves. In debug mode we also
1493 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001494 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001495
1496 if (destination.IsRegister() || destination.IsFpuRegister()) {
1497 if (unspecified_type) {
1498 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1499 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001500 (src_cst != nullptr && (src_cst->IsIntConstant()
1501 || src_cst->IsFloatConstant()
1502 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001503 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001504 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001505 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001506 // If the source is a double stack slot or a 64bit constant, a 64bit
1507 // type is appropriate. Else the source is a register, and since the
1508 // type has not been specified, we chose a 64bit type to force a 64bit
1509 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001510 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001511 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001512 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001513 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1514 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001515 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001516 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1517 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1518 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001519 } else if (source.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001520 GetInstructionCodeGeneratorArm64()->LoadSIMDRegFromStack(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001521 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001522 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001523 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001524 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001525 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001526 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001527 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001528 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001529 DataType::Type source_type = DataType::Is64BitType(dst_type)
1530 ? DataType::Type::kInt64
1531 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001532 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1533 }
1534 } else {
1535 DCHECK(source.IsFpuRegister());
1536 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001537 DataType::Type source_type = DataType::Is64BitType(dst_type)
1538 ? DataType::Type::kFloat64
1539 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001540 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1541 } else {
1542 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001543 if (GetGraph()->HasSIMD()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001544 GetInstructionCodeGeneratorArm64()->MoveSIMDRegToSIMDReg(destination, source);
Artem Serovd4bccf12017-04-03 18:47:32 +01001545 } else {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001546 __ Fmov(VRegister(dst), FPRegisterFrom(source, dst_type));
Artem Serovd4bccf12017-04-03 18:47:32 +01001547 }
1548 }
1549 }
1550 } else if (destination.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001551 GetInstructionCodeGeneratorArm64()->MoveToSIMDStackSlot(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001552 } else { // The destination is not a register. It must be a stack slot.
1553 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1554 if (source.IsRegister() || source.IsFpuRegister()) {
1555 if (unspecified_type) {
1556 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001557 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001558 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001559 dst_type =
1560 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001561 }
1562 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001563 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1564 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001565 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001566 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001567 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1568 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001569 UseScratchRegisterScope temps(GetVIXLAssembler());
1570 HConstant* src_cst = source.GetConstant();
1571 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001572 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001573 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1574 ? Register(xzr)
1575 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001576 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001577 if (src_cst->IsIntConstant()) {
1578 temp = temps.AcquireW();
1579 } else if (src_cst->IsLongConstant()) {
1580 temp = temps.AcquireX();
1581 } else if (src_cst->IsFloatConstant()) {
1582 temp = temps.AcquireS();
1583 } else {
1584 DCHECK(src_cst->IsDoubleConstant());
1585 temp = temps.AcquireD();
1586 }
1587 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001588 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001589 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001590 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001591 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001592 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001593 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001594 // Use any scratch register (a core or a floating-point one)
1595 // from VIXL scratch register pools as a temporary.
1596 //
1597 // We used to only use the FP scratch register pool, but in some
1598 // rare cases the only register from this pool (D31) would
1599 // already be used (e.g. within a ParallelMove instruction, when
1600 // a move is blocked by a another move requiring a scratch FP
1601 // register, which would reserve D31). To prevent this issue, we
1602 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001603 //
1604 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001605 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001606 // use AcquireFPOrCoreCPURegisterOfSize instead of
1607 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1608 // allocates core scratch registers first.
1609 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1610 GetVIXLAssembler(),
1611 &temps,
1612 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001613 __ Ldr(temp, StackOperandFrom(source));
1614 __ Str(temp, StackOperandFrom(destination));
1615 }
1616 }
1617}
1618
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001619void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001620 CPURegister dst,
1621 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001622 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001623 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001624 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001625 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001626 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001627 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001628 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001629 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001630 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001631 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001632 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001633 case DataType::Type::kInt16:
1634 __ Ldrsh(Register(dst), src);
1635 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001636 case DataType::Type::kInt32:
1637 case DataType::Type::kReference:
1638 case DataType::Type::kInt64:
1639 case DataType::Type::kFloat32:
1640 case DataType::Type::kFloat64:
1641 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001642 __ Ldr(dst, src);
1643 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001644 case DataType::Type::kUint32:
1645 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001646 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001647 LOG(FATAL) << "Unreachable type " << type;
1648 }
1649}
1650
Calin Juravle77520bc2015-01-12 18:45:46 +00001651void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001652 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001653 const MemOperand& src,
1654 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001655 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001656 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001657 Register temp_base = temps.AcquireX();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001658 DataType::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001659
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001660 DCHECK(!src.IsPreIndex());
1661 DCHECK(!src.IsPostIndex());
1662
1663 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001664 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001665 {
1666 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1667 MemOperand base = MemOperand(temp_base);
1668 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001669 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001670 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001671 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001672 {
1673 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1674 __ ldarb(Register(dst), base);
1675 if (needs_null_check) {
1676 MaybeRecordImplicitNullCheck(instruction);
1677 }
1678 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001679 if (type == DataType::Type::kInt8) {
1680 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001681 }
1682 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001683 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001684 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001685 {
1686 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1687 __ ldarh(Register(dst), base);
1688 if (needs_null_check) {
1689 MaybeRecordImplicitNullCheck(instruction);
1690 }
1691 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001692 if (type == DataType::Type::kInt16) {
1693 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
1694 }
Artem Serov914d7a82017-02-07 14:33:49 +00001695 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001696 case DataType::Type::kInt32:
1697 case DataType::Type::kReference:
1698 case DataType::Type::kInt64:
1699 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001700 {
1701 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1702 __ ldar(Register(dst), base);
1703 if (needs_null_check) {
1704 MaybeRecordImplicitNullCheck(instruction);
1705 }
1706 }
1707 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001708 case DataType::Type::kFloat32:
1709 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00001710 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001711 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001712
Artem Serov914d7a82017-02-07 14:33:49 +00001713 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1714 {
1715 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1716 __ ldar(temp, base);
1717 if (needs_null_check) {
1718 MaybeRecordImplicitNullCheck(instruction);
1719 }
1720 }
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001721 __ Fmov(VRegister(dst), temp);
Artem Serov914d7a82017-02-07 14:33:49 +00001722 break;
Roland Levillain44015862016-01-22 11:47:17 +00001723 }
Aart Bik66c158e2018-01-31 12:55:04 -08001724 case DataType::Type::kUint32:
1725 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001726 case DataType::Type::kVoid:
Artem Serov914d7a82017-02-07 14:33:49 +00001727 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001728 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001729 }
1730}
1731
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001732void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001733 CPURegister src,
1734 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001735 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001736 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001737 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001738 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001739 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001740 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001741 case DataType::Type::kUint16:
1742 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001743 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001744 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001745 case DataType::Type::kInt32:
1746 case DataType::Type::kReference:
1747 case DataType::Type::kInt64:
1748 case DataType::Type::kFloat32:
1749 case DataType::Type::kFloat64:
1750 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001751 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001752 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001753 case DataType::Type::kUint32:
1754 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001755 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001756 LOG(FATAL) << "Unreachable type " << type;
1757 }
1758}
1759
Artem Serov914d7a82017-02-07 14:33:49 +00001760void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001761 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001762 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00001763 const MemOperand& dst,
1764 bool needs_null_check) {
1765 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001766 UseScratchRegisterScope temps(GetVIXLAssembler());
1767 Register temp_base = temps.AcquireX();
1768
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001769 DCHECK(!dst.IsPreIndex());
1770 DCHECK(!dst.IsPostIndex());
1771
1772 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001773 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001774 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001775 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00001776 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001777 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001778 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001779 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001780 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001781 {
1782 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1783 __ stlrb(Register(src), base);
1784 if (needs_null_check) {
1785 MaybeRecordImplicitNullCheck(instruction);
1786 }
1787 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001788 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001789 case DataType::Type::kUint16:
1790 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001791 {
1792 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1793 __ stlrh(Register(src), base);
1794 if (needs_null_check) {
1795 MaybeRecordImplicitNullCheck(instruction);
1796 }
1797 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001798 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001799 case DataType::Type::kInt32:
1800 case DataType::Type::kReference:
1801 case DataType::Type::kInt64:
1802 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001803 {
1804 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1805 __ stlr(Register(src), base);
1806 if (needs_null_check) {
1807 MaybeRecordImplicitNullCheck(instruction);
1808 }
1809 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001810 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001811 case DataType::Type::kFloat32:
1812 case DataType::Type::kFloat64: {
1813 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001814 Register temp_src;
1815 if (src.IsZero()) {
1816 // The zero register is used to avoid synthesizing zero constants.
1817 temp_src = Register(src);
1818 } else {
1819 DCHECK(src.IsFPRegister());
1820 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001821 __ Fmov(temp_src, VRegister(src));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001822 }
Artem Serov914d7a82017-02-07 14:33:49 +00001823 {
1824 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1825 __ stlr(temp_src, base);
1826 if (needs_null_check) {
1827 MaybeRecordImplicitNullCheck(instruction);
1828 }
1829 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001830 break;
1831 }
Aart Bik66c158e2018-01-31 12:55:04 -08001832 case DataType::Type::kUint32:
1833 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001834 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001835 LOG(FATAL) << "Unreachable type " << type;
1836 }
1837}
1838
Calin Juravle175dc732015-08-25 15:42:32 +01001839void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1840 HInstruction* instruction,
1841 uint32_t dex_pc,
1842 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001843 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00001844
Vladimir Markof6675082019-05-17 12:05:28 +01001845 ThreadOffset64 entrypoint_offset = GetThreadOffset<kArm64PointerSize>(entrypoint);
1846 // Reduce code size for AOT by using shared trampolines for slow path runtime calls across the
1847 // entire oat file. This adds an extra branch and we do not want to slow down the main path.
1848 // For JIT, thunk sharing is per-method, so the gains would be smaller or even negative.
Vladimir Marko695348f2020-05-19 14:42:02 +01001849 if (slow_path == nullptr || GetCompilerOptions().IsJitCompiler()) {
Vladimir Markof6675082019-05-17 12:05:28 +01001850 __ Ldr(lr, MemOperand(tr, entrypoint_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00001851 // Ensure the pc position is recorded immediately after the `blr` instruction.
1852 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1853 __ blr(lr);
1854 if (EntrypointRequiresStackMap(entrypoint)) {
1855 RecordPcInfo(instruction, dex_pc, slow_path);
1856 }
Vladimir Markof6675082019-05-17 12:05:28 +01001857 } else {
1858 // Ensure the pc position is recorded immediately after the `bl` instruction.
1859 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1860 EmitEntrypointThunkCall(entrypoint_offset);
1861 if (EntrypointRequiresStackMap(entrypoint)) {
1862 RecordPcInfo(instruction, dex_pc, slow_path);
1863 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001864 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001865}
1866
Roland Levillaindec8f632016-07-22 17:10:06 +01001867void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1868 HInstruction* instruction,
1869 SlowPathCode* slow_path) {
1870 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01001871 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1872 __ Blr(lr);
1873}
1874
Alexandre Rames67555f72014-11-18 10:55:16 +00001875void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001876 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001877 UseScratchRegisterScope temps(GetVIXLAssembler());
1878 Register temp = temps.AcquireW();
Vladimir Markodc682aa2018-01-04 18:42:57 +00001879 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001880 const size_t status_byte_offset =
1881 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1882 constexpr uint32_t shifted_visibly_initialized_value =
1883 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001884
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001885 // CMP (immediate) is limited to imm12 or imm12<<12, so we would need to materialize
1886 // the constant 0xf0000000 for comparison with the full 32-bit field. To reduce the code
1887 // size, load only the high byte of the field and compare with 0xf0.
1888 // Note: The same code size could be achieved with LDR+MNV(asr #24)+CBNZ but benchmarks
1889 // show that this pattern is slower (tested on little cores).
1890 __ Ldrb(temp, HeapOperand(class_reg, status_byte_offset));
1891 __ Cmp(temp, shifted_visibly_initialized_value);
1892 __ B(lo, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001893 __ Bind(slow_path->GetExitLabel());
1894}
Alexandre Rames5319def2014-10-23 10:03:10 +01001895
Vladimir Marko175e7862018-03-27 09:03:13 +00001896void InstructionCodeGeneratorARM64::GenerateBitstringTypeCheckCompare(
1897 HTypeCheckInstruction* check, vixl::aarch64::Register temp) {
1898 uint32_t path_to_root = check->GetBitstringPathToRoot();
1899 uint32_t mask = check->GetBitstringMask();
1900 DCHECK(IsPowerOfTwo(mask + 1));
1901 size_t mask_bits = WhichPowerOf2(mask + 1);
1902
1903 if (mask_bits == 16u) {
1904 // Load only the bitstring part of the status word.
1905 __ Ldrh(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1906 } else {
1907 // /* uint32_t */ temp = temp->status_
1908 __ Ldr(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1909 // Extract the bitstring bits.
1910 __ Ubfx(temp, temp, 0, mask_bits);
1911 }
1912 // Compare the bitstring bits to `path_to_root`.
1913 __ Cmp(temp, path_to_root);
1914}
1915
Roland Levillain44015862016-01-22 11:47:17 +00001916void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001917 BarrierType type = BarrierAll;
1918
1919 switch (kind) {
1920 case MemBarrierKind::kAnyAny:
1921 case MemBarrierKind::kAnyStore: {
1922 type = BarrierAll;
1923 break;
1924 }
1925 case MemBarrierKind::kLoadAny: {
1926 type = BarrierReads;
1927 break;
1928 }
1929 case MemBarrierKind::kStoreStore: {
1930 type = BarrierWrites;
1931 break;
1932 }
1933 default:
1934 LOG(FATAL) << "Unexpected memory barrier " << kind;
1935 }
1936 __ Dmb(InnerShareable, type);
1937}
1938
Serban Constantinescu02164b32014-11-13 14:05:07 +00001939void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1940 HBasicBlock* successor) {
1941 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001942 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1943 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001944 slow_path =
1945 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001946 instruction->SetSlowPath(slow_path);
1947 codegen_->AddSlowPath(slow_path);
1948 if (successor != nullptr) {
1949 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001950 }
1951 } else {
1952 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1953 }
1954
Serban Constantinescu02164b32014-11-13 14:05:07 +00001955 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1956 Register temp = temps.AcquireW();
1957
Andreas Gampe542451c2016-07-26 09:02:02 -07001958 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001959 if (successor == nullptr) {
1960 __ Cbnz(temp, slow_path->GetEntryLabel());
1961 __ Bind(slow_path->GetReturnLabel());
1962 } else {
1963 __ Cbz(temp, codegen_->GetLabelOf(successor));
1964 __ B(slow_path->GetEntryLabel());
1965 // slow_path will return to GetLabelOf(successor).
1966 }
1967}
1968
Alexandre Rames5319def2014-10-23 10:03:10 +01001969InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1970 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001971 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001972 assembler_(codegen->GetAssembler()),
1973 codegen_(codegen) {}
1974
Alexandre Rames67555f72014-11-18 10:55:16 +00001975void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001976 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001977 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001978 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001979 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001980 case DataType::Type::kInt32:
1981 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01001982 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001983 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001984 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001985 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001986
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001987 case DataType::Type::kFloat32:
1988 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001989 locations->SetInAt(0, Location::RequiresFpuRegister());
1990 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001991 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001992 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001993
Alexandre Rames5319def2014-10-23 10:03:10 +01001994 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001995 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001996 }
1997}
1998
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001999void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2000 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002001 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2002
2003 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002004 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01002005 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002006 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2007 object_field_get_with_read_barrier
2008 ? LocationSummary::kCallOnSlowPath
2009 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002010 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002011 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko0ecac682018-08-07 10:40:38 +01002012 // We need a temporary register for the read barrier load in
2013 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2014 // only if the field is volatile or the offset is too big.
2015 if (field_info.IsVolatile() ||
2016 field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2017 locations->AddTemp(FixedTempLocation());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002018 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002019 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002020 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002021 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002022 locations->SetOut(Location::RequiresFpuRegister());
2023 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002024 // The output overlaps for an object field get when read barriers
2025 // are enabled: we do not want the load to overwrite the object's
2026 // location, as we need it to emit the read barrier.
2027 locations->SetOut(
2028 Location::RequiresRegister(),
2029 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002030 }
2031}
2032
2033void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2034 const FieldInfo& field_info) {
2035 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002036 LocationSummary* locations = instruction->GetLocations();
2037 Location base_loc = locations->InAt(0);
2038 Location out = locations->Out();
2039 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01002040 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
2041 DataType::Type load_type = instruction->GetType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002042 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002043
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002044 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01002045 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002046 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002047 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002048 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002049 Location maybe_temp =
2050 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002051 // Note that potential implicit null checks are handled in this
2052 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2053 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2054 instruction,
2055 out,
2056 base,
2057 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002058 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002059 /* needs_null_check= */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002060 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002061 } else {
2062 // General case.
2063 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002064 // Note that a potential implicit null check is handled in this
2065 // CodeGeneratorARM64::LoadAcquire call.
2066 // NB: LoadAcquire will record the pc info if needed.
2067 codegen_->LoadAcquire(
Andreas Gampe3db70682018-12-26 15:12:03 -08002068 instruction, OutputCPURegister(instruction), field, /* needs_null_check= */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002069 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002070 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2071 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01002072 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002073 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002074 }
Vladimir Marko61b92282017-10-11 13:23:17 +01002075 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002076 // If read barriers are enabled, emit read barriers other than
2077 // Baker's using a slow path (and also unpoison the loaded
2078 // reference, if heap poisoning is enabled).
2079 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2080 }
Roland Levillain4d027112015-07-01 15:41:14 +01002081 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002082}
2083
2084void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2085 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002086 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01002087 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002088 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2089 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002090 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002091 locations->SetInAt(1, Location::RequiresFpuRegister());
2092 } else {
2093 locations->SetInAt(1, Location::RequiresRegister());
2094 }
2095}
2096
2097void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002098 const FieldInfo& field_info,
2099 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002100 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2101
2102 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002103 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002104 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002105 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002106 DataType::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002107
Roland Levillain4d027112015-07-01 15:41:14 +01002108 {
2109 // We use a block to end the scratch scope before the write barrier, thus
2110 // freeing the temporary registers so they can be used in `MarkGCCard`.
2111 UseScratchRegisterScope temps(GetVIXLAssembler());
2112
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002113 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002114 DCHECK(value.IsW());
2115 Register temp = temps.AcquireW();
2116 __ Mov(temp, value.W());
2117 GetAssembler()->PoisonHeapReference(temp.W());
2118 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002119 }
Roland Levillain4d027112015-07-01 15:41:14 +01002120
2121 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002122 codegen_->StoreRelease(
Andreas Gampe3db70682018-12-26 15:12:03 -08002123 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check= */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002124 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002125 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2126 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002127 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2128 codegen_->MaybeRecordImplicitNullCheck(instruction);
2129 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002130 }
2131
2132 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002133 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002134 }
2135}
2136
Alexandre Rames67555f72014-11-18 10:55:16 +00002137void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002138 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002139
2140 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002141 case DataType::Type::kInt32:
2142 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002143 Register dst = OutputRegister(instr);
2144 Register lhs = InputRegisterAt(instr, 0);
2145 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002146 if (instr->IsAdd()) {
2147 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002148 } else if (instr->IsAnd()) {
2149 __ And(dst, lhs, rhs);
2150 } else if (instr->IsOr()) {
2151 __ Orr(dst, lhs, rhs);
2152 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002153 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002154 } else if (instr->IsRor()) {
2155 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002156 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002157 __ Ror(dst, lhs, shift);
2158 } else {
2159 // Ensure shift distance is in the same size register as the result. If
2160 // we are rotating a long and the shift comes in a w register originally,
2161 // we don't need to sxtw for use as an x since the shift distances are
2162 // all & reg_bits - 1.
2163 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2164 }
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002165 } else if (instr->IsMin() || instr->IsMax()) {
2166 __ Cmp(lhs, rhs);
2167 __ Csel(dst, lhs, rhs, instr->IsMin() ? lt : gt);
Alexandre Rames67555f72014-11-18 10:55:16 +00002168 } else {
2169 DCHECK(instr->IsXor());
2170 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002171 }
2172 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002173 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002174 case DataType::Type::kFloat32:
2175 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002176 VRegister dst = OutputFPRegister(instr);
2177 VRegister lhs = InputFPRegisterAt(instr, 0);
2178 VRegister rhs = InputFPRegisterAt(instr, 1);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002179 if (instr->IsAdd()) {
2180 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002181 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002182 __ Fsub(dst, lhs, rhs);
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002183 } else if (instr->IsMin()) {
2184 __ Fmin(dst, lhs, rhs);
2185 } else if (instr->IsMax()) {
2186 __ Fmax(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002187 } else {
2188 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002189 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002190 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002191 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002192 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002193 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002194 }
2195}
2196
Serban Constantinescu02164b32014-11-13 14:05:07 +00002197void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2198 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2199
Vladimir Markoca6fff82017-10-03 14:49:14 +01002200 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002201 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002202 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002203 case DataType::Type::kInt32:
2204 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002205 locations->SetInAt(0, Location::RequiresRegister());
2206 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002207 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002208 break;
2209 }
2210 default:
2211 LOG(FATAL) << "Unexpected shift type " << type;
2212 }
2213}
2214
2215void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2216 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2217
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002218 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002219 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002220 case DataType::Type::kInt32:
2221 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002222 Register dst = OutputRegister(instr);
2223 Register lhs = InputRegisterAt(instr, 0);
2224 Operand rhs = InputOperandAt(instr, 1);
2225 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002226 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002227 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002228 if (instr->IsShl()) {
2229 __ Lsl(dst, lhs, shift_value);
2230 } else if (instr->IsShr()) {
2231 __ Asr(dst, lhs, shift_value);
2232 } else {
2233 __ Lsr(dst, lhs, shift_value);
2234 }
2235 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002236 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002237
2238 if (instr->IsShl()) {
2239 __ Lsl(dst, lhs, rhs_reg);
2240 } else if (instr->IsShr()) {
2241 __ Asr(dst, lhs, rhs_reg);
2242 } else {
2243 __ Lsr(dst, lhs, rhs_reg);
2244 }
2245 }
2246 break;
2247 }
2248 default:
2249 LOG(FATAL) << "Unexpected shift operation type " << type;
2250 }
2251}
2252
Alexandre Rames5319def2014-10-23 10:03:10 +01002253void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002254 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002255}
2256
2257void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002258 HandleBinaryOp(instruction);
2259}
2260
2261void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2262 HandleBinaryOp(instruction);
2263}
2264
2265void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2266 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002267}
2268
Artem Serov7fc63502016-02-09 17:15:29 +00002269void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002270 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002271 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002272 locations->SetInAt(0, Location::RequiresRegister());
2273 // There is no immediate variant of negated bitwise instructions in AArch64.
2274 locations->SetInAt(1, Location::RequiresRegister());
2275 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2276}
2277
Artem Serov7fc63502016-02-09 17:15:29 +00002278void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002279 Register dst = OutputRegister(instr);
2280 Register lhs = InputRegisterAt(instr, 0);
2281 Register rhs = InputRegisterAt(instr, 1);
2282
2283 switch (instr->GetOpKind()) {
2284 case HInstruction::kAnd:
2285 __ Bic(dst, lhs, rhs);
2286 break;
2287 case HInstruction::kOr:
2288 __ Orn(dst, lhs, rhs);
2289 break;
2290 case HInstruction::kXor:
2291 __ Eon(dst, lhs, rhs);
2292 break;
2293 default:
2294 LOG(FATAL) << "Unreachable";
2295 }
2296}
2297
Anton Kirilov74234da2017-01-13 14:42:47 +00002298void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2299 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002300 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2301 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002302 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002303 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002304 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2305 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2306 } else {
2307 locations->SetInAt(0, Location::RequiresRegister());
2308 }
2309 locations->SetInAt(1, Location::RequiresRegister());
2310 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2311}
2312
Anton Kirilov74234da2017-01-13 14:42:47 +00002313void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2314 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002315 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002316 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002317 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002318 Register out = OutputRegister(instruction);
2319 Register left;
2320 if (kind != HInstruction::kNeg) {
2321 left = InputRegisterAt(instruction, 0);
2322 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002323 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002324 // shifter operand operation, the IR generating `right_reg` (input to the type
2325 // conversion) can have a different type from the current instruction's type,
2326 // so we manually indicate the type.
2327 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002328 Operand right_operand(0);
2329
Anton Kirilov74234da2017-01-13 14:42:47 +00002330 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2331 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002332 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2333 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002334 right_operand = Operand(right_reg,
2335 helpers::ShiftFromOpKind(op_kind),
2336 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002337 }
2338
2339 // Logical binary operations do not support extension operations in the
2340 // operand. Note that VIXL would still manage if it was passed by generating
2341 // the extension as a separate instruction.
2342 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2343 DCHECK(!right_operand.IsExtendedRegister() ||
2344 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2345 kind != HInstruction::kNeg));
2346 switch (kind) {
2347 case HInstruction::kAdd:
2348 __ Add(out, left, right_operand);
2349 break;
2350 case HInstruction::kAnd:
2351 __ And(out, left, right_operand);
2352 break;
2353 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002354 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002355 __ Neg(out, right_operand);
2356 break;
2357 case HInstruction::kOr:
2358 __ Orr(out, left, right_operand);
2359 break;
2360 case HInstruction::kSub:
2361 __ Sub(out, left, right_operand);
2362 break;
2363 case HInstruction::kXor:
2364 __ Eor(out, left, right_operand);
2365 break;
2366 default:
2367 LOG(FATAL) << "Unexpected operation kind: " << kind;
2368 UNREACHABLE();
2369 }
2370}
2371
Artem Serov328429f2016-07-06 16:23:04 +01002372void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002373 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002374 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002375 locations->SetInAt(0, Location::RequiresRegister());
2376 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002377 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002378}
2379
Roland Levillain19c54192016-11-04 13:44:09 +00002380void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002381 __ Add(OutputRegister(instruction),
2382 InputRegisterAt(instruction, 0),
2383 Operand(InputOperandAt(instruction, 1)));
2384}
2385
Artem Serove1811ed2017-04-27 16:50:47 +01002386void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2387 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002388 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002389
2390 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2391
2392 locations->SetInAt(0, Location::RequiresRegister());
2393 // For byte case we don't need to shift the index variable so we can encode the data offset into
2394 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2395 // data offset constant generation out of the loop and reduce the critical path length in the
2396 // loop.
2397 locations->SetInAt(1, shift->GetValue() == 0
2398 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2399 : Location::RequiresRegister());
2400 locations->SetInAt(2, Location::ConstantLocation(shift));
2401 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2402}
2403
2404void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2405 HIntermediateAddressIndex* instruction) {
2406 Register index_reg = InputRegisterAt(instruction, 0);
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002407 uint32_t shift = Int64FromLocation(instruction->GetLocations()->InAt(2));
Artem Serove1811ed2017-04-27 16:50:47 +01002408 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2409
2410 if (shift == 0) {
2411 __ Add(OutputRegister(instruction), index_reg, offset);
2412 } else {
2413 Register offset_reg = InputRegisterAt(instruction, 1);
2414 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2415 }
2416}
2417
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002418void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002419 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002420 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002421 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2422 if (instr->GetOpKind() == HInstruction::kSub &&
2423 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002424 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002425 // Don't allocate register for Mneg instruction.
2426 } else {
2427 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2428 Location::RequiresRegister());
2429 }
2430 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2431 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002432 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2433}
2434
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002435void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002436 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002437 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2438 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002439
2440 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2441 // This fixup should be carried out for all multiply-accumulate instructions:
2442 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002443 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002444 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2445 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002446 vixl::aarch64::Instruction* prev =
2447 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002448 if (prev->IsLoadOrStore()) {
2449 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002450 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002451 __ nop();
2452 }
2453 }
2454
2455 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002456 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002457 __ Madd(res, mul_left, mul_right, accumulator);
2458 } else {
2459 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002460 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002461 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002462 __ Mneg(res, mul_left, mul_right);
2463 } else {
2464 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2465 __ Msub(res, mul_left, mul_right, accumulator);
2466 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002467 }
2468}
2469
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002470void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002471 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002472 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002473 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002474 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2475 object_array_get_with_read_barrier
2476 ? LocationSummary::kCallOnSlowPath
2477 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002478 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002479 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002480 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002481 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002482 // We need a temporary register for the read barrier load in
2483 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2484 // only if the offset is too big.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002485 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2486 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002487 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002488 if (offset >= kReferenceLoadMinFarOffset) {
2489 locations->AddTemp(FixedTempLocation());
2490 }
Artem Serov0806f582018-10-11 20:14:20 +01002491 } else if (!instruction->GetArray()->IsIntermediateAddress()) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01002492 // We need a non-scratch temporary for the array data pointer in
Artem Serov0806f582018-10-11 20:14:20 +01002493 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier() for the case with no
2494 // intermediate address.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002495 locations->AddTemp(Location::RequiresRegister());
2496 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002497 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002498 locations->SetInAt(0, Location::RequiresRegister());
2499 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002500 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002501 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2502 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002503 // The output overlaps in the case of an object array get with
2504 // read barriers enabled: we do not want the move to overwrite the
2505 // array's location, as we need it to emit the read barrier.
2506 locations->SetOut(
2507 Location::RequiresRegister(),
2508 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002509 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002510}
2511
2512void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002513 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002514 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002515 LocationSummary* locations = instruction->GetLocations();
2516 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002517 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002518 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002519 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2520 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002521 MacroAssembler* masm = GetVIXLAssembler();
2522 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002523
Artem Serov0806f582018-10-11 20:14:20 +01002524 // The non-Baker read barrier instrumentation of object ArrayGet instructions
Roland Levillain19c54192016-11-04 13:44:09 +00002525 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002526 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002527 instruction->GetArray()->IsIntermediateAddress() &&
Artem Serov0806f582018-10-11 20:14:20 +01002528 kEmitCompilerReadBarrier &&
2529 !kUseBakerReadBarrier));
Roland Levillain19c54192016-11-04 13:44:09 +00002530
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002531 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002532 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002533 // Note that a potential implicit null check is handled in the
2534 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002535 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002536 if (index.IsConstant()) {
Artem Serov0806f582018-10-11 20:14:20 +01002537 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002538 // Array load with a constant index can be treated as a field load.
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002539 offset += Int64FromLocation(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002540 Location maybe_temp =
2541 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2542 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2543 out,
2544 obj.W(),
2545 offset,
2546 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002547 /* needs_null_check= */ false,
2548 /* use_load_acquire= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002549 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002550 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08002551 instruction, out, obj.W(), offset, index, /* needs_null_check= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002552 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002553 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002554 // General case.
2555 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002556 Register length;
2557 if (maybe_compressed_char_at) {
2558 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2559 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002560 {
2561 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2562 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2563
2564 if (instruction->GetArray()->IsIntermediateAddress()) {
2565 DCHECK_LT(count_offset, offset);
2566 int64_t adjusted_offset =
2567 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2568 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2569 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2570 } else {
2571 __ Ldr(length, HeapOperand(obj, count_offset));
2572 }
2573 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002574 }
jessicahandojo05765752016-09-09 19:01:32 -07002575 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002576 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002577 if (maybe_compressed_char_at) {
2578 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002579 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2580 "Expecting 0=compressed, 1=uncompressed");
2581 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002582 __ Ldrb(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002583 HeapOperand(obj, offset + Int64FromLocation(index)));
jessicahandojo05765752016-09-09 19:01:32 -07002584 __ B(&done);
2585 __ Bind(&uncompressed_load);
2586 __ Ldrh(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002587 HeapOperand(obj, offset + (Int64FromLocation(index) << 1)));
jessicahandojo05765752016-09-09 19:01:32 -07002588 __ Bind(&done);
2589 } else {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002590 offset += Int64FromLocation(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002591 source = HeapOperand(obj, offset);
2592 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002593 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002594 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002595 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002596 // We do not need to compute the intermediate address from the array: the
2597 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002598 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002599 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002600 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2601 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
Roland Levillain44015862016-01-22 11:47:17 +00002602 }
2603 temp = obj;
2604 } else {
2605 __ Add(temp, obj, offset);
2606 }
jessicahandojo05765752016-09-09 19:01:32 -07002607 if (maybe_compressed_char_at) {
2608 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002609 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2610 "Expecting 0=compressed, 1=uncompressed");
2611 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002612 __ Ldrb(Register(OutputCPURegister(instruction)),
2613 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2614 __ B(&done);
2615 __ Bind(&uncompressed_load);
2616 __ Ldrh(Register(OutputCPURegister(instruction)),
2617 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2618 __ Bind(&done);
2619 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002620 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002621 }
Roland Levillain44015862016-01-22 11:47:17 +00002622 }
jessicahandojo05765752016-09-09 19:01:32 -07002623 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002624 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2625 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002626 codegen_->Load(type, OutputCPURegister(instruction), source);
2627 codegen_->MaybeRecordImplicitNullCheck(instruction);
2628 }
Roland Levillain44015862016-01-22 11:47:17 +00002629
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002630 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002631 static_assert(
2632 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2633 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2634 Location obj_loc = locations->InAt(0);
2635 if (index.IsConstant()) {
2636 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2637 } else {
2638 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2639 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002640 }
Roland Levillain4d027112015-07-01 15:41:14 +01002641 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002642}
2643
Alexandre Rames5319def2014-10-23 10:03:10 +01002644void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002645 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002646 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002647 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002648}
2649
2650void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002651 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002652 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002653 {
2654 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2655 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2656 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2657 codegen_->MaybeRecordImplicitNullCheck(instruction);
2658 }
jessicahandojo05765752016-09-09 19:01:32 -07002659 // Mask out compression flag from String's array length.
2660 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002661 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002662 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002663}
2664
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002665void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002666 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002667
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002668 bool needs_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002669 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002670 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002671 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002672 locations->SetInAt(0, Location::RequiresRegister());
2673 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002674 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2675 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002676 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002677 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002678 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002679 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002680 }
2681}
2682
2683void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002684 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002685 LocationSummary* locations = instruction->GetLocations();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002686 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002687 bool needs_write_barrier =
2688 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002689
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002690 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002691 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002692 CPURegister source = value;
2693 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002694 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002695 MemOperand destination = HeapOperand(array);
2696 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002697
2698 if (!needs_write_barrier) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002699 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002700 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002701 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002702 destination = HeapOperand(array, offset);
2703 } else {
2704 UseScratchRegisterScope temps(masm);
2705 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002706 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002707 // We do not need to compute the intermediate address from the array: the
2708 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002709 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002710 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002711 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2712 DCHECK(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002713 }
2714 temp = array;
2715 } else {
2716 __ Add(temp, array, offset);
2717 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002718 destination = HeapOperand(temp,
2719 XRegisterFrom(index),
2720 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002721 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002722 }
Artem Serov914d7a82017-02-07 14:33:49 +00002723 {
2724 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2725 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2726 codegen_->Store(value_type, value, destination);
2727 codegen_->MaybeRecordImplicitNullCheck(instruction);
2728 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002729 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002730 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002731
2732 bool can_value_be_null = instruction->GetValueCanBeNull();
2733 vixl::aarch64::Label do_store;
2734 if (can_value_be_null) {
2735 __ Cbz(Register(value), &do_store);
2736 }
2737
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002738 SlowPathCodeARM64* slow_path = nullptr;
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002739 if (needs_type_check) {
2740 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
2741 codegen_->AddSlowPath(slow_path);
2742
2743 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2744 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2745 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2746
Alexandre Rames97833a02015-04-16 15:07:12 +01002747 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002748 Register temp = temps.AcquireSameSizeAs(array);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002749 Register temp2 = temps.AcquireSameSizeAs(array);
2750
2751 // Note that when Baker read barriers are enabled, the type
2752 // checks are performed without read barriers. This is fine,
2753 // even in the case where a class object is in the from-space
2754 // after the flip, as a comparison involving such a type would
2755 // not produce a false positive; it may of course produce a
2756 // false negative, in which case we would take the ArraySet
2757 // slow path.
2758
2759 // /* HeapReference<Class> */ temp = array->klass_
2760 {
2761 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2762 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2763 __ Ldr(temp, HeapOperand(array, class_offset));
2764 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames97833a02015-04-16 15:07:12 +01002765 }
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002766 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Alexandre Rames97833a02015-04-16 15:07:12 +01002767
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002768 // /* HeapReference<Class> */ temp = temp->component_type_
2769 __ Ldr(temp, HeapOperand(temp, component_offset));
2770 // /* HeapReference<Class> */ temp2 = value->klass_
2771 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2772 // If heap poisoning is enabled, no need to unpoison `temp`
2773 // nor `temp2`, as we are comparing two poisoned references.
2774 __ Cmp(temp, temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002775
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002776 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2777 vixl::aarch64::Label do_put;
2778 __ B(eq, &do_put);
2779 // If heap poisoning is enabled, the `temp` reference has
2780 // not been unpoisoned yet; unpoison it now.
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002781 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002782
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002783 // /* HeapReference<Class> */ temp = temp->super_class_
2784 __ Ldr(temp, HeapOperand(temp, super_offset));
2785 // If heap poisoning is enabled, no need to unpoison
2786 // `temp`, as we are comparing against null below.
2787 __ Cbnz(temp, slow_path->GetEntryLabel());
2788 __ Bind(&do_put);
Vladimir Markod1ef8732017-04-18 13:55:13 +01002789 } else {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002790 __ B(ne, slow_path->GetEntryLabel());
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002791 }
2792 }
2793
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002794 codegen_->MarkGCCard(array, value.W(), /* value_can_be_null= */ false);
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002795
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002796 if (can_value_be_null) {
2797 DCHECK(do_store.IsLinked());
2798 __ Bind(&do_store);
2799 }
2800
2801 UseScratchRegisterScope temps(masm);
2802 if (kPoisonHeapReferences) {
2803 Register temp_source = temps.AcquireSameSizeAs(array);
2804 DCHECK(value.IsW());
2805 __ Mov(temp_source, value.W());
2806 GetAssembler()->PoisonHeapReference(temp_source);
2807 source = temp_source;
2808 }
2809
2810 if (index.IsConstant()) {
2811 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
2812 destination = HeapOperand(array, offset);
2813 } else {
2814 Register temp_base = temps.AcquireSameSizeAs(array);
2815 __ Add(temp_base, array, offset);
2816 destination = HeapOperand(temp_base,
2817 XRegisterFrom(index),
2818 LSL,
2819 DataType::SizeShift(value_type));
2820 }
2821
2822 {
2823 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2824 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2825 __ Str(source, destination);
2826
2827 if (can_value_be_null || !needs_type_check) {
2828 codegen_->MaybeRecordImplicitNullCheck(instruction);
2829 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002830 }
2831
2832 if (slow_path != nullptr) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002833 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002834 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002835 }
2836}
2837
Alexandre Rames67555f72014-11-18 10:55:16 +00002838void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002839 RegisterSet caller_saves = RegisterSet::Empty();
2840 InvokeRuntimeCallingConvention calling_convention;
2841 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2842 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2843 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002844
2845 // If both index and length are constant, we can check the bounds statically and
2846 // generate code accordingly. We want to make sure we generate constant locations
2847 // in that case, regardless of whether they are encodable in the comparison or not.
2848 HInstruction* index = instruction->InputAt(0);
2849 HInstruction* length = instruction->InputAt(1);
2850 bool both_const = index->IsConstant() && length->IsConstant();
2851 locations->SetInAt(0, both_const
2852 ? Location::ConstantLocation(index->AsConstant())
2853 : ARM64EncodableConstantOrRegister(index, instruction));
2854 locations->SetInAt(1, both_const
2855 ? Location::ConstantLocation(length->AsConstant())
2856 : ARM64EncodableConstantOrRegister(length, instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002857}
2858
2859void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Georgia Kouvelibe530852019-01-17 10:46:41 +00002860 LocationSummary* locations = instruction->GetLocations();
2861 Location index_loc = locations->InAt(0);
2862 Location length_loc = locations->InAt(1);
2863
2864 int cmp_first_input = 0;
2865 int cmp_second_input = 1;
2866 Condition cond = hs;
2867
2868 if (index_loc.IsConstant()) {
2869 int64_t index = Int64FromLocation(index_loc);
2870 if (length_loc.IsConstant()) {
2871 int64_t length = Int64FromLocation(length_loc);
2872 if (index < 0 || index >= length) {
2873 BoundsCheckSlowPathARM64* slow_path =
2874 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
2875 codegen_->AddSlowPath(slow_path);
2876 __ B(slow_path->GetEntryLabel());
2877 } else {
2878 // BCE will remove the bounds check if we are guaranteed to pass.
2879 // However, some optimization after BCE may have generated this, and we should not
2880 // generate a bounds check if it is a valid range.
2881 }
2882 return;
2883 }
2884 // Only the index is constant: change the order of the operands and commute the condition
2885 // so we can use an immediate constant for the index (only the second input to a cmp
2886 // instruction can be an immediate).
2887 cmp_first_input = 1;
2888 cmp_second_input = 0;
2889 cond = ls;
2890 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002891 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002892 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002893 __ Cmp(InputRegisterAt(instruction, cmp_first_input),
2894 InputOperandAt(instruction, cmp_second_input));
Alexandre Rames67555f72014-11-18 10:55:16 +00002895 codegen_->AddSlowPath(slow_path);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002896 __ B(slow_path->GetEntryLabel(), cond);
Alexandre Rames67555f72014-11-18 10:55:16 +00002897}
2898
Alexandre Rames67555f72014-11-18 10:55:16 +00002899void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2900 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002901 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00002902 locations->SetInAt(0, Location::RequiresRegister());
2903 if (check->HasUses()) {
2904 locations->SetOut(Location::SameAsFirstInput());
2905 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01002906 // Rely on the type initialization to save everything we need.
2907 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Alexandre Rames67555f72014-11-18 10:55:16 +00002908}
2909
2910void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2911 // We assume the class is not null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01002912 SlowPathCodeARM64* slow_path =
2913 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(check->GetLoadClass(), check);
Alexandre Rames67555f72014-11-18 10:55:16 +00002914 codegen_->AddSlowPath(slow_path);
2915 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2916}
2917
Roland Levillain1a653882016-03-18 18:05:57 +00002918static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2919 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2920 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2921}
2922
2923void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002924 VRegister lhs_reg = InputFPRegisterAt(instruction, 0);
Roland Levillain1a653882016-03-18 18:05:57 +00002925 Location rhs_loc = instruction->GetLocations()->InAt(1);
2926 if (rhs_loc.IsConstant()) {
2927 // 0.0 is the only immediate that can be encoded directly in
2928 // an FCMP instruction.
2929 //
2930 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2931 // specify that in a floating-point comparison, positive zero
2932 // and negative zero are considered equal, so we can use the
2933 // literal 0.0 for both cases here.
2934 //
2935 // Note however that some methods (Float.equal, Float.compare,
2936 // Float.compareTo, Double.equal, Double.compare,
2937 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2938 // StrictMath.min) consider 0.0 to be (strictly) greater than
2939 // -0.0. So if we ever translate calls to these methods into a
2940 // HCompare instruction, we must handle the -0.0 case with
2941 // care here.
2942 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2943 __ Fcmp(lhs_reg, 0.0);
2944 } else {
2945 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2946 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002947}
2948
Serban Constantinescu02164b32014-11-13 14:05:07 +00002949void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002950 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002951 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002952 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002953 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002954 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002955 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002956 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002957 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002958 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002959 case DataType::Type::kInt32:
2960 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002961 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002962 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002963 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2964 break;
2965 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002966 case DataType::Type::kFloat32:
2967 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002968 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002969 locations->SetInAt(1,
2970 IsFloatingPointZeroConstant(compare->InputAt(1))
2971 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2972 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002973 locations->SetOut(Location::RequiresRegister());
2974 break;
2975 }
2976 default:
2977 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2978 }
2979}
2980
2981void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002982 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002983
2984 // 0 if: left == right
2985 // 1 if: left > right
2986 // -1 if: left < right
2987 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002988 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002989 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002990 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002991 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002992 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002993 case DataType::Type::kInt32:
2994 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002995 Register result = OutputRegister(compare);
2996 Register left = InputRegisterAt(compare, 0);
2997 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002998 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002999 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3000 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003001 break;
3002 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003003 case DataType::Type::kFloat32:
3004 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003005 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003006 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003007 __ Cset(result, ne);
3008 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003009 break;
3010 }
3011 default:
3012 LOG(FATAL) << "Unimplemented compare type " << in_type;
3013 }
3014}
3015
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003016void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003017 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003018
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003019 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003020 locations->SetInAt(0, Location::RequiresFpuRegister());
3021 locations->SetInAt(1,
3022 IsFloatingPointZeroConstant(instruction->InputAt(1))
3023 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3024 : Location::RequiresFpuRegister());
3025 } else {
3026 // Integer cases.
3027 locations->SetInAt(0, Location::RequiresRegister());
3028 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3029 }
3030
David Brazdilb3e773e2016-01-26 11:28:37 +00003031 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003032 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003033 }
3034}
3035
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003036void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003037 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003038 return;
3039 }
3040
3041 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003042 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003043 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003044
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003045 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003046 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003047 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003048 } else {
3049 // Integer cases.
3050 Register lhs = InputRegisterAt(instruction, 0);
3051 Operand rhs = InputOperandAt(instruction, 1);
3052 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003053 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003054 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003055}
3056
3057#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3058 M(Equal) \
3059 M(NotEqual) \
3060 M(LessThan) \
3061 M(LessThanOrEqual) \
3062 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003063 M(GreaterThanOrEqual) \
3064 M(Below) \
3065 M(BelowOrEqual) \
3066 M(Above) \
3067 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003068#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003069void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3070void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003071FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003072#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003073#undef FOR_EACH_CONDITION_INSTRUCTION
3074
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003075void InstructionCodeGeneratorARM64::GenerateIntDivForPower2Denom(HDiv* instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003076 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003077 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003078 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
3079
3080 Register out = OutputRegister(instruction);
3081 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003082
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003083 Register final_dividend;
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003084 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003085 // No need to adjust the result for non-negative dividends or the INT32_MIN/INT64_MIN dividends.
3086 // NOTE: The generated code for HDiv correctly works for the INT32_MIN/INT64_MIN dividends:
3087 // imm == 2
3088 // add out, dividend(0x80000000), dividend(0x80000000), lsr #31 => out = 0x80000001
3089 // asr out, out(0x80000001), #1 => out = 0xc0000000
3090 // This is the same as 'asr out, 0x80000000, #1'
3091 //
3092 // imm > 2
3093 // add temp, dividend(0x80000000), imm - 1 => temp = 0b10..01..1, where the number
3094 // of the rightmost 1s is ctz_imm.
3095 // cmp dividend(0x80000000), 0 => N = 1, V = 0 (lt is true)
3096 // csel out, temp(0b10..01..1), dividend(0x80000000), lt => out = 0b10..01..1
3097 // asr out, out(0b10..01..1), #ctz_imm => out = 0b1..10..0, where the number of the
3098 // leftmost 1s is ctz_imm + 1.
3099 // This is the same as 'asr out, dividend(0x80000000), #ctz_imm'.
3100 //
3101 // imm == INT32_MIN
3102 // add tmp, dividend(0x80000000), #0x7fffffff => tmp = -1
3103 // cmp dividend(0x80000000), 0 => N = 1, V = 0 (lt is true)
3104 // csel out, temp(-1), dividend(0x80000000), lt => out = -1
3105 // neg out, out(-1), asr #31 => out = 1
3106 // This is the same as 'neg out, dividend(0x80000000), asr #31'.
3107 final_dividend = dividend;
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003108 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003109 if (abs_imm == 2) {
3110 int bits = DataType::Size(instruction->GetResultType()) * kBitsPerByte;
3111 __ Add(out, dividend, Operand(dividend, LSR, bits - 1));
3112 } else {
3113 UseScratchRegisterScope temps(GetVIXLAssembler());
3114 Register temp = temps.AcquireSameSizeAs(out);
3115 __ Add(temp, dividend, abs_imm - 1);
3116 __ Cmp(dividend, 0);
3117 __ Csel(out, temp, dividend, lt);
3118 }
3119 final_dividend = out;
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003120 }
3121
Zheng Xuc6667102015-05-15 16:08:45 +08003122 int ctz_imm = CTZ(abs_imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003123 if (imm > 0) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003124 __ Asr(out, final_dividend, ctz_imm);
Zheng Xuc6667102015-05-15 16:08:45 +08003125 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003126 __ Neg(out, Operand(final_dividend, ASR, ctz_imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003127 }
3128}
3129
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003130// Return true if the magic number was modified by subtracting 2^32(Int32 div) or 2^64(Int64 div).
3131// So dividend needs to be added.
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003132static inline bool NeedToAddDividend(int64_t magic_number, int64_t divisor) {
3133 return divisor > 0 && magic_number < 0;
3134}
3135
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003136// Return true if the magic number was modified by adding 2^32(Int32 div) or 2^64(Int64 div).
3137// So dividend needs to be subtracted.
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003138static inline bool NeedToSubDividend(int64_t magic_number, int64_t divisor) {
3139 return divisor < 0 && magic_number > 0;
3140}
3141
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003142// Generate code which increments the value in register 'in' by 1 if the value is negative.
3143// It is done with 'add out, in, in, lsr #31 or #63'.
3144// If the value is a result of an operation setting the N flag, CINC MI can be used
3145// instead of ADD. 'use_cond_inc' controls this.
3146void InstructionCodeGeneratorARM64::GenerateIncrementNegativeByOne(
3147 Register out,
3148 Register in,
3149 bool use_cond_inc) {
3150 if (use_cond_inc) {
3151 __ Cinc(out, in, mi);
3152 } else {
3153 __ Add(out, in, Operand(in, LSR, in.GetSizeInBits() - 1));
3154 }
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003155}
3156
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003157// Helper to generate code producing the result of HRem with a constant divisor.
3158void InstructionCodeGeneratorARM64::GenerateResultRemWithAnyConstant(
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003159 Register out,
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003160 Register dividend,
3161 Register quotient,
3162 int64_t divisor,
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003163 UseScratchRegisterScope* temps_scope) {
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003164 Register temp_imm = temps_scope->AcquireSameSizeAs(out);
3165 __ Mov(temp_imm, divisor);
3166 __ Msub(out, quotient, temp_imm, dividend);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003167}
3168
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003169// Helper to generate code for HDiv/HRem instructions when a dividend is non-negative and
3170// a divisor is a positive constant, not power of 2.
3171void InstructionCodeGeneratorARM64::GenerateInt64UnsignedDivRemWithAnyPositiveConstant(
3172 HBinaryOperation* instruction) {
3173 DCHECK(instruction->IsDiv() || instruction->IsRem());
3174 DCHECK(instruction->GetResultType() == DataType::Type::kInt64);
3175
3176 LocationSummary* locations = instruction->GetLocations();
3177 Location second = locations->InAt(1);
3178 DCHECK(second.IsConstant());
3179
3180 Register out = OutputRegister(instruction);
3181 Register dividend = InputRegisterAt(instruction, 0);
3182 int64_t imm = Int64FromConstant(second.GetConstant());
3183 DCHECK_GT(imm, 0);
3184
3185 int64_t magic;
3186 int shift;
3187 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ true, &magic, &shift);
3188
3189 UseScratchRegisterScope temps(GetVIXLAssembler());
3190 Register temp = temps.AcquireSameSizeAs(out);
3191
3192 auto generate_unsigned_div_code = [this, magic, shift](Register out,
3193 Register dividend,
3194 Register temp) {
3195 // temp = get_high(dividend * magic)
3196 __ Mov(temp, magic);
3197 if (magic > 0 && shift == 0) {
3198 __ Smulh(out, dividend, temp);
3199 } else {
3200 __ Smulh(temp, dividend, temp);
3201 if (magic < 0) {
3202 // The negative magic means that the multiplier m is greater than INT64_MAX.
3203 // In such a case shift is never 0. See the proof in
3204 // InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant.
3205 __ Add(temp, temp, dividend);
3206 }
3207 DCHECK_NE(shift, 0);
3208 __ Lsr(out, temp, shift);
3209 }
3210 };
3211
3212 if (instruction->IsDiv()) {
3213 generate_unsigned_div_code(out, dividend, temp);
3214 } else {
3215 generate_unsigned_div_code(temp, dividend, temp);
3216 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3217 }
3218}
3219
3220// Helper to generate code for HDiv/HRem instructions for any dividend and a constant divisor
3221// (not power of 2).
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003222void InstructionCodeGeneratorARM64::GenerateInt64DivRemWithAnyConstant(
3223 HBinaryOperation* instruction) {
Zheng Xuc6667102015-05-15 16:08:45 +08003224 DCHECK(instruction->IsDiv() || instruction->IsRem());
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003225 DCHECK(instruction->GetResultType() == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003226
3227 LocationSummary* locations = instruction->GetLocations();
3228 Location second = locations->InAt(1);
3229 DCHECK(second.IsConstant());
3230
3231 Register out = OutputRegister(instruction);
3232 Register dividend = InputRegisterAt(instruction, 0);
3233 int64_t imm = Int64FromConstant(second.GetConstant());
3234
Zheng Xuc6667102015-05-15 16:08:45 +08003235 int64_t magic;
3236 int shift;
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003237 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ true, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08003238
3239 UseScratchRegisterScope temps(GetVIXLAssembler());
3240 Register temp = temps.AcquireSameSizeAs(out);
3241
3242 // temp = get_high(dividend * magic)
3243 __ Mov(temp, magic);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003244 __ Smulh(temp, dividend, temp);
3245
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003246 // The multiplication result might need some corrections to be finalized.
3247 // The last correction is to increment by 1, if the result is negative.
3248 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3249 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3250 // However if one of the corrections is ADD or SUB, the sign can be detected
3251 // with ADDS/SUBS. They set the N flag if the result is negative.
3252 // This allows to use CINC MI which has latency 1.
3253 bool use_cond_inc = false;
3254
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003255 // Some combinations of magic_number and the divisor require to correct the result.
3256 // Check whether the correction is needed.
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003257 if (NeedToAddDividend(magic, imm)) {
3258 __ Adds(temp, temp, dividend);
3259 use_cond_inc = true;
3260 } else if (NeedToSubDividend(magic, imm)) {
3261 __ Subs(temp, temp, dividend);
3262 use_cond_inc = true;
3263 }
3264
3265 if (shift != 0) {
3266 __ Asr(temp, temp, shift);
3267 }
3268
3269 if (instruction->IsRem()) {
3270 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3271 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3272 } else {
3273 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3274 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003275}
3276
3277void InstructionCodeGeneratorARM64::GenerateInt32DivRemWithAnyConstant(
3278 HBinaryOperation* instruction) {
3279 DCHECK(instruction->IsDiv() || instruction->IsRem());
3280 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
3281
3282 LocationSummary* locations = instruction->GetLocations();
3283 Location second = locations->InAt(1);
3284 DCHECK(second.IsConstant());
3285
3286 Register out = OutputRegister(instruction);
3287 Register dividend = InputRegisterAt(instruction, 0);
3288 int64_t imm = Int64FromConstant(second.GetConstant());
3289
3290 int64_t magic;
3291 int shift;
3292 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
3293 UseScratchRegisterScope temps(GetVIXLAssembler());
3294 Register temp = temps.AcquireSameSizeAs(out);
3295
3296 // temp = get_high(dividend * magic)
3297 __ Mov(temp, magic);
3298 __ Smull(temp.X(), dividend, temp);
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003299
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003300 // The multiplication result might need some corrections to be finalized.
3301 // The last correction is to increment by 1, if the result is negative.
3302 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3303 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3304 // However if one of the corrections is ADD or SUB, the sign can be detected
3305 // with ADDS/SUBS. They set the N flag if the result is negative.
3306 // This allows to use CINC MI which has latency 1.
3307 bool use_cond_inc = false;
3308
3309 // ADD/SUB correction is performed in the high 32 bits
3310 // as high 32 bits are ignored because type are kInt32.
3311 if (NeedToAddDividend(magic, imm)) {
3312 __ Adds(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3313 use_cond_inc = true;
3314 } else if (NeedToSubDividend(magic, imm)) {
3315 __ Subs(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3316 use_cond_inc = true;
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003317 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003318
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003319 // Extract the result from the high 32 bits and apply the final right shift.
3320 DCHECK_LT(shift, 32);
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003321 if (imm > 0 && HasNonNegativeInputAt(instruction, 0)) {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01003322 // No need to adjust the result for a non-negative dividend and a positive divisor.
3323 if (instruction->IsDiv()) {
3324 __ Lsr(out.X(), temp.X(), 32 + shift);
3325 } else {
3326 __ Lsr(temp.X(), temp.X(), 32 + shift);
3327 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3328 }
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003329 } else {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01003330 __ Asr(temp.X(), temp.X(), 32 + shift);
3331
3332 if (instruction->IsRem()) {
3333 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3334 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3335 } else {
3336 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3337 }
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003338 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003339}
3340
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003341void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction,
3342 int64_t divisor) {
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003343 DCHECK(instruction->IsDiv() || instruction->IsRem());
3344 if (instruction->GetResultType() == DataType::Type::kInt64) {
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003345 if (divisor > 0 && HasNonNegativeInputAt(instruction, 0)) {
3346 GenerateInt64UnsignedDivRemWithAnyPositiveConstant(instruction);
3347 } else {
3348 GenerateInt64DivRemWithAnyConstant(instruction);
3349 }
Zheng Xuc6667102015-05-15 16:08:45 +08003350 } else {
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003351 GenerateInt32DivRemWithAnyConstant(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003352 }
3353}
3354
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003355void InstructionCodeGeneratorARM64::GenerateIntDivForConstDenom(HDiv *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003356 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Zheng Xuc6667102015-05-15 16:08:45 +08003357
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003358 if (imm == 0) {
3359 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3360 return;
3361 }
Zheng Xuc6667102015-05-15 16:08:45 +08003362
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003363 if (IsPowerOfTwo(AbsOrMin(imm))) {
3364 GenerateIntDivForPower2Denom(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003365 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003366 // Cases imm == -1 or imm == 1 are handled by InstructionSimplifier.
3367 DCHECK(imm < -2 || imm > 2) << imm;
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003368 GenerateDivRemWithAnyConstant(instruction, imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003369 }
3370}
3371
3372void InstructionCodeGeneratorARM64::GenerateIntDiv(HDiv *instruction) {
3373 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
3374 << instruction->GetResultType();
3375
3376 if (instruction->GetLocations()->InAt(1).IsConstant()) {
3377 GenerateIntDivForConstDenom(instruction);
3378 } else {
3379 Register out = OutputRegister(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003380 Register dividend = InputRegisterAt(instruction, 0);
3381 Register divisor = InputRegisterAt(instruction, 1);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003382 __ Sdiv(out, dividend, divisor);
Zheng Xuc6667102015-05-15 16:08:45 +08003383 }
3384}
3385
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003386void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3387 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003388 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003389 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003390 case DataType::Type::kInt32:
3391 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003392 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003393 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003394 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3395 break;
3396
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003397 case DataType::Type::kFloat32:
3398 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003399 locations->SetInAt(0, Location::RequiresFpuRegister());
3400 locations->SetInAt(1, Location::RequiresFpuRegister());
3401 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3402 break;
3403
3404 default:
3405 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3406 }
3407}
3408
3409void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003410 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003411 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003412 case DataType::Type::kInt32:
3413 case DataType::Type::kInt64:
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003414 GenerateIntDiv(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003415 break;
3416
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003417 case DataType::Type::kFloat32:
3418 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003419 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3420 break;
3421
3422 default:
3423 LOG(FATAL) << "Unexpected div type " << type;
3424 }
3425}
3426
Alexandre Rames67555f72014-11-18 10:55:16 +00003427void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003428 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003429 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003430}
3431
3432void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3433 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003434 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003435 codegen_->AddSlowPath(slow_path);
3436 Location value = instruction->GetLocations()->InAt(0);
3437
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003438 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003439
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003440 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003441 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Elliott Hughesc1896c92018-11-29 11:33:18 -08003442 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003443 }
3444
Alexandre Rames67555f72014-11-18 10:55:16 +00003445 if (value.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003446 int64_t divisor = Int64FromLocation(value);
Alexandre Rames67555f72014-11-18 10:55:16 +00003447 if (divisor == 0) {
3448 __ B(slow_path->GetEntryLabel());
3449 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003450 // A division by a non-null constant is valid. We don't need to perform
3451 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003452 }
3453 } else {
3454 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3455 }
3456}
3457
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003458void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3459 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003460 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003461 locations->SetOut(Location::ConstantLocation(constant));
3462}
3463
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003464void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3465 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003466 // Will be generated at use site.
3467}
3468
Alexandre Rames5319def2014-10-23 10:03:10 +01003469void LocationsBuilderARM64::VisitExit(HExit* exit) {
3470 exit->SetLocations(nullptr);
3471}
3472
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003473void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003474}
3475
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003476void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3477 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003478 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003479 locations->SetOut(Location::ConstantLocation(constant));
3480}
3481
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003482void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003483 // Will be generated at use site.
3484}
3485
David Brazdilfc6a86a2015-06-26 10:33:45 +00003486void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003487 if (successor->IsExitBlock()) {
3488 DCHECK(got->GetPrevious()->AlwaysThrows());
3489 return; // no code needed
3490 }
3491
Serban Constantinescu02164b32014-11-13 14:05:07 +00003492 HBasicBlock* block = got->GetBlock();
3493 HInstruction* previous = got->GetPrevious();
3494 HLoopInformation* info = block->GetLoopInformation();
3495
David Brazdil46e2a392015-03-16 17:31:52 +00003496 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00003497 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003498 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3499 return;
3500 }
3501 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3502 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08003503 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003504 }
3505 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003506 __ B(codegen_->GetLabelOf(successor));
3507 }
3508}
3509
David Brazdilfc6a86a2015-06-26 10:33:45 +00003510void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3511 got->SetLocations(nullptr);
3512}
3513
3514void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3515 HandleGoto(got, got->GetSuccessor());
3516}
3517
3518void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3519 try_boundary->SetLocations(nullptr);
3520}
3521
3522void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3523 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3524 if (!successor->IsExitBlock()) {
3525 HandleGoto(try_boundary, successor);
3526 }
3527}
3528
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003529void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003530 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003531 vixl::aarch64::Label* true_target,
3532 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003533 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003534
David Brazdil0debae72015-11-12 18:37:00 +00003535 if (true_target == nullptr && false_target == nullptr) {
3536 // Nothing to do. The code always falls through.
3537 return;
3538 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003539 // Constant condition, statically compared against "true" (integer value 1).
3540 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003541 if (true_target != nullptr) {
3542 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003543 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003544 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003545 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003546 if (false_target != nullptr) {
3547 __ B(false_target);
3548 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003549 }
David Brazdil0debae72015-11-12 18:37:00 +00003550 return;
3551 }
3552
3553 // The following code generates these patterns:
3554 // (1) true_target == nullptr && false_target != nullptr
3555 // - opposite condition true => branch to false_target
3556 // (2) true_target != nullptr && false_target == nullptr
3557 // - condition true => branch to true_target
3558 // (3) true_target != nullptr && false_target != nullptr
3559 // - condition true => branch to true_target
3560 // - branch to false_target
3561 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003562 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003563 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003564 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003565 if (true_target == nullptr) {
3566 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3567 } else {
3568 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3569 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003570 } else {
3571 // The condition instruction has not been materialized, use its inputs as
3572 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003573 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003574
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003575 DataType::Type type = condition->InputAt(0)->GetType();
3576 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003577 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003578 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003579 IfCondition opposite_condition = condition->GetOppositeCondition();
3580 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003581 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003582 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003583 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003584 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003585 // Integer cases.
3586 Register lhs = InputRegisterAt(condition, 0);
3587 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003588
3589 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003590 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003591 if (true_target == nullptr) {
3592 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3593 non_fallthrough_target = false_target;
3594 } else {
3595 arm64_cond = ARM64Condition(condition->GetCondition());
3596 non_fallthrough_target = true_target;
3597 }
3598
Aart Bik086d27e2016-01-20 17:02:00 -08003599 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003600 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003601 switch (arm64_cond) {
3602 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003603 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003604 break;
3605 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003606 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003607 break;
3608 case lt:
3609 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003610 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003611 break;
3612 case ge:
3613 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003614 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003615 break;
3616 default:
3617 // Without the `static_cast` the compiler throws an error for
3618 // `-Werror=sign-promo`.
3619 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3620 }
3621 } else {
3622 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003623 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003624 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003625 }
3626 }
David Brazdil0debae72015-11-12 18:37:00 +00003627
3628 // If neither branch falls through (case 3), the conditional branch to `true_target`
3629 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3630 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003631 __ B(false_target);
3632 }
3633}
3634
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003635void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003636 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003637 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003638 locations->SetInAt(0, Location::RequiresRegister());
3639 }
3640}
3641
3642void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003643 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3644 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003645 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3646 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3647 true_target = nullptr;
3648 }
3649 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3650 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3651 false_target = nullptr;
3652 }
Andreas Gampe3db70682018-12-26 15:12:03 -08003653 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003654}
3655
3656void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003657 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003658 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003659 InvokeRuntimeCallingConvention calling_convention;
3660 RegisterSet caller_saves = RegisterSet::Empty();
3661 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3662 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003663 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003664 locations->SetInAt(0, Location::RequiresRegister());
3665 }
3666}
3667
3668void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003669 SlowPathCodeARM64* slow_path =
3670 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003671 GenerateTestAndBranch(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08003672 /* condition_input_index= */ 0,
David Brazdil0debae72015-11-12 18:37:00 +00003673 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08003674 /* false_target= */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003675}
3676
Mingyao Yang063fc772016-08-02 11:02:54 -07003677void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003678 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003679 LocationSummary(flag, LocationSummary::kNoCall);
3680 locations->SetOut(Location::RequiresRegister());
3681}
3682
3683void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3684 __ Ldr(OutputRegister(flag),
3685 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3686}
3687
David Brazdilc0b601b2016-02-08 14:20:45 +00003688static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3689 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003690 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003691}
3692
Alexandre Rames880f1192016-06-13 16:04:50 +01003693static inline Condition GetConditionForSelect(HCondition* condition) {
3694 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003695 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3696 : ARM64Condition(cond);
3697}
3698
David Brazdil74eb1b22015-12-14 11:44:01 +00003699void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003700 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003701 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003702 locations->SetInAt(0, Location::RequiresFpuRegister());
3703 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003704 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003705 } else {
3706 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3707 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3708 bool is_true_value_constant = cst_true_value != nullptr;
3709 bool is_false_value_constant = cst_false_value != nullptr;
3710 // Ask VIXL whether we should synthesize constants in registers.
3711 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3712 Operand true_op = is_true_value_constant ?
3713 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3714 Operand false_op = is_false_value_constant ?
3715 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3716 bool true_value_in_register = false;
3717 bool false_value_in_register = false;
3718 MacroAssembler::GetCselSynthesisInformation(
3719 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3720 true_value_in_register |= !is_true_value_constant;
3721 false_value_in_register |= !is_false_value_constant;
3722
3723 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3724 : Location::ConstantLocation(cst_true_value));
3725 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3726 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003727 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003728 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003729
David Brazdil74eb1b22015-12-14 11:44:01 +00003730 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3731 locations->SetInAt(2, Location::RequiresRegister());
3732 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003733}
3734
3735void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003736 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003737 Condition csel_cond;
3738
3739 if (IsBooleanValueOrMaterializedCondition(cond)) {
3740 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003741 // Use the condition flags set by the previous instruction.
3742 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003743 } else {
3744 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003745 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003746 }
3747 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003748 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003749 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003750 } else {
3751 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003752 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003753 }
3754
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003755 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003756 __ Fcsel(OutputFPRegister(select),
3757 InputFPRegisterAt(select, 1),
3758 InputFPRegisterAt(select, 0),
3759 csel_cond);
3760 } else {
3761 __ Csel(OutputRegister(select),
3762 InputOperandAt(select, 1),
3763 InputOperandAt(select, 0),
3764 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003765 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003766}
3767
David Srbecky0cf44932015-12-09 14:09:59 +00003768void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003769 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00003770}
3771
David Srbeckyd28f4a02016-03-14 17:14:24 +00003772void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3773 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003774}
3775
Vladimir Markodec78172020-06-19 15:31:23 +01003776void CodeGeneratorARM64::IncreaseFrame(size_t adjustment) {
3777 __ Claim(adjustment);
3778 GetAssembler()->cfi().AdjustCFAOffset(adjustment);
3779}
3780
3781void CodeGeneratorARM64::DecreaseFrame(size_t adjustment) {
3782 __ Drop(adjustment);
3783 GetAssembler()->cfi().AdjustCFAOffset(-adjustment);
3784}
3785
David Srbeckyc7098ff2016-02-09 14:30:11 +00003786void CodeGeneratorARM64::GenerateNop() {
3787 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003788}
3789
Alexandre Rames5319def2014-10-23 10:03:10 +01003790void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003791 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003792}
3793
3794void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003795 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003796}
3797
3798void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003799 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003800}
3801
3802void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003803 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003804}
3805
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003806// Temp is used for read barrier.
3807static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3808 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003809 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003810 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3811 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3812 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3813 return 1;
3814 }
3815 return 0;
3816}
3817
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003818// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003819// interface pointer, one for loading the current interface.
3820// The other checks have one temp for loading the object's class.
3821static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3822 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3823 return 3;
3824 }
3825 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003826}
3827
Alexandre Rames67555f72014-11-18 10:55:16 +00003828void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003829 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003830 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003831 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003832 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003833 case TypeCheckKind::kExactCheck:
3834 case TypeCheckKind::kAbstractClassCheck:
3835 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00003836 case TypeCheckKind::kArrayObjectCheck: {
3837 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
3838 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
3839 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003840 break;
Vladimir Marko87584542017-12-12 17:47:52 +00003841 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003842 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003843 case TypeCheckKind::kUnresolvedCheck:
3844 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003845 call_kind = LocationSummary::kCallOnSlowPath;
3846 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00003847 case TypeCheckKind::kBitstringCheck:
3848 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003849 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003850
Vladimir Markoca6fff82017-10-03 14:49:14 +01003851 LocationSummary* locations =
3852 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003853 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003854 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003855 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003856 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003857 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3858 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3859 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3860 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3861 } else {
3862 locations->SetInAt(1, Location::RequiresRegister());
3863 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003864 // The "out" register is used as a temporary, so it overlaps with the inputs.
3865 // Note that TypeCheckSlowPathARM64 uses this register too.
3866 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003867 // Add temps if necessary for read barriers.
3868 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003869}
3870
3871void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003872 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003873 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003874 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003875 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003876 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3877 ? Register()
3878 : InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003879 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003880 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003881 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3882 DCHECK_LE(num_temps, 1u);
3883 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003884 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3885 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3886 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3887 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003888
Scott Wakeling97c72b72016-06-24 16:19:36 +01003889 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003890 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003891
3892 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003893 // Avoid null check if we know `obj` is not null.
3894 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003895 __ Cbz(obj, &zero);
3896 }
3897
Roland Levillain44015862016-01-22 11:47:17 +00003898 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003899 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003900 ReadBarrierOption read_barrier_option =
3901 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003902 // /* HeapReference<Class> */ out = obj->klass_
3903 GenerateReferenceLoadTwoRegisters(instruction,
3904 out_loc,
3905 obj_loc,
3906 class_offset,
3907 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003908 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003909 __ Cmp(out, cls);
3910 __ Cset(out, eq);
3911 if (zero.IsLinked()) {
3912 __ B(&done);
3913 }
3914 break;
3915 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003916
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003917 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003918 ReadBarrierOption read_barrier_option =
3919 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003920 // /* HeapReference<Class> */ out = obj->klass_
3921 GenerateReferenceLoadTwoRegisters(instruction,
3922 out_loc,
3923 obj_loc,
3924 class_offset,
3925 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003926 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003927 // If the class is abstract, we eagerly fetch the super class of the
3928 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003929 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003930 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003931 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003932 GenerateReferenceLoadOneRegister(instruction,
3933 out_loc,
3934 super_offset,
3935 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003936 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003937 // If `out` is null, we use it for the result, and jump to `done`.
3938 __ Cbz(out, &done);
3939 __ Cmp(out, cls);
3940 __ B(ne, &loop);
3941 __ Mov(out, 1);
3942 if (zero.IsLinked()) {
3943 __ B(&done);
3944 }
3945 break;
3946 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003947
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003948 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003949 ReadBarrierOption read_barrier_option =
3950 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003951 // /* HeapReference<Class> */ out = obj->klass_
3952 GenerateReferenceLoadTwoRegisters(instruction,
3953 out_loc,
3954 obj_loc,
3955 class_offset,
3956 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003957 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003958 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003959 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003960 __ Bind(&loop);
3961 __ Cmp(out, cls);
3962 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003963 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003964 GenerateReferenceLoadOneRegister(instruction,
3965 out_loc,
3966 super_offset,
3967 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003968 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003969 __ Cbnz(out, &loop);
3970 // If `out` is null, we use it for the result, and jump to `done`.
3971 __ B(&done);
3972 __ Bind(&success);
3973 __ Mov(out, 1);
3974 if (zero.IsLinked()) {
3975 __ B(&done);
3976 }
3977 break;
3978 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003979
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003980 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003981 ReadBarrierOption read_barrier_option =
3982 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003983 // /* HeapReference<Class> */ out = obj->klass_
3984 GenerateReferenceLoadTwoRegisters(instruction,
3985 out_loc,
3986 obj_loc,
3987 class_offset,
3988 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003989 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003990 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003991 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003992 __ Cmp(out, cls);
3993 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003994 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003995 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003996 GenerateReferenceLoadOneRegister(instruction,
3997 out_loc,
3998 component_offset,
3999 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004000 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004001 // If `out` is null, we use it for the result, and jump to `done`.
4002 __ Cbz(out, &done);
4003 __ Ldrh(out, HeapOperand(out, primitive_offset));
4004 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4005 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004006 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004007 __ Mov(out, 1);
4008 __ B(&done);
4009 break;
4010 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004011
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004012 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004013 // No read barrier since the slow path will retry upon failure.
4014 // /* HeapReference<Class> */ out = obj->klass_
4015 GenerateReferenceLoadTwoRegisters(instruction,
4016 out_loc,
4017 obj_loc,
4018 class_offset,
4019 maybe_temp_loc,
4020 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004021 __ Cmp(out, cls);
4022 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004023 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08004024 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004025 codegen_->AddSlowPath(slow_path);
4026 __ B(ne, slow_path->GetEntryLabel());
4027 __ Mov(out, 1);
4028 if (zero.IsLinked()) {
4029 __ B(&done);
4030 }
4031 break;
4032 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004033
Calin Juravle98893e12015-10-02 21:05:03 +01004034 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004035 case TypeCheckKind::kInterfaceCheck: {
4036 // Note that we indeed only call on slow path, but we always go
4037 // into the slow path for the unresolved and interface check
4038 // cases.
4039 //
4040 // We cannot directly call the InstanceofNonTrivial runtime
4041 // entry point without resorting to a type checking slow path
4042 // here (i.e. by calling InvokeRuntime directly), as it would
4043 // require to assign fixed registers for the inputs of this
4044 // HInstanceOf instruction (following the runtime calling
4045 // convention), which might be cluttered by the potential first
4046 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004047 //
4048 // TODO: Introduce a new runtime entry point taking the object
4049 // to test (instead of its class) as argument, and let it deal
4050 // with the read barrier issues. This will let us refactor this
4051 // case of the `switch` code as it was previously (with a direct
4052 // call to the runtime not using a type checking slow path).
4053 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004054 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004055 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08004056 instruction, /* is_fatal= */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004057 codegen_->AddSlowPath(slow_path);
4058 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004059 if (zero.IsLinked()) {
4060 __ B(&done);
4061 }
4062 break;
4063 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004064
4065 case TypeCheckKind::kBitstringCheck: {
4066 // /* HeapReference<Class> */ temp = obj->klass_
4067 GenerateReferenceLoadTwoRegisters(instruction,
4068 out_loc,
4069 obj_loc,
4070 class_offset,
4071 maybe_temp_loc,
4072 kWithoutReadBarrier);
4073
4074 GenerateBitstringTypeCheckCompare(instruction, out);
4075 __ Cset(out, eq);
4076 if (zero.IsLinked()) {
4077 __ B(&done);
4078 }
4079 break;
4080 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004081 }
4082
4083 if (zero.IsLinked()) {
4084 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004085 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004086 }
4087
4088 if (done.IsLinked()) {
4089 __ Bind(&done);
4090 }
4091
4092 if (slow_path != nullptr) {
4093 __ Bind(slow_path->GetExitLabel());
4094 }
4095}
4096
4097void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004098 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00004099 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004100 LocationSummary* locations =
4101 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004102 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00004103 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
4104 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
4105 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
4106 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
4107 } else {
4108 locations->SetInAt(1, Location::RequiresRegister());
4109 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004110 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4111 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004112}
4113
4114void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004115 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004116 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004117 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004118 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00004119 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
4120 ? Register()
4121 : InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004122 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4123 DCHECK_GE(num_temps, 1u);
4124 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004125 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004126 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4127 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004128 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004129 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4130 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4131 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4132 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4133 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4134 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4135 const uint32_t object_array_data_offset =
4136 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004137
Vladimir Marko87584542017-12-12 17:47:52 +00004138 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004139 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004140 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4141 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004142 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004143
Scott Wakeling97c72b72016-06-24 16:19:36 +01004144 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004145 // Avoid null check if we know obj is not null.
4146 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004147 __ Cbz(obj, &done);
4148 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004149
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004150 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004151 case TypeCheckKind::kExactCheck:
4152 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004153 // /* HeapReference<Class> */ temp = obj->klass_
4154 GenerateReferenceLoadTwoRegisters(instruction,
4155 temp_loc,
4156 obj_loc,
4157 class_offset,
4158 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004159 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004160
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004161 __ Cmp(temp, cls);
4162 // Jump to slow path for throwing the exception or doing a
4163 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004164 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004165 break;
4166 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004167
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004168 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004169 // /* HeapReference<Class> */ temp = obj->klass_
4170 GenerateReferenceLoadTwoRegisters(instruction,
4171 temp_loc,
4172 obj_loc,
4173 class_offset,
4174 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004175 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004176
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004177 // If the class is abstract, we eagerly fetch the super class of the
4178 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004179 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004180 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004181 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004182 GenerateReferenceLoadOneRegister(instruction,
4183 temp_loc,
4184 super_offset,
4185 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004186 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004187
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004188 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4189 // exception.
4190 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4191 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004192 __ Cmp(temp, cls);
4193 __ B(ne, &loop);
4194 break;
4195 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004196
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004197 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004198 // /* HeapReference<Class> */ temp = obj->klass_
4199 GenerateReferenceLoadTwoRegisters(instruction,
4200 temp_loc,
4201 obj_loc,
4202 class_offset,
4203 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004204 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004205
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004206 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004207 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004208 __ Bind(&loop);
4209 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004210 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004211
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004212 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004213 GenerateReferenceLoadOneRegister(instruction,
4214 temp_loc,
4215 super_offset,
4216 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004217 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004218
4219 // If the class reference currently in `temp` is not null, jump
4220 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004221 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004222 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004223 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004224 break;
4225 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004226
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004227 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004228 // /* HeapReference<Class> */ temp = obj->klass_
4229 GenerateReferenceLoadTwoRegisters(instruction,
4230 temp_loc,
4231 obj_loc,
4232 class_offset,
4233 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004234 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004235
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004236 // Do an exact check.
4237 __ Cmp(temp, cls);
4238 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004239
4240 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004241 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004242 GenerateReferenceLoadOneRegister(instruction,
4243 temp_loc,
4244 component_offset,
4245 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004246 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004247
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004248 // If the component type is null, jump to the slow path to throw the exception.
4249 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4250 // Otherwise, the object is indeed an array. Further check that this component type is not a
4251 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004252 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4253 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004254 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004255 break;
4256 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004257
Calin Juravle98893e12015-10-02 21:05:03 +01004258 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004259 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004260 //
4261 // We cannot directly call the CheckCast runtime entry point
4262 // without resorting to a type checking slow path here (i.e. by
4263 // calling InvokeRuntime directly), as it would require to
4264 // assign fixed registers for the inputs of this HInstanceOf
4265 // instruction (following the runtime calling convention), which
4266 // might be cluttered by the potential first read barrier
4267 // emission at the beginning of this method.
4268 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004269 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004270 case TypeCheckKind::kInterfaceCheck: {
4271 // /* HeapReference<Class> */ temp = obj->klass_
4272 GenerateReferenceLoadTwoRegisters(instruction,
4273 temp_loc,
4274 obj_loc,
4275 class_offset,
4276 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004277 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004278
4279 // /* HeapReference<Class> */ temp = temp->iftable_
4280 GenerateReferenceLoadTwoRegisters(instruction,
4281 temp_loc,
4282 temp_loc,
4283 iftable_offset,
4284 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004285 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004286 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004287 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004288 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004289 vixl::aarch64::Label start_loop;
4290 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004291 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004292 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4293 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004294 // Go to next interface.
4295 __ Add(temp, temp, 2 * kHeapReferenceSize);
4296 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004297 // Compare the classes and continue the loop if they do not match.
4298 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4299 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004300 break;
4301 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004302
4303 case TypeCheckKind::kBitstringCheck: {
4304 // /* HeapReference<Class> */ temp = obj->klass_
4305 GenerateReferenceLoadTwoRegisters(instruction,
4306 temp_loc,
4307 obj_loc,
4308 class_offset,
4309 maybe_temp2_loc,
4310 kWithoutReadBarrier);
4311
4312 GenerateBitstringTypeCheckCompare(instruction, temp);
4313 __ B(ne, type_check_slow_path->GetEntryLabel());
4314 break;
4315 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004316 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004317 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004318
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004319 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004320}
4321
Alexandre Rames5319def2014-10-23 10:03:10 +01004322void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004323 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004324 locations->SetOut(Location::ConstantLocation(constant));
4325}
4326
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004327void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004328 // Will be generated at use site.
4329}
4330
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004331void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004332 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004333 locations->SetOut(Location::ConstantLocation(constant));
4334}
4335
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004336void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004337 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004338}
4339
Calin Juravle175dc732015-08-25 15:42:32 +01004340void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4341 // The trampoline uses the same calling convention as dex calling conventions,
4342 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4343 // the method_idx.
4344 HandleInvoke(invoke);
4345}
4346
4347void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4348 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004349 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004350}
4351
Alexandre Rames5319def2014-10-23 10:03:10 +01004352void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004353 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004354 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004355}
4356
Alexandre Rames67555f72014-11-18 10:55:16 +00004357void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4358 HandleInvoke(invoke);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004359 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
4360 // We cannot request ip1 as it's blocked by the register allocator.
4361 invoke->GetLocations()->SetInAt(invoke->GetNumberOfArguments() - 1, Location::Any());
4362 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004363}
4364
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004365void CodeGeneratorARM64::MaybeGenerateInlineCacheCheck(HInstruction* instruction,
4366 Register klass) {
4367 DCHECK_EQ(klass.GetCode(), 0u);
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004368 // We know the destination of an intrinsic, so no need to record inline
4369 // caches.
4370 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00004371 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004372 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004373 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
Nicolas Geoffray095dc462020-08-17 16:40:28 +01004374 ScopedProfilingInfoUse spiu(
4375 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
4376 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00004377 if (info != nullptr) {
4378 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
4379 uint64_t address = reinterpret_cast64<uint64_t>(cache);
4380 vixl::aarch64::Label done;
4381 __ Mov(x8, address);
4382 __ Ldr(x9, MemOperand(x8, InlineCache::ClassesOffset().Int32Value()));
4383 // Fast path for a monomorphic cache.
4384 __ Cmp(klass, x9);
4385 __ B(eq, &done);
4386 InvokeRuntime(kQuickUpdateInlineCache, instruction, instruction->GetDexPc());
4387 __ Bind(&done);
4388 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004389 }
4390}
4391
Alexandre Rames67555f72014-11-18 10:55:16 +00004392void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4393 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004394 LocationSummary* locations = invoke->GetLocations();
4395 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004396 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004397 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004398 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004399
Artem Serov914d7a82017-02-07 14:33:49 +00004400 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004401 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004402 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004403 {
4404 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4405 // /* HeapReference<Class> */ temp = temp->klass_
4406 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4407 codegen_->MaybeRecordImplicitNullCheck(invoke);
4408 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004409 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004410 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004411 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004412 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004413 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004414 }
Artem Serov914d7a82017-02-07 14:33:49 +00004415
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004416 // Instead of simply (possibly) unpoisoning `temp` here, we should
4417 // emit a read barrier for the previous class reference load.
4418 // However this is not required in practice, as this is an
4419 // intermediate/temporary reference and because the current
4420 // concurrent copying collector keeps the from-space memory
4421 // intact/accessible until the end of the marking phase (the
4422 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004423 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004424
4425 // If we're compiling baseline, update the inline cache.
4426 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
4427
4428 // The register ip1 is required to be used for the hidden argument in
4429 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
4430 MacroAssembler* masm = GetVIXLAssembler();
4431 UseScratchRegisterScope scratch_scope(masm);
4432 scratch_scope.Exclude(ip1);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004433 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
4434 Location interface_method = locations->InAt(invoke->GetNumberOfArguments() - 1);
4435 if (interface_method.IsStackSlot()) {
4436 __ Ldr(ip1, StackOperandFrom(receiver));
4437 } else {
4438 __ Mov(ip1, XRegisterFrom(interface_method));
4439 }
4440 } else {
4441 codegen_->LoadMethod(
4442 invoke->GetHiddenArgumentLoadKind(), Location::RegisterLocation(ip1.GetCode()), invoke);
4443 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004444
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004445 __ Ldr(temp,
4446 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4447 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004448 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004449 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004450 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004451 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004452 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004453
4454 {
4455 // Ensure the pc position is recorded immediately after the `blr` instruction.
4456 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4457
4458 // lr();
4459 __ blr(lr);
4460 DCHECK(!codegen_->IsLeafMethod());
4461 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4462 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004463
Andreas Gampe3db70682018-12-26 15:12:03 -08004464 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004465}
4466
4467void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004468 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004469 if (intrinsic.TryDispatch(invoke)) {
4470 return;
4471 }
4472
Alexandre Rames67555f72014-11-18 10:55:16 +00004473 HandleInvoke(invoke);
4474}
4475
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004476void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004477 // Explicit clinit checks triggered by static invokes must have been pruned by
4478 // art::PrepareForRegisterAllocation.
4479 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004480
Vladimir Markoca6fff82017-10-03 14:49:14 +01004481 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004482 if (intrinsic.TryDispatch(invoke)) {
4483 return;
4484 }
4485
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004486 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
Vladimir Marko86c87522020-05-11 16:55:55 +01004487 CriticalNativeCallingConventionVisitorARM64 calling_convention_visitor(
4488 /*for_register_allocation=*/ true);
4489 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4490 } else {
4491 HandleInvoke(invoke);
4492 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004493}
4494
Andreas Gampe878d58c2015-01-15 23:24:00 -08004495static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4496 if (invoke->GetLocations()->Intrinsified()) {
4497 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4498 intrinsic.Dispatch(invoke);
4499 return true;
4500 }
4501 return false;
4502}
4503
Vladimir Markodc151b22015-10-15 18:02:30 +01004504HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4505 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004506 ArtMethod* method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004507 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004508 return desired_dispatch_info;
4509}
4510
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004511void CodeGeneratorARM64::LoadMethod(MethodLoadKind load_kind, Location temp, HInvoke* invoke) {
4512 switch (load_kind) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004513 case MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01004514 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01004515 // Add ADRP with its PC-relative method patch.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004516 vixl::aarch64::Label* adrp_label =
4517 NewBootImageMethodPatch(invoke->GetResolvedMethodReference());
Vladimir Marko65979462017-05-19 17:25:12 +01004518 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4519 // Add ADD with its PC-relative method patch.
4520 vixl::aarch64::Label* add_label =
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004521 NewBootImageMethodPatch(invoke->GetResolvedMethodReference(), adrp_label);
Vladimir Marko65979462017-05-19 17:25:12 +01004522 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4523 break;
4524 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004525 case MethodLoadKind::kBootImageRelRo: {
Vladimir Markob066d432018-01-03 13:14:37 +00004526 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004527 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00004528 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_offset);
4529 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4530 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
4531 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_offset, adrp_label);
4532 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
4533 EmitLdrOffsetPlaceholder(ldr_label, WRegisterFrom(temp), XRegisterFrom(temp));
4534 break;
4535 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004536 case MethodLoadKind::kBssEntry: {
Vladimir Markob066d432018-01-03 13:14:37 +00004537 // Add ADRP with its PC-relative .bss entry patch.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004538 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(invoke->GetMethodReference());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004539 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Markob066d432018-01-03 13:14:37 +00004540 // Add LDR with its PC-relative .bss entry patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004541 vixl::aarch64::Label* ldr_label =
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004542 NewMethodBssEntryPatch(invoke->GetMethodReference(), adrp_label);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01004543 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004544 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004545 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004546 }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004547 case MethodLoadKind::kJitDirectAddress: {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004548 // Load method address from literal pool.
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004549 __ Ldr(XRegisterFrom(temp),
4550 DeduplicateUint64Literal(reinterpret_cast<uint64_t>(invoke->GetResolvedMethod())));
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004551 break;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004552 }
4553 case MethodLoadKind::kRuntimeCall: {
4554 // Test situation, don't do anything.
4555 break;
4556 }
4557 default: {
4558 LOG(FATAL) << "Load kind should have already been handled " << load_kind;
4559 UNREACHABLE();
4560 }
4561 }
4562}
4563
4564void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4565 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
4566 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
4567 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4568 switch (invoke->GetMethodLoadKind()) {
4569 case MethodLoadKind::kStringInit: {
4570 uint32_t offset =
4571 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4572 // temp = thread->string_init_entrypoint
4573 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
4574 break;
4575 }
4576 case MethodLoadKind::kRecursive: {
4577 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
4578 break;
4579 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004580 case MethodLoadKind::kRuntimeCall: {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004581 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4582 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004583 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004584 case MethodLoadKind::kBootImageLinkTimePcRelative:
4585 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
4586 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
4587 // Do not materialize the method pointer, load directly the entrypoint.
4588 // Add ADRP with its PC-relative JNI entrypoint patch.
4589 vixl::aarch64::Label* adrp_label =
4590 NewBootImageJniEntrypointPatch(invoke->GetResolvedMethodReference());
4591 EmitAdrpPlaceholder(adrp_label, lr);
4592 // Add the LDR with its PC-relative method patch.
4593 vixl::aarch64::Label* add_label =
4594 NewBootImageJniEntrypointPatch(invoke->GetResolvedMethodReference(), adrp_label);
4595 EmitLdrOffsetPlaceholder(add_label, lr, lr);
4596 break;
4597 }
4598 FALLTHROUGH_INTENDED;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004599 default: {
4600 LoadMethod(invoke->GetMethodLoadKind(), temp, invoke);
4601 break;
4602 }
Vladimir Marko58155012015-08-19 12:49:41 +00004603 }
4604
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004605 auto call_lr = [&]() {
4606 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4607 ExactAssemblyScope eas(GetVIXLAssembler(),
4608 kInstructionSize,
4609 CodeBufferCheckScope::kExactSize);
4610 // lr()
4611 __ blr(lr);
4612 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko86c87522020-05-11 16:55:55 +01004613 };
Vladimir Marko58155012015-08-19 12:49:41 +00004614 switch (invoke->GetCodePtrLocation()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004615 case CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004616 {
4617 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4618 ExactAssemblyScope eas(GetVIXLAssembler(),
4619 kInstructionSize,
4620 CodeBufferCheckScope::kExactSize);
4621 __ bl(&frame_entry_label_);
4622 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4623 }
Vladimir Marko58155012015-08-19 12:49:41 +00004624 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004625 case CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01004626 size_t out_frame_size =
4627 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorARM64,
4628 kAapcs64StackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01004629 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004630 if (invoke->GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative) {
4631 call_lr();
4632 } else {
4633 // LR = callee_method->ptr_sized_fields_.data_; // EntryPointFromJni
4634 MemberOffset offset = ArtMethod::EntryPointFromJniOffset(kArm64PointerSize);
4635 __ Ldr(lr, MemOperand(XRegisterFrom(callee_method), offset.Int32Value()));
4636 // lr()
4637 call_lr();
4638 }
Vladimir Marko86c87522020-05-11 16:55:55 +01004639 // Zero-/sign-extend the result when needed due to native and managed ABI mismatch.
4640 switch (invoke->GetType()) {
4641 case DataType::Type::kBool:
4642 __ Ubfx(w0, w0, 0, 8);
4643 break;
4644 case DataType::Type::kInt8:
4645 __ Sbfx(w0, w0, 0, 8);
4646 break;
4647 case DataType::Type::kUint16:
4648 __ Ubfx(w0, w0, 0, 16);
4649 break;
4650 case DataType::Type::kInt16:
4651 __ Sbfx(w0, w0, 0, 16);
4652 break;
4653 case DataType::Type::kInt32:
4654 case DataType::Type::kInt64:
4655 case DataType::Type::kFloat32:
4656 case DataType::Type::kFloat64:
4657 case DataType::Type::kVoid:
4658 break;
4659 default:
4660 DCHECK(false) << invoke->GetType();
4661 break;
4662 }
4663 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01004664 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01004665 }
4666 break;
4667 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004668 case CodePtrLocation::kCallArtMethod: {
4669 // LR = callee_method->ptr_sized_fields_.entry_point_from_quick_compiled_code_;
4670 MemberOffset offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
4671 __ Ldr(lr, MemOperand(XRegisterFrom(callee_method), offset.Int32Value()));
4672 // lr()
4673 call_lr();
Vladimir Marko58155012015-08-19 12:49:41 +00004674 break;
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004675 }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004676 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004677
Andreas Gampe878d58c2015-01-15 23:24:00 -08004678 DCHECK(!IsLeafMethod());
4679}
4680
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004681void CodeGeneratorARM64::GenerateVirtualCall(
4682 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004683 // Use the calling convention instead of the location of the receiver, as
4684 // intrinsics may have put the receiver in a different register. In the intrinsics
4685 // slow path, the arguments have been moved to the right place, so here we are
4686 // guaranteed that the receiver is the first register of the calling convention.
4687 InvokeDexCallingConvention calling_convention;
4688 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004689 Register temp = XRegisterFrom(temp_in);
4690 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4691 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4692 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004693 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004694
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004695 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004696
4697 {
4698 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4699 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4700 // /* HeapReference<Class> */ temp = receiver->klass_
4701 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4702 MaybeRecordImplicitNullCheck(invoke);
4703 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004704 // Instead of simply (possibly) unpoisoning `temp` here, we should
4705 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004706 // intermediate/temporary reference and because the current
4707 // concurrent copying collector keeps the from-space memory
4708 // intact/accessible until the end of the marking phase (the
4709 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004710 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004711
4712 // If we're compiling baseline, update the inline cache.
4713 MaybeGenerateInlineCacheCheck(invoke, temp);
4714
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004715 // temp = temp->GetMethodAt(method_offset);
4716 __ Ldr(temp, MemOperand(temp, method_offset));
4717 // lr = temp->GetEntryPoint();
4718 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004719 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004720 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004721 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4722 // lr();
4723 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004724 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004725 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004726}
4727
Vladimir Marko9922f002020-06-08 15:05:15 +01004728void CodeGeneratorARM64::MoveFromReturnRegister(Location trg, DataType::Type type) {
4729 if (!trg.IsValid()) {
4730 DCHECK(type == DataType::Type::kVoid);
4731 return;
4732 }
4733
4734 DCHECK_NE(type, DataType::Type::kVoid);
4735
4736 if (DataType::IsIntegralType(type) || type == DataType::Type::kReference) {
4737 Register trg_reg = RegisterFrom(trg, type);
4738 Register res_reg = RegisterFrom(ARM64ReturnLocation(type), type);
4739 __ Mov(trg_reg, res_reg, kDiscardForSameWReg);
4740 } else {
4741 VRegister trg_reg = FPRegisterFrom(trg, type);
4742 VRegister res_reg = FPRegisterFrom(ARM64ReturnLocation(type), type);
4743 __ Fmov(trg_reg, res_reg);
4744 }
4745}
4746
Orion Hodsonac141392017-01-13 11:53:47 +00004747void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00004748 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
4749 if (intrinsic.TryDispatch(invoke)) {
4750 return;
4751 }
Orion Hodsonac141392017-01-13 11:53:47 +00004752 HandleInvoke(invoke);
4753}
4754
4755void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00004756 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4757 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
4758 return;
4759 }
Orion Hodsonac141392017-01-13 11:53:47 +00004760 codegen_->GenerateInvokePolymorphicCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004761 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004762}
4763
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004764void LocationsBuilderARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4765 HandleInvoke(invoke);
4766}
4767
4768void InstructionCodeGeneratorARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4769 codegen_->GenerateInvokeCustomCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004770 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004771}
4772
Vladimir Marko6fd16062018-06-26 11:02:04 +01004773vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageIntrinsicPatch(
4774 uint32_t intrinsic_data,
4775 vixl::aarch64::Label* adrp_label) {
4776 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004777 /* dex_file= */ nullptr, intrinsic_data, adrp_label, &boot_image_other_patches_);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004778}
4779
Vladimir Markob066d432018-01-03 13:14:37 +00004780vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageRelRoPatch(
4781 uint32_t boot_image_offset,
4782 vixl::aarch64::Label* adrp_label) {
4783 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004784 /* dex_file= */ nullptr, boot_image_offset, adrp_label, &boot_image_other_patches_);
Vladimir Markob066d432018-01-03 13:14:37 +00004785}
4786
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004787vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004788 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004789 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004790 return NewPcRelativePatch(
4791 target_method.dex_file, target_method.index, adrp_label, &boot_image_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004792}
4793
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004794vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4795 MethodReference target_method,
4796 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004797 return NewPcRelativePatch(
4798 target_method.dex_file, target_method.index, adrp_label, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004799}
4800
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004801vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageTypePatch(
Scott Wakeling97c72b72016-06-24 16:19:36 +01004802 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004803 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004804 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004805 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &boot_image_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004806}
4807
Vladimir Marko1998cd02017-01-13 13:02:58 +00004808vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
Vladimir Marko8f63f102020-09-28 12:10:28 +01004809 HLoadClass* load_class,
Vladimir Marko1998cd02017-01-13 13:02:58 +00004810 vixl::aarch64::Label* adrp_label) {
Vladimir Marko8f63f102020-09-28 12:10:28 +01004811 const DexFile& dex_file = load_class->GetDexFile();
4812 dex::TypeIndex type_index = load_class->GetTypeIndex();
4813 ArenaDeque<PcRelativePatchInfo>* patches = nullptr;
4814 switch (load_class->GetLoadKind()) {
4815 case HLoadClass::LoadKind::kBssEntry:
4816 patches = &type_bss_entry_patches_;
4817 break;
4818 case HLoadClass::LoadKind::kBssEntryPublic:
4819 patches = &public_type_bss_entry_patches_;
4820 break;
4821 case HLoadClass::LoadKind::kBssEntryPackage:
4822 patches = &package_type_bss_entry_patches_;
4823 break;
4824 default:
4825 LOG(FATAL) << "Unexpected load kind: " << load_class->GetLoadKind();
4826 UNREACHABLE();
4827 }
4828 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004829}
4830
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004831vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004832 const DexFile& dex_file,
4833 dex::StringIndex string_index,
4834 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004835 return NewPcRelativePatch(
4836 &dex_file, string_index.index_, adrp_label, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01004837}
4838
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004839vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4840 const DexFile& dex_file,
4841 dex::StringIndex string_index,
4842 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004843 return NewPcRelativePatch(&dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004844}
4845
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004846vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageJniEntrypointPatch(
4847 MethodReference target_method,
4848 vixl::aarch64::Label* adrp_label) {
4849 return NewPcRelativePatch(
4850 target_method.dex_file, target_method.index, adrp_label, &boot_image_jni_entrypoint_patches_);
4851}
4852
Vladimir Markof6675082019-05-17 12:05:28 +01004853void CodeGeneratorARM64::EmitEntrypointThunkCall(ThreadOffset64 entrypoint_offset) {
4854 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01004855 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markof6675082019-05-17 12:05:28 +01004856 call_entrypoint_patches_.emplace_back(/*dex_file*/ nullptr, entrypoint_offset.Uint32Value());
4857 vixl::aarch64::Label* bl_label = &call_entrypoint_patches_.back().label;
4858 __ bind(bl_label);
4859 __ bl(static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4860}
4861
Vladimir Marko966b46f2018-08-03 10:20:19 +00004862void CodeGeneratorARM64::EmitBakerReadBarrierCbnz(uint32_t custom_data) {
Vladimir Marko94796f82018-08-08 15:15:33 +01004863 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01004864 if (GetCompilerOptions().IsJitCompiler()) {
Vladimir Marko966b46f2018-08-03 10:20:19 +00004865 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
4866 vixl::aarch64::Label* slow_path_entry = &it->second.label;
4867 __ cbnz(mr, slow_path_entry);
4868 } else {
4869 baker_read_barrier_patches_.emplace_back(custom_data);
4870 vixl::aarch64::Label* cbnz_label = &baker_read_barrier_patches_.back().label;
4871 __ bind(cbnz_label);
4872 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4873 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004874}
4875
Scott Wakeling97c72b72016-06-24 16:19:36 +01004876vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004877 const DexFile* dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004878 uint32_t offset_or_index,
4879 vixl::aarch64::Label* adrp_label,
4880 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004881 // Add a patch entry and return the label.
4882 patches->emplace_back(dex_file, offset_or_index);
4883 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004884 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004885 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4886 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4887 return label;
4888}
4889
Scott Wakeling97c72b72016-06-24 16:19:36 +01004890vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4891 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004892 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004893}
4894
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004895vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004896 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004897 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004898 return jit_string_patches_.GetOrCreate(
4899 StringReference(&dex_file, string_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004900 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004901}
4902
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004903vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004904 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004905 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004906 return jit_class_patches_.GetOrCreate(
4907 TypeReference(&dex_file, type_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004908 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004909}
4910
Vladimir Markoaad75c62016-10-03 08:46:48 +00004911void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4912 vixl::aarch64::Register reg) {
4913 DCHECK(reg.IsX());
4914 SingleEmissionCheckScope guard(GetVIXLAssembler());
4915 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004916 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004917}
4918
4919void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4920 vixl::aarch64::Register out,
4921 vixl::aarch64::Register base) {
4922 DCHECK(out.IsX());
4923 DCHECK(base.IsX());
4924 SingleEmissionCheckScope guard(GetVIXLAssembler());
4925 __ Bind(fixup_label);
4926 __ add(out, base, Operand(/* offset placeholder */ 0));
4927}
4928
4929void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4930 vixl::aarch64::Register out,
4931 vixl::aarch64::Register base) {
4932 DCHECK(base.IsX());
4933 SingleEmissionCheckScope guard(GetVIXLAssembler());
4934 __ Bind(fixup_label);
4935 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4936}
4937
Vladimir Markoeebb8212018-06-05 14:57:24 +01004938void CodeGeneratorARM64::LoadBootImageAddress(vixl::aarch64::Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01004939 uint32_t boot_image_reference) {
4940 if (GetCompilerOptions().IsBootImage()) {
4941 // Add ADRP with its PC-relative type patch.
4942 vixl::aarch64::Label* adrp_label = NewBootImageIntrinsicPatch(boot_image_reference);
4943 EmitAdrpPlaceholder(adrp_label, reg.X());
4944 // Add ADD with its PC-relative type patch.
4945 vixl::aarch64::Label* add_label = NewBootImageIntrinsicPatch(boot_image_reference, adrp_label);
4946 EmitAddPlaceholder(add_label, reg.X(), reg.X());
Vladimir Markoa2da9b92018-10-10 14:21:55 +01004947 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01004948 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004949 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004950 EmitAdrpPlaceholder(adrp_label, reg.X());
4951 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004952 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_reference, adrp_label);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004953 EmitLdrOffsetPlaceholder(ldr_label, reg.W(), reg.X());
4954 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01004955 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01004956 gc::Heap* heap = Runtime::Current()->GetHeap();
4957 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004958 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01004959 __ Ldr(reg.W(), DeduplicateBootImageAddressLiteral(reinterpret_cast<uintptr_t>(address)));
4960 }
4961}
4962
Vladimir Markode91ca92020-10-27 13:41:40 +00004963void CodeGeneratorARM64::LoadIntrinsicDeclaringClass(vixl::aarch64::Register reg, HInvoke* invoke) {
4964 DCHECK_NE(invoke->GetIntrinsic(), Intrinsics::kNone);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004965 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01004966 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004967 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01004968 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
4969 // Add ADRP with its PC-relative type patch.
4970 vixl::aarch64::Label* adrp_label = NewBootImageTypePatch(*target_method.dex_file, type_idx);
Vladimir Markode91ca92020-10-27 13:41:40 +00004971 EmitAdrpPlaceholder(adrp_label, reg.X());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004972 // Add ADD with its PC-relative type patch.
4973 vixl::aarch64::Label* add_label =
4974 NewBootImageTypePatch(*target_method.dex_file, type_idx, adrp_label);
Vladimir Markode91ca92020-10-27 13:41:40 +00004975 EmitAddPlaceholder(add_label, reg.X(), reg.X());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004976 } else {
Vladimir Markode91ca92020-10-27 13:41:40 +00004977 uint32_t boot_image_offset = GetBootImageOffsetOfIntrinsicDeclaringClass(invoke);
4978 LoadBootImageAddress(reg, boot_image_offset);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004979 }
Vladimir Marko6fd16062018-06-26 11:02:04 +01004980}
4981
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004982template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004983inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4984 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004985 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004986 for (const PcRelativePatchInfo& info : infos) {
4987 linker_patches->push_back(Factory(info.label.GetLocation(),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004988 info.target_dex_file,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004989 info.pc_insn_label->GetLocation(),
4990 info.offset_or_index));
4991 }
4992}
4993
Vladimir Marko6fd16062018-06-26 11:02:04 +01004994template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
4995linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
4996 const DexFile* target_dex_file,
4997 uint32_t pc_insn_offset,
4998 uint32_t boot_image_offset) {
4999 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5000 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005001}
5002
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005003void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005004 DCHECK(linker_patches->empty());
5005 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005006 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005007 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005008 boot_image_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005009 type_bss_entry_patches_.size() +
Vladimir Marko8f63f102020-09-28 12:10:28 +01005010 public_type_bss_entry_patches_.size() +
5011 package_type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005012 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005013 string_bss_entry_patches_.size() +
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005014 boot_image_jni_entrypoint_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005015 boot_image_other_patches_.size() +
Vladimir Markof6675082019-05-17 12:05:28 +01005016 call_entrypoint_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005017 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00005018 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005019 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005020 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005021 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005022 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005023 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005024 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005025 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01005026 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005027 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005028 DCHECK(boot_image_type_patches_.empty());
5029 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005030 }
5031 if (GetCompilerOptions().IsBootImage()) {
5032 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5033 boot_image_other_patches_, linker_patches);
5034 } else {
5035 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5036 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005037 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005038 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5039 method_bss_entry_patches_, linker_patches);
5040 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5041 type_bss_entry_patches_, linker_patches);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005042 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PublicTypeBssEntryPatch>(
5043 public_type_bss_entry_patches_, linker_patches);
5044 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PackageTypeBssEntryPatch>(
5045 package_type_bss_entry_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005046 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5047 string_bss_entry_patches_, linker_patches);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005048 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeJniEntrypointPatch>(
5049 boot_image_jni_entrypoint_patches_, linker_patches);
Vladimir Markof6675082019-05-17 12:05:28 +01005050 for (const PatchInfo<vixl::aarch64::Label>& info : call_entrypoint_patches_) {
5051 DCHECK(info.target_dex_file == nullptr);
5052 linker_patches->push_back(linker::LinkerPatch::CallEntrypointPatch(
5053 info.label.GetLocation(), info.offset_or_index));
5054 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005055 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005056 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
5057 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005058 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00005059 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005060}
5061
Vladimir Markoca1e0382018-04-11 09:58:41 +00005062bool CodeGeneratorARM64::NeedsThunkCode(const linker::LinkerPatch& patch) const {
Vladimir Markof6675082019-05-17 12:05:28 +01005063 return patch.GetType() == linker::LinkerPatch::Type::kCallEntrypoint ||
5064 patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
Vladimir Markoca1e0382018-04-11 09:58:41 +00005065 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
5066}
5067
5068void CodeGeneratorARM64::EmitThunkCode(const linker::LinkerPatch& patch,
5069 /*out*/ ArenaVector<uint8_t>* code,
5070 /*out*/ std::string* debug_name) {
5071 Arm64Assembler assembler(GetGraph()->GetAllocator());
5072 switch (patch.GetType()) {
5073 case linker::LinkerPatch::Type::kCallRelative: {
5074 // The thunk just uses the entry point in the ArtMethod. This works even for calls
5075 // to the generic JNI and interpreter trampolines.
5076 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
5077 kArm64PointerSize).Int32Value());
5078 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
5079 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
5080 *debug_name = "MethodCallThunk";
5081 }
5082 break;
5083 }
Vladimir Markof6675082019-05-17 12:05:28 +01005084 case linker::LinkerPatch::Type::kCallEntrypoint: {
5085 Offset offset(patch.EntrypointOffset());
5086 assembler.JumpTo(ManagedRegister(arm64::TR), offset, ManagedRegister(arm64::IP0));
5087 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
5088 *debug_name = "EntrypointCallThunk_" + std::to_string(offset.Uint32Value());
5089 }
5090 break;
5091 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00005092 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
5093 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
5094 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
5095 break;
5096 }
5097 default:
5098 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
5099 UNREACHABLE();
5100 }
5101
5102 // Ensure we emit the literal pool if any.
5103 assembler.FinalizeCode();
5104 code->resize(assembler.CodeSize());
5105 MemoryRegion code_region(code->data(), code->size());
5106 assembler.FinalizeInstructions(code_region);
5107}
5108
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005109vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
5110 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005111 value,
5112 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
5113}
5114
Scott Wakeling97c72b72016-06-24 16:19:36 +01005115vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005116 return uint64_literals_.GetOrCreate(
5117 value,
5118 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00005119}
5120
Andreas Gampe878d58c2015-01-15 23:24:00 -08005121void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005122 // Explicit clinit checks triggered by static invokes must have been pruned by
5123 // art::PrepareForRegisterAllocation.
5124 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01005125
Andreas Gampe878d58c2015-01-15 23:24:00 -08005126 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08005127 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08005128 return;
5129 }
5130
Vladimir Marko86c87522020-05-11 16:55:55 +01005131 LocationSummary* locations = invoke->GetLocations();
5132 codegen_->GenerateStaticOrDirectCall(
5133 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005134
Andreas Gampe3db70682018-12-26 15:12:03 -08005135 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005136}
5137
5138void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08005139 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08005140 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08005141 return;
5142 }
5143
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005144 {
5145 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
5146 // are no pools emitted.
5147 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
5148 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
5149 DCHECK(!codegen_->IsLeafMethod());
5150 }
5151
Andreas Gampe3db70682018-12-26 15:12:03 -08005152 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005153}
5154
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005155HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
5156 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005157 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005158 case HLoadClass::LoadKind::kInvalid:
5159 LOG(FATAL) << "UNREACHABLE";
5160 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005161 case HLoadClass::LoadKind::kReferrersClass:
5162 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005163 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005164 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005165 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8f63f102020-09-28 12:10:28 +01005166 case HLoadClass::LoadKind::kBssEntryPublic:
5167 case HLoadClass::LoadKind::kBssEntryPackage:
Vladimir Marko695348f2020-05-19 14:42:02 +01005168 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005169 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005170 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005171 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01005172 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005173 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005174 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005175 break;
5176 }
5177 return desired_class_load_kind;
5178}
5179
Alexandre Rames67555f72014-11-18 10:55:16 +00005180void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005181 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005182 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005183 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005184 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005185 cls,
5186 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005187 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00005188 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005189 return;
5190 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005191 DCHECK_EQ(cls->NeedsAccessCheck(),
5192 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
5193 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005194
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005195 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5196 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005197 ? LocationSummary::kCallOnSlowPath
5198 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005199 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005200 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005201 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005202 }
5203
Vladimir Marko41559982017-01-06 14:04:23 +00005204 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005205 locations->SetInAt(0, Location::RequiresRegister());
5206 }
5207 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005208 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
5209 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5210 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005211 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005212 } else {
5213 // For non-Baker read barrier we have a temp-clobbering call.
5214 }
5215 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005216}
5217
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005218// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5219// move.
5220void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005221 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005222 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005223 codegen_->GenerateLoadClassRuntimeCall(cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08005224 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01005225 return;
5226 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005227 DCHECK_EQ(cls->NeedsAccessCheck(),
5228 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
5229 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Calin Juravle580b6092015-10-06 17:35:58 +01005230
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005231 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01005232 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00005233
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005234 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5235 ? kWithoutReadBarrier
5236 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005237 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005238 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005239 case HLoadClass::LoadKind::kReferrersClass: {
5240 DCHECK(!cls->CanCallRuntime());
5241 DCHECK(!cls->MustGenerateClinitCheck());
5242 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5243 Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00005244 codegen_->GenerateGcRootFieldLoad(cls,
5245 out_loc,
5246 current_method,
5247 ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005248 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005249 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005250 break;
5251 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005252 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005253 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5254 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005255 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005256 // Add ADRP with its PC-relative type patch.
5257 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08005258 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005259 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005260 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005261 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005262 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005263 codegen_->NewBootImageTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005264 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005265 break;
5266 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005267 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005268 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markode91ca92020-10-27 13:41:40 +00005269 uint32_t boot_image_offset = CodeGenerator::GetBootImageOffset(cls);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005270 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5271 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005272 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005273 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005274 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005275 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005276 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005277 break;
5278 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005279 case HLoadClass::LoadKind::kBssEntry:
5280 case HLoadClass::LoadKind::kBssEntryPublic:
5281 case HLoadClass::LoadKind::kBssEntryPackage: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005282 // Add ADRP with its PC-relative Class .bss entry patch.
Vladimir Markof3c52b42017-11-17 17:32:12 +00005283 vixl::aarch64::Register temp = XRegisterFrom(out_loc);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005284 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(cls);
Vladimir Markof3c52b42017-11-17 17:32:12 +00005285 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005286 // Add LDR with its PC-relative Class .bss entry patch.
Vladimir Marko8f63f102020-09-28 12:10:28 +01005287 vixl::aarch64::Label* ldr_label = codegen_->NewBssEntryTypePatch(cls, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005288 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005289 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005290 codegen_->GenerateGcRootFieldLoad(cls,
5291 out_loc,
5292 temp,
5293 /* offset placeholder */ 0u,
5294 ldr_label,
5295 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005296 generate_null_check = true;
5297 break;
5298 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005299 case HLoadClass::LoadKind::kJitBootImageAddress: {
5300 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5301 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
5302 DCHECK_NE(address, 0u);
5303 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5304 break;
5305 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005306 case HLoadClass::LoadKind::kJitTableAddress: {
5307 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5308 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005309 cls->GetClass()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005310 codegen_->GenerateGcRootFieldLoad(cls,
5311 out_loc,
5312 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005313 /* offset= */ 0,
5314 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005315 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005316 break;
5317 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005318 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005319 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005320 LOG(FATAL) << "UNREACHABLE";
5321 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005322 }
5323
Vladimir Markoea4c1262017-02-06 19:59:33 +00005324 bool do_clinit = cls->MustGenerateClinitCheck();
5325 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005326 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01005327 SlowPathCodeARM64* slow_path =
5328 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005329 codegen_->AddSlowPath(slow_path);
5330 if (generate_null_check) {
5331 __ Cbz(out, slow_path->GetEntryLabel());
5332 }
5333 if (cls->MustGenerateClinitCheck()) {
5334 GenerateClassInitializationCheck(slow_path, out);
5335 } else {
5336 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005337 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005338 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005339 }
5340}
5341
Orion Hodsondbaa5c72018-05-10 08:22:46 +01005342void LocationsBuilderARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5343 InvokeRuntimeCallingConvention calling_convention;
5344 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5345 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
5346}
5347
5348void InstructionCodeGeneratorARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5349 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
5350}
5351
Orion Hodson18259d72018-04-12 11:18:23 +01005352void LocationsBuilderARM64::VisitLoadMethodType(HLoadMethodType* load) {
5353 InvokeRuntimeCallingConvention calling_convention;
5354 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5355 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
5356}
5357
5358void InstructionCodeGeneratorARM64::VisitLoadMethodType(HLoadMethodType* load) {
5359 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
5360}
5361
David Brazdilcb1c0552015-08-04 16:22:25 +01005362static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005363 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005364}
5365
Alexandre Rames67555f72014-11-18 10:55:16 +00005366void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5367 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005368 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00005369 locations->SetOut(Location::RequiresRegister());
5370}
5371
5372void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005373 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5374}
5375
5376void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005377 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005378}
5379
5380void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5381 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005382}
5383
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005384HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5385 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005386 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005387 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005388 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005389 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01005390 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005391 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005392 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005393 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01005394 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005395 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005396 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005397 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005398 }
5399 return desired_string_load_kind;
5400}
5401
Alexandre Rames67555f72014-11-18 10:55:16 +00005402void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005403 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005404 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005405 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005406 InvokeRuntimeCallingConvention calling_convention;
5407 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5408 } else {
5409 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005410 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5411 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005412 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005413 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005414 } else {
5415 // For non-Baker read barrier we have a temp-clobbering call.
5416 }
5417 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005418 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005419}
5420
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005421// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5422// move.
5423void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005424 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005425 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005426
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005427 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005428 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005429 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5430 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005431 // Add ADRP with its PC-relative String patch.
5432 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005433 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005434 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005435 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005436 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005437 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005438 codegen_->NewBootImageStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005439 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005440 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005441 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005442 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005443 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005444 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markode91ca92020-10-27 13:41:40 +00005445 uint32_t boot_image_offset = CodeGenerator::GetBootImageOffset(load);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005446 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005447 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005448 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005449 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005450 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005451 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
5452 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005453 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005454 case HLoadString::LoadKind::kBssEntry: {
5455 // Add ADRP with its PC-relative String .bss entry patch.
5456 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005457 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00005458 Register temp = XRegisterFrom(out_loc);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005459 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005460 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005461 // Add LDR with its PC-relative String .bss entry patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005462 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005463 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005464 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005465 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005466 codegen_->GenerateGcRootFieldLoad(load,
5467 out_loc,
5468 temp,
5469 /* offset placeholder */ 0u,
5470 ldr_label,
5471 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005472 SlowPathCodeARM64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005473 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005474 codegen_->AddSlowPath(slow_path);
5475 __ Cbz(out.X(), slow_path->GetEntryLabel());
5476 __ Bind(slow_path->GetExitLabel());
Andreas Gampe3db70682018-12-26 15:12:03 -08005477 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005478 return;
5479 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005480 case HLoadString::LoadKind::kJitBootImageAddress: {
5481 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
5482 DCHECK_NE(address, 0u);
5483 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5484 return;
5485 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005486 case HLoadString::LoadKind::kJitTableAddress: {
5487 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005488 load->GetStringIndex(),
5489 load->GetString()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005490 codegen_->GenerateGcRootFieldLoad(load,
5491 out_loc,
5492 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005493 /* offset= */ 0,
5494 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005495 kCompilerReadBarrierOption);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005496 return;
5497 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005498 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005499 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005500 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005501
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005502 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005503 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005504 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005505 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005506 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5507 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005508 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005509}
5510
Alexandre Rames5319def2014-10-23 10:03:10 +01005511void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005512 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01005513 locations->SetOut(Location::ConstantLocation(constant));
5514}
5515
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005516void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005517 // Will be generated at use site.
5518}
5519
Alexandre Rames67555f72014-11-18 10:55:16 +00005520void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005521 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5522 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005523 InvokeRuntimeCallingConvention calling_convention;
5524 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5525}
5526
5527void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005528 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005529 instruction,
5530 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005531 if (instruction->IsEnter()) {
5532 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5533 } else {
5534 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5535 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005536 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005537}
5538
Alexandre Rames42d641b2014-10-27 14:00:51 +00005539void LocationsBuilderARM64::VisitMul(HMul* mul) {
5540 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005541 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005542 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005543 case DataType::Type::kInt32:
5544 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005545 locations->SetInAt(0, Location::RequiresRegister());
5546 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005547 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005548 break;
5549
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005550 case DataType::Type::kFloat32:
5551 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005552 locations->SetInAt(0, Location::RequiresFpuRegister());
5553 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005554 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005555 break;
5556
5557 default:
5558 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5559 }
5560}
5561
5562void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5563 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005564 case DataType::Type::kInt32:
5565 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005566 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5567 break;
5568
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005569 case DataType::Type::kFloat32:
5570 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005571 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005572 break;
5573
5574 default:
5575 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5576 }
5577}
5578
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005579void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5580 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005581 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005582 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005583 case DataType::Type::kInt32:
5584 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005585 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005586 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005587 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005588
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005589 case DataType::Type::kFloat32:
5590 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005591 locations->SetInAt(0, Location::RequiresFpuRegister());
5592 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005593 break;
5594
5595 default:
5596 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5597 }
5598}
5599
5600void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5601 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005602 case DataType::Type::kInt32:
5603 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005604 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5605 break;
5606
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005607 case DataType::Type::kFloat32:
5608 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005609 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005610 break;
5611
5612 default:
5613 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5614 }
5615}
5616
5617void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005618 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5619 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005620 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005621 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005622 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5623 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005624}
5625
5626void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005627 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5628 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005629 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005630 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005631 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005632}
5633
Alexandre Rames5319def2014-10-23 10:03:10 +01005634void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005635 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5636 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005637 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07005638 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005639 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005640}
5641
5642void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005643 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5644 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005645 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005646}
5647
5648void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005649 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005650 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005651 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005652}
5653
5654void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005655 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005656 case DataType::Type::kInt32:
5657 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005658 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005659 break;
5660
5661 default:
5662 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5663 }
5664}
5665
David Brazdil66d126e2015-04-03 16:02:44 +01005666void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005667 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005668 locations->SetInAt(0, Location::RequiresRegister());
5669 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5670}
5671
5672void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005673 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005674}
5675
Alexandre Rames5319def2014-10-23 10:03:10 +01005676void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005677 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5678 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005679}
5680
Calin Juravle2ae48182016-03-16 14:05:09 +00005681void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5682 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005683 return;
5684 }
Artem Serov914d7a82017-02-07 14:33:49 +00005685 {
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01005686 // Ensure that between load and RecordPcInfo there are no pools emitted.
Artem Serov914d7a82017-02-07 14:33:49 +00005687 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5688 Location obj = instruction->GetLocations()->InAt(0);
5689 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5690 RecordPcInfo(instruction, instruction->GetDexPc());
5691 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005692}
5693
Calin Juravle2ae48182016-03-16 14:05:09 +00005694void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005695 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005696 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005697
5698 LocationSummary* locations = instruction->GetLocations();
5699 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005700
5701 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005702}
5703
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005704void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005705 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005706}
5707
Alexandre Rames67555f72014-11-18 10:55:16 +00005708void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5709 HandleBinaryOp(instruction);
5710}
5711
5712void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5713 HandleBinaryOp(instruction);
5714}
5715
Alexandre Rames3e69f162014-12-10 10:36:50 +00005716void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5717 LOG(FATAL) << "Unreachable";
5718}
5719
5720void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005721 if (instruction->GetNext()->IsSuspendCheck() &&
5722 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5723 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5724 // The back edge will generate the suspend check.
5725 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5726 }
5727
Alexandre Rames3e69f162014-12-10 10:36:50 +00005728 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5729}
5730
Alexandre Rames5319def2014-10-23 10:03:10 +01005731void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005732 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005733 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5734 if (location.IsStackSlot()) {
5735 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5736 } else if (location.IsDoubleStackSlot()) {
5737 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5738 }
5739 locations->SetOut(location);
5740}
5741
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005742void InstructionCodeGeneratorARM64::VisitParameterValue(
5743 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005744 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005745}
5746
5747void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5748 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005749 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005750 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005751}
5752
5753void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5754 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5755 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005756}
5757
5758void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005759 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005760 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005761 locations->SetInAt(i, Location::Any());
5762 }
5763 locations->SetOut(Location::Any());
5764}
5765
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005766void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005767 LOG(FATAL) << "Unreachable";
5768}
5769
Serban Constantinescu02164b32014-11-13 14:05:07 +00005770void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005771 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005772 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005773 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005774 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005775 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005776
5777 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005778 case DataType::Type::kInt32:
5779 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005780 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005781 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005782 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5783 break;
5784
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005785 case DataType::Type::kFloat32:
5786 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005787 InvokeRuntimeCallingConvention calling_convention;
5788 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5789 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5790 locations->SetOut(calling_convention.GetReturnLocation(type));
5791
5792 break;
5793 }
5794
Serban Constantinescu02164b32014-11-13 14:05:07 +00005795 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005796 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005797 }
5798}
5799
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005800void InstructionCodeGeneratorARM64::GenerateIntRemForPower2Denom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005801 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005802 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
5803 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
5804
5805 Register out = OutputRegister(instruction);
5806 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005807
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01005808 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01005809 // No need to adjust the result for non-negative dividends or the INT32_MIN/INT64_MIN dividends.
5810 // NOTE: The generated code for HRem correctly works for the INT32_MIN/INT64_MIN dividends.
5811 // INT*_MIN % imm must be 0 for any imm of power 2. 'and' works only with bits
5812 // 0..30 (Int32 case)/0..62 (Int64 case) of a dividend. For INT32_MIN/INT64_MIN they are zeros.
5813 // So 'and' always produces zero.
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005814 __ And(out, dividend, abs_imm - 1);
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01005815 } else {
5816 if (abs_imm == 2) {
5817 __ Cmp(dividend, 0);
5818 __ And(out, dividend, 1);
5819 __ Csneg(out, out, out, ge);
5820 } else {
5821 UseScratchRegisterScope temps(GetVIXLAssembler());
5822 Register temp = temps.AcquireSameSizeAs(out);
5823
5824 __ Negs(temp, dividend);
5825 __ And(out, dividend, abs_imm - 1);
5826 __ And(temp, temp, abs_imm - 1);
5827 __ Csneg(out, out, temp, mi);
5828 }
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005829 }
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005830}
5831
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005832void InstructionCodeGeneratorARM64::GenerateIntRemForConstDenom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005833 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005834
5835 if (imm == 0) {
5836 // Do not generate anything.
5837 // DivZeroCheck would prevent any code to be executed.
5838 return;
5839 }
5840
Evgeny Astigeevichf58dc652018-06-25 17:54:07 +01005841 if (IsPowerOfTwo(AbsOrMin(imm))) {
5842 // Cases imm == -1 or imm == 1 are handled in constant folding by
5843 // InstructionWithAbsorbingInputSimplifier.
5844 // If the cases have survided till code generation they are handled in
5845 // GenerateIntRemForPower2Denom becauses -1 and 1 are the power of 2 (2^0).
5846 // The correct code is generated for them, just more instructions.
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005847 GenerateIntRemForPower2Denom(instruction);
5848 } else {
5849 DCHECK(imm < -2 || imm > 2) << imm;
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01005850 GenerateDivRemWithAnyConstant(instruction, imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005851 }
5852}
5853
5854void InstructionCodeGeneratorARM64::GenerateIntRem(HRem* instruction) {
5855 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
5856 << instruction->GetResultType();
5857
5858 if (instruction->GetLocations()->InAt(1).IsConstant()) {
5859 GenerateIntRemForConstDenom(instruction);
5860 } else {
5861 Register out = OutputRegister(instruction);
5862 Register dividend = InputRegisterAt(instruction, 0);
5863 Register divisor = InputRegisterAt(instruction, 1);
5864 UseScratchRegisterScope temps(GetVIXLAssembler());
5865 Register temp = temps.AcquireSameSizeAs(out);
5866 __ Sdiv(temp, dividend, divisor);
5867 __ Msub(out, temp, divisor, dividend);
5868 }
5869}
5870
Serban Constantinescu02164b32014-11-13 14:05:07 +00005871void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005872 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005873
Serban Constantinescu02164b32014-11-13 14:05:07 +00005874 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005875 case DataType::Type::kInt32:
5876 case DataType::Type::kInt64: {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005877 GenerateIntRem(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005878 break;
5879 }
5880
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005881 case DataType::Type::kFloat32:
5882 case DataType::Type::kFloat64: {
5883 QuickEntrypointEnum entrypoint =
5884 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005885 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005886 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00005887 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5888 } else {
5889 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5890 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005891 break;
5892 }
5893
Serban Constantinescu02164b32014-11-13 14:05:07 +00005894 default:
5895 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005896 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005897 }
5898}
5899
Aart Bik1f8d51b2018-02-15 10:42:37 -08005900void LocationsBuilderARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005901 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005902}
5903
Aart Bik1f8d51b2018-02-15 10:42:37 -08005904void InstructionCodeGeneratorARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005905 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005906}
5907
5908void LocationsBuilderARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005909 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005910}
5911
5912void InstructionCodeGeneratorARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005913 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005914}
5915
Aart Bik3dad3412018-02-28 12:01:46 -08005916void LocationsBuilderARM64::VisitAbs(HAbs* abs) {
5917 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
5918 switch (abs->GetResultType()) {
5919 case DataType::Type::kInt32:
5920 case DataType::Type::kInt64:
5921 locations->SetInAt(0, Location::RequiresRegister());
5922 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5923 break;
5924 case DataType::Type::kFloat32:
5925 case DataType::Type::kFloat64:
5926 locations->SetInAt(0, Location::RequiresFpuRegister());
5927 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5928 break;
5929 default:
5930 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5931 }
5932}
5933
5934void InstructionCodeGeneratorARM64::VisitAbs(HAbs* abs) {
5935 switch (abs->GetResultType()) {
5936 case DataType::Type::kInt32:
5937 case DataType::Type::kInt64: {
5938 Register in_reg = InputRegisterAt(abs, 0);
5939 Register out_reg = OutputRegister(abs);
5940 __ Cmp(in_reg, Operand(0));
5941 __ Cneg(out_reg, in_reg, lt);
5942 break;
5943 }
5944 case DataType::Type::kFloat32:
5945 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01005946 VRegister in_reg = InputFPRegisterAt(abs, 0);
5947 VRegister out_reg = OutputFPRegister(abs);
Aart Bik3dad3412018-02-28 12:01:46 -08005948 __ Fabs(out_reg, in_reg);
5949 break;
5950 }
5951 default:
5952 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5953 }
5954}
5955
Igor Murashkind01745e2017-04-05 16:40:31 -07005956void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5957 constructor_fence->SetLocations(nullptr);
5958}
5959
5960void InstructionCodeGeneratorARM64::VisitConstructorFence(
5961 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5962 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5963}
5964
Calin Juravle27df7582015-04-17 19:12:31 +01005965void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5966 memory_barrier->SetLocations(nullptr);
5967}
5968
5969void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005970 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005971}
5972
Alexandre Rames5319def2014-10-23 10:03:10 +01005973void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005974 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005975 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005976 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005977}
5978
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00005979void InstructionCodeGeneratorARM64::VisitReturn(HReturn* ret) {
5980 if (GetGraph()->IsCompilingOsr()) {
5981 // To simplify callers of an OSR method, we put the return value in both
5982 // floating point and core register.
5983 switch (ret->InputAt(0)->GetType()) {
5984 case DataType::Type::kFloat32:
5985 __ Fmov(w0, s0);
5986 break;
5987 case DataType::Type::kFloat64:
5988 __ Fmov(x0, d0);
5989 break;
5990 default:
5991 break;
5992 }
5993 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005994 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005995}
5996
5997void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5998 instruction->SetLocations(nullptr);
5999}
6000
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006001void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01006002 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01006003}
6004
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006005void LocationsBuilderARM64::VisitRor(HRor* ror) {
6006 HandleBinaryOp(ror);
6007}
6008
6009void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
6010 HandleBinaryOp(ror);
6011}
6012
Serban Constantinescu02164b32014-11-13 14:05:07 +00006013void LocationsBuilderARM64::VisitShl(HShl* shl) {
6014 HandleShift(shl);
6015}
6016
6017void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
6018 HandleShift(shl);
6019}
6020
6021void LocationsBuilderARM64::VisitShr(HShr* shr) {
6022 HandleShift(shr);
6023}
6024
6025void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
6026 HandleShift(shr);
6027}
6028
Alexandre Rames5319def2014-10-23 10:03:10 +01006029void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006030 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006031}
6032
6033void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006034 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006035}
6036
Alexandre Rames67555f72014-11-18 10:55:16 +00006037void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006038 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00006039}
6040
6041void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01006042 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00006043}
6044
6045void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01006046 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006047}
6048
Alexandre Rames67555f72014-11-18 10:55:16 +00006049void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006050 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01006051}
6052
Vladimir Marko552a1342017-10-31 10:56:47 +00006053void LocationsBuilderARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6054 codegen_->CreateStringBuilderAppendLocations(instruction, LocationFrom(x0));
6055}
6056
6057void InstructionCodeGeneratorARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6058 __ Mov(w0, instruction->GetFormat()->GetValue());
6059 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
6060}
6061
Calin Juravlee460d1d2015-09-29 04:52:17 +01006062void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
6063 HUnresolvedInstanceFieldGet* instruction) {
6064 FieldAccessCallingConventionARM64 calling_convention;
6065 codegen_->CreateUnresolvedFieldLocationSummary(
6066 instruction, instruction->GetFieldType(), calling_convention);
6067}
6068
6069void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
6070 HUnresolvedInstanceFieldGet* instruction) {
6071 FieldAccessCallingConventionARM64 calling_convention;
6072 codegen_->GenerateUnresolvedFieldAccess(instruction,
6073 instruction->GetFieldType(),
6074 instruction->GetFieldIndex(),
6075 instruction->GetDexPc(),
6076 calling_convention);
6077}
6078
6079void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
6080 HUnresolvedInstanceFieldSet* instruction) {
6081 FieldAccessCallingConventionARM64 calling_convention;
6082 codegen_->CreateUnresolvedFieldLocationSummary(
6083 instruction, instruction->GetFieldType(), calling_convention);
6084}
6085
6086void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
6087 HUnresolvedInstanceFieldSet* instruction) {
6088 FieldAccessCallingConventionARM64 calling_convention;
6089 codegen_->GenerateUnresolvedFieldAccess(instruction,
6090 instruction->GetFieldType(),
6091 instruction->GetFieldIndex(),
6092 instruction->GetDexPc(),
6093 calling_convention);
6094}
6095
6096void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
6097 HUnresolvedStaticFieldGet* instruction) {
6098 FieldAccessCallingConventionARM64 calling_convention;
6099 codegen_->CreateUnresolvedFieldLocationSummary(
6100 instruction, instruction->GetFieldType(), calling_convention);
6101}
6102
6103void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
6104 HUnresolvedStaticFieldGet* instruction) {
6105 FieldAccessCallingConventionARM64 calling_convention;
6106 codegen_->GenerateUnresolvedFieldAccess(instruction,
6107 instruction->GetFieldType(),
6108 instruction->GetFieldIndex(),
6109 instruction->GetDexPc(),
6110 calling_convention);
6111}
6112
6113void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
6114 HUnresolvedStaticFieldSet* instruction) {
6115 FieldAccessCallingConventionARM64 calling_convention;
6116 codegen_->CreateUnresolvedFieldLocationSummary(
6117 instruction, instruction->GetFieldType(), calling_convention);
6118}
6119
6120void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
6121 HUnresolvedStaticFieldSet* instruction) {
6122 FieldAccessCallingConventionARM64 calling_convention;
6123 codegen_->GenerateUnresolvedFieldAccess(instruction,
6124 instruction->GetFieldType(),
6125 instruction->GetFieldIndex(),
6126 instruction->GetDexPc(),
6127 calling_convention);
6128}
6129
Alexandre Rames5319def2014-10-23 10:03:10 +01006130void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006131 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6132 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01006133 // In suspend check slow path, usually there are no caller-save registers at all.
6134 // If SIMD instructions are present, however, we force spilling all live SIMD
6135 // registers in full width (since the runtime only saves/restores lower part).
6136 locations->SetCustomSlowPathCallerSaves(
6137 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01006138}
6139
6140void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006141 HBasicBlock* block = instruction->GetBlock();
6142 if (block->GetLoopInformation() != nullptr) {
6143 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6144 // The back edge will generate the suspend check.
6145 return;
6146 }
6147 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6148 // The goto will generate the suspend check.
6149 return;
6150 }
6151 GenerateSuspendCheck(instruction, nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08006152 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01006153}
6154
Alexandre Rames67555f72014-11-18 10:55:16 +00006155void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006156 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6157 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00006158 InvokeRuntimeCallingConvention calling_convention;
6159 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
6160}
6161
6162void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00006163 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08006164 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00006165}
6166
6167void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
6168 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006169 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006170 DataType::Type input_type = conversion->GetInputType();
6171 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006172 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6173 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006174 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
6175 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006176 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6177 }
6178
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006179 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006180 locations->SetInAt(0, Location::RequiresFpuRegister());
6181 } else {
6182 locations->SetInAt(0, Location::RequiresRegister());
6183 }
6184
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006185 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006186 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6187 } else {
6188 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6189 }
6190}
6191
6192void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006193 DataType::Type result_type = conversion->GetResultType();
6194 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00006195
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006196 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6197 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006198
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006199 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
6200 int result_size = DataType::Size(result_type);
6201 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00006202 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006203 Register output = OutputRegister(conversion);
6204 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006205 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01006206 // 'int' values are used directly as W registers, discarding the top
6207 // bits, so we don't need to sign-extend and can just perform a move.
6208 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
6209 // top 32 bits of the target register. We theoretically could leave those
6210 // bits unchanged, but we would have to make sure that no code uses a
6211 // 32bit input value as a 64bit value assuming that the top 32 bits are
6212 // zero.
6213 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006214 } else if (DataType::IsUnsignedType(result_type) ||
6215 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
6216 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006217 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00006218 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006219 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006220 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006221 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006222 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6223 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006224 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006225 } else if (DataType::IsFloatingPointType(result_type) &&
6226 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006227 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
6228 } else {
6229 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6230 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006231 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00006232}
Alexandre Rames67555f72014-11-18 10:55:16 +00006233
Serban Constantinescu02164b32014-11-13 14:05:07 +00006234void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
6235 HandleShift(ushr);
6236}
6237
6238void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
6239 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00006240}
6241
6242void LocationsBuilderARM64::VisitXor(HXor* instruction) {
6243 HandleBinaryOp(instruction);
6244}
6245
6246void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
6247 HandleBinaryOp(instruction);
6248}
6249
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006250void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006251 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006252 LOG(FATAL) << "Unreachable";
6253}
6254
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006255void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006256 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006257 LOG(FATAL) << "Unreachable";
6258}
6259
Mark Mendellfe57faa2015-09-18 09:26:15 -04006260// Simple implementation of packed switch - generate cascaded compare/jumps.
6261void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6262 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006263 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006264 locations->SetInAt(0, Location::RequiresRegister());
6265}
6266
6267void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6268 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08006269 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006270 Register value_reg = InputRegisterAt(switch_instr, 0);
6271 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6272
Zheng Xu3927c8b2015-11-18 17:46:25 +08006273 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01006274 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08006275 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
6276 // make sure we don't emit it if the target may run out of range.
6277 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
6278 // ranges and emit the tables only as required.
6279 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04006280
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006281 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08006282 // Current instruction id is an upper bound of the number of HIRs in the graph.
6283 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
6284 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006285 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6286 Register temp = temps.AcquireW();
6287 __ Subs(temp, value_reg, Operand(lower_bound));
6288
Zheng Xu3927c8b2015-11-18 17:46:25 +08006289 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006290 // Jump to successors[0] if value == lower_bound.
6291 __ B(eq, codegen_->GetLabelOf(successors[0]));
6292 int32_t last_index = 0;
6293 for (; num_entries - last_index > 2; last_index += 2) {
6294 __ Subs(temp, temp, Operand(2));
6295 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6296 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
6297 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6298 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
6299 }
6300 if (num_entries - last_index == 2) {
6301 // The last missing case_value.
6302 __ Cmp(temp, Operand(1));
6303 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08006304 }
6305
6306 // And the default for any other value.
6307 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6308 __ B(codegen_->GetLabelOf(default_block));
6309 }
6310 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01006311 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08006312
6313 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6314
6315 // Below instructions should use at most one blocked register. Since there are two blocked
6316 // registers, we are free to block one.
6317 Register temp_w = temps.AcquireW();
6318 Register index;
6319 // Remove the bias.
6320 if (lower_bound != 0) {
6321 index = temp_w;
6322 __ Sub(index, value_reg, Operand(lower_bound));
6323 } else {
6324 index = value_reg;
6325 }
6326
6327 // Jump to default block if index is out of the range.
6328 __ Cmp(index, Operand(num_entries));
6329 __ B(hs, codegen_->GetLabelOf(default_block));
6330
6331 // In current VIXL implementation, it won't require any blocked registers to encode the
6332 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
6333 // register pressure.
6334 Register table_base = temps.AcquireX();
6335 // Load jump offset from the table.
6336 __ Adr(table_base, jump_table->GetTableStartLabel());
6337 Register jump_offset = temp_w;
6338 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
6339
6340 // Jump to target block by branching to table_base(pc related) + offset.
6341 Register target_address = table_base;
6342 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
6343 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006344 }
6345}
6346
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006347void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
6348 HInstruction* instruction,
6349 Location out,
6350 uint32_t offset,
6351 Location maybe_temp,
6352 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006353 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006354 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006355 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006356 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006357 if (kUseBakerReadBarrier) {
6358 // Load with fast path based Baker's read barrier.
6359 // /* HeapReference<Object> */ out = *(out + offset)
6360 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6361 out,
6362 out_reg,
6363 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006364 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006365 /* needs_null_check= */ false,
6366 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006367 } else {
6368 // Load with slow path based read barrier.
6369 // Save the value of `out` into `maybe_temp` before overwriting it
6370 // in the following move operation, as we will need it for the
6371 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006372 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00006373 __ Mov(temp_reg, out_reg);
6374 // /* HeapReference<Object> */ out = *(out + offset)
6375 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6376 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6377 }
6378 } else {
6379 // Plain load with no read barrier.
6380 // /* HeapReference<Object> */ out = *(out + offset)
6381 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6382 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6383 }
6384}
6385
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006386void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
6387 HInstruction* instruction,
6388 Location out,
6389 Location obj,
6390 uint32_t offset,
6391 Location maybe_temp,
6392 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006393 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006394 Register out_reg = RegisterFrom(out, type);
6395 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006396 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006397 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006398 if (kUseBakerReadBarrier) {
6399 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00006400 // /* HeapReference<Object> */ out = *(obj + offset)
6401 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6402 out,
6403 obj_reg,
6404 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006405 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006406 /* needs_null_check= */ false,
6407 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006408 } else {
6409 // Load with slow path based read barrier.
6410 // /* HeapReference<Object> */ out = *(obj + offset)
6411 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6412 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6413 }
6414 } else {
6415 // Plain load with no read barrier.
6416 // /* HeapReference<Object> */ out = *(obj + offset)
6417 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6418 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6419 }
6420}
6421
Vladimir Markoca1e0382018-04-11 09:58:41 +00006422void CodeGeneratorARM64::GenerateGcRootFieldLoad(
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006423 HInstruction* instruction,
6424 Location root,
6425 Register obj,
6426 uint32_t offset,
6427 vixl::aarch64::Label* fixup_label,
6428 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006429 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006430 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006431 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006432 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006433 if (kUseBakerReadBarrier) {
6434 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00006435 // Baker's read barrier are used.
Roland Levillain44015862016-01-22 11:47:17 +00006436
Vladimir Marko008e09f32018-08-06 15:42:43 +01006437 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6438 // the Marking Register) to decide whether we need to enter
6439 // the slow path to mark the GC root.
6440 //
6441 // We use shared thunks for the slow path; shared within the method
6442 // for JIT, across methods for AOT. That thunk checks the reference
6443 // and jumps to the entrypoint if needed.
6444 //
6445 // lr = &return_address;
6446 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6447 // if (mr) { // Thread::Current()->GetIsGcMarking()
6448 // goto gc_root_thunk<root_reg>(lr)
6449 // }
6450 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00006451
Vladimir Marko008e09f32018-08-06 15:42:43 +01006452 UseScratchRegisterScope temps(GetVIXLAssembler());
6453 DCHECK(temps.IsAvailable(ip0));
6454 DCHECK(temps.IsAvailable(ip1));
6455 temps.Exclude(ip0, ip1);
6456 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
Roland Levillain44015862016-01-22 11:47:17 +00006457
Vladimir Marko008e09f32018-08-06 15:42:43 +01006458 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6459 vixl::aarch64::Label return_address;
6460 __ adr(lr, &return_address);
6461 if (fixup_label != nullptr) {
6462 __ bind(fixup_label);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006463 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006464 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
Vladimir Marko94796f82018-08-08 15:15:33 +01006465 "GC root LDR must be 2 instructions (8B) before the return address label.");
Vladimir Marko008e09f32018-08-06 15:42:43 +01006466 __ ldr(root_reg, MemOperand(obj.X(), offset));
6467 EmitBakerReadBarrierCbnz(custom_data);
6468 __ bind(&return_address);
Roland Levillain44015862016-01-22 11:47:17 +00006469 } else {
6470 // GC root loaded through a slow path for read barriers other
6471 // than Baker's.
6472 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006473 if (fixup_label == nullptr) {
6474 __ Add(root_reg.X(), obj.X(), offset);
6475 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006476 EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006477 }
Roland Levillain44015862016-01-22 11:47:17 +00006478 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00006479 GenerateReadBarrierForRootSlow(instruction, root, root);
Roland Levillain44015862016-01-22 11:47:17 +00006480 }
6481 } else {
6482 // Plain GC root load with no read barrier.
6483 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006484 if (fixup_label == nullptr) {
6485 __ Ldr(root_reg, MemOperand(obj, offset));
6486 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006487 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006488 }
Roland Levillain44015862016-01-22 11:47:17 +00006489 // Note that GC roots are not affected by heap poisoning, thus we
6490 // do not have to unpoison `root_reg` here.
6491 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006492 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00006493}
6494
Vladimir Marko94796f82018-08-08 15:15:33 +01006495void CodeGeneratorARM64::GenerateUnsafeCasOldValueMovWithBakerReadBarrier(
6496 vixl::aarch64::Register marked,
6497 vixl::aarch64::Register old_value) {
6498 DCHECK(kEmitCompilerReadBarrier);
6499 DCHECK(kUseBakerReadBarrier);
6500
6501 // Similar to the Baker RB path in GenerateGcRootFieldLoad(), with a MOV instead of LDR.
6502 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(marked.GetCode());
6503
6504 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6505 vixl::aarch64::Label return_address;
6506 __ adr(lr, &return_address);
6507 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6508 "GC root LDR must be 2 instructions (8B) before the return address label.");
6509 __ mov(marked, old_value);
6510 EmitBakerReadBarrierCbnz(custom_data);
6511 __ bind(&return_address);
6512}
6513
Roland Levillain44015862016-01-22 11:47:17 +00006514void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6515 Location ref,
Vladimir Marko248141f2018-08-10 10:40:07 +01006516 vixl::aarch64::Register obj,
6517 const vixl::aarch64::MemOperand& src,
Roland Levillain44015862016-01-22 11:47:17 +00006518 bool needs_null_check,
6519 bool use_load_acquire) {
6520 DCHECK(kEmitCompilerReadBarrier);
6521 DCHECK(kUseBakerReadBarrier);
6522
Vladimir Marko0ecac682018-08-07 10:40:38 +01006523 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6524 // Marking Register) to decide whether we need to enter the slow
6525 // path to mark the reference. Then, in the slow path, check the
6526 // gray bit in the lock word of the reference's holder (`obj`) to
6527 // decide whether to mark `ref` or not.
6528 //
6529 // We use shared thunks for the slow path; shared within the method
6530 // for JIT, across methods for AOT. That thunk checks the holder
6531 // and jumps to the entrypoint if needed. If the holder is not gray,
6532 // it creates a fake dependency and returns to the LDR instruction.
6533 //
6534 // lr = &gray_return_address;
6535 // if (mr) { // Thread::Current()->GetIsGcMarking()
6536 // goto field_thunk<holder_reg, base_reg, use_load_acquire>(lr)
6537 // }
6538 // not_gray_return_address:
6539 // // Original reference load. If the offset is too large to fit
6540 // // into LDR, we use an adjusted base register here.
6541 // HeapReference<mirror::Object> reference = *(obj+offset);
6542 // gray_return_address:
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006543
Vladimir Marko248141f2018-08-10 10:40:07 +01006544 DCHECK(src.GetAddrMode() == vixl::aarch64::Offset);
6545 DCHECK_ALIGNED(src.GetOffset(), sizeof(mirror::HeapReference<mirror::Object>));
6546
6547 UseScratchRegisterScope temps(GetVIXLAssembler());
6548 DCHECK(temps.IsAvailable(ip0));
6549 DCHECK(temps.IsAvailable(ip1));
6550 temps.Exclude(ip0, ip1);
6551 uint32_t custom_data = use_load_acquire
6552 ? EncodeBakerReadBarrierAcquireData(src.GetBaseRegister().GetCode(), obj.GetCode())
6553 : EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode());
6554
6555 {
6556 ExactAssemblyScope guard(GetVIXLAssembler(),
6557 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6558 vixl::aarch64::Label return_address;
6559 __ adr(lr, &return_address);
6560 EmitBakerReadBarrierCbnz(custom_data);
6561 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6562 "Field LDR must be 1 instruction (4B) before the return address label; "
6563 " 2 instructions (8B) for heap poisoning.");
6564 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
6565 if (use_load_acquire) {
6566 DCHECK_EQ(src.GetOffset(), 0);
6567 __ ldar(ref_reg, src);
6568 } else {
6569 __ ldr(ref_reg, src);
6570 }
6571 if (needs_null_check) {
6572 MaybeRecordImplicitNullCheck(instruction);
6573 }
6574 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6575 // macro instructions disallowed in ExactAssemblyScope.
6576 if (kPoisonHeapReferences) {
6577 __ neg(ref_reg, Operand(ref_reg));
6578 }
6579 __ bind(&return_address);
6580 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006581 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Vladimir Marko248141f2018-08-10 10:40:07 +01006582}
6583
6584void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6585 Location ref,
6586 Register obj,
6587 uint32_t offset,
6588 Location maybe_temp,
6589 bool needs_null_check,
6590 bool use_load_acquire) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006591 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6592 Register base = obj;
6593 if (use_load_acquire) {
6594 DCHECK(maybe_temp.IsRegister());
6595 base = WRegisterFrom(maybe_temp);
6596 __ Add(base, obj, offset);
6597 offset = 0u;
6598 } else if (offset >= kReferenceLoadMinFarOffset) {
6599 DCHECK(maybe_temp.IsRegister());
6600 base = WRegisterFrom(maybe_temp);
6601 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6602 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6603 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006604 }
Vladimir Marko248141f2018-08-10 10:40:07 +01006605 MemOperand src(base.X(), offset);
6606 GenerateFieldLoadWithBakerReadBarrier(
6607 instruction, ref, obj, src, needs_null_check, use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006608}
6609
Artem Serov0806f582018-10-11 20:14:20 +01006610void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HArrayGet* instruction,
6611 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006612 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006613 uint32_t data_offset,
6614 Location index,
Roland Levillain44015862016-01-22 11:47:17 +00006615 bool needs_null_check) {
6616 DCHECK(kEmitCompilerReadBarrier);
6617 DCHECK(kUseBakerReadBarrier);
6618
Vladimir Marko66d691d2017-04-07 17:53:39 +01006619 static_assert(
6620 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6621 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006622 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006623
Vladimir Marko008e09f32018-08-06 15:42:43 +01006624 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6625 // Marking Register) to decide whether we need to enter the slow
6626 // path to mark the reference. Then, in the slow path, check the
6627 // gray bit in the lock word of the reference's holder (`obj`) to
6628 // decide whether to mark `ref` or not.
6629 //
6630 // We use shared thunks for the slow path; shared within the method
6631 // for JIT, across methods for AOT. That thunk checks the holder
6632 // and jumps to the entrypoint if needed. If the holder is not gray,
6633 // it creates a fake dependency and returns to the LDR instruction.
6634 //
6635 // lr = &gray_return_address;
6636 // if (mr) { // Thread::Current()->GetIsGcMarking()
6637 // goto array_thunk<base_reg>(lr)
6638 // }
6639 // not_gray_return_address:
6640 // // Original reference load. If the offset is too large to fit
6641 // // into LDR, we use an adjusted base register here.
6642 // HeapReference<mirror::Object> reference = data[index];
6643 // gray_return_address:
Vladimir Marko66d691d2017-04-07 17:53:39 +01006644
Vladimir Marko008e09f32018-08-06 15:42:43 +01006645 DCHECK(index.IsValid());
6646 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
6647 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006648
Vladimir Marko008e09f32018-08-06 15:42:43 +01006649 UseScratchRegisterScope temps(GetVIXLAssembler());
6650 DCHECK(temps.IsAvailable(ip0));
6651 DCHECK(temps.IsAvailable(ip1));
6652 temps.Exclude(ip0, ip1);
Artem Serov0806f582018-10-11 20:14:20 +01006653
6654 Register temp;
6655 if (instruction->GetArray()->IsIntermediateAddress()) {
6656 // We do not need to compute the intermediate address from the array: the
6657 // input instruction has done it already. See the comment in
6658 // `TryExtractArrayAccessAddress()`.
6659 if (kIsDebugBuild) {
6660 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
6661 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
6662 }
6663 temp = obj;
6664 } else {
6665 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
6666 __ Add(temp.X(), obj.X(), Operand(data_offset));
6667 }
6668
Vladimir Marko008e09f32018-08-06 15:42:43 +01006669 uint32_t custom_data = EncodeBakerReadBarrierArrayData(temp.GetCode());
Vladimir Marko66d691d2017-04-07 17:53:39 +01006670
Vladimir Marko008e09f32018-08-06 15:42:43 +01006671 {
6672 ExactAssemblyScope guard(GetVIXLAssembler(),
6673 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6674 vixl::aarch64::Label return_address;
6675 __ adr(lr, &return_address);
6676 EmitBakerReadBarrierCbnz(custom_data);
6677 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6678 "Array LDR must be 1 instruction (4B) before the return address label; "
6679 " 2 instructions (8B) for heap poisoning.");
6680 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6681 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6682 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6683 // macro instructions disallowed in ExactAssemblyScope.
6684 if (kPoisonHeapReferences) {
6685 __ neg(ref_reg, Operand(ref_reg));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006686 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006687 __ bind(&return_address);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006688 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006689 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Roland Levillain44015862016-01-22 11:47:17 +00006690}
6691
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006692void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6693 // The following condition is a compile-time one, so it does not have a run-time cost.
6694 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
6695 // The following condition is a run-time one; it is executed after the
6696 // previous compile-time test, to avoid penalizing non-debug builds.
6697 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6698 UseScratchRegisterScope temps(GetVIXLAssembler());
6699 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6700 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6701 }
6702 }
6703}
6704
Vladimir Marko1bff99f2020-11-02 15:07:33 +00006705SlowPathCodeARM64* CodeGeneratorARM64::AddReadBarrierSlowPath(HInstruction* instruction,
6706 Location out,
6707 Location ref,
6708 Location obj,
6709 uint32_t offset,
6710 Location index) {
6711 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
6712 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6713 AddSlowPath(slow_path);
6714 return slow_path;
6715}
6716
Roland Levillain44015862016-01-22 11:47:17 +00006717void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6718 Location out,
6719 Location ref,
6720 Location obj,
6721 uint32_t offset,
6722 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006723 DCHECK(kEmitCompilerReadBarrier);
6724
Roland Levillain44015862016-01-22 11:47:17 +00006725 // Insert a slow path based read barrier *after* the reference load.
6726 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006727 // If heap poisoning is enabled, the unpoisoning of the loaded
6728 // reference will be carried out by the runtime within the slow
6729 // path.
6730 //
6731 // Note that `ref` currently does not get unpoisoned (when heap
6732 // poisoning is enabled), which is alright as the `ref` argument is
6733 // not used by the artReadBarrierSlow entry point.
6734 //
6735 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko1bff99f2020-11-02 15:07:33 +00006736 SlowPathCodeARM64* slow_path = AddReadBarrierSlowPath(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006737
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006738 __ B(slow_path->GetEntryLabel());
6739 __ Bind(slow_path->GetExitLabel());
6740}
6741
Roland Levillain44015862016-01-22 11:47:17 +00006742void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6743 Location out,
6744 Location ref,
6745 Location obj,
6746 uint32_t offset,
6747 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006748 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006749 // Baker's read barriers shall be handled by the fast path
6750 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6751 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006752 // If heap poisoning is enabled, unpoisoning will be taken care of
6753 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006754 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006755 } else if (kPoisonHeapReferences) {
6756 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6757 }
6758}
6759
Roland Levillain44015862016-01-22 11:47:17 +00006760void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6761 Location out,
6762 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006763 DCHECK(kEmitCompilerReadBarrier);
6764
Roland Levillain44015862016-01-22 11:47:17 +00006765 // Insert a slow path based read barrier *after* the GC root load.
6766 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006767 // Note that GC roots are not affected by heap poisoning, so we do
6768 // not need to do anything special for this here.
6769 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006770 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006771 AddSlowPath(slow_path);
6772
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006773 __ B(slow_path->GetEntryLabel());
6774 __ Bind(slow_path->GetExitLabel());
6775}
6776
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006777void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6778 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006779 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006780 locations->SetInAt(0, Location::RequiresRegister());
6781 locations->SetOut(Location::RequiresRegister());
6782}
6783
6784void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6785 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006786 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006787 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006788 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006789 __ Ldr(XRegisterFrom(locations->Out()),
6790 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006791 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006792 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006793 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006794 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6795 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006796 __ Ldr(XRegisterFrom(locations->Out()),
6797 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006798 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006799}
6800
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006801static void PatchJitRootUse(uint8_t* code,
6802 const uint8_t* roots_data,
6803 vixl::aarch64::Literal<uint32_t>* literal,
6804 uint64_t index_in_table) {
6805 uint32_t literal_offset = literal->GetOffset();
6806 uintptr_t address =
6807 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6808 uint8_t* data = code + literal_offset;
6809 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6810}
6811
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006812void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6813 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006814 const StringReference& string_reference = entry.first;
6815 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006816 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006817 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006818 }
6819 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006820 const TypeReference& type_reference = entry.first;
6821 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006822 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006823 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006824 }
6825}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006826
Artem Serov1a719e42019-07-18 14:24:55 +01006827MemOperand InstructionCodeGeneratorARM64::VecNeonAddress(
6828 HVecMemoryOperation* instruction,
6829 UseScratchRegisterScope* temps_scope,
6830 size_t size,
6831 bool is_string_char_at,
6832 /*out*/ Register* scratch) {
6833 LocationSummary* locations = instruction->GetLocations();
6834 Register base = InputRegisterAt(instruction, 0);
6835
6836 if (instruction->InputAt(1)->IsIntermediateAddressIndex()) {
6837 DCHECK(!is_string_char_at);
6838 return MemOperand(base.X(), InputRegisterAt(instruction, 1).X());
6839 }
6840
6841 Location index = locations->InAt(1);
6842 uint32_t offset = is_string_char_at
6843 ? mirror::String::ValueOffset().Uint32Value()
6844 : mirror::Array::DataOffset(size).Uint32Value();
6845 size_t shift = ComponentSizeShiftWidth(size);
6846
6847 // HIntermediateAddress optimization is only applied for scalar ArrayGet and ArraySet.
6848 DCHECK(!instruction->InputAt(0)->IsIntermediateAddress());
6849
6850 if (index.IsConstant()) {
6851 offset += Int64FromLocation(index) << shift;
6852 return HeapOperand(base, offset);
6853 } else {
6854 *scratch = temps_scope->AcquireSameSizeAs(base);
6855 __ Add(*scratch, base, Operand(WRegisterFrom(index), LSL, shift));
6856 return HeapOperand(*scratch, offset);
6857 }
6858}
6859
Alexandre Rames67555f72014-11-18 10:55:16 +00006860#undef __
6861#undef QUICK_ENTRY_POINT
6862
Vladimir Markoca1e0382018-04-11 09:58:41 +00006863#define __ assembler.GetVIXLAssembler()->
6864
6865static void EmitGrayCheckAndFastPath(arm64::Arm64Assembler& assembler,
6866 vixl::aarch64::Register base_reg,
6867 vixl::aarch64::MemOperand& lock_word,
Vladimir Marko7a695052018-04-12 10:26:50 +01006868 vixl::aarch64::Label* slow_path,
6869 vixl::aarch64::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006870 // Load the lock word containing the rb_state.
6871 __ Ldr(ip0.W(), lock_word);
6872 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01006873 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Vladimir Markoca1e0382018-04-11 09:58:41 +00006874 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6875 __ Tbnz(ip0.W(), LockWord::kReadBarrierStateShift, slow_path);
6876 static_assert(
6877 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET,
6878 "Field and array LDR offsets must be the same to reuse the same code.");
Vladimir Marko7a695052018-04-12 10:26:50 +01006879 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
6880 if (throw_npe != nullptr) {
6881 __ Bind(throw_npe);
6882 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006883 // Adjust the return address back to the LDR (1 instruction; 2 for heap poisoning).
6884 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6885 "Field LDR must be 1 instruction (4B) before the return address label; "
6886 " 2 instructions (8B) for heap poisoning.");
6887 __ Add(lr, lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6888 // Introduce a dependency on the lock_word including rb_state,
6889 // to prevent load-load reordering, and without using
6890 // a memory barrier (which would be more expensive).
6891 __ Add(base_reg, base_reg, Operand(ip0, LSR, 32));
6892 __ Br(lr); // And return back to the function.
6893 // Note: The fake dependency is unnecessary for the slow path.
6894}
6895
6896// Load the read barrier introspection entrypoint in register `entrypoint`.
6897static void LoadReadBarrierMarkIntrospectionEntrypoint(arm64::Arm64Assembler& assembler,
6898 vixl::aarch64::Register entrypoint) {
6899 // entrypoint = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6900 DCHECK_EQ(ip0.GetCode(), 16u);
6901 const int32_t entry_point_offset =
6902 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6903 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
6904}
6905
6906void CodeGeneratorARM64::CompileBakerReadBarrierThunk(Arm64Assembler& assembler,
6907 uint32_t encoded_data,
6908 /*out*/ std::string* debug_name) {
6909 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
6910 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006911 case BakerReadBarrierKind::kField:
6912 case BakerReadBarrierKind::kAcquire: {
Ulyana Trafimovicheeaf47f2020-10-28 15:59:29 +00006913 auto base_reg =
6914 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00006915 CheckValidReg(base_reg.GetCode());
Ulyana Trafimovicheeaf47f2020-10-28 15:59:29 +00006916 auto holder_reg =
6917 Register::GetXRegFromCode(BakerReadBarrierSecondRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00006918 CheckValidReg(holder_reg.GetCode());
6919 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6920 temps.Exclude(ip0, ip1);
Roland Levillain988c3912019-09-25 19:33:35 +01006921 // In the case of a field load (with relaxed semantic), if `base_reg` differs from
6922 // `holder_reg`, the offset was too large and we must have emitted (during the construction
6923 // of the HIR graph, see `art::HInstructionBuilder::BuildInstanceFieldAccess`) and preserved
6924 // (see `art::PrepareForRegisterAllocation::VisitNullCheck`) an explicit null check before
6925 // the load. Otherwise, for implicit null checks, we need to null-check the holder as we do
6926 // not necessarily do that check before going to the thunk.
6927 //
6928 // In the case of a field load with load-acquire semantics (where `base_reg` always differs
6929 // from `holder_reg`), we also need an explicit null check when implicit null checks are
6930 // allowed, as we do not emit one before going to the thunk.
Vladimir Marko7a695052018-04-12 10:26:50 +01006931 vixl::aarch64::Label throw_npe_label;
6932 vixl::aarch64::Label* throw_npe = nullptr;
Roland Levillain988c3912019-09-25 19:33:35 +01006933 if (GetCompilerOptions().GetImplicitNullChecks() &&
6934 (holder_reg.Is(base_reg) || (kind == BakerReadBarrierKind::kAcquire))) {
Vladimir Marko7a695052018-04-12 10:26:50 +01006935 throw_npe = &throw_npe_label;
6936 __ Cbz(holder_reg.W(), throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006937 }
Vladimir Marko7a695052018-04-12 10:26:50 +01006938 // Check if the holder is gray and, if not, add fake dependency to the base register
6939 // and return to the LDR instruction to load the reference. Otherwise, use introspection
6940 // to load the reference and call the entrypoint that performs further checks on the
6941 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006942 vixl::aarch64::Label slow_path;
6943 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
Vladimir Marko7a695052018-04-12 10:26:50 +01006944 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006945 __ Bind(&slow_path);
Vladimir Marko0ecac682018-08-07 10:40:38 +01006946 if (kind == BakerReadBarrierKind::kField) {
6947 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6948 __ Ldr(ip0.W(), ldr_address); // Load the LDR (immediate) unsigned offset.
6949 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6950 __ Ubfx(ip0.W(), ip0.W(), 10, 12); // Extract the offset.
6951 __ Ldr(ip0.W(), MemOperand(base_reg, ip0, LSL, 2)); // Load the reference.
6952 } else {
6953 DCHECK(kind == BakerReadBarrierKind::kAcquire);
6954 DCHECK(!base_reg.Is(holder_reg));
6955 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6956 __ Ldar(ip0.W(), MemOperand(base_reg));
6957 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006958 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
6959 __ Br(ip1); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006960 break;
6961 }
6962 case BakerReadBarrierKind::kArray: {
Ulyana Trafimovicheeaf47f2020-10-28 15:59:29 +00006963 auto base_reg =
6964 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00006965 CheckValidReg(base_reg.GetCode());
6966 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6967 BakerReadBarrierSecondRegField::Decode(encoded_data));
6968 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6969 temps.Exclude(ip0, ip1);
6970 vixl::aarch64::Label slow_path;
6971 int32_t data_offset =
6972 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
6973 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
6974 DCHECK_LT(lock_word.GetOffset(), 0);
6975 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path);
6976 __ Bind(&slow_path);
6977 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
6978 __ Ldr(ip0.W(), ldr_address); // Load the LDR (register) unsigned offset.
6979 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6980 __ Ubfx(ip0, ip0, 16, 6); // Extract the index register, plus 32 (bit 21 is set).
6981 __ Bfi(ip1, ip0, 3, 6); // Insert ip0 to the entrypoint address to create
6982 // a switch case target based on the index register.
6983 __ Mov(ip0, base_reg); // Move the base register to ip0.
6984 __ Br(ip1); // Jump to the entrypoint's array switch case.
6985 break;
6986 }
6987 case BakerReadBarrierKind::kGcRoot: {
6988 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
6989 // and it does not have a forwarding address), call the correct introspection entrypoint;
6990 // otherwise return the reference (or the extracted forwarding address).
6991 // There is no gray bit check for GC roots.
Ulyana Trafimovicheeaf47f2020-10-28 15:59:29 +00006992 auto root_reg =
6993 Register::GetWRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00006994 CheckValidReg(root_reg.GetCode());
6995 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6996 BakerReadBarrierSecondRegField::Decode(encoded_data));
6997 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6998 temps.Exclude(ip0, ip1);
6999 vixl::aarch64::Label return_label, not_marked, forwarding_address;
7000 __ Cbz(root_reg, &return_label);
7001 MemOperand lock_word(root_reg.X(), mirror::Object::MonitorOffset().Int32Value());
7002 __ Ldr(ip0.W(), lock_word);
7003 __ Tbz(ip0.W(), LockWord::kMarkBitStateShift, &not_marked);
7004 __ Bind(&return_label);
7005 __ Br(lr);
7006 __ Bind(&not_marked);
7007 __ Tst(ip0.W(), Operand(ip0.W(), LSL, 1));
7008 __ B(&forwarding_address, mi);
7009 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
7010 // Adjust the art_quick_read_barrier_mark_introspection address in IP1 to
7011 // art_quick_read_barrier_mark_introspection_gc_roots.
7012 __ Add(ip1, ip1, Operand(BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRYPOINT_OFFSET));
7013 __ Mov(ip0.W(), root_reg);
7014 __ Br(ip1);
7015 __ Bind(&forwarding_address);
7016 __ Lsl(root_reg, ip0.W(), LockWord::kForwardingAddressShift);
7017 __ Br(lr);
7018 break;
7019 }
7020 default:
7021 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
7022 UNREACHABLE();
7023 }
7024
Vladimir Marko966b46f2018-08-03 10:20:19 +00007025 // For JIT, the slow path is considered part of the compiled method,
Vladimir Markof91fc122020-05-13 09:21:00 +01007026 // so JIT should pass null as `debug_name`.
Vladimir Marko695348f2020-05-19 14:42:02 +01007027 DCHECK(!GetCompilerOptions().IsJitCompiler() || debug_name == nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00007028 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00007029 std::ostringstream oss;
7030 oss << "BakerReadBarrierThunk";
7031 switch (kind) {
7032 case BakerReadBarrierKind::kField:
7033 oss << "Field_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
7034 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
7035 break;
Vladimir Marko0ecac682018-08-07 10:40:38 +01007036 case BakerReadBarrierKind::kAcquire:
7037 oss << "Acquire_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
7038 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
7039 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +00007040 case BakerReadBarrierKind::kArray:
7041 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
7042 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7043 BakerReadBarrierSecondRegField::Decode(encoded_data));
7044 break;
7045 case BakerReadBarrierKind::kGcRoot:
7046 oss << "GcRoot_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
7047 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7048 BakerReadBarrierSecondRegField::Decode(encoded_data));
7049 break;
7050 }
7051 *debug_name = oss.str();
7052 }
7053}
7054
7055#undef __
7056
Alexandre Rames5319def2014-10-23 10:03:10 +01007057} // namespace arm64
7058} // namespace art