blob: 9b9a878ad6c7bef2e4221ac72537b167e64efe7c [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"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000040#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010041#include "thread.h"
42#include "utils/arm64/assembler_arm64.h"
43#include "utils/assembler.h"
44#include "utils/stack_checks.h"
45
Scott Wakeling97c72b72016-06-24 16:19:36 +010046using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000047using vixl::ExactAssemblyScope;
48using vixl::CodeBufferCheckScope;
49using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010050
51#ifdef __
52#error "ARM64 Codegen VIXL macro-assembler macro already defined."
53#endif
54
Vladimir Marko0a516052019-10-14 13:00:44 +000055namespace art {
Alexandre Rames5319def2014-10-23 10:03:10 +010056
Roland Levillain22ccc3a2015-11-24 13:10:05 +000057template<class MirrorType>
58class GcRoot;
59
Alexandre Rames5319def2014-10-23 10:03:10 +010060namespace arm64 {
61
Alexandre Ramesbe919d92016-08-23 18:33:36 +010062using helpers::ARM64EncodableConstantOrRegister;
63using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080064using helpers::CPURegisterFrom;
65using helpers::DRegisterFrom;
66using helpers::FPRegisterFrom;
67using helpers::HeapOperand;
68using helpers::HeapOperandFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010069using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080070using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080071using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010072using helpers::InputRegisterAt;
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +010073using helpers::Int64FromLocation;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010074using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080075using helpers::LocationFrom;
76using helpers::OperandFromMemOperand;
77using helpers::OutputCPURegister;
78using helpers::OutputFPRegister;
79using helpers::OutputRegister;
80using helpers::RegisterFrom;
81using helpers::StackOperandFrom;
82using helpers::VIXLRegCodeFromART;
83using helpers::WRegisterFrom;
84using helpers::XRegisterFrom;
85
Vladimir Markof3e0ee22015-12-17 15:23:13 +000086// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080087// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
88// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000089static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010090
Vladimir Markof4f2daa2017-03-20 18:26:59 +000091// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
92// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
Vladimir Marko008e09f32018-08-06 15:42:43 +010093// For the Baker read barrier implementation using link-time generated thunks we need to split
Vladimir Markof4f2daa2017-03-20 18:26:59 +000094// the offset explicitly.
95constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
96
Alexandre Rames5319def2014-10-23 10:03:10 +010097inline Condition ARM64Condition(IfCondition cond) {
98 switch (cond) {
99 case kCondEQ: return eq;
100 case kCondNE: return ne;
101 case kCondLT: return lt;
102 case kCondLE: return le;
103 case kCondGT: return gt;
104 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700105 case kCondB: return lo;
106 case kCondBE: return ls;
107 case kCondA: return hi;
108 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100109 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000110 LOG(FATAL) << "Unreachable";
111 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100112}
113
Vladimir Markod6e069b2016-01-18 11:11:01 +0000114inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
115 // The ARM64 condition codes can express all the necessary branches, see the
116 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
117 // There is no dex instruction or HIR that would need the missing conditions
118 // "equal or unordered" or "not equal".
119 switch (cond) {
120 case kCondEQ: return eq;
121 case kCondNE: return ne /* unordered */;
122 case kCondLT: return gt_bias ? cc : lt /* unordered */;
123 case kCondLE: return gt_bias ? ls : le /* unordered */;
124 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
125 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
126 default:
127 LOG(FATAL) << "UNREACHABLE";
128 UNREACHABLE();
129 }
130}
131
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100132Location ARM64ReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000133 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
134 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
135 // but we use the exact registers for clarity.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100136 if (return_type == DataType::Type::kFloat32) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000137 return LocationFrom(s0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100138 } else if (return_type == DataType::Type::kFloat64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000139 return LocationFrom(d0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100140 } else if (return_type == DataType::Type::kInt64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000141 return LocationFrom(x0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100142 } else if (return_type == DataType::Type::kVoid) {
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100143 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000144 } else {
145 return LocationFrom(w0);
146 }
147}
148
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100149Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000150 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100151}
152
Vladimir Marko3232dbb2018-07-25 15:42:46 +0100153static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
154 InvokeRuntimeCallingConvention calling_convention;
155 RegisterSet caller_saves = RegisterSet::Empty();
156 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
157 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
158 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
159 DataType::Type::kReference).GetCode());
160 return caller_saves;
161}
162
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100163// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
164#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700165#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100166
Zheng Xuda403092015-04-24 17:35:39 +0800167// Calculate memory accessing operand for save/restore live registers.
168static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100169 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800170 int64_t spill_offset,
171 bool is_save) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800172 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
173 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100174 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800175 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100176 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800177 codegen->GetNumberOfFloatingPointRegisters()));
178
Vladimir Marko804b03f2016-09-14 16:26:36 +0100179 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serovc8150b52019-07-31 18:28:00 +0100180 const unsigned v_reg_size_in_bits = codegen->GetSlowPathFPWidth() * 8;
Artem Serov1a719e42019-07-18 14:24:55 +0100181 DCHECK_LE(codegen->GetSIMDRegisterWidth(), kQRegSizeInBytes);
Artem Serovc8150b52019-07-31 18:28:00 +0100182 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size_in_bits, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800183
184 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
185 UseScratchRegisterScope temps(masm);
186
187 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100188 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
189 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800190 int64_t reg_size = kXRegSizeInBytes;
191 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
192 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100193 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800194 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
195 // If the offset does not fit in the instruction's immediate field, use an alternate register
196 // to compute the base address(float point registers spill base address).
197 Register new_base = temps.AcquireSameSizeAs(base);
198 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
199 base = new_base;
200 spill_offset = -core_spill_size;
201 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
202 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
203 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
204 }
205
206 if (is_save) {
207 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
208 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
209 } else {
210 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
211 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
212 }
213}
214
215void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800216 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Andreas Gampe3db70682018-12-26 15:12:03 -0800217 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100218 for (uint32_t i : LowToHighBits(core_spills)) {
219 // If the register holds an object, update the stack mask.
220 if (locations->RegisterContainsObject(i)) {
221 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800222 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100223 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
224 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
225 saved_core_stack_offsets_[i] = stack_offset;
226 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800227 }
228
Artem Serovc8150b52019-07-31 18:28:00 +0100229 const size_t fp_reg_size = codegen->GetSlowPathFPWidth();
Andreas Gampe3db70682018-12-26 15:12:03 -0800230 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100231 for (uint32_t i : LowToHighBits(fp_spills)) {
232 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
233 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
234 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serov9df37b92019-07-23 16:41:54 +0100235 stack_offset += fp_reg_size;
Zheng Xuda403092015-04-24 17:35:39 +0800236 }
237
Vladimir Marko804b03f2016-09-14 16:26:36 +0100238 SaveRestoreLiveRegistersHelper(codegen,
239 locations,
Andreas Gampe3db70682018-12-26 15:12:03 -0800240 codegen->GetFirstRegisterSlotInSlowPath(), /* is_save= */ true);
Zheng Xuda403092015-04-24 17:35:39 +0800241}
242
243void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100244 SaveRestoreLiveRegistersHelper(codegen,
245 locations,
Andreas Gampe3db70682018-12-26 15:12:03 -0800246 codegen->GetFirstRegisterSlotInSlowPath(), /* is_save= */ false);
Zheng Xuda403092015-04-24 17:35:39 +0800247}
248
Alexandre Rames5319def2014-10-23 10:03:10 +0100249class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
250 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000251 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100252
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100253 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100254 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000255 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100256
Alexandre Rames5319def2014-10-23 10:03:10 +0100257 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000258 if (instruction_->CanThrowIntoCatchBlock()) {
259 // Live registers will be restored in the catch block if caught.
260 SaveLiveRegisters(codegen, instruction_->GetLocations());
261 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000262 // We're moving two locations to locations that could overlap, so we need a parallel
263 // move resolver.
264 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100265 codegen->EmitParallelMoves(locations->InAt(0),
266 LocationFrom(calling_convention.GetRegisterAt(0)),
267 DataType::Type::kInt32,
268 locations->InAt(1),
269 LocationFrom(calling_convention.GetRegisterAt(1)),
270 DataType::Type::kInt32);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000271 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
272 ? kQuickThrowStringBounds
273 : kQuickThrowArrayBounds;
274 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100275 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800276 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100277 }
278
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100279 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100280
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100281 const char* GetDescription() const override { return "BoundsCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100282
Alexandre Rames5319def2014-10-23 10:03:10 +0100283 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100284 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
285};
286
Alexandre Rames67555f72014-11-18 10:55:16 +0000287class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
288 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000289 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000290
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100291 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000292 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
293 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000294 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800295 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000296 }
297
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100298 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100299
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100300 const char* GetDescription() const override { return "DivZeroCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100301
Alexandre Rames67555f72014-11-18 10:55:16 +0000302 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000303 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
304};
305
306class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
307 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100308 LoadClassSlowPathARM64(HLoadClass* cls, HInstruction* at)
309 : SlowPathCodeARM64(at), cls_(cls) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000310 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100311 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Alexandre Rames67555f72014-11-18 10:55:16 +0000312 }
313
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100314 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000315 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000316 Location out = locations->Out();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100317 const uint32_t dex_pc = instruction_->GetDexPc();
318 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
319 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
Alexandre Rames67555f72014-11-18 10:55:16 +0000320
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100321 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames67555f72014-11-18 10:55:16 +0000322 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000323 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000324
Vladimir Markof3c52b42017-11-17 17:32:12 +0000325 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100326 if (must_resolve_type) {
327 DCHECK(IsSameDexFile(cls_->GetDexFile(), arm64_codegen->GetGraph()->GetDexFile()));
328 dex::TypeIndex type_index = cls_->GetTypeIndex();
329 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Vladimir Marko9d479252018-07-24 11:35:20 +0100330 arm64_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
331 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100332 // If we also must_do_clinit, the resolved type is now in the correct register.
333 } else {
334 DCHECK(must_do_clinit);
335 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
336 arm64_codegen->MoveLocation(LocationFrom(calling_convention.GetRegisterAt(0)),
337 source,
338 cls_->GetType());
339 }
340 if (must_do_clinit) {
341 arm64_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
342 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800343 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000344
345 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000346 if (out.IsValid()) {
347 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100348 DataType::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000349 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000350 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000351 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000352 __ B(GetExitLabel());
353 }
354
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100355 const char* GetDescription() const override { return "LoadClassSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100356
Alexandre Rames67555f72014-11-18 10:55:16 +0000357 private:
358 // The class this slow path will load.
359 HLoadClass* const cls_;
360
Alexandre Rames67555f72014-11-18 10:55:16 +0000361 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
362};
363
Vladimir Markoaad75c62016-10-03 08:46:48 +0000364class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
365 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000366 explicit LoadStringSlowPathARM64(HLoadString* instruction)
367 : SlowPathCodeARM64(instruction) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000368
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100369 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000370 LocationSummary* locations = instruction_->GetLocations();
371 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
372 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
373
374 __ Bind(GetEntryLabel());
375 SaveLiveRegisters(codegen, locations);
376
Vladimir Markof3c52b42017-11-17 17:32:12 +0000377 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000378 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
379 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000380 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
381 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100382 DataType::Type type = instruction_->GetType();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000383 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
384
385 RestoreLiveRegisters(codegen, locations);
386
Vladimir Markoaad75c62016-10-03 08:46:48 +0000387 __ B(GetExitLabel());
388 }
389
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100390 const char* GetDescription() const override { return "LoadStringSlowPathARM64"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000391
392 private:
393 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
394};
395
Alexandre Rames5319def2014-10-23 10:03:10 +0100396class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
397 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000398 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100399
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100400 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000401 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100402 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000403 if (instruction_->CanThrowIntoCatchBlock()) {
404 // Live registers will be restored in the catch block if caught.
405 SaveLiveRegisters(codegen, instruction_->GetLocations());
406 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000407 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
408 instruction_,
409 instruction_->GetDexPc(),
410 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800411 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100412 }
413
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100414 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100415
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100416 const char* GetDescription() const override { return "NullCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100417
Alexandre Rames5319def2014-10-23 10:03:10 +0100418 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100419 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
420};
421
422class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
423 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100424 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000425 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100426
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100427 void EmitNativeCode(CodeGenerator* codegen) override {
Artem Serov7957d952017-04-04 15:44:09 +0100428 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000429 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100430 __ Bind(GetEntryLabel());
Artem Serov1a719e42019-07-18 14:24:55 +0100431 SaveLiveRegisters(codegen, locations); // Only saves live vector regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000432 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800433 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov1a719e42019-07-18 14:24:55 +0100434 RestoreLiveRegisters(codegen, locations); // Only restores live vector regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000435 if (successor_ == nullptr) {
436 __ B(GetReturnLabel());
437 } else {
438 __ B(arm64_codegen->GetLabelOf(successor_));
439 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100440 }
441
Scott Wakeling97c72b72016-06-24 16:19:36 +0100442 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100443 DCHECK(successor_ == nullptr);
444 return &return_label_;
445 }
446
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100447 HBasicBlock* GetSuccessor() const {
448 return successor_;
449 }
450
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100451 const char* GetDescription() const override { return "SuspendCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100452
Alexandre Rames5319def2014-10-23 10:03:10 +0100453 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100454 // If not null, the block to branch to after the suspend check.
455 HBasicBlock* const successor_;
456
457 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100458 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100459
460 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
461};
462
Alexandre Rames67555f72014-11-18 10:55:16 +0000463class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
464 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000465 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000466 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000467
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100468 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000469 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800470
Alexandre Rames3e69f162014-12-10 10:36:50 +0000471 DCHECK(instruction_->IsCheckCast()
472 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
473 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100474 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000475
Alexandre Rames67555f72014-11-18 10:55:16 +0000476 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000477
Vladimir Marko87584542017-12-12 17:47:52 +0000478 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000479 SaveLiveRegisters(codegen, locations);
480 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000481
482 // We're moving two locations to locations that could overlap, so we need a parallel
483 // move resolver.
484 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800485 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800486 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100487 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800488 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800489 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100490 DataType::Type::kReference);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000491 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000492 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800493 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100494 DataType::Type ret_type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000495 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
496 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
497 } else {
498 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800499 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
500 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000501 }
502
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000503 if (!is_fatal_) {
504 RestoreLiveRegisters(codegen, locations);
505 __ B(GetExitLabel());
506 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000507 }
508
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100509 const char* GetDescription() const override { return "TypeCheckSlowPathARM64"; }
510 bool IsFatal() const override { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100511
Alexandre Rames67555f72014-11-18 10:55:16 +0000512 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000513 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000514
Alexandre Rames67555f72014-11-18 10:55:16 +0000515 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
516};
517
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700518class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
519 public:
Aart Bik42249c32016-01-07 15:33:50 -0800520 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000521 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700522
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100523 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bik42249c32016-01-07 15:33:50 -0800524 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700525 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100526 LocationSummary* locations = instruction_->GetLocations();
527 SaveLiveRegisters(codegen, locations);
528 InvokeRuntimeCallingConvention calling_convention;
529 __ Mov(calling_convention.GetRegisterAt(0),
530 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000531 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100532 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700533 }
534
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100535 const char* GetDescription() const override { return "DeoptimizationSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100536
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700537 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700538 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
539};
540
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100541class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
542 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000543 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100544
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100545 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100546 LocationSummary* locations = instruction_->GetLocations();
547 __ Bind(GetEntryLabel());
548 SaveLiveRegisters(codegen, locations);
549
550 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100551 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100552 parallel_move.AddMove(
553 locations->InAt(0),
554 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100555 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100556 nullptr);
557 parallel_move.AddMove(
558 locations->InAt(1),
559 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100560 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100561 nullptr);
562 parallel_move.AddMove(
563 locations->InAt(2),
564 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100565 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100566 nullptr);
567 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
568
569 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000570 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100571 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
572 RestoreLiveRegisters(codegen, locations);
573 __ B(GetExitLabel());
574 }
575
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100576 const char* GetDescription() const override { return "ArraySetSlowPathARM64"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100577
578 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100579 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
580};
581
Zheng Xu3927c8b2015-11-18 17:46:25 +0800582void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
583 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000584 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800585
586 // We are about to use the assembler to place literals directly. Make sure we have enough
587 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000588 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
589 num_entries * sizeof(int32_t),
590 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800591
592 __ Bind(&table_start_);
593 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
594 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100595 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800596 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100597 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800598 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
599 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
600 Literal<int32_t> literal(jump_offset);
601 __ place(&literal);
602 }
603}
604
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000605// Slow path generating a read barrier for a heap reference.
606class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
607 public:
608 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
609 Location out,
610 Location ref,
611 Location obj,
612 uint32_t offset,
613 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000614 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000615 out_(out),
616 ref_(ref),
617 obj_(obj),
618 offset_(offset),
619 index_(index) {
620 DCHECK(kEmitCompilerReadBarrier);
621 // If `obj` is equal to `out` or `ref`, it means the initial object
622 // has been overwritten by (or after) the heap object reference load
623 // to be instrumented, e.g.:
624 //
625 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000626 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000627 //
628 // In that case, we have lost the information about the original
629 // object, and the emitted read barrier cannot work properly.
630 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
631 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
632 }
633
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100634 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000635 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
636 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100637 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000638 DCHECK(locations->CanCall());
639 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100640 DCHECK(instruction_->IsInstanceFieldGet() ||
641 instruction_->IsStaticFieldGet() ||
642 instruction_->IsArrayGet() ||
643 instruction_->IsInstanceOf() ||
644 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700645 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000646 << "Unexpected instruction in read barrier for heap reference slow path: "
647 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000648 // The read barrier instrumentation of object ArrayGet
649 // instructions does not support the HIntermediateAddress
650 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000651 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100652 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000653
654 __ Bind(GetEntryLabel());
655
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000656 SaveLiveRegisters(codegen, locations);
657
658 // We may have to change the index's value, but as `index_` is a
659 // constant member (like other "inputs" of this slow path),
660 // introduce a copy of it, `index`.
661 Location index = index_;
662 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100663 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000664 if (instruction_->IsArrayGet()) {
665 // Compute the actual memory offset and store it in `index`.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100666 Register index_reg = RegisterFrom(index_, DataType::Type::kInt32);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000667 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
668 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
669 // We are about to change the value of `index_reg` (see the
670 // calls to vixl::MacroAssembler::Lsl and
671 // vixl::MacroAssembler::Mov below), but it has
672 // not been saved by the previous call to
673 // art::SlowPathCode::SaveLiveRegisters, as it is a
674 // callee-save register --
675 // art::SlowPathCode::SaveLiveRegisters does not consider
676 // callee-save registers, as it has been designed with the
677 // assumption that callee-save registers are supposed to be
678 // handled by the called function. So, as a callee-save
679 // register, `index_reg` _would_ eventually be saved onto
680 // the stack, but it would be too late: we would have
681 // changed its value earlier. Therefore, we manually save
682 // it here into another freely available register,
683 // `free_reg`, chosen of course among the caller-save
684 // registers (as a callee-save `free_reg` register would
685 // exhibit the same problem).
686 //
687 // Note we could have requested a temporary register from
688 // the register allocator instead; but we prefer not to, as
689 // this is a slow path, and we know we can find a
690 // caller-save register that is available.
691 Register free_reg = FindAvailableCallerSaveRegister(codegen);
692 __ Mov(free_reg.W(), index_reg);
693 index_reg = free_reg;
694 index = LocationFrom(index_reg);
695 } else {
696 // The initial register stored in `index_` has already been
697 // saved in the call to art::SlowPathCode::SaveLiveRegisters
698 // (as it is not a callee-save register), so we can freely
699 // use it.
700 }
701 // Shifting the index value contained in `index_reg` by the scale
702 // factor (2) cannot overflow in practice, as the runtime is
703 // unable to allocate object arrays with a size larger than
704 // 2^26 - 1 (that is, 2^28 - 4 bytes).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100705 __ Lsl(index_reg, index_reg, DataType::SizeShift(type));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000706 static_assert(
707 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
708 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
709 __ Add(index_reg, index_reg, Operand(offset_));
710 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100711 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
712 // intrinsics, `index_` is not shifted by a scale factor of 2
713 // (as in the case of ArrayGet), as it is actually an offset
714 // to an object field within an object.
715 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000716 DCHECK(instruction_->GetLocations()->Intrinsified());
717 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
718 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
719 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100720 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100721 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000722 }
723 }
724
725 // We're moving two or three locations to locations that could
726 // overlap, so we need a parallel move resolver.
727 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100728 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000729 parallel_move.AddMove(ref_,
730 LocationFrom(calling_convention.GetRegisterAt(0)),
731 type,
732 nullptr);
733 parallel_move.AddMove(obj_,
734 LocationFrom(calling_convention.GetRegisterAt(1)),
735 type,
736 nullptr);
737 if (index.IsValid()) {
738 parallel_move.AddMove(index,
739 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100740 DataType::Type::kInt32,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000741 nullptr);
742 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
743 } else {
744 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
745 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
746 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000747 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000748 instruction_,
749 instruction_->GetDexPc(),
750 this);
751 CheckEntrypointTypes<
752 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
753 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
754
755 RestoreLiveRegisters(codegen, locations);
756
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000757 __ B(GetExitLabel());
758 }
759
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100760 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000761
762 private:
763 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100764 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
765 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000766 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
767 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
768 return Register(VIXLRegCodeFromART(i), kXRegSize);
769 }
770 }
771 // We shall never fail to find a free caller-save register, as
772 // there are more than two core caller-save registers on ARM64
773 // (meaning it is possible to find one which is different from
774 // `ref` and `obj`).
775 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
776 LOG(FATAL) << "Could not find a free register";
777 UNREACHABLE();
778 }
779
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000780 const Location out_;
781 const Location ref_;
782 const Location obj_;
783 const uint32_t offset_;
784 // An additional location containing an index to an array.
785 // Only used for HArrayGet and the UnsafeGetObject &
786 // UnsafeGetObjectVolatile intrinsics.
787 const Location index_;
788
789 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
790};
791
792// Slow path generating a read barrier for a GC root.
793class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
794 public:
795 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000796 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000797 DCHECK(kEmitCompilerReadBarrier);
798 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000799
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100800 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000801 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100802 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000803 DCHECK(locations->CanCall());
804 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000805 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
806 << "Unexpected instruction in read barrier for GC root slow path: "
807 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000808
809 __ Bind(GetEntryLabel());
810 SaveLiveRegisters(codegen, locations);
811
812 InvokeRuntimeCallingConvention calling_convention;
813 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
814 // The argument of the ReadBarrierForRootSlow is not a managed
815 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
816 // thus we need a 64-bit move here, and we cannot use
817 //
818 // arm64_codegen->MoveLocation(
819 // LocationFrom(calling_convention.GetRegisterAt(0)),
820 // root_,
821 // type);
822 //
823 // which would emit a 32-bit move, as `type` is a (32-bit wide)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100824 // reference type (`DataType::Type::kReference`).
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000825 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000826 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000827 instruction_,
828 instruction_->GetDexPc(),
829 this);
830 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
831 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
832
833 RestoreLiveRegisters(codegen, locations);
834 __ B(GetExitLabel());
835 }
836
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100837 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000838
839 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000840 const Location out_;
841 const Location root_;
842
843 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
844};
845
Alexandre Rames5319def2014-10-23 10:03:10 +0100846#undef __
847
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100848Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100849 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100850 if (type == DataType::Type::kVoid) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100851 LOG(FATAL) << "Unreachable type " << type;
852 }
853
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100854 if (DataType::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100855 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
856 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100857 } else if (!DataType::IsFloatingPointType(type) &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000858 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000859 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
860 } else {
861 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100862 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
863 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100864 }
865
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000866 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100867 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100868 return next_location;
869}
870
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100871Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100872 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100873}
874
Vladimir Marko86c87522020-05-11 16:55:55 +0100875Location CriticalNativeCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
876 DCHECK_NE(type, DataType::Type::kReference);
877
878 Location location = Location::NoLocation();
879 if (DataType::IsFloatingPointType(type)) {
880 if (fpr_index_ < kParameterFPRegistersLength) {
881 location = LocationFrom(kParameterFPRegisters[fpr_index_]);
882 ++fpr_index_;
883 }
884 } else {
885 // Native ABI uses the same registers as managed, except that the method register x0
886 // is a normal argument.
887 if (gpr_index_ < 1u + kParameterCoreRegistersLength) {
888 location = LocationFrom(gpr_index_ == 0u ? x0 : kParameterCoreRegisters[gpr_index_ - 1u]);
889 ++gpr_index_;
890 }
891 }
892 if (location.IsInvalid()) {
893 if (DataType::Is64BitType(type)) {
894 location = Location::DoubleStackSlot(stack_offset_);
895 } else {
896 location = Location::StackSlot(stack_offset_);
897 }
898 stack_offset_ += kFramePointerSize;
899
900 if (for_register_allocation_) {
901 location = Location::Any();
902 }
903 }
904 return location;
905}
906
907Location CriticalNativeCallingConventionVisitorARM64::GetReturnLocation(DataType::Type type) const {
908 // We perform conversion to the managed ABI return register after the call if needed.
909 InvokeDexCallingConventionVisitorARM64 dex_calling_convention;
910 return dex_calling_convention.GetReturnLocation(type);
911}
912
913Location CriticalNativeCallingConventionVisitorARM64::GetMethodLocation() const {
914 // Pass the method in the hidden argument x15.
915 return Location::RegisterLocation(x15.GetCode());
916}
917
Serban Constantinescu579885a2015-02-22 20:51:33 +0000918CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100919 const CompilerOptions& compiler_options,
920 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100921 : CodeGenerator(graph,
922 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000923 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000924 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100925 callee_saved_core_registers.GetList(),
926 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100927 compiler_options,
928 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100929 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
930 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov1a719e42019-07-18 14:24:55 +0100931 location_builder_neon_(graph, this),
932 instruction_visitor_neon_(graph, this),
933 location_builder_sve_(graph, this),
934 instruction_visitor_sve_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100935 move_resolver_(graph->GetAllocator(), this),
Artem Serovaa6f4832018-11-21 18:57:54 +0000936 assembler_(graph->GetAllocator(),
937 compiler_options.GetInstructionSetFeatures()->AsArm64InstructionSetFeatures()),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000938 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100939 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000940 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100941 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000942 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100943 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +0100944 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100945 call_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100946 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100947 uint32_literals_(std::less<uint32_t>(),
948 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
949 uint64_literals_(std::less<uint64_t>(),
950 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000951 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100952 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000953 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +0000954 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
955 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
956 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000957 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000958 AddAllocatedRegister(LocationFrom(lr));
Artem Serov1a719e42019-07-18 14:24:55 +0100959
960 bool use_sve = ShouldUseSVE();
961 if (use_sve) {
962 location_builder_ = &location_builder_sve_;
963 instruction_visitor_ = &instruction_visitor_sve_;
964 } else {
965 location_builder_ = &location_builder_neon_;
966 instruction_visitor_ = &instruction_visitor_neon_;
967 }
968}
969
970bool CodeGeneratorARM64::ShouldUseSVE() const {
971 return kArm64AllowSVE && GetInstructionSetFeatures().HasSVE();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000972}
Alexandre Rames5319def2014-10-23 10:03:10 +0100973
Alexandre Rames67555f72014-11-18 10:55:16 +0000974#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100975
Zheng Xu3927c8b2015-11-18 17:46:25 +0800976void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100977 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800978 jump_table->EmitTable(this);
979 }
980}
981
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000982void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800983 EmitJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +0000984
985 // Emit JIT baker read barrier slow paths.
Vladimir Marko695348f2020-05-19 14:42:02 +0100986 DCHECK(GetCompilerOptions().IsJitCompiler() || jit_baker_read_barrier_slow_paths_.empty());
Vladimir Marko966b46f2018-08-03 10:20:19 +0000987 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
988 uint32_t encoded_data = entry.first;
989 vixl::aarch64::Label* slow_path_entry = &entry.second.label;
990 __ Bind(slow_path_entry);
Andreas Gampe3db70682018-12-26 15:12:03 -0800991 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name= */ nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +0000992 }
993
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000994 // Ensure we emit the literal pool.
995 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000996
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000997 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +0000998
999 // Verify Baker read barrier linker patches.
1000 if (kIsDebugBuild) {
1001 ArrayRef<const uint8_t> code = allocator->GetMemory();
1002 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
1003 DCHECK(info.label.IsBound());
1004 uint32_t literal_offset = info.label.GetLocation();
1005 DCHECK_ALIGNED(literal_offset, 4u);
1006
1007 auto GetInsn = [&code](uint32_t offset) {
1008 DCHECK_ALIGNED(offset, 4u);
1009 return
1010 (static_cast<uint32_t>(code[offset + 0]) << 0) +
1011 (static_cast<uint32_t>(code[offset + 1]) << 8) +
1012 (static_cast<uint32_t>(code[offset + 2]) << 16)+
1013 (static_cast<uint32_t>(code[offset + 3]) << 24);
1014 };
1015
1016 const uint32_t encoded_data = info.custom_data;
1017 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
1018 // Check that the next instruction matches the expected LDR.
1019 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01001020 case BakerReadBarrierKind::kField:
1021 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00001022 DCHECK_GE(code.size() - literal_offset, 8u);
1023 uint32_t next_insn = GetInsn(literal_offset + 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001024 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1025 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko0ecac682018-08-07 10:40:38 +01001026 if (kind == BakerReadBarrierKind::kField) {
1027 // LDR (immediate) with correct base_reg.
1028 CHECK_EQ(next_insn & 0xffc003e0u, 0xb9400000u | (base_reg << 5));
1029 } else {
1030 DCHECK(kind == BakerReadBarrierKind::kAcquire);
1031 // LDAR with correct base_reg.
1032 CHECK_EQ(next_insn & 0xffffffe0u, 0x88dffc00u | (base_reg << 5));
1033 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001034 break;
1035 }
1036 case BakerReadBarrierKind::kArray: {
1037 DCHECK_GE(code.size() - literal_offset, 8u);
1038 uint32_t next_insn = GetInsn(literal_offset + 4u);
1039 // LDR (register) with the correct base_reg, size=10 (32-bit), option=011 (extend = LSL),
1040 // and S=1 (shift amount = 2 for 32-bit version), i.e. LDR Wt, [Xn, Xm, LSL #2].
1041 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1042 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1043 CHECK_EQ(next_insn & 0xffe0ffe0u, 0xb8607800u | (base_reg << 5));
1044 CheckValidReg((next_insn >> 16) & 0x1f); // Check index register
1045 break;
1046 }
1047 case BakerReadBarrierKind::kGcRoot: {
1048 DCHECK_GE(literal_offset, 4u);
1049 uint32_t prev_insn = GetInsn(literal_offset - 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001050 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko94796f82018-08-08 15:15:33 +01001051 // Usually LDR (immediate) with correct root_reg but
1052 // we may have a "MOV marked, old_value" for UnsafeCASObject.
1053 if ((prev_insn & 0xffe0ffff) != (0x2a0003e0 | root_reg)) { // MOV?
1054 CHECK_EQ(prev_insn & 0xffc0001fu, 0xb9400000u | root_reg); // LDR?
1055 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001056 break;
1057 }
1058 default:
1059 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
1060 UNREACHABLE();
1061 }
1062 }
1063 }
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001064}
1065
Zheng Xuad4450e2015-04-17 18:48:56 +08001066void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1067 // Note: There are 6 kinds of moves:
1068 // 1. constant -> GPR/FPR (non-cycle)
1069 // 2. constant -> stack (non-cycle)
1070 // 3. GPR/FPR -> GPR/FPR
1071 // 4. GPR/FPR -> stack
1072 // 5. stack -> GPR/FPR
1073 // 6. stack -> stack (non-cycle)
1074 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1075 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1076 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1077 // dependency.
1078 vixl_temps_.Open(GetVIXLAssembler());
1079}
1080
1081void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1082 vixl_temps_.Close();
1083}
1084
1085Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001086 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1087 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1088 || kind == Location::kSIMDStackSlot);
1089 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1090 ? Location::kFpuRegister
1091 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001092 Location scratch = GetScratchLocation(kind);
1093 if (!scratch.Equals(Location::NoLocation())) {
1094 return scratch;
1095 }
1096 // Allocate from VIXL temp registers.
1097 if (kind == Location::kRegister) {
1098 scratch = LocationFrom(vixl_temps_.AcquireX());
1099 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001100 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serov1a719e42019-07-18 14:24:55 +01001101 scratch = codegen_->GetGraph()->HasSIMD()
1102 ? codegen_->GetInstructionCodeGeneratorArm64()->AllocateSIMDScratchLocation(&vixl_temps_)
1103 : LocationFrom(vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001104 }
1105 AddScratchLocation(scratch);
1106 return scratch;
1107}
1108
1109void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1110 if (loc.IsRegister()) {
1111 vixl_temps_.Release(XRegisterFrom(loc));
1112 } else {
1113 DCHECK(loc.IsFpuRegister());
Artem Serov1a719e42019-07-18 14:24:55 +01001114 if (codegen_->GetGraph()->HasSIMD()) {
1115 codegen_->GetInstructionCodeGeneratorArm64()->FreeSIMDScratchLocation(loc, &vixl_temps_);
1116 } else {
1117 vixl_temps_.Release(DRegisterFrom(loc));
1118 }
Zheng Xuad4450e2015-04-17 18:48:56 +08001119 }
1120 RemoveScratchLocation(loc);
1121}
1122
Alexandre Rames3e69f162014-12-10 10:36:50 +00001123void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001124 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001125 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001126}
1127
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001128void CodeGeneratorARM64::MaybeIncrementHotness(bool is_frame_entry) {
1129 MacroAssembler* masm = GetVIXLAssembler();
1130 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1131 UseScratchRegisterScope temps(masm);
1132 Register counter = temps.AcquireX();
1133 Register method = is_frame_entry ? kArtMethodRegister : temps.AcquireX();
1134 if (!is_frame_entry) {
1135 __ Ldr(method, MemOperand(sp, 0));
1136 }
1137 __ Ldrh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
1138 __ Add(counter, counter, 1);
1139 // Subtract one if the counter would overflow.
1140 __ Sub(counter, counter, Operand(counter, LSR, 16));
1141 __ Strh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
1142 }
1143
1144 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffray095dc462020-08-17 16:40:28 +01001145 ScopedProfilingInfoUse spiu(
1146 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
1147 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001148 if (info != nullptr) {
Nicolas Geoffrayc1cd1332020-01-25 13:08:24 +00001149 uint64_t address = reinterpret_cast64<uint64_t>(info);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001150 vixl::aarch64::Label done;
1151 UseScratchRegisterScope temps(masm);
1152 Register temp = temps.AcquireX();
1153 Register counter = temps.AcquireW();
1154 __ Mov(temp, address);
1155 __ Ldrh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
1156 __ Add(counter, counter, 1);
Nicolas Geoffray4313ccb2020-08-26 17:01:15 +01001157 __ And(counter, counter, interpreter::kTieredHotnessMask);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001158 __ Strh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
Nicolas Geoffray4313ccb2020-08-26 17:01:15 +01001159 __ Cbnz(counter, &done);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001160 if (is_frame_entry) {
1161 if (HasEmptyFrame()) {
Vladimir Markodec78172020-06-19 15:31:23 +01001162 // The entrypoint expects the method at the bottom of the stack. We
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001163 // claim stack space necessary for alignment.
Vladimir Markodec78172020-06-19 15:31:23 +01001164 IncreaseFrame(kStackAlignment);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001165 __ Stp(kArtMethodRegister, lr, MemOperand(sp, 0));
1166 } else if (!RequiresCurrentMethod()) {
1167 __ Str(kArtMethodRegister, MemOperand(sp, 0));
1168 }
1169 } else {
1170 CHECK(RequiresCurrentMethod());
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001171 }
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001172 uint32_t entrypoint_offset =
1173 GetThreadOffset<kArm64PointerSize>(kQuickCompileOptimized).Int32Value();
1174 __ Ldr(lr, MemOperand(tr, entrypoint_offset));
1175 // Note: we don't record the call here (and therefore don't generate a stack
1176 // map), as the entrypoint should never be suspended.
1177 __ Blr(lr);
1178 if (HasEmptyFrame()) {
1179 CHECK(is_frame_entry);
1180 __ Ldr(lr, MemOperand(sp, 8));
Vladimir Markodec78172020-06-19 15:31:23 +01001181 DecreaseFrame(kStackAlignment);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001182 }
1183 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001184 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001185 }
1186}
1187
Alexandre Rames5319def2014-10-23 10:03:10 +01001188void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001189 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001190 __ Bind(&frame_entry_label_);
1191
Vladimir Marko33bff252017-11-01 14:35:42 +00001192 bool do_overflow_check =
1193 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001194 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001195 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001196 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001197 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001198 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001199 {
1200 // Ensure that between load and RecordPcInfo there are no pools emitted.
1201 ExactAssemblyScope eas(GetVIXLAssembler(),
1202 kInstructionSize,
1203 CodeBufferCheckScope::kExactSize);
1204 __ ldr(wzr, MemOperand(temp, 0));
1205 RecordPcInfo(nullptr, 0);
1206 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001207 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001208
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001209 if (!HasEmptyFrame()) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001210 // Stack layout:
1211 // sp[frame_size - 8] : lr.
1212 // ... : other preserved core registers.
1213 // ... : other preserved fp registers.
1214 // ... : reserved frame space.
1215 // sp[0] : current method.
Vladimir Marko1a225a72019-07-05 13:37:42 +01001216 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1217 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1218 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1219 DCHECK(!preserved_core_registers.IsEmpty());
1220 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1221 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001222
Vladimir Marko1a225a72019-07-05 13:37:42 +01001223 // Save the current method if we need it, or if using STP reduces code
1224 // size. Note that we do not do this in HCurrentMethod, as the
1225 // instruction might have been removed in the SSA graph.
1226 CPURegister lowest_spill;
1227 if (core_spills_offset == kXRegSizeInBytes) {
1228 // If there is no gap between the method and the lowest core spill, use
1229 // aligned STP pre-index to store both. Max difference is 512. We do
1230 // that to reduce code size even if we do not have to save the method.
1231 DCHECK_LE(frame_size, 512); // 32 core registers are only 256 bytes.
1232 lowest_spill = preserved_core_registers.PopLowestIndex();
1233 __ Stp(kArtMethodRegister, lowest_spill, MemOperand(sp, -frame_size, PreIndex));
1234 } else if (RequiresCurrentMethod()) {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001235 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001236 } else {
1237 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001238 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001239 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Vladimir Marko1a225a72019-07-05 13:37:42 +01001240 if (lowest_spill.IsValid()) {
1241 GetAssembler()->cfi().RelOffset(DWARFReg(lowest_spill), core_spills_offset);
1242 core_spills_offset += kXRegSizeInBytes;
1243 }
1244 GetAssembler()->SpillRegisters(preserved_core_registers, core_spills_offset);
1245 GetAssembler()->SpillRegisters(preserved_fp_registers, fp_spills_offset);
Mingyao Yang063fc772016-08-02 11:02:54 -07001246
1247 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1248 // Initialize should_deoptimize flag to 0.
1249 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1250 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1251 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001252 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001253 MaybeIncrementHotness(/* is_frame_entry= */ true);
Andreas Gampe3db70682018-12-26 15:12:03 -08001254 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001255}
1256
1257void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001258 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001259 if (!HasEmptyFrame()) {
Vladimir Marko1a225a72019-07-05 13:37:42 +01001260 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1261 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1262 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1263 DCHECK(!preserved_core_registers.IsEmpty());
1264 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1265 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
1266
1267 CPURegister lowest_spill;
1268 if (core_spills_offset == kXRegSizeInBytes) {
1269 // If there is no gap between the method and the lowest core spill, use
1270 // aligned LDP pre-index to pop both. Max difference is 504. We do
1271 // that to reduce code size even though the loaded method is unused.
1272 DCHECK_LE(frame_size, 504); // 32 core registers are only 256 bytes.
1273 lowest_spill = preserved_core_registers.PopLowestIndex();
1274 core_spills_offset += kXRegSizeInBytes;
1275 }
1276 GetAssembler()->UnspillRegisters(preserved_fp_registers, fp_spills_offset);
1277 GetAssembler()->UnspillRegisters(preserved_core_registers, core_spills_offset);
1278 if (lowest_spill.IsValid()) {
1279 __ Ldp(xzr, lowest_spill, MemOperand(sp, frame_size, PostIndex));
1280 GetAssembler()->cfi().Restore(DWARFReg(lowest_spill));
1281 } else {
1282 __ Drop(frame_size);
1283 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001284 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001285 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001286 __ Ret();
1287 GetAssembler()->cfi().RestoreState();
1288 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001289}
1290
Scott Wakeling97c72b72016-06-24 16:19:36 +01001291CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001292 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001293 return CPURegList(CPURegister::kRegister, kXRegSize,
1294 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001295}
1296
Scott Wakeling97c72b72016-06-24 16:19:36 +01001297CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001298 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1299 GetNumberOfFloatingPointRegisters()));
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001300 return CPURegList(CPURegister::kVRegister, kDRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001301 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001302}
1303
Alexandre Rames5319def2014-10-23 10:03:10 +01001304void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1305 __ Bind(GetLabelOf(block));
1306}
1307
Calin Juravle175dc732015-08-25 15:42:32 +01001308void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1309 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001310 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001311}
1312
Calin Juravlee460d1d2015-09-29 04:52:17 +01001313void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1314 if (location.IsRegister()) {
1315 locations->AddTemp(location);
1316 } else {
1317 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1318 }
1319}
1320
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001321void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001322 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001323 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001324 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001325 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001326 if (value_can_be_null) {
1327 __ Cbz(value, &done);
1328 }
Roland Levillainc73f0522018-08-14 15:16:50 +01001329 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07001330 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01001331 // Calculate the offset (in the card table) of the card corresponding to
1332 // `object`.
Alexandre Rames5319def2014-10-23 10:03:10 +01001333 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Roland Levillainc73f0522018-08-14 15:16:50 +01001334 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
1335 // `object`'s card.
1336 //
1337 // Register `card` contains the address of the card table. Note that the card
1338 // table's base is biased during its creation so that it always starts at an
1339 // address whose least-significant byte is equal to `kCardDirty` (see
1340 // art::gc::accounting::CardTable::Create). Therefore the STRB instruction
1341 // below writes the `kCardDirty` (byte) value into the `object`'s card
1342 // (located at `card + object >> kCardShift`).
1343 //
1344 // This dual use of the value in register `card` (1. to calculate the location
1345 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
1346 // (no need to explicitly load `kCardDirty` as an immediate value).
Serban Constantinescu02164b32014-11-13 14:05:07 +00001347 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001348 if (value_can_be_null) {
1349 __ Bind(&done);
1350 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001351}
1352
David Brazdil58282f42016-01-14 12:45:10 +00001353void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001354 // Blocked core registers:
1355 // lr : Runtime reserved.
1356 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001357 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001358 // ip1 : VIXL core temp.
1359 // ip0 : VIXL core temp.
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001360 // x18 : Platform register.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001361 //
1362 // Blocked fp registers:
1363 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001364 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1365 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001366 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001367 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001368 }
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001369 blocked_core_registers_[X18] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001370
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001371 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001372 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001373 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001374 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001375
David Brazdil58282f42016-01-14 12:45:10 +00001376 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001377 // Stubs do not save callee-save floating point registers. If the graph
1378 // is debuggable, we need to deal with these registers differently. For
1379 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001380 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1381 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001382 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001383 }
1384 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001385}
1386
Alexandre Rames3e69f162014-12-10 10:36:50 +00001387size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1388 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1389 __ Str(reg, MemOperand(sp, stack_index));
1390 return kArm64WordSize;
1391}
1392
1393size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1394 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1395 __ Ldr(reg, MemOperand(sp, stack_index));
1396 return kArm64WordSize;
1397}
1398
Artem Serov9df37b92019-07-23 16:41:54 +01001399size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1400 uint32_t reg_id ATTRIBUTE_UNUSED) {
1401 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1402 << "use SaveRestoreLiveRegistersHelper";
1403 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001404}
1405
Artem Serov9df37b92019-07-23 16:41:54 +01001406size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1407 uint32_t reg_id ATTRIBUTE_UNUSED) {
1408 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1409 << "use SaveRestoreLiveRegistersHelper";
1410 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001411}
1412
Alexandre Rames5319def2014-10-23 10:03:10 +01001413void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001414 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001415}
1416
1417void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001418 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001419}
1420
Vladimir Markoa0431112018-06-25 09:32:54 +01001421const Arm64InstructionSetFeatures& CodeGeneratorARM64::GetInstructionSetFeatures() const {
1422 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArm64InstructionSetFeatures();
1423}
1424
Alexandre Rames67555f72014-11-18 10:55:16 +00001425void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001426 if (constant->IsIntConstant()) {
1427 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1428 } else if (constant->IsLongConstant()) {
1429 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1430 } else if (constant->IsNullConstant()) {
1431 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001432 } else if (constant->IsFloatConstant()) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001433 __ Fmov(VRegister(destination), constant->AsFloatConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001434 } else {
1435 DCHECK(constant->IsDoubleConstant());
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001436 __ Fmov(VRegister(destination), constant->AsDoubleConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001437 }
1438}
1439
Alexandre Rames3e69f162014-12-10 10:36:50 +00001440
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001441static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001442 DCHECK(constant.IsConstant());
1443 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001444 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001445 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001446 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1447 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1448 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1449 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001450}
1451
Roland Levillain952b2352017-05-03 19:49:14 +01001452// Allocate a scratch register from the VIXL pool, querying first
1453// the floating-point register pool, and then the core register
1454// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001455// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1456// using a different allocation strategy.
1457static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1458 vixl::aarch64::UseScratchRegisterScope* temps,
1459 int size_in_bits) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001460 return masm->GetScratchVRegisterList()->IsEmpty()
Roland Levillain558dea12017-01-27 19:40:44 +00001461 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1462 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1463}
1464
Calin Juravlee460d1d2015-09-29 04:52:17 +01001465void CodeGeneratorARM64::MoveLocation(Location destination,
1466 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001467 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001468 if (source.Equals(destination)) {
1469 return;
1470 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001471
1472 // A valid move can always be inferred from the destination and source
1473 // locations. When moving from and to a register, the argument type can be
1474 // used to generate 32bit instead of 64bit moves. In debug mode we also
1475 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001476 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001477
1478 if (destination.IsRegister() || destination.IsFpuRegister()) {
1479 if (unspecified_type) {
1480 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1481 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001482 (src_cst != nullptr && (src_cst->IsIntConstant()
1483 || src_cst->IsFloatConstant()
1484 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001485 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001486 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001487 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001488 // If the source is a double stack slot or a 64bit constant, a 64bit
1489 // type is appropriate. Else the source is a register, and since the
1490 // type has not been specified, we chose a 64bit type to force a 64bit
1491 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001492 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001493 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001494 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001495 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1496 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001497 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001498 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1499 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1500 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001501 } else if (source.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001502 GetInstructionCodeGeneratorArm64()->LoadSIMDRegFromStack(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001503 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001504 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001505 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001506 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001507 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001508 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001509 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001510 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001511 DataType::Type source_type = DataType::Is64BitType(dst_type)
1512 ? DataType::Type::kInt64
1513 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001514 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1515 }
1516 } else {
1517 DCHECK(source.IsFpuRegister());
1518 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001519 DataType::Type source_type = DataType::Is64BitType(dst_type)
1520 ? DataType::Type::kFloat64
1521 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001522 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1523 } else {
1524 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001525 if (GetGraph()->HasSIMD()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001526 GetInstructionCodeGeneratorArm64()->MoveSIMDRegToSIMDReg(destination, source);
Artem Serovd4bccf12017-04-03 18:47:32 +01001527 } else {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001528 __ Fmov(VRegister(dst), FPRegisterFrom(source, dst_type));
Artem Serovd4bccf12017-04-03 18:47:32 +01001529 }
1530 }
1531 }
1532 } else if (destination.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001533 GetInstructionCodeGeneratorArm64()->MoveToSIMDStackSlot(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001534 } else { // The destination is not a register. It must be a stack slot.
1535 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1536 if (source.IsRegister() || source.IsFpuRegister()) {
1537 if (unspecified_type) {
1538 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001539 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001540 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001541 dst_type =
1542 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001543 }
1544 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001545 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1546 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001547 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001548 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001549 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1550 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001551 UseScratchRegisterScope temps(GetVIXLAssembler());
1552 HConstant* src_cst = source.GetConstant();
1553 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001554 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001555 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1556 ? Register(xzr)
1557 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001558 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001559 if (src_cst->IsIntConstant()) {
1560 temp = temps.AcquireW();
1561 } else if (src_cst->IsLongConstant()) {
1562 temp = temps.AcquireX();
1563 } else if (src_cst->IsFloatConstant()) {
1564 temp = temps.AcquireS();
1565 } else {
1566 DCHECK(src_cst->IsDoubleConstant());
1567 temp = temps.AcquireD();
1568 }
1569 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001570 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001571 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001572 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001573 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001574 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001575 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001576 // Use any scratch register (a core or a floating-point one)
1577 // from VIXL scratch register pools as a temporary.
1578 //
1579 // We used to only use the FP scratch register pool, but in some
1580 // rare cases the only register from this pool (D31) would
1581 // already be used (e.g. within a ParallelMove instruction, when
1582 // a move is blocked by a another move requiring a scratch FP
1583 // register, which would reserve D31). To prevent this issue, we
1584 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001585 //
1586 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001587 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001588 // use AcquireFPOrCoreCPURegisterOfSize instead of
1589 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1590 // allocates core scratch registers first.
1591 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1592 GetVIXLAssembler(),
1593 &temps,
1594 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001595 __ Ldr(temp, StackOperandFrom(source));
1596 __ Str(temp, StackOperandFrom(destination));
1597 }
1598 }
1599}
1600
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001601void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001602 CPURegister dst,
1603 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001604 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001605 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001606 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001607 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001608 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001609 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001610 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001611 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001612 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001613 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001614 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001615 case DataType::Type::kInt16:
1616 __ Ldrsh(Register(dst), src);
1617 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001618 case DataType::Type::kInt32:
1619 case DataType::Type::kReference:
1620 case DataType::Type::kInt64:
1621 case DataType::Type::kFloat32:
1622 case DataType::Type::kFloat64:
1623 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001624 __ Ldr(dst, src);
1625 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001626 case DataType::Type::kUint32:
1627 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001628 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001629 LOG(FATAL) << "Unreachable type " << type;
1630 }
1631}
1632
Calin Juravle77520bc2015-01-12 18:45:46 +00001633void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001634 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001635 const MemOperand& src,
1636 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001637 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001638 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001639 Register temp_base = temps.AcquireX();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001640 DataType::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001641
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001642 DCHECK(!src.IsPreIndex());
1643 DCHECK(!src.IsPostIndex());
1644
1645 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001646 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001647 {
1648 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1649 MemOperand base = MemOperand(temp_base);
1650 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001651 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001652 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001653 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001654 {
1655 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1656 __ ldarb(Register(dst), base);
1657 if (needs_null_check) {
1658 MaybeRecordImplicitNullCheck(instruction);
1659 }
1660 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001661 if (type == DataType::Type::kInt8) {
1662 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001663 }
1664 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001665 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001666 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001667 {
1668 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1669 __ ldarh(Register(dst), base);
1670 if (needs_null_check) {
1671 MaybeRecordImplicitNullCheck(instruction);
1672 }
1673 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001674 if (type == DataType::Type::kInt16) {
1675 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
1676 }
Artem Serov914d7a82017-02-07 14:33:49 +00001677 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001678 case DataType::Type::kInt32:
1679 case DataType::Type::kReference:
1680 case DataType::Type::kInt64:
1681 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001682 {
1683 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1684 __ ldar(Register(dst), base);
1685 if (needs_null_check) {
1686 MaybeRecordImplicitNullCheck(instruction);
1687 }
1688 }
1689 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001690 case DataType::Type::kFloat32:
1691 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00001692 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001693 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001694
Artem Serov914d7a82017-02-07 14:33:49 +00001695 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1696 {
1697 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1698 __ ldar(temp, base);
1699 if (needs_null_check) {
1700 MaybeRecordImplicitNullCheck(instruction);
1701 }
1702 }
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001703 __ Fmov(VRegister(dst), temp);
Artem Serov914d7a82017-02-07 14:33:49 +00001704 break;
Roland Levillain44015862016-01-22 11:47:17 +00001705 }
Aart Bik66c158e2018-01-31 12:55:04 -08001706 case DataType::Type::kUint32:
1707 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001708 case DataType::Type::kVoid:
Artem Serov914d7a82017-02-07 14:33:49 +00001709 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001710 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001711 }
1712}
1713
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001714void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001715 CPURegister src,
1716 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001717 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001718 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001719 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001720 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001721 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001722 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001723 case DataType::Type::kUint16:
1724 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001725 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001726 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001727 case DataType::Type::kInt32:
1728 case DataType::Type::kReference:
1729 case DataType::Type::kInt64:
1730 case DataType::Type::kFloat32:
1731 case DataType::Type::kFloat64:
1732 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001733 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001734 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001735 case DataType::Type::kUint32:
1736 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001737 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001738 LOG(FATAL) << "Unreachable type " << type;
1739 }
1740}
1741
Artem Serov914d7a82017-02-07 14:33:49 +00001742void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001743 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001744 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00001745 const MemOperand& dst,
1746 bool needs_null_check) {
1747 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001748 UseScratchRegisterScope temps(GetVIXLAssembler());
1749 Register temp_base = temps.AcquireX();
1750
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001751 DCHECK(!dst.IsPreIndex());
1752 DCHECK(!dst.IsPostIndex());
1753
1754 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001755 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001756 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001757 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00001758 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001759 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001760 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001761 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001762 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001763 {
1764 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1765 __ stlrb(Register(src), base);
1766 if (needs_null_check) {
1767 MaybeRecordImplicitNullCheck(instruction);
1768 }
1769 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001770 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001771 case DataType::Type::kUint16:
1772 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001773 {
1774 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1775 __ stlrh(Register(src), base);
1776 if (needs_null_check) {
1777 MaybeRecordImplicitNullCheck(instruction);
1778 }
1779 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001780 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001781 case DataType::Type::kInt32:
1782 case DataType::Type::kReference:
1783 case DataType::Type::kInt64:
1784 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001785 {
1786 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1787 __ stlr(Register(src), base);
1788 if (needs_null_check) {
1789 MaybeRecordImplicitNullCheck(instruction);
1790 }
1791 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001792 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001793 case DataType::Type::kFloat32:
1794 case DataType::Type::kFloat64: {
1795 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001796 Register temp_src;
1797 if (src.IsZero()) {
1798 // The zero register is used to avoid synthesizing zero constants.
1799 temp_src = Register(src);
1800 } else {
1801 DCHECK(src.IsFPRegister());
1802 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001803 __ Fmov(temp_src, VRegister(src));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001804 }
Artem Serov914d7a82017-02-07 14:33:49 +00001805 {
1806 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1807 __ stlr(temp_src, base);
1808 if (needs_null_check) {
1809 MaybeRecordImplicitNullCheck(instruction);
1810 }
1811 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001812 break;
1813 }
Aart Bik66c158e2018-01-31 12:55:04 -08001814 case DataType::Type::kUint32:
1815 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001816 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001817 LOG(FATAL) << "Unreachable type " << type;
1818 }
1819}
1820
Calin Juravle175dc732015-08-25 15:42:32 +01001821void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1822 HInstruction* instruction,
1823 uint32_t dex_pc,
1824 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001825 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00001826
Vladimir Markof6675082019-05-17 12:05:28 +01001827 ThreadOffset64 entrypoint_offset = GetThreadOffset<kArm64PointerSize>(entrypoint);
1828 // Reduce code size for AOT by using shared trampolines for slow path runtime calls across the
1829 // entire oat file. This adds an extra branch and we do not want to slow down the main path.
1830 // For JIT, thunk sharing is per-method, so the gains would be smaller or even negative.
Vladimir Marko695348f2020-05-19 14:42:02 +01001831 if (slow_path == nullptr || GetCompilerOptions().IsJitCompiler()) {
Vladimir Markof6675082019-05-17 12:05:28 +01001832 __ Ldr(lr, MemOperand(tr, entrypoint_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00001833 // Ensure the pc position is recorded immediately after the `blr` instruction.
1834 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1835 __ blr(lr);
1836 if (EntrypointRequiresStackMap(entrypoint)) {
1837 RecordPcInfo(instruction, dex_pc, slow_path);
1838 }
Vladimir Markof6675082019-05-17 12:05:28 +01001839 } else {
1840 // Ensure the pc position is recorded immediately after the `bl` instruction.
1841 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1842 EmitEntrypointThunkCall(entrypoint_offset);
1843 if (EntrypointRequiresStackMap(entrypoint)) {
1844 RecordPcInfo(instruction, dex_pc, slow_path);
1845 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001846 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001847}
1848
Roland Levillaindec8f632016-07-22 17:10:06 +01001849void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1850 HInstruction* instruction,
1851 SlowPathCode* slow_path) {
1852 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01001853 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1854 __ Blr(lr);
1855}
1856
Alexandre Rames67555f72014-11-18 10:55:16 +00001857void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001858 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001859 UseScratchRegisterScope temps(GetVIXLAssembler());
1860 Register temp = temps.AcquireW();
Vladimir Markodc682aa2018-01-04 18:42:57 +00001861 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001862 const size_t status_byte_offset =
1863 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1864 constexpr uint32_t shifted_visibly_initialized_value =
1865 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001866
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001867 // CMP (immediate) is limited to imm12 or imm12<<12, so we would need to materialize
1868 // the constant 0xf0000000 for comparison with the full 32-bit field. To reduce the code
1869 // size, load only the high byte of the field and compare with 0xf0.
1870 // Note: The same code size could be achieved with LDR+MNV(asr #24)+CBNZ but benchmarks
1871 // show that this pattern is slower (tested on little cores).
1872 __ Ldrb(temp, HeapOperand(class_reg, status_byte_offset));
1873 __ Cmp(temp, shifted_visibly_initialized_value);
1874 __ B(lo, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001875 __ Bind(slow_path->GetExitLabel());
1876}
Alexandre Rames5319def2014-10-23 10:03:10 +01001877
Vladimir Marko175e7862018-03-27 09:03:13 +00001878void InstructionCodeGeneratorARM64::GenerateBitstringTypeCheckCompare(
1879 HTypeCheckInstruction* check, vixl::aarch64::Register temp) {
1880 uint32_t path_to_root = check->GetBitstringPathToRoot();
1881 uint32_t mask = check->GetBitstringMask();
1882 DCHECK(IsPowerOfTwo(mask + 1));
1883 size_t mask_bits = WhichPowerOf2(mask + 1);
1884
1885 if (mask_bits == 16u) {
1886 // Load only the bitstring part of the status word.
1887 __ Ldrh(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1888 } else {
1889 // /* uint32_t */ temp = temp->status_
1890 __ Ldr(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1891 // Extract the bitstring bits.
1892 __ Ubfx(temp, temp, 0, mask_bits);
1893 }
1894 // Compare the bitstring bits to `path_to_root`.
1895 __ Cmp(temp, path_to_root);
1896}
1897
Roland Levillain44015862016-01-22 11:47:17 +00001898void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001899 BarrierType type = BarrierAll;
1900
1901 switch (kind) {
1902 case MemBarrierKind::kAnyAny:
1903 case MemBarrierKind::kAnyStore: {
1904 type = BarrierAll;
1905 break;
1906 }
1907 case MemBarrierKind::kLoadAny: {
1908 type = BarrierReads;
1909 break;
1910 }
1911 case MemBarrierKind::kStoreStore: {
1912 type = BarrierWrites;
1913 break;
1914 }
1915 default:
1916 LOG(FATAL) << "Unexpected memory barrier " << kind;
1917 }
1918 __ Dmb(InnerShareable, type);
1919}
1920
Serban Constantinescu02164b32014-11-13 14:05:07 +00001921void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1922 HBasicBlock* successor) {
1923 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001924 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1925 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001926 slow_path =
1927 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001928 instruction->SetSlowPath(slow_path);
1929 codegen_->AddSlowPath(slow_path);
1930 if (successor != nullptr) {
1931 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001932 }
1933 } else {
1934 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1935 }
1936
Serban Constantinescu02164b32014-11-13 14:05:07 +00001937 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1938 Register temp = temps.AcquireW();
1939
Andreas Gampe542451c2016-07-26 09:02:02 -07001940 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001941 if (successor == nullptr) {
1942 __ Cbnz(temp, slow_path->GetEntryLabel());
1943 __ Bind(slow_path->GetReturnLabel());
1944 } else {
1945 __ Cbz(temp, codegen_->GetLabelOf(successor));
1946 __ B(slow_path->GetEntryLabel());
1947 // slow_path will return to GetLabelOf(successor).
1948 }
1949}
1950
Alexandre Rames5319def2014-10-23 10:03:10 +01001951InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1952 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001953 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001954 assembler_(codegen->GetAssembler()),
1955 codegen_(codegen) {}
1956
Alexandre Rames67555f72014-11-18 10:55:16 +00001957void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001958 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001959 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001960 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001961 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001962 case DataType::Type::kInt32:
1963 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01001964 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001965 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001966 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001967 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001968
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001969 case DataType::Type::kFloat32:
1970 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001971 locations->SetInAt(0, Location::RequiresFpuRegister());
1972 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001973 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001974 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001975
Alexandre Rames5319def2014-10-23 10:03:10 +01001976 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001977 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001978 }
1979}
1980
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001981void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
1982 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001983 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1984
1985 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001986 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01001987 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001988 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
1989 object_field_get_with_read_barrier
1990 ? LocationSummary::kCallOnSlowPath
1991 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001992 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001993 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko0ecac682018-08-07 10:40:38 +01001994 // We need a temporary register for the read barrier load in
1995 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
1996 // only if the field is volatile or the offset is too big.
1997 if (field_info.IsVolatile() ||
1998 field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
1999 locations->AddTemp(FixedTempLocation());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002000 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002001 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002002 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002003 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002004 locations->SetOut(Location::RequiresFpuRegister());
2005 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002006 // The output overlaps for an object field get when read barriers
2007 // are enabled: we do not want the load to overwrite the object's
2008 // location, as we need it to emit the read barrier.
2009 locations->SetOut(
2010 Location::RequiresRegister(),
2011 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002012 }
2013}
2014
2015void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2016 const FieldInfo& field_info) {
2017 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002018 LocationSummary* locations = instruction->GetLocations();
2019 Location base_loc = locations->InAt(0);
2020 Location out = locations->Out();
2021 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01002022 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
2023 DataType::Type load_type = instruction->GetType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002024 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002025
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002026 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01002027 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002028 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002029 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002030 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002031 Location maybe_temp =
2032 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002033 // Note that potential implicit null checks are handled in this
2034 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2035 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2036 instruction,
2037 out,
2038 base,
2039 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002040 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002041 /* needs_null_check= */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002042 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002043 } else {
2044 // General case.
2045 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002046 // Note that a potential implicit null check is handled in this
2047 // CodeGeneratorARM64::LoadAcquire call.
2048 // NB: LoadAcquire will record the pc info if needed.
2049 codegen_->LoadAcquire(
Andreas Gampe3db70682018-12-26 15:12:03 -08002050 instruction, OutputCPURegister(instruction), field, /* needs_null_check= */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002051 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002052 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2053 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01002054 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002055 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002056 }
Vladimir Marko61b92282017-10-11 13:23:17 +01002057 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002058 // If read barriers are enabled, emit read barriers other than
2059 // Baker's using a slow path (and also unpoison the loaded
2060 // reference, if heap poisoning is enabled).
2061 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2062 }
Roland Levillain4d027112015-07-01 15:41:14 +01002063 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002064}
2065
2066void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2067 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002068 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01002069 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002070 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2071 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002072 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002073 locations->SetInAt(1, Location::RequiresFpuRegister());
2074 } else {
2075 locations->SetInAt(1, Location::RequiresRegister());
2076 }
2077}
2078
2079void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002080 const FieldInfo& field_info,
2081 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002082 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2083
2084 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002085 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002086 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002087 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002088 DataType::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002089
Roland Levillain4d027112015-07-01 15:41:14 +01002090 {
2091 // We use a block to end the scratch scope before the write barrier, thus
2092 // freeing the temporary registers so they can be used in `MarkGCCard`.
2093 UseScratchRegisterScope temps(GetVIXLAssembler());
2094
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002095 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002096 DCHECK(value.IsW());
2097 Register temp = temps.AcquireW();
2098 __ Mov(temp, value.W());
2099 GetAssembler()->PoisonHeapReference(temp.W());
2100 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002101 }
Roland Levillain4d027112015-07-01 15:41:14 +01002102
2103 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002104 codegen_->StoreRelease(
Andreas Gampe3db70682018-12-26 15:12:03 -08002105 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check= */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002106 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002107 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2108 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002109 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2110 codegen_->MaybeRecordImplicitNullCheck(instruction);
2111 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002112 }
2113
2114 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002115 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002116 }
2117}
2118
Alexandre Rames67555f72014-11-18 10:55:16 +00002119void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002120 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002121
2122 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002123 case DataType::Type::kInt32:
2124 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002125 Register dst = OutputRegister(instr);
2126 Register lhs = InputRegisterAt(instr, 0);
2127 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002128 if (instr->IsAdd()) {
2129 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002130 } else if (instr->IsAnd()) {
2131 __ And(dst, lhs, rhs);
2132 } else if (instr->IsOr()) {
2133 __ Orr(dst, lhs, rhs);
2134 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002135 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002136 } else if (instr->IsRor()) {
2137 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002138 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002139 __ Ror(dst, lhs, shift);
2140 } else {
2141 // Ensure shift distance is in the same size register as the result. If
2142 // we are rotating a long and the shift comes in a w register originally,
2143 // we don't need to sxtw for use as an x since the shift distances are
2144 // all & reg_bits - 1.
2145 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2146 }
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002147 } else if (instr->IsMin() || instr->IsMax()) {
2148 __ Cmp(lhs, rhs);
2149 __ Csel(dst, lhs, rhs, instr->IsMin() ? lt : gt);
Alexandre Rames67555f72014-11-18 10:55:16 +00002150 } else {
2151 DCHECK(instr->IsXor());
2152 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002153 }
2154 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002155 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002156 case DataType::Type::kFloat32:
2157 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002158 VRegister dst = OutputFPRegister(instr);
2159 VRegister lhs = InputFPRegisterAt(instr, 0);
2160 VRegister rhs = InputFPRegisterAt(instr, 1);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002161 if (instr->IsAdd()) {
2162 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002163 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002164 __ Fsub(dst, lhs, rhs);
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002165 } else if (instr->IsMin()) {
2166 __ Fmin(dst, lhs, rhs);
2167 } else if (instr->IsMax()) {
2168 __ Fmax(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002169 } else {
2170 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002171 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002172 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002173 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002174 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002175 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002176 }
2177}
2178
Serban Constantinescu02164b32014-11-13 14:05:07 +00002179void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2180 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2181
Vladimir Markoca6fff82017-10-03 14:49:14 +01002182 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002183 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002184 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002185 case DataType::Type::kInt32:
2186 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002187 locations->SetInAt(0, Location::RequiresRegister());
2188 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002189 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002190 break;
2191 }
2192 default:
2193 LOG(FATAL) << "Unexpected shift type " << type;
2194 }
2195}
2196
2197void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2198 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2199
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002200 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002201 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002202 case DataType::Type::kInt32:
2203 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002204 Register dst = OutputRegister(instr);
2205 Register lhs = InputRegisterAt(instr, 0);
2206 Operand rhs = InputOperandAt(instr, 1);
2207 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002208 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002209 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002210 if (instr->IsShl()) {
2211 __ Lsl(dst, lhs, shift_value);
2212 } else if (instr->IsShr()) {
2213 __ Asr(dst, lhs, shift_value);
2214 } else {
2215 __ Lsr(dst, lhs, shift_value);
2216 }
2217 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002218 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002219
2220 if (instr->IsShl()) {
2221 __ Lsl(dst, lhs, rhs_reg);
2222 } else if (instr->IsShr()) {
2223 __ Asr(dst, lhs, rhs_reg);
2224 } else {
2225 __ Lsr(dst, lhs, rhs_reg);
2226 }
2227 }
2228 break;
2229 }
2230 default:
2231 LOG(FATAL) << "Unexpected shift operation type " << type;
2232 }
2233}
2234
Alexandre Rames5319def2014-10-23 10:03:10 +01002235void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002236 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002237}
2238
2239void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002240 HandleBinaryOp(instruction);
2241}
2242
2243void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2244 HandleBinaryOp(instruction);
2245}
2246
2247void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2248 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002249}
2250
Artem Serov7fc63502016-02-09 17:15:29 +00002251void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002252 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002253 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002254 locations->SetInAt(0, Location::RequiresRegister());
2255 // There is no immediate variant of negated bitwise instructions in AArch64.
2256 locations->SetInAt(1, Location::RequiresRegister());
2257 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2258}
2259
Artem Serov7fc63502016-02-09 17:15:29 +00002260void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002261 Register dst = OutputRegister(instr);
2262 Register lhs = InputRegisterAt(instr, 0);
2263 Register rhs = InputRegisterAt(instr, 1);
2264
2265 switch (instr->GetOpKind()) {
2266 case HInstruction::kAnd:
2267 __ Bic(dst, lhs, rhs);
2268 break;
2269 case HInstruction::kOr:
2270 __ Orn(dst, lhs, rhs);
2271 break;
2272 case HInstruction::kXor:
2273 __ Eon(dst, lhs, rhs);
2274 break;
2275 default:
2276 LOG(FATAL) << "Unreachable";
2277 }
2278}
2279
Anton Kirilov74234da2017-01-13 14:42:47 +00002280void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2281 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002282 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2283 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002284 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002285 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002286 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2287 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2288 } else {
2289 locations->SetInAt(0, Location::RequiresRegister());
2290 }
2291 locations->SetInAt(1, Location::RequiresRegister());
2292 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2293}
2294
Anton Kirilov74234da2017-01-13 14:42:47 +00002295void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2296 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002297 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002298 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002299 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002300 Register out = OutputRegister(instruction);
2301 Register left;
2302 if (kind != HInstruction::kNeg) {
2303 left = InputRegisterAt(instruction, 0);
2304 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002305 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002306 // shifter operand operation, the IR generating `right_reg` (input to the type
2307 // conversion) can have a different type from the current instruction's type,
2308 // so we manually indicate the type.
2309 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002310 Operand right_operand(0);
2311
Anton Kirilov74234da2017-01-13 14:42:47 +00002312 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2313 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002314 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2315 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002316 right_operand = Operand(right_reg,
2317 helpers::ShiftFromOpKind(op_kind),
2318 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002319 }
2320
2321 // Logical binary operations do not support extension operations in the
2322 // operand. Note that VIXL would still manage if it was passed by generating
2323 // the extension as a separate instruction.
2324 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2325 DCHECK(!right_operand.IsExtendedRegister() ||
2326 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2327 kind != HInstruction::kNeg));
2328 switch (kind) {
2329 case HInstruction::kAdd:
2330 __ Add(out, left, right_operand);
2331 break;
2332 case HInstruction::kAnd:
2333 __ And(out, left, right_operand);
2334 break;
2335 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002336 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002337 __ Neg(out, right_operand);
2338 break;
2339 case HInstruction::kOr:
2340 __ Orr(out, left, right_operand);
2341 break;
2342 case HInstruction::kSub:
2343 __ Sub(out, left, right_operand);
2344 break;
2345 case HInstruction::kXor:
2346 __ Eor(out, left, right_operand);
2347 break;
2348 default:
2349 LOG(FATAL) << "Unexpected operation kind: " << kind;
2350 UNREACHABLE();
2351 }
2352}
2353
Artem Serov328429f2016-07-06 16:23:04 +01002354void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002355 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002356 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002357 locations->SetInAt(0, Location::RequiresRegister());
2358 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002359 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002360}
2361
Roland Levillain19c54192016-11-04 13:44:09 +00002362void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002363 __ Add(OutputRegister(instruction),
2364 InputRegisterAt(instruction, 0),
2365 Operand(InputOperandAt(instruction, 1)));
2366}
2367
Artem Serove1811ed2017-04-27 16:50:47 +01002368void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2369 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002370 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002371
2372 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2373
2374 locations->SetInAt(0, Location::RequiresRegister());
2375 // For byte case we don't need to shift the index variable so we can encode the data offset into
2376 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2377 // data offset constant generation out of the loop and reduce the critical path length in the
2378 // loop.
2379 locations->SetInAt(1, shift->GetValue() == 0
2380 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2381 : Location::RequiresRegister());
2382 locations->SetInAt(2, Location::ConstantLocation(shift));
2383 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2384}
2385
2386void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2387 HIntermediateAddressIndex* instruction) {
2388 Register index_reg = InputRegisterAt(instruction, 0);
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002389 uint32_t shift = Int64FromLocation(instruction->GetLocations()->InAt(2));
Artem Serove1811ed2017-04-27 16:50:47 +01002390 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2391
2392 if (shift == 0) {
2393 __ Add(OutputRegister(instruction), index_reg, offset);
2394 } else {
2395 Register offset_reg = InputRegisterAt(instruction, 1);
2396 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2397 }
2398}
2399
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002400void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002401 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002402 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002403 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2404 if (instr->GetOpKind() == HInstruction::kSub &&
2405 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002406 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002407 // Don't allocate register for Mneg instruction.
2408 } else {
2409 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2410 Location::RequiresRegister());
2411 }
2412 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2413 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002414 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2415}
2416
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002417void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002418 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002419 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2420 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002421
2422 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2423 // This fixup should be carried out for all multiply-accumulate instructions:
2424 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002425 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002426 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2427 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002428 vixl::aarch64::Instruction* prev =
2429 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002430 if (prev->IsLoadOrStore()) {
2431 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002432 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002433 __ nop();
2434 }
2435 }
2436
2437 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002438 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002439 __ Madd(res, mul_left, mul_right, accumulator);
2440 } else {
2441 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002442 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002443 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002444 __ Mneg(res, mul_left, mul_right);
2445 } else {
2446 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2447 __ Msub(res, mul_left, mul_right, accumulator);
2448 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002449 }
2450}
2451
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002452void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002453 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002454 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002455 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002456 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2457 object_array_get_with_read_barrier
2458 ? LocationSummary::kCallOnSlowPath
2459 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002460 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002461 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002462 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002463 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002464 // We need a temporary register for the read barrier load in
2465 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2466 // only if the offset is too big.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002467 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2468 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002469 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002470 if (offset >= kReferenceLoadMinFarOffset) {
2471 locations->AddTemp(FixedTempLocation());
2472 }
Artem Serov0806f582018-10-11 20:14:20 +01002473 } else if (!instruction->GetArray()->IsIntermediateAddress()) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01002474 // We need a non-scratch temporary for the array data pointer in
Artem Serov0806f582018-10-11 20:14:20 +01002475 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier() for the case with no
2476 // intermediate address.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002477 locations->AddTemp(Location::RequiresRegister());
2478 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002479 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002480 locations->SetInAt(0, Location::RequiresRegister());
2481 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002482 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002483 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2484 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002485 // The output overlaps in the case of an object array get with
2486 // read barriers enabled: we do not want the move to overwrite the
2487 // array's location, as we need it to emit the read barrier.
2488 locations->SetOut(
2489 Location::RequiresRegister(),
2490 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002491 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002492}
2493
2494void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002495 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002496 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002497 LocationSummary* locations = instruction->GetLocations();
2498 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002499 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002500 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002501 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2502 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002503 MacroAssembler* masm = GetVIXLAssembler();
2504 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002505
Artem Serov0806f582018-10-11 20:14:20 +01002506 // The non-Baker read barrier instrumentation of object ArrayGet instructions
Roland Levillain19c54192016-11-04 13:44:09 +00002507 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002508 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002509 instruction->GetArray()->IsIntermediateAddress() &&
Artem Serov0806f582018-10-11 20:14:20 +01002510 kEmitCompilerReadBarrier &&
2511 !kUseBakerReadBarrier));
Roland Levillain19c54192016-11-04 13:44:09 +00002512
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002513 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002514 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002515 // Note that a potential implicit null check is handled in the
2516 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002517 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002518 if (index.IsConstant()) {
Artem Serov0806f582018-10-11 20:14:20 +01002519 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002520 // Array load with a constant index can be treated as a field load.
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002521 offset += Int64FromLocation(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002522 Location maybe_temp =
2523 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2524 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2525 out,
2526 obj.W(),
2527 offset,
2528 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002529 /* needs_null_check= */ false,
2530 /* use_load_acquire= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002531 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002532 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08002533 instruction, out, obj.W(), offset, index, /* needs_null_check= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002534 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002535 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002536 // General case.
2537 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002538 Register length;
2539 if (maybe_compressed_char_at) {
2540 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2541 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002542 {
2543 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2544 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2545
2546 if (instruction->GetArray()->IsIntermediateAddress()) {
2547 DCHECK_LT(count_offset, offset);
2548 int64_t adjusted_offset =
2549 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2550 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2551 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2552 } else {
2553 __ Ldr(length, HeapOperand(obj, count_offset));
2554 }
2555 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002556 }
jessicahandojo05765752016-09-09 19:01:32 -07002557 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002558 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002559 if (maybe_compressed_char_at) {
2560 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002561 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2562 "Expecting 0=compressed, 1=uncompressed");
2563 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002564 __ Ldrb(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002565 HeapOperand(obj, offset + Int64FromLocation(index)));
jessicahandojo05765752016-09-09 19:01:32 -07002566 __ B(&done);
2567 __ Bind(&uncompressed_load);
2568 __ Ldrh(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002569 HeapOperand(obj, offset + (Int64FromLocation(index) << 1)));
jessicahandojo05765752016-09-09 19:01:32 -07002570 __ Bind(&done);
2571 } else {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002572 offset += Int64FromLocation(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002573 source = HeapOperand(obj, offset);
2574 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002575 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002576 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002577 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002578 // We do not need to compute the intermediate address from the array: the
2579 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002580 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002581 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002582 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2583 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
Roland Levillain44015862016-01-22 11:47:17 +00002584 }
2585 temp = obj;
2586 } else {
2587 __ Add(temp, obj, offset);
2588 }
jessicahandojo05765752016-09-09 19:01:32 -07002589 if (maybe_compressed_char_at) {
2590 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002591 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2592 "Expecting 0=compressed, 1=uncompressed");
2593 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002594 __ Ldrb(Register(OutputCPURegister(instruction)),
2595 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2596 __ B(&done);
2597 __ Bind(&uncompressed_load);
2598 __ Ldrh(Register(OutputCPURegister(instruction)),
2599 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2600 __ Bind(&done);
2601 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002602 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002603 }
Roland Levillain44015862016-01-22 11:47:17 +00002604 }
jessicahandojo05765752016-09-09 19:01:32 -07002605 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002606 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2607 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002608 codegen_->Load(type, OutputCPURegister(instruction), source);
2609 codegen_->MaybeRecordImplicitNullCheck(instruction);
2610 }
Roland Levillain44015862016-01-22 11:47:17 +00002611
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002612 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002613 static_assert(
2614 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2615 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2616 Location obj_loc = locations->InAt(0);
2617 if (index.IsConstant()) {
2618 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2619 } else {
2620 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2621 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002622 }
Roland Levillain4d027112015-07-01 15:41:14 +01002623 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002624}
2625
Alexandre Rames5319def2014-10-23 10:03:10 +01002626void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002627 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002628 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002629 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002630}
2631
2632void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002633 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002634 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002635 {
2636 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2637 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2638 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2639 codegen_->MaybeRecordImplicitNullCheck(instruction);
2640 }
jessicahandojo05765752016-09-09 19:01:32 -07002641 // Mask out compression flag from String's array length.
2642 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002643 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002644 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002645}
2646
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002647void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002648 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002649
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002650 bool needs_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002651 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002652 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002653 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002654 locations->SetInAt(0, Location::RequiresRegister());
2655 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002656 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2657 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002658 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002659 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002660 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002661 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002662 }
2663}
2664
2665void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002666 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002667 LocationSummary* locations = instruction->GetLocations();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002668 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002669 bool needs_write_barrier =
2670 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002671
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002672 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002673 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002674 CPURegister source = value;
2675 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002676 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002677 MemOperand destination = HeapOperand(array);
2678 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002679
2680 if (!needs_write_barrier) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002681 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002682 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002683 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002684 destination = HeapOperand(array, offset);
2685 } else {
2686 UseScratchRegisterScope temps(masm);
2687 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002688 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002689 // We do not need to compute the intermediate address from the array: the
2690 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002691 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002692 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002693 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2694 DCHECK(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002695 }
2696 temp = array;
2697 } else {
2698 __ Add(temp, array, offset);
2699 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002700 destination = HeapOperand(temp,
2701 XRegisterFrom(index),
2702 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002703 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002704 }
Artem Serov914d7a82017-02-07 14:33:49 +00002705 {
2706 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2707 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2708 codegen_->Store(value_type, value, destination);
2709 codegen_->MaybeRecordImplicitNullCheck(instruction);
2710 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002711 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002712 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002713
2714 bool can_value_be_null = instruction->GetValueCanBeNull();
2715 vixl::aarch64::Label do_store;
2716 if (can_value_be_null) {
2717 __ Cbz(Register(value), &do_store);
2718 }
2719
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002720 SlowPathCodeARM64* slow_path = nullptr;
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002721 if (needs_type_check) {
2722 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
2723 codegen_->AddSlowPath(slow_path);
2724
2725 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2726 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2727 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2728
Alexandre Rames97833a02015-04-16 15:07:12 +01002729 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002730 Register temp = temps.AcquireSameSizeAs(array);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002731 Register temp2 = temps.AcquireSameSizeAs(array);
2732
2733 // Note that when Baker read barriers are enabled, the type
2734 // checks are performed without read barriers. This is fine,
2735 // even in the case where a class object is in the from-space
2736 // after the flip, as a comparison involving such a type would
2737 // not produce a false positive; it may of course produce a
2738 // false negative, in which case we would take the ArraySet
2739 // slow path.
2740
2741 // /* HeapReference<Class> */ temp = array->klass_
2742 {
2743 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2744 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2745 __ Ldr(temp, HeapOperand(array, class_offset));
2746 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames97833a02015-04-16 15:07:12 +01002747 }
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002748 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Alexandre Rames97833a02015-04-16 15:07:12 +01002749
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002750 // /* HeapReference<Class> */ temp = temp->component_type_
2751 __ Ldr(temp, HeapOperand(temp, component_offset));
2752 // /* HeapReference<Class> */ temp2 = value->klass_
2753 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2754 // If heap poisoning is enabled, no need to unpoison `temp`
2755 // nor `temp2`, as we are comparing two poisoned references.
2756 __ Cmp(temp, temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002757
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002758 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2759 vixl::aarch64::Label do_put;
2760 __ B(eq, &do_put);
2761 // If heap poisoning is enabled, the `temp` reference has
2762 // not been unpoisoned yet; unpoison it now.
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002763 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002764
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002765 // /* HeapReference<Class> */ temp = temp->super_class_
2766 __ Ldr(temp, HeapOperand(temp, super_offset));
2767 // If heap poisoning is enabled, no need to unpoison
2768 // `temp`, as we are comparing against null below.
2769 __ Cbnz(temp, slow_path->GetEntryLabel());
2770 __ Bind(&do_put);
Vladimir Markod1ef8732017-04-18 13:55:13 +01002771 } else {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002772 __ B(ne, slow_path->GetEntryLabel());
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002773 }
2774 }
2775
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002776 codegen_->MarkGCCard(array, value.W(), /* value_can_be_null= */ false);
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002777
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002778 if (can_value_be_null) {
2779 DCHECK(do_store.IsLinked());
2780 __ Bind(&do_store);
2781 }
2782
2783 UseScratchRegisterScope temps(masm);
2784 if (kPoisonHeapReferences) {
2785 Register temp_source = temps.AcquireSameSizeAs(array);
2786 DCHECK(value.IsW());
2787 __ Mov(temp_source, value.W());
2788 GetAssembler()->PoisonHeapReference(temp_source);
2789 source = temp_source;
2790 }
2791
2792 if (index.IsConstant()) {
2793 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
2794 destination = HeapOperand(array, offset);
2795 } else {
2796 Register temp_base = temps.AcquireSameSizeAs(array);
2797 __ Add(temp_base, array, offset);
2798 destination = HeapOperand(temp_base,
2799 XRegisterFrom(index),
2800 LSL,
2801 DataType::SizeShift(value_type));
2802 }
2803
2804 {
2805 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2806 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2807 __ Str(source, destination);
2808
2809 if (can_value_be_null || !needs_type_check) {
2810 codegen_->MaybeRecordImplicitNullCheck(instruction);
2811 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002812 }
2813
2814 if (slow_path != nullptr) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002815 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002816 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002817 }
2818}
2819
Alexandre Rames67555f72014-11-18 10:55:16 +00002820void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002821 RegisterSet caller_saves = RegisterSet::Empty();
2822 InvokeRuntimeCallingConvention calling_convention;
2823 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2824 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2825 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002826
2827 // If both index and length are constant, we can check the bounds statically and
2828 // generate code accordingly. We want to make sure we generate constant locations
2829 // in that case, regardless of whether they are encodable in the comparison or not.
2830 HInstruction* index = instruction->InputAt(0);
2831 HInstruction* length = instruction->InputAt(1);
2832 bool both_const = index->IsConstant() && length->IsConstant();
2833 locations->SetInAt(0, both_const
2834 ? Location::ConstantLocation(index->AsConstant())
2835 : ARM64EncodableConstantOrRegister(index, instruction));
2836 locations->SetInAt(1, both_const
2837 ? Location::ConstantLocation(length->AsConstant())
2838 : ARM64EncodableConstantOrRegister(length, instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002839}
2840
2841void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Georgia Kouvelibe530852019-01-17 10:46:41 +00002842 LocationSummary* locations = instruction->GetLocations();
2843 Location index_loc = locations->InAt(0);
2844 Location length_loc = locations->InAt(1);
2845
2846 int cmp_first_input = 0;
2847 int cmp_second_input = 1;
2848 Condition cond = hs;
2849
2850 if (index_loc.IsConstant()) {
2851 int64_t index = Int64FromLocation(index_loc);
2852 if (length_loc.IsConstant()) {
2853 int64_t length = Int64FromLocation(length_loc);
2854 if (index < 0 || index >= length) {
2855 BoundsCheckSlowPathARM64* slow_path =
2856 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
2857 codegen_->AddSlowPath(slow_path);
2858 __ B(slow_path->GetEntryLabel());
2859 } else {
2860 // BCE will remove the bounds check if we are guaranteed to pass.
2861 // However, some optimization after BCE may have generated this, and we should not
2862 // generate a bounds check if it is a valid range.
2863 }
2864 return;
2865 }
2866 // Only the index is constant: change the order of the operands and commute the condition
2867 // so we can use an immediate constant for the index (only the second input to a cmp
2868 // instruction can be an immediate).
2869 cmp_first_input = 1;
2870 cmp_second_input = 0;
2871 cond = ls;
2872 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002873 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002874 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002875 __ Cmp(InputRegisterAt(instruction, cmp_first_input),
2876 InputOperandAt(instruction, cmp_second_input));
Alexandre Rames67555f72014-11-18 10:55:16 +00002877 codegen_->AddSlowPath(slow_path);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002878 __ B(slow_path->GetEntryLabel(), cond);
Alexandre Rames67555f72014-11-18 10:55:16 +00002879}
2880
Alexandre Rames67555f72014-11-18 10:55:16 +00002881void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2882 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002883 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00002884 locations->SetInAt(0, Location::RequiresRegister());
2885 if (check->HasUses()) {
2886 locations->SetOut(Location::SameAsFirstInput());
2887 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01002888 // Rely on the type initialization to save everything we need.
2889 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Alexandre Rames67555f72014-11-18 10:55:16 +00002890}
2891
2892void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2893 // We assume the class is not null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01002894 SlowPathCodeARM64* slow_path =
2895 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(check->GetLoadClass(), check);
Alexandre Rames67555f72014-11-18 10:55:16 +00002896 codegen_->AddSlowPath(slow_path);
2897 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2898}
2899
Roland Levillain1a653882016-03-18 18:05:57 +00002900static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2901 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2902 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2903}
2904
2905void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002906 VRegister lhs_reg = InputFPRegisterAt(instruction, 0);
Roland Levillain1a653882016-03-18 18:05:57 +00002907 Location rhs_loc = instruction->GetLocations()->InAt(1);
2908 if (rhs_loc.IsConstant()) {
2909 // 0.0 is the only immediate that can be encoded directly in
2910 // an FCMP instruction.
2911 //
2912 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2913 // specify that in a floating-point comparison, positive zero
2914 // and negative zero are considered equal, so we can use the
2915 // literal 0.0 for both cases here.
2916 //
2917 // Note however that some methods (Float.equal, Float.compare,
2918 // Float.compareTo, Double.equal, Double.compare,
2919 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2920 // StrictMath.min) consider 0.0 to be (strictly) greater than
2921 // -0.0. So if we ever translate calls to these methods into a
2922 // HCompare instruction, we must handle the -0.0 case with
2923 // care here.
2924 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2925 __ Fcmp(lhs_reg, 0.0);
2926 } else {
2927 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2928 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002929}
2930
Serban Constantinescu02164b32014-11-13 14:05:07 +00002931void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002932 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002933 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002934 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002935 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002936 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002937 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002938 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002939 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002940 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002941 case DataType::Type::kInt32:
2942 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002943 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002944 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002945 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2946 break;
2947 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002948 case DataType::Type::kFloat32:
2949 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002950 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002951 locations->SetInAt(1,
2952 IsFloatingPointZeroConstant(compare->InputAt(1))
2953 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2954 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002955 locations->SetOut(Location::RequiresRegister());
2956 break;
2957 }
2958 default:
2959 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2960 }
2961}
2962
2963void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002964 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002965
2966 // 0 if: left == right
2967 // 1 if: left > right
2968 // -1 if: left < right
2969 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002970 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002971 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002972 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002973 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002974 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002975 case DataType::Type::kInt32:
2976 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002977 Register result = OutputRegister(compare);
2978 Register left = InputRegisterAt(compare, 0);
2979 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002980 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002981 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2982 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002983 break;
2984 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002985 case DataType::Type::kFloat32:
2986 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002987 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002988 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002989 __ Cset(result, ne);
2990 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002991 break;
2992 }
2993 default:
2994 LOG(FATAL) << "Unimplemented compare type " << in_type;
2995 }
2996}
2997
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002998void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002999 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003000
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003001 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003002 locations->SetInAt(0, Location::RequiresFpuRegister());
3003 locations->SetInAt(1,
3004 IsFloatingPointZeroConstant(instruction->InputAt(1))
3005 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3006 : Location::RequiresFpuRegister());
3007 } else {
3008 // Integer cases.
3009 locations->SetInAt(0, Location::RequiresRegister());
3010 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3011 }
3012
David Brazdilb3e773e2016-01-26 11:28:37 +00003013 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003014 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003015 }
3016}
3017
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003018void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003019 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003020 return;
3021 }
3022
3023 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003024 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003025 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003026
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003027 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003028 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003029 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003030 } else {
3031 // Integer cases.
3032 Register lhs = InputRegisterAt(instruction, 0);
3033 Operand rhs = InputOperandAt(instruction, 1);
3034 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003035 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003036 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003037}
3038
3039#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3040 M(Equal) \
3041 M(NotEqual) \
3042 M(LessThan) \
3043 M(LessThanOrEqual) \
3044 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003045 M(GreaterThanOrEqual) \
3046 M(Below) \
3047 M(BelowOrEqual) \
3048 M(Above) \
3049 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003050#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003051void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3052void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003053FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003054#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003055#undef FOR_EACH_CONDITION_INSTRUCTION
3056
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003057void InstructionCodeGeneratorARM64::GenerateIntDivForPower2Denom(HDiv* instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003058 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003059 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003060 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
3061
3062 Register out = OutputRegister(instruction);
3063 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003064
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003065 Register final_dividend;
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003066 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003067 // No need to adjust the result for non-negative dividends or the INT32_MIN/INT64_MIN dividends.
3068 // NOTE: The generated code for HDiv correctly works for the INT32_MIN/INT64_MIN dividends:
3069 // imm == 2
3070 // add out, dividend(0x80000000), dividend(0x80000000), lsr #31 => out = 0x80000001
3071 // asr out, out(0x80000001), #1 => out = 0xc0000000
3072 // This is the same as 'asr out, 0x80000000, #1'
3073 //
3074 // imm > 2
3075 // add temp, dividend(0x80000000), imm - 1 => temp = 0b10..01..1, where the number
3076 // of the rightmost 1s is ctz_imm.
3077 // cmp dividend(0x80000000), 0 => N = 1, V = 0 (lt is true)
3078 // csel out, temp(0b10..01..1), dividend(0x80000000), lt => out = 0b10..01..1
3079 // asr out, out(0b10..01..1), #ctz_imm => out = 0b1..10..0, where the number of the
3080 // leftmost 1s is ctz_imm + 1.
3081 // This is the same as 'asr out, dividend(0x80000000), #ctz_imm'.
3082 //
3083 // imm == INT32_MIN
3084 // add tmp, dividend(0x80000000), #0x7fffffff => tmp = -1
3085 // cmp dividend(0x80000000), 0 => N = 1, V = 0 (lt is true)
3086 // csel out, temp(-1), dividend(0x80000000), lt => out = -1
3087 // neg out, out(-1), asr #31 => out = 1
3088 // This is the same as 'neg out, dividend(0x80000000), asr #31'.
3089 final_dividend = dividend;
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003090 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003091 if (abs_imm == 2) {
3092 int bits = DataType::Size(instruction->GetResultType()) * kBitsPerByte;
3093 __ Add(out, dividend, Operand(dividend, LSR, bits - 1));
3094 } else {
3095 UseScratchRegisterScope temps(GetVIXLAssembler());
3096 Register temp = temps.AcquireSameSizeAs(out);
3097 __ Add(temp, dividend, abs_imm - 1);
3098 __ Cmp(dividend, 0);
3099 __ Csel(out, temp, dividend, lt);
3100 }
3101 final_dividend = out;
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003102 }
3103
Zheng Xuc6667102015-05-15 16:08:45 +08003104 int ctz_imm = CTZ(abs_imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003105 if (imm > 0) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003106 __ Asr(out, final_dividend, ctz_imm);
Zheng Xuc6667102015-05-15 16:08:45 +08003107 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003108 __ Neg(out, Operand(final_dividend, ASR, ctz_imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003109 }
3110}
3111
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003112// Return true if the magic number was modified by subtracting 2^32(Int32 div) or 2^64(Int64 div).
3113// So dividend needs to be added.
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003114static inline bool NeedToAddDividend(int64_t magic_number, int64_t divisor) {
3115 return divisor > 0 && magic_number < 0;
3116}
3117
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003118// Return true if the magic number was modified by adding 2^32(Int32 div) or 2^64(Int64 div).
3119// So dividend needs to be subtracted.
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003120static inline bool NeedToSubDividend(int64_t magic_number, int64_t divisor) {
3121 return divisor < 0 && magic_number > 0;
3122}
3123
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003124// Generate code which increments the value in register 'in' by 1 if the value is negative.
3125// It is done with 'add out, in, in, lsr #31 or #63'.
3126// If the value is a result of an operation setting the N flag, CINC MI can be used
3127// instead of ADD. 'use_cond_inc' controls this.
3128void InstructionCodeGeneratorARM64::GenerateIncrementNegativeByOne(
3129 Register out,
3130 Register in,
3131 bool use_cond_inc) {
3132 if (use_cond_inc) {
3133 __ Cinc(out, in, mi);
3134 } else {
3135 __ Add(out, in, Operand(in, LSR, in.GetSizeInBits() - 1));
3136 }
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003137}
3138
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003139// Helper to generate code producing the result of HRem with a constant divisor.
3140void InstructionCodeGeneratorARM64::GenerateResultRemWithAnyConstant(
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003141 Register out,
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003142 Register dividend,
3143 Register quotient,
3144 int64_t divisor,
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003145 UseScratchRegisterScope* temps_scope) {
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003146 Register temp_imm = temps_scope->AcquireSameSizeAs(out);
3147 __ Mov(temp_imm, divisor);
3148 __ Msub(out, quotient, temp_imm, dividend);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003149}
3150
3151void InstructionCodeGeneratorARM64::GenerateInt64DivRemWithAnyConstant(
3152 HBinaryOperation* instruction) {
Zheng Xuc6667102015-05-15 16:08:45 +08003153 DCHECK(instruction->IsDiv() || instruction->IsRem());
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003154 DCHECK(instruction->GetResultType() == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003155
3156 LocationSummary* locations = instruction->GetLocations();
3157 Location second = locations->InAt(1);
3158 DCHECK(second.IsConstant());
3159
3160 Register out = OutputRegister(instruction);
3161 Register dividend = InputRegisterAt(instruction, 0);
3162 int64_t imm = Int64FromConstant(second.GetConstant());
3163
Zheng Xuc6667102015-05-15 16:08:45 +08003164 int64_t magic;
3165 int shift;
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003166 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ true, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08003167
3168 UseScratchRegisterScope temps(GetVIXLAssembler());
3169 Register temp = temps.AcquireSameSizeAs(out);
3170
3171 // temp = get_high(dividend * magic)
3172 __ Mov(temp, magic);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003173 __ Smulh(temp, dividend, temp);
3174
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003175 // The multiplication result might need some corrections to be finalized.
3176 // The last correction is to increment by 1, if the result is negative.
3177 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3178 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3179 // However if one of the corrections is ADD or SUB, the sign can be detected
3180 // with ADDS/SUBS. They set the N flag if the result is negative.
3181 // This allows to use CINC MI which has latency 1.
3182 bool use_cond_inc = false;
3183
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003184 // Some combinations of magic_number and the divisor require to correct the result.
3185 // Check whether the correction is needed.
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003186 if (NeedToAddDividend(magic, imm)) {
3187 __ Adds(temp, temp, dividend);
3188 use_cond_inc = true;
3189 } else if (NeedToSubDividend(magic, imm)) {
3190 __ Subs(temp, temp, dividend);
3191 use_cond_inc = true;
3192 }
3193
3194 if (shift != 0) {
3195 __ Asr(temp, temp, shift);
3196 }
3197
3198 if (instruction->IsRem()) {
3199 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3200 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3201 } else {
3202 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3203 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003204}
3205
3206void InstructionCodeGeneratorARM64::GenerateInt32DivRemWithAnyConstant(
3207 HBinaryOperation* instruction) {
3208 DCHECK(instruction->IsDiv() || instruction->IsRem());
3209 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
3210
3211 LocationSummary* locations = instruction->GetLocations();
3212 Location second = locations->InAt(1);
3213 DCHECK(second.IsConstant());
3214
3215 Register out = OutputRegister(instruction);
3216 Register dividend = InputRegisterAt(instruction, 0);
3217 int64_t imm = Int64FromConstant(second.GetConstant());
3218
3219 int64_t magic;
3220 int shift;
3221 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
3222 UseScratchRegisterScope temps(GetVIXLAssembler());
3223 Register temp = temps.AcquireSameSizeAs(out);
3224
3225 // temp = get_high(dividend * magic)
3226 __ Mov(temp, magic);
3227 __ Smull(temp.X(), dividend, temp);
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003228
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003229 // The multiplication result might need some corrections to be finalized.
3230 // The last correction is to increment by 1, if the result is negative.
3231 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3232 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3233 // However if one of the corrections is ADD or SUB, the sign can be detected
3234 // with ADDS/SUBS. They set the N flag if the result is negative.
3235 // This allows to use CINC MI which has latency 1.
3236 bool use_cond_inc = false;
3237
3238 // ADD/SUB correction is performed in the high 32 bits
3239 // as high 32 bits are ignored because type are kInt32.
3240 if (NeedToAddDividend(magic, imm)) {
3241 __ Adds(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3242 use_cond_inc = true;
3243 } else if (NeedToSubDividend(magic, imm)) {
3244 __ Subs(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3245 use_cond_inc = true;
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003246 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003247
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003248 // Extract the result from the high 32 bits and apply the final right shift.
3249 DCHECK_LT(shift, 32);
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003250 if (imm > 0 && HasNonNegativeInputAt(instruction, 0)) {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01003251 // No need to adjust the result for a non-negative dividend and a positive divisor.
3252 if (instruction->IsDiv()) {
3253 __ Lsr(out.X(), temp.X(), 32 + shift);
3254 } else {
3255 __ Lsr(temp.X(), temp.X(), 32 + shift);
3256 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3257 }
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003258 } else {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01003259 __ Asr(temp.X(), temp.X(), 32 + shift);
3260
3261 if (instruction->IsRem()) {
3262 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3263 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3264 } else {
3265 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3266 }
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003267 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003268}
3269
3270void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3271 DCHECK(instruction->IsDiv() || instruction->IsRem());
3272 if (instruction->GetResultType() == DataType::Type::kInt64) {
3273 GenerateInt64DivRemWithAnyConstant(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003274 } else {
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003275 GenerateInt32DivRemWithAnyConstant(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003276 }
3277}
3278
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003279void InstructionCodeGeneratorARM64::GenerateIntDivForConstDenom(HDiv *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003280 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Zheng Xuc6667102015-05-15 16:08:45 +08003281
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003282 if (imm == 0) {
3283 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3284 return;
3285 }
Zheng Xuc6667102015-05-15 16:08:45 +08003286
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003287 if (IsPowerOfTwo(AbsOrMin(imm))) {
3288 GenerateIntDivForPower2Denom(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003289 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003290 // Cases imm == -1 or imm == 1 are handled by InstructionSimplifier.
3291 DCHECK(imm < -2 || imm > 2) << imm;
3292 GenerateDivRemWithAnyConstant(instruction);
3293 }
3294}
3295
3296void InstructionCodeGeneratorARM64::GenerateIntDiv(HDiv *instruction) {
3297 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
3298 << instruction->GetResultType();
3299
3300 if (instruction->GetLocations()->InAt(1).IsConstant()) {
3301 GenerateIntDivForConstDenom(instruction);
3302 } else {
3303 Register out = OutputRegister(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003304 Register dividend = InputRegisterAt(instruction, 0);
3305 Register divisor = InputRegisterAt(instruction, 1);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003306 __ Sdiv(out, dividend, divisor);
Zheng Xuc6667102015-05-15 16:08:45 +08003307 }
3308}
3309
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003310void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3311 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003312 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003313 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003314 case DataType::Type::kInt32:
3315 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003316 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003317 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003318 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3319 break;
3320
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003321 case DataType::Type::kFloat32:
3322 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003323 locations->SetInAt(0, Location::RequiresFpuRegister());
3324 locations->SetInAt(1, Location::RequiresFpuRegister());
3325 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3326 break;
3327
3328 default:
3329 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3330 }
3331}
3332
3333void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003334 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003335 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003336 case DataType::Type::kInt32:
3337 case DataType::Type::kInt64:
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003338 GenerateIntDiv(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003339 break;
3340
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003341 case DataType::Type::kFloat32:
3342 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003343 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3344 break;
3345
3346 default:
3347 LOG(FATAL) << "Unexpected div type " << type;
3348 }
3349}
3350
Alexandre Rames67555f72014-11-18 10:55:16 +00003351void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003352 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003353 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003354}
3355
3356void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3357 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003358 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003359 codegen_->AddSlowPath(slow_path);
3360 Location value = instruction->GetLocations()->InAt(0);
3361
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003362 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003363
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003364 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003365 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Elliott Hughesc1896c92018-11-29 11:33:18 -08003366 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003367 }
3368
Alexandre Rames67555f72014-11-18 10:55:16 +00003369 if (value.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003370 int64_t divisor = Int64FromLocation(value);
Alexandre Rames67555f72014-11-18 10:55:16 +00003371 if (divisor == 0) {
3372 __ B(slow_path->GetEntryLabel());
3373 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003374 // A division by a non-null constant is valid. We don't need to perform
3375 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003376 }
3377 } else {
3378 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3379 }
3380}
3381
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003382void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3383 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003384 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003385 locations->SetOut(Location::ConstantLocation(constant));
3386}
3387
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003388void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3389 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003390 // Will be generated at use site.
3391}
3392
Alexandre Rames5319def2014-10-23 10:03:10 +01003393void LocationsBuilderARM64::VisitExit(HExit* exit) {
3394 exit->SetLocations(nullptr);
3395}
3396
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003397void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003398}
3399
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003400void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3401 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003402 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003403 locations->SetOut(Location::ConstantLocation(constant));
3404}
3405
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003406void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003407 // Will be generated at use site.
3408}
3409
David Brazdilfc6a86a2015-06-26 10:33:45 +00003410void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003411 if (successor->IsExitBlock()) {
3412 DCHECK(got->GetPrevious()->AlwaysThrows());
3413 return; // no code needed
3414 }
3415
Serban Constantinescu02164b32014-11-13 14:05:07 +00003416 HBasicBlock* block = got->GetBlock();
3417 HInstruction* previous = got->GetPrevious();
3418 HLoopInformation* info = block->GetLoopInformation();
3419
David Brazdil46e2a392015-03-16 17:31:52 +00003420 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00003421 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003422 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3423 return;
3424 }
3425 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3426 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08003427 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003428 }
3429 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003430 __ B(codegen_->GetLabelOf(successor));
3431 }
3432}
3433
David Brazdilfc6a86a2015-06-26 10:33:45 +00003434void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3435 got->SetLocations(nullptr);
3436}
3437
3438void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3439 HandleGoto(got, got->GetSuccessor());
3440}
3441
3442void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3443 try_boundary->SetLocations(nullptr);
3444}
3445
3446void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3447 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3448 if (!successor->IsExitBlock()) {
3449 HandleGoto(try_boundary, successor);
3450 }
3451}
3452
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003453void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003454 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003455 vixl::aarch64::Label* true_target,
3456 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003457 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003458
David Brazdil0debae72015-11-12 18:37:00 +00003459 if (true_target == nullptr && false_target == nullptr) {
3460 // Nothing to do. The code always falls through.
3461 return;
3462 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003463 // Constant condition, statically compared against "true" (integer value 1).
3464 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003465 if (true_target != nullptr) {
3466 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003467 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003468 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003469 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003470 if (false_target != nullptr) {
3471 __ B(false_target);
3472 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003473 }
David Brazdil0debae72015-11-12 18:37:00 +00003474 return;
3475 }
3476
3477 // The following code generates these patterns:
3478 // (1) true_target == nullptr && false_target != nullptr
3479 // - opposite condition true => branch to false_target
3480 // (2) true_target != nullptr && false_target == nullptr
3481 // - condition true => branch to true_target
3482 // (3) true_target != nullptr && false_target != nullptr
3483 // - condition true => branch to true_target
3484 // - branch to false_target
3485 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003486 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003487 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003488 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003489 if (true_target == nullptr) {
3490 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3491 } else {
3492 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3493 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003494 } else {
3495 // The condition instruction has not been materialized, use its inputs as
3496 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003497 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003498
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003499 DataType::Type type = condition->InputAt(0)->GetType();
3500 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003501 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003502 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003503 IfCondition opposite_condition = condition->GetOppositeCondition();
3504 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003505 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003506 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003507 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003508 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003509 // Integer cases.
3510 Register lhs = InputRegisterAt(condition, 0);
3511 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003512
3513 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003514 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003515 if (true_target == nullptr) {
3516 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3517 non_fallthrough_target = false_target;
3518 } else {
3519 arm64_cond = ARM64Condition(condition->GetCondition());
3520 non_fallthrough_target = true_target;
3521 }
3522
Aart Bik086d27e2016-01-20 17:02:00 -08003523 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003524 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003525 switch (arm64_cond) {
3526 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003527 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003528 break;
3529 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003530 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003531 break;
3532 case lt:
3533 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003534 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003535 break;
3536 case ge:
3537 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003538 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003539 break;
3540 default:
3541 // Without the `static_cast` the compiler throws an error for
3542 // `-Werror=sign-promo`.
3543 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3544 }
3545 } else {
3546 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003547 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003548 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003549 }
3550 }
David Brazdil0debae72015-11-12 18:37:00 +00003551
3552 // If neither branch falls through (case 3), the conditional branch to `true_target`
3553 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3554 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003555 __ B(false_target);
3556 }
3557}
3558
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003559void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003560 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003561 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003562 locations->SetInAt(0, Location::RequiresRegister());
3563 }
3564}
3565
3566void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003567 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3568 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003569 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3570 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3571 true_target = nullptr;
3572 }
3573 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3574 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3575 false_target = nullptr;
3576 }
Andreas Gampe3db70682018-12-26 15:12:03 -08003577 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003578}
3579
3580void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003581 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003582 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003583 InvokeRuntimeCallingConvention calling_convention;
3584 RegisterSet caller_saves = RegisterSet::Empty();
3585 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3586 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003587 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003588 locations->SetInAt(0, Location::RequiresRegister());
3589 }
3590}
3591
3592void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003593 SlowPathCodeARM64* slow_path =
3594 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003595 GenerateTestAndBranch(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08003596 /* condition_input_index= */ 0,
David Brazdil0debae72015-11-12 18:37:00 +00003597 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08003598 /* false_target= */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003599}
3600
Mingyao Yang063fc772016-08-02 11:02:54 -07003601void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003602 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003603 LocationSummary(flag, LocationSummary::kNoCall);
3604 locations->SetOut(Location::RequiresRegister());
3605}
3606
3607void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3608 __ Ldr(OutputRegister(flag),
3609 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3610}
3611
David Brazdilc0b601b2016-02-08 14:20:45 +00003612static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3613 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003614 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003615}
3616
Alexandre Rames880f1192016-06-13 16:04:50 +01003617static inline Condition GetConditionForSelect(HCondition* condition) {
3618 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003619 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3620 : ARM64Condition(cond);
3621}
3622
David Brazdil74eb1b22015-12-14 11:44:01 +00003623void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003624 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003625 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003626 locations->SetInAt(0, Location::RequiresFpuRegister());
3627 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003628 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003629 } else {
3630 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3631 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3632 bool is_true_value_constant = cst_true_value != nullptr;
3633 bool is_false_value_constant = cst_false_value != nullptr;
3634 // Ask VIXL whether we should synthesize constants in registers.
3635 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3636 Operand true_op = is_true_value_constant ?
3637 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3638 Operand false_op = is_false_value_constant ?
3639 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3640 bool true_value_in_register = false;
3641 bool false_value_in_register = false;
3642 MacroAssembler::GetCselSynthesisInformation(
3643 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3644 true_value_in_register |= !is_true_value_constant;
3645 false_value_in_register |= !is_false_value_constant;
3646
3647 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3648 : Location::ConstantLocation(cst_true_value));
3649 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3650 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003651 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003652 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003653
David Brazdil74eb1b22015-12-14 11:44:01 +00003654 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3655 locations->SetInAt(2, Location::RequiresRegister());
3656 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003657}
3658
3659void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003660 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003661 Condition csel_cond;
3662
3663 if (IsBooleanValueOrMaterializedCondition(cond)) {
3664 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003665 // Use the condition flags set by the previous instruction.
3666 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003667 } else {
3668 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003669 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003670 }
3671 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003672 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003673 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003674 } else {
3675 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003676 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003677 }
3678
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003679 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003680 __ Fcsel(OutputFPRegister(select),
3681 InputFPRegisterAt(select, 1),
3682 InputFPRegisterAt(select, 0),
3683 csel_cond);
3684 } else {
3685 __ Csel(OutputRegister(select),
3686 InputOperandAt(select, 1),
3687 InputOperandAt(select, 0),
3688 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003689 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003690}
3691
David Srbecky0cf44932015-12-09 14:09:59 +00003692void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003693 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00003694}
3695
David Srbeckyd28f4a02016-03-14 17:14:24 +00003696void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3697 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003698}
3699
Vladimir Markodec78172020-06-19 15:31:23 +01003700void CodeGeneratorARM64::IncreaseFrame(size_t adjustment) {
3701 __ Claim(adjustment);
3702 GetAssembler()->cfi().AdjustCFAOffset(adjustment);
3703}
3704
3705void CodeGeneratorARM64::DecreaseFrame(size_t adjustment) {
3706 __ Drop(adjustment);
3707 GetAssembler()->cfi().AdjustCFAOffset(-adjustment);
3708}
3709
David Srbeckyc7098ff2016-02-09 14:30:11 +00003710void CodeGeneratorARM64::GenerateNop() {
3711 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003712}
3713
Alexandre Rames5319def2014-10-23 10:03:10 +01003714void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003715 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003716}
3717
3718void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003719 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003720}
3721
3722void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003723 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003724}
3725
3726void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003727 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003728}
3729
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003730// Temp is used for read barrier.
3731static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3732 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003733 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003734 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3735 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3736 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3737 return 1;
3738 }
3739 return 0;
3740}
3741
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003742// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003743// interface pointer, one for loading the current interface.
3744// The other checks have one temp for loading the object's class.
3745static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3746 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3747 return 3;
3748 }
3749 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003750}
3751
Alexandre Rames67555f72014-11-18 10:55:16 +00003752void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003753 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003754 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003755 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003756 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003757 case TypeCheckKind::kExactCheck:
3758 case TypeCheckKind::kAbstractClassCheck:
3759 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00003760 case TypeCheckKind::kArrayObjectCheck: {
3761 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
3762 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
3763 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003764 break;
Vladimir Marko87584542017-12-12 17:47:52 +00003765 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003766 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003767 case TypeCheckKind::kUnresolvedCheck:
3768 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003769 call_kind = LocationSummary::kCallOnSlowPath;
3770 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00003771 case TypeCheckKind::kBitstringCheck:
3772 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003773 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003774
Vladimir Markoca6fff82017-10-03 14:49:14 +01003775 LocationSummary* locations =
3776 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003777 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003778 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003779 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003780 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003781 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3782 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3783 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3784 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3785 } else {
3786 locations->SetInAt(1, Location::RequiresRegister());
3787 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003788 // The "out" register is used as a temporary, so it overlaps with the inputs.
3789 // Note that TypeCheckSlowPathARM64 uses this register too.
3790 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003791 // Add temps if necessary for read barriers.
3792 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003793}
3794
3795void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003796 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003797 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003798 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003799 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003800 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3801 ? Register()
3802 : InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003803 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003804 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003805 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3806 DCHECK_LE(num_temps, 1u);
3807 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003808 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3809 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3810 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3811 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003812
Scott Wakeling97c72b72016-06-24 16:19:36 +01003813 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003814 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003815
3816 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003817 // Avoid null check if we know `obj` is not null.
3818 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003819 __ Cbz(obj, &zero);
3820 }
3821
Roland Levillain44015862016-01-22 11:47:17 +00003822 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003823 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003824 ReadBarrierOption read_barrier_option =
3825 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003826 // /* HeapReference<Class> */ out = obj->klass_
3827 GenerateReferenceLoadTwoRegisters(instruction,
3828 out_loc,
3829 obj_loc,
3830 class_offset,
3831 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003832 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003833 __ Cmp(out, cls);
3834 __ Cset(out, eq);
3835 if (zero.IsLinked()) {
3836 __ B(&done);
3837 }
3838 break;
3839 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003840
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003841 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003842 ReadBarrierOption read_barrier_option =
3843 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003844 // /* HeapReference<Class> */ out = obj->klass_
3845 GenerateReferenceLoadTwoRegisters(instruction,
3846 out_loc,
3847 obj_loc,
3848 class_offset,
3849 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003850 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003851 // If the class is abstract, we eagerly fetch the super class of the
3852 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003853 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003854 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003855 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003856 GenerateReferenceLoadOneRegister(instruction,
3857 out_loc,
3858 super_offset,
3859 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003860 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003861 // If `out` is null, we use it for the result, and jump to `done`.
3862 __ Cbz(out, &done);
3863 __ Cmp(out, cls);
3864 __ B(ne, &loop);
3865 __ Mov(out, 1);
3866 if (zero.IsLinked()) {
3867 __ B(&done);
3868 }
3869 break;
3870 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003871
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003872 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003873 ReadBarrierOption read_barrier_option =
3874 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003875 // /* HeapReference<Class> */ out = obj->klass_
3876 GenerateReferenceLoadTwoRegisters(instruction,
3877 out_loc,
3878 obj_loc,
3879 class_offset,
3880 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003881 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003882 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003883 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003884 __ Bind(&loop);
3885 __ Cmp(out, cls);
3886 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003887 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003888 GenerateReferenceLoadOneRegister(instruction,
3889 out_loc,
3890 super_offset,
3891 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003892 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003893 __ Cbnz(out, &loop);
3894 // If `out` is null, we use it for the result, and jump to `done`.
3895 __ B(&done);
3896 __ Bind(&success);
3897 __ Mov(out, 1);
3898 if (zero.IsLinked()) {
3899 __ B(&done);
3900 }
3901 break;
3902 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003903
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003904 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003905 ReadBarrierOption read_barrier_option =
3906 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003907 // /* HeapReference<Class> */ out = obj->klass_
3908 GenerateReferenceLoadTwoRegisters(instruction,
3909 out_loc,
3910 obj_loc,
3911 class_offset,
3912 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003913 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003914 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003915 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003916 __ Cmp(out, cls);
3917 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003918 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003919 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003920 GenerateReferenceLoadOneRegister(instruction,
3921 out_loc,
3922 component_offset,
3923 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003924 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003925 // If `out` is null, we use it for the result, and jump to `done`.
3926 __ Cbz(out, &done);
3927 __ Ldrh(out, HeapOperand(out, primitive_offset));
3928 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3929 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003930 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003931 __ Mov(out, 1);
3932 __ B(&done);
3933 break;
3934 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003935
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003936 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003937 // No read barrier since the slow path will retry upon failure.
3938 // /* HeapReference<Class> */ out = obj->klass_
3939 GenerateReferenceLoadTwoRegisters(instruction,
3940 out_loc,
3941 obj_loc,
3942 class_offset,
3943 maybe_temp_loc,
3944 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003945 __ Cmp(out, cls);
3946 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01003947 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08003948 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003949 codegen_->AddSlowPath(slow_path);
3950 __ B(ne, slow_path->GetEntryLabel());
3951 __ Mov(out, 1);
3952 if (zero.IsLinked()) {
3953 __ B(&done);
3954 }
3955 break;
3956 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003957
Calin Juravle98893e12015-10-02 21:05:03 +01003958 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003959 case TypeCheckKind::kInterfaceCheck: {
3960 // Note that we indeed only call on slow path, but we always go
3961 // into the slow path for the unresolved and interface check
3962 // cases.
3963 //
3964 // We cannot directly call the InstanceofNonTrivial runtime
3965 // entry point without resorting to a type checking slow path
3966 // here (i.e. by calling InvokeRuntime directly), as it would
3967 // require to assign fixed registers for the inputs of this
3968 // HInstanceOf instruction (following the runtime calling
3969 // convention), which might be cluttered by the potential first
3970 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003971 //
3972 // TODO: Introduce a new runtime entry point taking the object
3973 // to test (instead of its class) as argument, and let it deal
3974 // with the read barrier issues. This will let us refactor this
3975 // case of the `switch` code as it was previously (with a direct
3976 // call to the runtime not using a type checking slow path).
3977 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003978 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01003979 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08003980 instruction, /* is_fatal= */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003981 codegen_->AddSlowPath(slow_path);
3982 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003983 if (zero.IsLinked()) {
3984 __ B(&done);
3985 }
3986 break;
3987 }
Vladimir Marko175e7862018-03-27 09:03:13 +00003988
3989 case TypeCheckKind::kBitstringCheck: {
3990 // /* HeapReference<Class> */ temp = obj->klass_
3991 GenerateReferenceLoadTwoRegisters(instruction,
3992 out_loc,
3993 obj_loc,
3994 class_offset,
3995 maybe_temp_loc,
3996 kWithoutReadBarrier);
3997
3998 GenerateBitstringTypeCheckCompare(instruction, out);
3999 __ Cset(out, eq);
4000 if (zero.IsLinked()) {
4001 __ B(&done);
4002 }
4003 break;
4004 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004005 }
4006
4007 if (zero.IsLinked()) {
4008 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004009 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004010 }
4011
4012 if (done.IsLinked()) {
4013 __ Bind(&done);
4014 }
4015
4016 if (slow_path != nullptr) {
4017 __ Bind(slow_path->GetExitLabel());
4018 }
4019}
4020
4021void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004022 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00004023 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004024 LocationSummary* locations =
4025 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004026 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00004027 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
4028 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
4029 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
4030 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
4031 } else {
4032 locations->SetInAt(1, Location::RequiresRegister());
4033 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004034 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4035 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004036}
4037
4038void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004039 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004040 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004041 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004042 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00004043 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
4044 ? Register()
4045 : InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004046 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4047 DCHECK_GE(num_temps, 1u);
4048 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004049 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004050 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4051 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004052 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004053 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4054 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4055 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4056 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4057 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4058 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4059 const uint32_t object_array_data_offset =
4060 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004061
Vladimir Marko87584542017-12-12 17:47:52 +00004062 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004063 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004064 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4065 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004066 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004067
Scott Wakeling97c72b72016-06-24 16:19:36 +01004068 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004069 // Avoid null check if we know obj is not null.
4070 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004071 __ Cbz(obj, &done);
4072 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004073
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004074 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004075 case TypeCheckKind::kExactCheck:
4076 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004077 // /* HeapReference<Class> */ temp = obj->klass_
4078 GenerateReferenceLoadTwoRegisters(instruction,
4079 temp_loc,
4080 obj_loc,
4081 class_offset,
4082 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004083 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004084
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004085 __ Cmp(temp, cls);
4086 // Jump to slow path for throwing the exception or doing a
4087 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004088 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004089 break;
4090 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004091
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004092 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004093 // /* HeapReference<Class> */ temp = obj->klass_
4094 GenerateReferenceLoadTwoRegisters(instruction,
4095 temp_loc,
4096 obj_loc,
4097 class_offset,
4098 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004099 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004100
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004101 // If the class is abstract, we eagerly fetch the super class of the
4102 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004103 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004104 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004105 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004106 GenerateReferenceLoadOneRegister(instruction,
4107 temp_loc,
4108 super_offset,
4109 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004110 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004111
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004112 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4113 // exception.
4114 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4115 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004116 __ Cmp(temp, cls);
4117 __ B(ne, &loop);
4118 break;
4119 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004120
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004121 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004122 // /* HeapReference<Class> */ temp = obj->klass_
4123 GenerateReferenceLoadTwoRegisters(instruction,
4124 temp_loc,
4125 obj_loc,
4126 class_offset,
4127 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004128 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004129
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004130 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004131 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004132 __ Bind(&loop);
4133 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004134 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004135
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004136 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004137 GenerateReferenceLoadOneRegister(instruction,
4138 temp_loc,
4139 super_offset,
4140 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004141 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004142
4143 // If the class reference currently in `temp` is not null, jump
4144 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004145 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004146 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004147 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004148 break;
4149 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004150
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004151 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004152 // /* HeapReference<Class> */ temp = obj->klass_
4153 GenerateReferenceLoadTwoRegisters(instruction,
4154 temp_loc,
4155 obj_loc,
4156 class_offset,
4157 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004158 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004159
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004160 // Do an exact check.
4161 __ Cmp(temp, cls);
4162 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004163
4164 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004165 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004166 GenerateReferenceLoadOneRegister(instruction,
4167 temp_loc,
4168 component_offset,
4169 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004170 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004171
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004172 // If the component type is null, jump to the slow path to throw the exception.
4173 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4174 // Otherwise, the object is indeed an array. Further check that this component type is not a
4175 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004176 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4177 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004178 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004179 break;
4180 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004181
Calin Juravle98893e12015-10-02 21:05:03 +01004182 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004183 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004184 //
4185 // We cannot directly call the CheckCast runtime entry point
4186 // without resorting to a type checking slow path here (i.e. by
4187 // calling InvokeRuntime directly), as it would require to
4188 // assign fixed registers for the inputs of this HInstanceOf
4189 // instruction (following the runtime calling convention), which
4190 // might be cluttered by the potential first read barrier
4191 // emission at the beginning of this method.
4192 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004193 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004194 case TypeCheckKind::kInterfaceCheck: {
4195 // /* HeapReference<Class> */ temp = obj->klass_
4196 GenerateReferenceLoadTwoRegisters(instruction,
4197 temp_loc,
4198 obj_loc,
4199 class_offset,
4200 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004201 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004202
4203 // /* HeapReference<Class> */ temp = temp->iftable_
4204 GenerateReferenceLoadTwoRegisters(instruction,
4205 temp_loc,
4206 temp_loc,
4207 iftable_offset,
4208 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004209 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004210 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004211 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004212 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004213 vixl::aarch64::Label start_loop;
4214 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004215 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004216 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4217 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004218 // Go to next interface.
4219 __ Add(temp, temp, 2 * kHeapReferenceSize);
4220 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004221 // Compare the classes and continue the loop if they do not match.
4222 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4223 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004224 break;
4225 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004226
4227 case TypeCheckKind::kBitstringCheck: {
4228 // /* HeapReference<Class> */ temp = obj->klass_
4229 GenerateReferenceLoadTwoRegisters(instruction,
4230 temp_loc,
4231 obj_loc,
4232 class_offset,
4233 maybe_temp2_loc,
4234 kWithoutReadBarrier);
4235
4236 GenerateBitstringTypeCheckCompare(instruction, temp);
4237 __ B(ne, type_check_slow_path->GetEntryLabel());
4238 break;
4239 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004240 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004241 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004242
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004243 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004244}
4245
Alexandre Rames5319def2014-10-23 10:03:10 +01004246void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004247 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004248 locations->SetOut(Location::ConstantLocation(constant));
4249}
4250
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004251void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004252 // Will be generated at use site.
4253}
4254
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004255void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004256 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004257 locations->SetOut(Location::ConstantLocation(constant));
4258}
4259
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004260void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004261 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004262}
4263
Calin Juravle175dc732015-08-25 15:42:32 +01004264void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4265 // The trampoline uses the same calling convention as dex calling conventions,
4266 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4267 // the method_idx.
4268 HandleInvoke(invoke);
4269}
4270
4271void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4272 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004273 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004274}
4275
Alexandre Rames5319def2014-10-23 10:03:10 +01004276void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004277 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004278 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004279}
4280
Alexandre Rames67555f72014-11-18 10:55:16 +00004281void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4282 HandleInvoke(invoke);
4283}
4284
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004285void CodeGeneratorARM64::MaybeGenerateInlineCacheCheck(HInstruction* instruction,
4286 Register klass) {
4287 DCHECK_EQ(klass.GetCode(), 0u);
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004288 // We know the destination of an intrinsic, so no need to record inline
4289 // caches.
4290 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00004291 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004292 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004293 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
Nicolas Geoffray095dc462020-08-17 16:40:28 +01004294 ScopedProfilingInfoUse spiu(
4295 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
4296 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00004297 if (info != nullptr) {
4298 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
4299 uint64_t address = reinterpret_cast64<uint64_t>(cache);
4300 vixl::aarch64::Label done;
4301 __ Mov(x8, address);
4302 __ Ldr(x9, MemOperand(x8, InlineCache::ClassesOffset().Int32Value()));
4303 // Fast path for a monomorphic cache.
4304 __ Cmp(klass, x9);
4305 __ B(eq, &done);
4306 InvokeRuntime(kQuickUpdateInlineCache, instruction, instruction->GetDexPc());
4307 __ Bind(&done);
4308 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004309 }
4310}
4311
Alexandre Rames67555f72014-11-18 10:55:16 +00004312void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4313 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004314 LocationSummary* locations = invoke->GetLocations();
4315 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004316 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004317 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004318 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004319
Artem Serov914d7a82017-02-07 14:33:49 +00004320 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004321 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004322 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004323 {
4324 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4325 // /* HeapReference<Class> */ temp = temp->klass_
4326 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4327 codegen_->MaybeRecordImplicitNullCheck(invoke);
4328 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004329 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004330 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004331 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004332 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004333 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004334 }
Artem Serov914d7a82017-02-07 14:33:49 +00004335
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004336 // Instead of simply (possibly) unpoisoning `temp` here, we should
4337 // emit a read barrier for the previous class reference load.
4338 // However this is not required in practice, as this is an
4339 // intermediate/temporary reference and because the current
4340 // concurrent copying collector keeps the from-space memory
4341 // intact/accessible until the end of the marking phase (the
4342 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004343 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004344
4345 // If we're compiling baseline, update the inline cache.
4346 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
4347
4348 // The register ip1 is required to be used for the hidden argument in
4349 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
4350 MacroAssembler* masm = GetVIXLAssembler();
4351 UseScratchRegisterScope scratch_scope(masm);
4352 scratch_scope.Exclude(ip1);
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004353 __ Mov(ip1, invoke->GetMethodReference().index);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004354
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004355 __ Ldr(temp,
4356 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4357 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004358 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004359 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004360 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004361 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004362 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004363
4364 {
4365 // Ensure the pc position is recorded immediately after the `blr` instruction.
4366 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4367
4368 // lr();
4369 __ blr(lr);
4370 DCHECK(!codegen_->IsLeafMethod());
4371 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4372 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004373
Andreas Gampe3db70682018-12-26 15:12:03 -08004374 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004375}
4376
4377void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004378 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004379 if (intrinsic.TryDispatch(invoke)) {
4380 return;
4381 }
4382
Alexandre Rames67555f72014-11-18 10:55:16 +00004383 HandleInvoke(invoke);
4384}
4385
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004386void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004387 // Explicit clinit checks triggered by static invokes must have been pruned by
4388 // art::PrepareForRegisterAllocation.
4389 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004390
Vladimir Markoca6fff82017-10-03 14:49:14 +01004391 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004392 if (intrinsic.TryDispatch(invoke)) {
4393 return;
4394 }
4395
Vladimir Marko86c87522020-05-11 16:55:55 +01004396 if (invoke->GetCodePtrLocation() == HInvokeStaticOrDirect::CodePtrLocation::kCallCriticalNative) {
4397 CriticalNativeCallingConventionVisitorARM64 calling_convention_visitor(
4398 /*for_register_allocation=*/ true);
4399 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4400 } else {
4401 HandleInvoke(invoke);
4402 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004403}
4404
Andreas Gampe878d58c2015-01-15 23:24:00 -08004405static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4406 if (invoke->GetLocations()->Intrinsified()) {
4407 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4408 intrinsic.Dispatch(invoke);
4409 return true;
4410 }
4411 return false;
4412}
4413
Vladimir Markodc151b22015-10-15 18:02:30 +01004414HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4415 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004416 ArtMethod* method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004417 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004418 return desired_dispatch_info;
4419}
4420
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004421void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4422 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004423 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004424 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4425 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004426 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4427 uint32_t offset =
4428 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004429 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004430 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004431 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004432 }
Vladimir Marko58155012015-08-19 12:49:41 +00004433 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Marko86c87522020-05-11 16:55:55 +01004434 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004435 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004436 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01004437 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01004438 // Add ADRP with its PC-relative method patch.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004439 vixl::aarch64::Label* adrp_label =
4440 NewBootImageMethodPatch(invoke->GetResolvedMethodReference());
Vladimir Marko65979462017-05-19 17:25:12 +01004441 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4442 // Add ADD with its PC-relative method patch.
4443 vixl::aarch64::Label* add_label =
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004444 NewBootImageMethodPatch(invoke->GetResolvedMethodReference(), adrp_label);
Vladimir Marko65979462017-05-19 17:25:12 +01004445 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4446 break;
4447 }
Vladimir Markob066d432018-01-03 13:14:37 +00004448 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4449 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004450 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00004451 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_offset);
4452 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4453 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
4454 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_offset, adrp_label);
4455 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
4456 EmitLdrOffsetPlaceholder(ldr_label, WRegisterFrom(temp), XRegisterFrom(temp));
4457 break;
4458 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004459 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Markob066d432018-01-03 13:14:37 +00004460 // Add ADRP with its PC-relative .bss entry patch.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004461 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(invoke->GetMethodReference());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004462 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Markob066d432018-01-03 13:14:37 +00004463 // Add LDR with its PC-relative .bss entry patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004464 vixl::aarch64::Label* ldr_label =
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004465 NewMethodBssEntryPatch(invoke->GetMethodReference(), adrp_label);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01004466 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004467 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004468 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004469 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004470 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
4471 // Load method address from literal pool.
4472 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
4473 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004474 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4475 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4476 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004477 }
4478 }
4479
Vladimir Marko86c87522020-05-11 16:55:55 +01004480 auto call_code_pointer_member = [&](MemberOffset offset) {
4481 // LR = callee_method->member;
4482 __ Ldr(lr, MemOperand(XRegisterFrom(callee_method), offset.Int32Value()));
4483 {
4484 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4485 ExactAssemblyScope eas(GetVIXLAssembler(),
4486 kInstructionSize,
4487 CodeBufferCheckScope::kExactSize);
4488 // lr()
4489 __ blr(lr);
4490 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4491 }
4492 };
Vladimir Marko58155012015-08-19 12:49:41 +00004493 switch (invoke->GetCodePtrLocation()) {
4494 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004495 {
4496 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4497 ExactAssemblyScope eas(GetVIXLAssembler(),
4498 kInstructionSize,
4499 CodeBufferCheckScope::kExactSize);
4500 __ bl(&frame_entry_label_);
4501 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4502 }
Vladimir Marko58155012015-08-19 12:49:41 +00004503 break;
Vladimir Marko86c87522020-05-11 16:55:55 +01004504 case HInvokeStaticOrDirect::CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01004505 size_t out_frame_size =
4506 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorARM64,
4507 kAapcs64StackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01004508 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Marko86c87522020-05-11 16:55:55 +01004509 call_code_pointer_member(ArtMethod::EntryPointFromJniOffset(kArm64PointerSize));
4510 // Zero-/sign-extend the result when needed due to native and managed ABI mismatch.
4511 switch (invoke->GetType()) {
4512 case DataType::Type::kBool:
4513 __ Ubfx(w0, w0, 0, 8);
4514 break;
4515 case DataType::Type::kInt8:
4516 __ Sbfx(w0, w0, 0, 8);
4517 break;
4518 case DataType::Type::kUint16:
4519 __ Ubfx(w0, w0, 0, 16);
4520 break;
4521 case DataType::Type::kInt16:
4522 __ Sbfx(w0, w0, 0, 16);
4523 break;
4524 case DataType::Type::kInt32:
4525 case DataType::Type::kInt64:
4526 case DataType::Type::kFloat32:
4527 case DataType::Type::kFloat64:
4528 case DataType::Type::kVoid:
4529 break;
4530 default:
4531 DCHECK(false) << invoke->GetType();
4532 break;
4533 }
4534 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01004535 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01004536 }
4537 break;
4538 }
4539 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4540 call_code_pointer_member(ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize));
Vladimir Marko58155012015-08-19 12:49:41 +00004541 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004542 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004543
Andreas Gampe878d58c2015-01-15 23:24:00 -08004544 DCHECK(!IsLeafMethod());
4545}
4546
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004547void CodeGeneratorARM64::GenerateVirtualCall(
4548 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004549 // Use the calling convention instead of the location of the receiver, as
4550 // intrinsics may have put the receiver in a different register. In the intrinsics
4551 // slow path, the arguments have been moved to the right place, so here we are
4552 // guaranteed that the receiver is the first register of the calling convention.
4553 InvokeDexCallingConvention calling_convention;
4554 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004555 Register temp = XRegisterFrom(temp_in);
4556 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4557 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4558 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004559 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004560
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004561 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004562
4563 {
4564 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4565 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4566 // /* HeapReference<Class> */ temp = receiver->klass_
4567 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4568 MaybeRecordImplicitNullCheck(invoke);
4569 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004570 // Instead of simply (possibly) unpoisoning `temp` here, we should
4571 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004572 // intermediate/temporary reference and because the current
4573 // concurrent copying collector keeps the from-space memory
4574 // intact/accessible until the end of the marking phase (the
4575 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004576 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004577
4578 // If we're compiling baseline, update the inline cache.
4579 MaybeGenerateInlineCacheCheck(invoke, temp);
4580
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004581 // temp = temp->GetMethodAt(method_offset);
4582 __ Ldr(temp, MemOperand(temp, method_offset));
4583 // lr = temp->GetEntryPoint();
4584 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004585 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004586 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004587 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4588 // lr();
4589 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004590 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004591 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004592}
4593
Vladimir Marko9922f002020-06-08 15:05:15 +01004594void CodeGeneratorARM64::MoveFromReturnRegister(Location trg, DataType::Type type) {
4595 if (!trg.IsValid()) {
4596 DCHECK(type == DataType::Type::kVoid);
4597 return;
4598 }
4599
4600 DCHECK_NE(type, DataType::Type::kVoid);
4601
4602 if (DataType::IsIntegralType(type) || type == DataType::Type::kReference) {
4603 Register trg_reg = RegisterFrom(trg, type);
4604 Register res_reg = RegisterFrom(ARM64ReturnLocation(type), type);
4605 __ Mov(trg_reg, res_reg, kDiscardForSameWReg);
4606 } else {
4607 VRegister trg_reg = FPRegisterFrom(trg, type);
4608 VRegister res_reg = FPRegisterFrom(ARM64ReturnLocation(type), type);
4609 __ Fmov(trg_reg, res_reg);
4610 }
4611}
4612
Orion Hodsonac141392017-01-13 11:53:47 +00004613void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00004614 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
4615 if (intrinsic.TryDispatch(invoke)) {
4616 return;
4617 }
Orion Hodsonac141392017-01-13 11:53:47 +00004618 HandleInvoke(invoke);
4619}
4620
4621void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00004622 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4623 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
4624 return;
4625 }
Orion Hodsonac141392017-01-13 11:53:47 +00004626 codegen_->GenerateInvokePolymorphicCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004627 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004628}
4629
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004630void LocationsBuilderARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4631 HandleInvoke(invoke);
4632}
4633
4634void InstructionCodeGeneratorARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4635 codegen_->GenerateInvokeCustomCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004636 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004637}
4638
Vladimir Marko6fd16062018-06-26 11:02:04 +01004639vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageIntrinsicPatch(
4640 uint32_t intrinsic_data,
4641 vixl::aarch64::Label* adrp_label) {
4642 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004643 /* dex_file= */ nullptr, intrinsic_data, adrp_label, &boot_image_other_patches_);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004644}
4645
Vladimir Markob066d432018-01-03 13:14:37 +00004646vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageRelRoPatch(
4647 uint32_t boot_image_offset,
4648 vixl::aarch64::Label* adrp_label) {
4649 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004650 /* dex_file= */ nullptr, boot_image_offset, adrp_label, &boot_image_other_patches_);
Vladimir Markob066d432018-01-03 13:14:37 +00004651}
4652
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004653vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004654 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004655 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004656 return NewPcRelativePatch(
4657 target_method.dex_file, target_method.index, adrp_label, &boot_image_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004658}
4659
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004660vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4661 MethodReference target_method,
4662 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004663 return NewPcRelativePatch(
4664 target_method.dex_file, target_method.index, adrp_label, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004665}
4666
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004667vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageTypePatch(
Scott Wakeling97c72b72016-06-24 16:19:36 +01004668 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004669 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004670 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004671 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &boot_image_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004672}
4673
Vladimir Marko1998cd02017-01-13 13:02:58 +00004674vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4675 const DexFile& dex_file,
4676 dex::TypeIndex type_index,
4677 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004678 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004679}
4680
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004681vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004682 const DexFile& dex_file,
4683 dex::StringIndex string_index,
4684 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004685 return NewPcRelativePatch(
4686 &dex_file, string_index.index_, adrp_label, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01004687}
4688
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004689vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4690 const DexFile& dex_file,
4691 dex::StringIndex string_index,
4692 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004693 return NewPcRelativePatch(&dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004694}
4695
Vladimir Markof6675082019-05-17 12:05:28 +01004696void CodeGeneratorARM64::EmitEntrypointThunkCall(ThreadOffset64 entrypoint_offset) {
4697 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01004698 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markof6675082019-05-17 12:05:28 +01004699 call_entrypoint_patches_.emplace_back(/*dex_file*/ nullptr, entrypoint_offset.Uint32Value());
4700 vixl::aarch64::Label* bl_label = &call_entrypoint_patches_.back().label;
4701 __ bind(bl_label);
4702 __ bl(static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4703}
4704
Vladimir Marko966b46f2018-08-03 10:20:19 +00004705void CodeGeneratorARM64::EmitBakerReadBarrierCbnz(uint32_t custom_data) {
Vladimir Marko94796f82018-08-08 15:15:33 +01004706 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01004707 if (GetCompilerOptions().IsJitCompiler()) {
Vladimir Marko966b46f2018-08-03 10:20:19 +00004708 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
4709 vixl::aarch64::Label* slow_path_entry = &it->second.label;
4710 __ cbnz(mr, slow_path_entry);
4711 } else {
4712 baker_read_barrier_patches_.emplace_back(custom_data);
4713 vixl::aarch64::Label* cbnz_label = &baker_read_barrier_patches_.back().label;
4714 __ bind(cbnz_label);
4715 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4716 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004717}
4718
Scott Wakeling97c72b72016-06-24 16:19:36 +01004719vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004720 const DexFile* dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004721 uint32_t offset_or_index,
4722 vixl::aarch64::Label* adrp_label,
4723 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004724 // Add a patch entry and return the label.
4725 patches->emplace_back(dex_file, offset_or_index);
4726 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004727 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004728 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4729 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4730 return label;
4731}
4732
Scott Wakeling97c72b72016-06-24 16:19:36 +01004733vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4734 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004735 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004736}
4737
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004738vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004739 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004740 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004741 return jit_string_patches_.GetOrCreate(
4742 StringReference(&dex_file, string_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004743 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004744}
4745
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004746vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004747 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004748 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004749 return jit_class_patches_.GetOrCreate(
4750 TypeReference(&dex_file, type_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004751 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004752}
4753
Vladimir Markoaad75c62016-10-03 08:46:48 +00004754void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4755 vixl::aarch64::Register reg) {
4756 DCHECK(reg.IsX());
4757 SingleEmissionCheckScope guard(GetVIXLAssembler());
4758 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004759 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004760}
4761
4762void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4763 vixl::aarch64::Register out,
4764 vixl::aarch64::Register base) {
4765 DCHECK(out.IsX());
4766 DCHECK(base.IsX());
4767 SingleEmissionCheckScope guard(GetVIXLAssembler());
4768 __ Bind(fixup_label);
4769 __ add(out, base, Operand(/* offset placeholder */ 0));
4770}
4771
4772void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4773 vixl::aarch64::Register out,
4774 vixl::aarch64::Register base) {
4775 DCHECK(base.IsX());
4776 SingleEmissionCheckScope guard(GetVIXLAssembler());
4777 __ Bind(fixup_label);
4778 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4779}
4780
Vladimir Markoeebb8212018-06-05 14:57:24 +01004781void CodeGeneratorARM64::LoadBootImageAddress(vixl::aarch64::Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01004782 uint32_t boot_image_reference) {
4783 if (GetCompilerOptions().IsBootImage()) {
4784 // Add ADRP with its PC-relative type patch.
4785 vixl::aarch64::Label* adrp_label = NewBootImageIntrinsicPatch(boot_image_reference);
4786 EmitAdrpPlaceholder(adrp_label, reg.X());
4787 // Add ADD with its PC-relative type patch.
4788 vixl::aarch64::Label* add_label = NewBootImageIntrinsicPatch(boot_image_reference, adrp_label);
4789 EmitAddPlaceholder(add_label, reg.X(), reg.X());
Vladimir Markoa2da9b92018-10-10 14:21:55 +01004790 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01004791 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004792 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004793 EmitAdrpPlaceholder(adrp_label, reg.X());
4794 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004795 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_reference, adrp_label);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004796 EmitLdrOffsetPlaceholder(ldr_label, reg.W(), reg.X());
4797 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01004798 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01004799 gc::Heap* heap = Runtime::Current()->GetHeap();
4800 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004801 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01004802 __ Ldr(reg.W(), DeduplicateBootImageAddressLiteral(reinterpret_cast<uintptr_t>(address)));
4803 }
4804}
4805
Vladimir Marko6fd16062018-06-26 11:02:04 +01004806void CodeGeneratorARM64::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
4807 uint32_t boot_image_offset) {
4808 DCHECK(invoke->IsStatic());
4809 InvokeRuntimeCallingConvention calling_convention;
4810 Register argument = calling_convention.GetRegisterAt(0);
4811 if (GetCompilerOptions().IsBootImage()) {
4812 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
4813 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004814 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01004815 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
4816 // Add ADRP with its PC-relative type patch.
4817 vixl::aarch64::Label* adrp_label = NewBootImageTypePatch(*target_method.dex_file, type_idx);
4818 EmitAdrpPlaceholder(adrp_label, argument.X());
4819 // Add ADD with its PC-relative type patch.
4820 vixl::aarch64::Label* add_label =
4821 NewBootImageTypePatch(*target_method.dex_file, type_idx, adrp_label);
4822 EmitAddPlaceholder(add_label, argument.X(), argument.X());
4823 } else {
4824 LoadBootImageAddress(argument, boot_image_offset);
4825 }
4826 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
4827 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4828}
4829
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004830template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004831inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4832 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004833 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004834 for (const PcRelativePatchInfo& info : infos) {
4835 linker_patches->push_back(Factory(info.label.GetLocation(),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004836 info.target_dex_file,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004837 info.pc_insn_label->GetLocation(),
4838 info.offset_or_index));
4839 }
4840}
4841
Vladimir Marko6fd16062018-06-26 11:02:04 +01004842template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
4843linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
4844 const DexFile* target_dex_file,
4845 uint32_t pc_insn_offset,
4846 uint32_t boot_image_offset) {
4847 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
4848 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00004849}
4850
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004851void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004852 DCHECK(linker_patches->empty());
4853 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004854 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004855 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004856 boot_image_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004857 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004858 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004859 string_bss_entry_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01004860 boot_image_other_patches_.size() +
Vladimir Markof6675082019-05-17 12:05:28 +01004861 call_entrypoint_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004862 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004863 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01004864 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004865 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004866 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004867 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004868 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004869 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004870 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01004871 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01004872 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004873 DCHECK(boot_image_type_patches_.empty());
4874 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01004875 }
4876 if (GetCompilerOptions().IsBootImage()) {
4877 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
4878 boot_image_other_patches_, linker_patches);
4879 } else {
4880 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
4881 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004882 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004883 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4884 method_bss_entry_patches_, linker_patches);
4885 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4886 type_bss_entry_patches_, linker_patches);
4887 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4888 string_bss_entry_patches_, linker_patches);
Vladimir Markof6675082019-05-17 12:05:28 +01004889 for (const PatchInfo<vixl::aarch64::Label>& info : call_entrypoint_patches_) {
4890 DCHECK(info.target_dex_file == nullptr);
4891 linker_patches->push_back(linker::LinkerPatch::CallEntrypointPatch(
4892 info.label.GetLocation(), info.offset_or_index));
4893 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004894 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004895 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
4896 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004897 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004898 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004899}
4900
Vladimir Markoca1e0382018-04-11 09:58:41 +00004901bool CodeGeneratorARM64::NeedsThunkCode(const linker::LinkerPatch& patch) const {
Vladimir Markof6675082019-05-17 12:05:28 +01004902 return patch.GetType() == linker::LinkerPatch::Type::kCallEntrypoint ||
4903 patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
Vladimir Markoca1e0382018-04-11 09:58:41 +00004904 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
4905}
4906
4907void CodeGeneratorARM64::EmitThunkCode(const linker::LinkerPatch& patch,
4908 /*out*/ ArenaVector<uint8_t>* code,
4909 /*out*/ std::string* debug_name) {
4910 Arm64Assembler assembler(GetGraph()->GetAllocator());
4911 switch (patch.GetType()) {
4912 case linker::LinkerPatch::Type::kCallRelative: {
4913 // The thunk just uses the entry point in the ArtMethod. This works even for calls
4914 // to the generic JNI and interpreter trampolines.
4915 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4916 kArm64PointerSize).Int32Value());
4917 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
4918 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
4919 *debug_name = "MethodCallThunk";
4920 }
4921 break;
4922 }
Vladimir Markof6675082019-05-17 12:05:28 +01004923 case linker::LinkerPatch::Type::kCallEntrypoint: {
4924 Offset offset(patch.EntrypointOffset());
4925 assembler.JumpTo(ManagedRegister(arm64::TR), offset, ManagedRegister(arm64::IP0));
4926 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
4927 *debug_name = "EntrypointCallThunk_" + std::to_string(offset.Uint32Value());
4928 }
4929 break;
4930 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00004931 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
4932 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
4933 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
4934 break;
4935 }
4936 default:
4937 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
4938 UNREACHABLE();
4939 }
4940
4941 // Ensure we emit the literal pool if any.
4942 assembler.FinalizeCode();
4943 code->resize(assembler.CodeSize());
4944 MemoryRegion code_region(code->data(), code->size());
4945 assembler.FinalizeInstructions(code_region);
4946}
4947
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004948vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
4949 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004950 value,
4951 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4952}
4953
Scott Wakeling97c72b72016-06-24 16:19:36 +01004954vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004955 return uint64_literals_.GetOrCreate(
4956 value,
4957 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004958}
4959
Andreas Gampe878d58c2015-01-15 23:24:00 -08004960void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004961 // Explicit clinit checks triggered by static invokes must have been pruned by
4962 // art::PrepareForRegisterAllocation.
4963 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004964
Andreas Gampe878d58c2015-01-15 23:24:00 -08004965 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08004966 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004967 return;
4968 }
4969
Vladimir Marko86c87522020-05-11 16:55:55 +01004970 LocationSummary* locations = invoke->GetLocations();
4971 codegen_->GenerateStaticOrDirectCall(
4972 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004973
Andreas Gampe3db70682018-12-26 15:12:03 -08004974 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004975}
4976
4977void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004978 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08004979 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004980 return;
4981 }
4982
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004983 {
4984 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4985 // are no pools emitted.
4986 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4987 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
4988 DCHECK(!codegen_->IsLeafMethod());
4989 }
4990
Andreas Gampe3db70682018-12-26 15:12:03 -08004991 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004992}
4993
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004994HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4995 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004996 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004997 case HLoadClass::LoadKind::kInvalid:
4998 LOG(FATAL) << "UNREACHABLE";
4999 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005000 case HLoadClass::LoadKind::kReferrersClass:
5001 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005002 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005003 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005004 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01005005 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005006 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005007 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005008 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01005009 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005010 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005011 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005012 break;
5013 }
5014 return desired_class_load_kind;
5015}
5016
Alexandre Rames67555f72014-11-18 10:55:16 +00005017void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005018 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005019 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005020 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005021 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005022 cls,
5023 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005024 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00005025 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005026 return;
5027 }
Vladimir Marko41559982017-01-06 14:04:23 +00005028 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005029
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005030 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5031 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005032 ? LocationSummary::kCallOnSlowPath
5033 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005034 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005035 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005036 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005037 }
5038
Vladimir Marko41559982017-01-06 14:04:23 +00005039 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005040 locations->SetInAt(0, Location::RequiresRegister());
5041 }
5042 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005043 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
5044 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5045 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005046 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005047 } else {
5048 // For non-Baker read barrier we have a temp-clobbering call.
5049 }
5050 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005051}
5052
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005053// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5054// move.
5055void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005056 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005057 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005058 codegen_->GenerateLoadClassRuntimeCall(cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08005059 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01005060 return;
5061 }
Vladimir Marko41559982017-01-06 14:04:23 +00005062 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005063
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005064 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01005065 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00005066
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005067 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5068 ? kWithoutReadBarrier
5069 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005070 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005071 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005072 case HLoadClass::LoadKind::kReferrersClass: {
5073 DCHECK(!cls->CanCallRuntime());
5074 DCHECK(!cls->MustGenerateClinitCheck());
5075 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5076 Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00005077 codegen_->GenerateGcRootFieldLoad(cls,
5078 out_loc,
5079 current_method,
5080 ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005081 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005082 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005083 break;
5084 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005085 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005086 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5087 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005088 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005089 // Add ADRP with its PC-relative type patch.
5090 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08005091 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005092 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005093 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005094 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005095 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005096 codegen_->NewBootImageTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005097 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005098 break;
5099 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005100 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005101 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005102 uint32_t boot_image_offset = codegen_->GetBootImageOffset(cls);
5103 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5104 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005105 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005106 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005107 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005108 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005109 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005110 break;
5111 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005112 case HLoadClass::LoadKind::kBssEntry: {
5113 // Add ADRP with its PC-relative Class .bss entry patch.
5114 const DexFile& dex_file = cls->GetDexFile();
5115 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00005116 vixl::aarch64::Register temp = XRegisterFrom(out_loc);
5117 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
5118 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005119 // Add LDR with its PC-relative Class .bss entry patch.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005120 vixl::aarch64::Label* ldr_label =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005121 codegen_->NewBssEntryTypePatch(dex_file, type_index, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005122 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005123 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005124 codegen_->GenerateGcRootFieldLoad(cls,
5125 out_loc,
5126 temp,
5127 /* offset placeholder */ 0u,
5128 ldr_label,
5129 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005130 generate_null_check = true;
5131 break;
5132 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005133 case HLoadClass::LoadKind::kJitBootImageAddress: {
5134 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5135 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
5136 DCHECK_NE(address, 0u);
5137 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5138 break;
5139 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005140 case HLoadClass::LoadKind::kJitTableAddress: {
5141 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5142 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005143 cls->GetClass()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005144 codegen_->GenerateGcRootFieldLoad(cls,
5145 out_loc,
5146 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005147 /* offset= */ 0,
5148 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005149 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005150 break;
5151 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005152 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005153 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005154 LOG(FATAL) << "UNREACHABLE";
5155 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005156 }
5157
Vladimir Markoea4c1262017-02-06 19:59:33 +00005158 bool do_clinit = cls->MustGenerateClinitCheck();
5159 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005160 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01005161 SlowPathCodeARM64* slow_path =
5162 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005163 codegen_->AddSlowPath(slow_path);
5164 if (generate_null_check) {
5165 __ Cbz(out, slow_path->GetEntryLabel());
5166 }
5167 if (cls->MustGenerateClinitCheck()) {
5168 GenerateClassInitializationCheck(slow_path, out);
5169 } else {
5170 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005171 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005172 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005173 }
5174}
5175
Orion Hodsondbaa5c72018-05-10 08:22:46 +01005176void LocationsBuilderARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5177 InvokeRuntimeCallingConvention calling_convention;
5178 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5179 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
5180}
5181
5182void InstructionCodeGeneratorARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5183 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
5184}
5185
Orion Hodson18259d72018-04-12 11:18:23 +01005186void LocationsBuilderARM64::VisitLoadMethodType(HLoadMethodType* load) {
5187 InvokeRuntimeCallingConvention calling_convention;
5188 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5189 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
5190}
5191
5192void InstructionCodeGeneratorARM64::VisitLoadMethodType(HLoadMethodType* load) {
5193 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
5194}
5195
David Brazdilcb1c0552015-08-04 16:22:25 +01005196static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005197 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005198}
5199
Alexandre Rames67555f72014-11-18 10:55:16 +00005200void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5201 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005202 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00005203 locations->SetOut(Location::RequiresRegister());
5204}
5205
5206void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005207 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5208}
5209
5210void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005211 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005212}
5213
5214void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5215 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005216}
5217
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005218HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5219 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005220 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005221 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005222 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005223 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01005224 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005225 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005226 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005227 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01005228 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005229 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005230 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005231 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005232 }
5233 return desired_string_load_kind;
5234}
5235
Alexandre Rames67555f72014-11-18 10:55:16 +00005236void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005237 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005238 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005239 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005240 InvokeRuntimeCallingConvention calling_convention;
5241 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5242 } else {
5243 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005244 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5245 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005246 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005247 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005248 } else {
5249 // For non-Baker read barrier we have a temp-clobbering call.
5250 }
5251 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005252 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005253}
5254
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005255// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5256// move.
5257void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005258 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005259 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005260
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005261 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005262 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005263 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5264 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005265 // Add ADRP with its PC-relative String patch.
5266 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005267 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005268 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005269 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005270 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005271 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005272 codegen_->NewBootImageStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005273 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005274 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005275 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005276 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005277 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005278 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5279 uint32_t boot_image_offset = codegen_->GetBootImageOffset(load);
5280 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005281 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005282 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005283 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005284 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005285 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
5286 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005287 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005288 case HLoadString::LoadKind::kBssEntry: {
5289 // Add ADRP with its PC-relative String .bss entry patch.
5290 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005291 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00005292 Register temp = XRegisterFrom(out_loc);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005293 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005294 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005295 // Add LDR with its PC-relative String .bss entry patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005296 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005297 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005298 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005299 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005300 codegen_->GenerateGcRootFieldLoad(load,
5301 out_loc,
5302 temp,
5303 /* offset placeholder */ 0u,
5304 ldr_label,
5305 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005306 SlowPathCodeARM64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005307 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005308 codegen_->AddSlowPath(slow_path);
5309 __ Cbz(out.X(), slow_path->GetEntryLabel());
5310 __ Bind(slow_path->GetExitLabel());
Andreas Gampe3db70682018-12-26 15:12:03 -08005311 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005312 return;
5313 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005314 case HLoadString::LoadKind::kJitBootImageAddress: {
5315 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
5316 DCHECK_NE(address, 0u);
5317 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5318 return;
5319 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005320 case HLoadString::LoadKind::kJitTableAddress: {
5321 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005322 load->GetStringIndex(),
5323 load->GetString()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005324 codegen_->GenerateGcRootFieldLoad(load,
5325 out_loc,
5326 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005327 /* offset= */ 0,
5328 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005329 kCompilerReadBarrierOption);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005330 return;
5331 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005332 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005333 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005334 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005335
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005336 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005337 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005338 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005339 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005340 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5341 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005342 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005343}
5344
Alexandre Rames5319def2014-10-23 10:03:10 +01005345void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005346 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01005347 locations->SetOut(Location::ConstantLocation(constant));
5348}
5349
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005350void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005351 // Will be generated at use site.
5352}
5353
Alexandre Rames67555f72014-11-18 10:55:16 +00005354void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005355 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5356 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005357 InvokeRuntimeCallingConvention calling_convention;
5358 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5359}
5360
5361void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005362 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005363 instruction,
5364 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005365 if (instruction->IsEnter()) {
5366 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5367 } else {
5368 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5369 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005370 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005371}
5372
Alexandre Rames42d641b2014-10-27 14:00:51 +00005373void LocationsBuilderARM64::VisitMul(HMul* mul) {
5374 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005375 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005376 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005377 case DataType::Type::kInt32:
5378 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005379 locations->SetInAt(0, Location::RequiresRegister());
5380 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005381 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005382 break;
5383
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005384 case DataType::Type::kFloat32:
5385 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005386 locations->SetInAt(0, Location::RequiresFpuRegister());
5387 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005388 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005389 break;
5390
5391 default:
5392 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5393 }
5394}
5395
5396void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5397 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005398 case DataType::Type::kInt32:
5399 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005400 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5401 break;
5402
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005403 case DataType::Type::kFloat32:
5404 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005405 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005406 break;
5407
5408 default:
5409 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5410 }
5411}
5412
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005413void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5414 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005415 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005416 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005417 case DataType::Type::kInt32:
5418 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005419 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005420 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005421 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005422
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005423 case DataType::Type::kFloat32:
5424 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005425 locations->SetInAt(0, Location::RequiresFpuRegister());
5426 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005427 break;
5428
5429 default:
5430 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5431 }
5432}
5433
5434void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5435 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005436 case DataType::Type::kInt32:
5437 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005438 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5439 break;
5440
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005441 case DataType::Type::kFloat32:
5442 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005443 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005444 break;
5445
5446 default:
5447 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5448 }
5449}
5450
5451void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005452 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5453 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005454 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005455 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005456 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5457 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005458}
5459
5460void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005461 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5462 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005463 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005464 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005465 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005466}
5467
Alexandre Rames5319def2014-10-23 10:03:10 +01005468void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005469 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5470 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005471 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07005472 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005473 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005474}
5475
5476void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005477 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5478 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005479 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005480}
5481
5482void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005483 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005484 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005485 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005486}
5487
5488void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005489 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005490 case DataType::Type::kInt32:
5491 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005492 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005493 break;
5494
5495 default:
5496 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5497 }
5498}
5499
David Brazdil66d126e2015-04-03 16:02:44 +01005500void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005501 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005502 locations->SetInAt(0, Location::RequiresRegister());
5503 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5504}
5505
5506void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005507 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005508}
5509
Alexandre Rames5319def2014-10-23 10:03:10 +01005510void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005511 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5512 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005513}
5514
Calin Juravle2ae48182016-03-16 14:05:09 +00005515void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5516 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005517 return;
5518 }
Artem Serov914d7a82017-02-07 14:33:49 +00005519 {
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01005520 // Ensure that between load and RecordPcInfo there are no pools emitted.
Artem Serov914d7a82017-02-07 14:33:49 +00005521 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5522 Location obj = instruction->GetLocations()->InAt(0);
5523 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5524 RecordPcInfo(instruction, instruction->GetDexPc());
5525 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005526}
5527
Calin Juravle2ae48182016-03-16 14:05:09 +00005528void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005529 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005530 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005531
5532 LocationSummary* locations = instruction->GetLocations();
5533 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005534
5535 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005536}
5537
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005538void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005539 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005540}
5541
Alexandre Rames67555f72014-11-18 10:55:16 +00005542void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5543 HandleBinaryOp(instruction);
5544}
5545
5546void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5547 HandleBinaryOp(instruction);
5548}
5549
Alexandre Rames3e69f162014-12-10 10:36:50 +00005550void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5551 LOG(FATAL) << "Unreachable";
5552}
5553
5554void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005555 if (instruction->GetNext()->IsSuspendCheck() &&
5556 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5557 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5558 // The back edge will generate the suspend check.
5559 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5560 }
5561
Alexandre Rames3e69f162014-12-10 10:36:50 +00005562 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5563}
5564
Alexandre Rames5319def2014-10-23 10:03:10 +01005565void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005566 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005567 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5568 if (location.IsStackSlot()) {
5569 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5570 } else if (location.IsDoubleStackSlot()) {
5571 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5572 }
5573 locations->SetOut(location);
5574}
5575
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005576void InstructionCodeGeneratorARM64::VisitParameterValue(
5577 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005578 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005579}
5580
5581void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5582 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005583 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005584 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005585}
5586
5587void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5588 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5589 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005590}
5591
5592void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005593 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005594 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005595 locations->SetInAt(i, Location::Any());
5596 }
5597 locations->SetOut(Location::Any());
5598}
5599
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005600void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005601 LOG(FATAL) << "Unreachable";
5602}
5603
Serban Constantinescu02164b32014-11-13 14:05:07 +00005604void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005605 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005606 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005607 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005608 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005609 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005610
5611 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005612 case DataType::Type::kInt32:
5613 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005614 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005615 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005616 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5617 break;
5618
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005619 case DataType::Type::kFloat32:
5620 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005621 InvokeRuntimeCallingConvention calling_convention;
5622 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5623 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5624 locations->SetOut(calling_convention.GetReturnLocation(type));
5625
5626 break;
5627 }
5628
Serban Constantinescu02164b32014-11-13 14:05:07 +00005629 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005630 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005631 }
5632}
5633
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005634void InstructionCodeGeneratorARM64::GenerateIntRemForPower2Denom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005635 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005636 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
5637 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
5638
5639 Register out = OutputRegister(instruction);
5640 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005641
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01005642 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01005643 // No need to adjust the result for non-negative dividends or the INT32_MIN/INT64_MIN dividends.
5644 // NOTE: The generated code for HRem correctly works for the INT32_MIN/INT64_MIN dividends.
5645 // INT*_MIN % imm must be 0 for any imm of power 2. 'and' works only with bits
5646 // 0..30 (Int32 case)/0..62 (Int64 case) of a dividend. For INT32_MIN/INT64_MIN they are zeros.
5647 // So 'and' always produces zero.
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005648 __ And(out, dividend, abs_imm - 1);
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01005649 } else {
5650 if (abs_imm == 2) {
5651 __ Cmp(dividend, 0);
5652 __ And(out, dividend, 1);
5653 __ Csneg(out, out, out, ge);
5654 } else {
5655 UseScratchRegisterScope temps(GetVIXLAssembler());
5656 Register temp = temps.AcquireSameSizeAs(out);
5657
5658 __ Negs(temp, dividend);
5659 __ And(out, dividend, abs_imm - 1);
5660 __ And(temp, temp, abs_imm - 1);
5661 __ Csneg(out, out, temp, mi);
5662 }
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005663 }
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005664}
5665
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005666void InstructionCodeGeneratorARM64::GenerateIntRemForConstDenom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005667 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005668
5669 if (imm == 0) {
5670 // Do not generate anything.
5671 // DivZeroCheck would prevent any code to be executed.
5672 return;
5673 }
5674
Evgeny Astigeevichf58dc652018-06-25 17:54:07 +01005675 if (IsPowerOfTwo(AbsOrMin(imm))) {
5676 // Cases imm == -1 or imm == 1 are handled in constant folding by
5677 // InstructionWithAbsorbingInputSimplifier.
5678 // If the cases have survided till code generation they are handled in
5679 // GenerateIntRemForPower2Denom becauses -1 and 1 are the power of 2 (2^0).
5680 // The correct code is generated for them, just more instructions.
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005681 GenerateIntRemForPower2Denom(instruction);
5682 } else {
5683 DCHECK(imm < -2 || imm > 2) << imm;
5684 GenerateDivRemWithAnyConstant(instruction);
5685 }
5686}
5687
5688void InstructionCodeGeneratorARM64::GenerateIntRem(HRem* instruction) {
5689 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
5690 << instruction->GetResultType();
5691
5692 if (instruction->GetLocations()->InAt(1).IsConstant()) {
5693 GenerateIntRemForConstDenom(instruction);
5694 } else {
5695 Register out = OutputRegister(instruction);
5696 Register dividend = InputRegisterAt(instruction, 0);
5697 Register divisor = InputRegisterAt(instruction, 1);
5698 UseScratchRegisterScope temps(GetVIXLAssembler());
5699 Register temp = temps.AcquireSameSizeAs(out);
5700 __ Sdiv(temp, dividend, divisor);
5701 __ Msub(out, temp, divisor, dividend);
5702 }
5703}
5704
Serban Constantinescu02164b32014-11-13 14:05:07 +00005705void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005706 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005707
Serban Constantinescu02164b32014-11-13 14:05:07 +00005708 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005709 case DataType::Type::kInt32:
5710 case DataType::Type::kInt64: {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005711 GenerateIntRem(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005712 break;
5713 }
5714
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005715 case DataType::Type::kFloat32:
5716 case DataType::Type::kFloat64: {
5717 QuickEntrypointEnum entrypoint =
5718 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005719 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005720 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00005721 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5722 } else {
5723 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5724 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005725 break;
5726 }
5727
Serban Constantinescu02164b32014-11-13 14:05:07 +00005728 default:
5729 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005730 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005731 }
5732}
5733
Aart Bik1f8d51b2018-02-15 10:42:37 -08005734void LocationsBuilderARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005735 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005736}
5737
Aart Bik1f8d51b2018-02-15 10:42:37 -08005738void InstructionCodeGeneratorARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005739 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005740}
5741
5742void LocationsBuilderARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005743 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005744}
5745
5746void InstructionCodeGeneratorARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005747 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005748}
5749
Aart Bik3dad3412018-02-28 12:01:46 -08005750void LocationsBuilderARM64::VisitAbs(HAbs* abs) {
5751 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
5752 switch (abs->GetResultType()) {
5753 case DataType::Type::kInt32:
5754 case DataType::Type::kInt64:
5755 locations->SetInAt(0, Location::RequiresRegister());
5756 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5757 break;
5758 case DataType::Type::kFloat32:
5759 case DataType::Type::kFloat64:
5760 locations->SetInAt(0, Location::RequiresFpuRegister());
5761 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5762 break;
5763 default:
5764 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5765 }
5766}
5767
5768void InstructionCodeGeneratorARM64::VisitAbs(HAbs* abs) {
5769 switch (abs->GetResultType()) {
5770 case DataType::Type::kInt32:
5771 case DataType::Type::kInt64: {
5772 Register in_reg = InputRegisterAt(abs, 0);
5773 Register out_reg = OutputRegister(abs);
5774 __ Cmp(in_reg, Operand(0));
5775 __ Cneg(out_reg, in_reg, lt);
5776 break;
5777 }
5778 case DataType::Type::kFloat32:
5779 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01005780 VRegister in_reg = InputFPRegisterAt(abs, 0);
5781 VRegister out_reg = OutputFPRegister(abs);
Aart Bik3dad3412018-02-28 12:01:46 -08005782 __ Fabs(out_reg, in_reg);
5783 break;
5784 }
5785 default:
5786 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5787 }
5788}
5789
Igor Murashkind01745e2017-04-05 16:40:31 -07005790void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5791 constructor_fence->SetLocations(nullptr);
5792}
5793
5794void InstructionCodeGeneratorARM64::VisitConstructorFence(
5795 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5796 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5797}
5798
Calin Juravle27df7582015-04-17 19:12:31 +01005799void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5800 memory_barrier->SetLocations(nullptr);
5801}
5802
5803void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005804 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005805}
5806
Alexandre Rames5319def2014-10-23 10:03:10 +01005807void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005808 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005809 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005810 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005811}
5812
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00005813void InstructionCodeGeneratorARM64::VisitReturn(HReturn* ret) {
5814 if (GetGraph()->IsCompilingOsr()) {
5815 // To simplify callers of an OSR method, we put the return value in both
5816 // floating point and core register.
5817 switch (ret->InputAt(0)->GetType()) {
5818 case DataType::Type::kFloat32:
5819 __ Fmov(w0, s0);
5820 break;
5821 case DataType::Type::kFloat64:
5822 __ Fmov(x0, d0);
5823 break;
5824 default:
5825 break;
5826 }
5827 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005828 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005829}
5830
5831void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5832 instruction->SetLocations(nullptr);
5833}
5834
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005835void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005836 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005837}
5838
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005839void LocationsBuilderARM64::VisitRor(HRor* ror) {
5840 HandleBinaryOp(ror);
5841}
5842
5843void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5844 HandleBinaryOp(ror);
5845}
5846
Serban Constantinescu02164b32014-11-13 14:05:07 +00005847void LocationsBuilderARM64::VisitShl(HShl* shl) {
5848 HandleShift(shl);
5849}
5850
5851void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5852 HandleShift(shl);
5853}
5854
5855void LocationsBuilderARM64::VisitShr(HShr* shr) {
5856 HandleShift(shr);
5857}
5858
5859void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5860 HandleShift(shr);
5861}
5862
Alexandre Rames5319def2014-10-23 10:03:10 +01005863void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005864 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005865}
5866
5867void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005868 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005869}
5870
Alexandre Rames67555f72014-11-18 10:55:16 +00005871void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005872 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005873}
5874
5875void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005876 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005877}
5878
5879void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005880 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005881}
5882
Alexandre Rames67555f72014-11-18 10:55:16 +00005883void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005884 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005885}
5886
Vladimir Marko552a1342017-10-31 10:56:47 +00005887void LocationsBuilderARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5888 codegen_->CreateStringBuilderAppendLocations(instruction, LocationFrom(x0));
5889}
5890
5891void InstructionCodeGeneratorARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5892 __ Mov(w0, instruction->GetFormat()->GetValue());
5893 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
5894}
5895
Calin Juravlee460d1d2015-09-29 04:52:17 +01005896void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5897 HUnresolvedInstanceFieldGet* instruction) {
5898 FieldAccessCallingConventionARM64 calling_convention;
5899 codegen_->CreateUnresolvedFieldLocationSummary(
5900 instruction, instruction->GetFieldType(), calling_convention);
5901}
5902
5903void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5904 HUnresolvedInstanceFieldGet* instruction) {
5905 FieldAccessCallingConventionARM64 calling_convention;
5906 codegen_->GenerateUnresolvedFieldAccess(instruction,
5907 instruction->GetFieldType(),
5908 instruction->GetFieldIndex(),
5909 instruction->GetDexPc(),
5910 calling_convention);
5911}
5912
5913void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5914 HUnresolvedInstanceFieldSet* instruction) {
5915 FieldAccessCallingConventionARM64 calling_convention;
5916 codegen_->CreateUnresolvedFieldLocationSummary(
5917 instruction, instruction->GetFieldType(), calling_convention);
5918}
5919
5920void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5921 HUnresolvedInstanceFieldSet* instruction) {
5922 FieldAccessCallingConventionARM64 calling_convention;
5923 codegen_->GenerateUnresolvedFieldAccess(instruction,
5924 instruction->GetFieldType(),
5925 instruction->GetFieldIndex(),
5926 instruction->GetDexPc(),
5927 calling_convention);
5928}
5929
5930void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5931 HUnresolvedStaticFieldGet* instruction) {
5932 FieldAccessCallingConventionARM64 calling_convention;
5933 codegen_->CreateUnresolvedFieldLocationSummary(
5934 instruction, instruction->GetFieldType(), calling_convention);
5935}
5936
5937void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5938 HUnresolvedStaticFieldGet* instruction) {
5939 FieldAccessCallingConventionARM64 calling_convention;
5940 codegen_->GenerateUnresolvedFieldAccess(instruction,
5941 instruction->GetFieldType(),
5942 instruction->GetFieldIndex(),
5943 instruction->GetDexPc(),
5944 calling_convention);
5945}
5946
5947void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5948 HUnresolvedStaticFieldSet* instruction) {
5949 FieldAccessCallingConventionARM64 calling_convention;
5950 codegen_->CreateUnresolvedFieldLocationSummary(
5951 instruction, instruction->GetFieldType(), calling_convention);
5952}
5953
5954void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5955 HUnresolvedStaticFieldSet* instruction) {
5956 FieldAccessCallingConventionARM64 calling_convention;
5957 codegen_->GenerateUnresolvedFieldAccess(instruction,
5958 instruction->GetFieldType(),
5959 instruction->GetFieldIndex(),
5960 instruction->GetDexPc(),
5961 calling_convention);
5962}
5963
Alexandre Rames5319def2014-10-23 10:03:10 +01005964void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005965 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5966 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005967 // In suspend check slow path, usually there are no caller-save registers at all.
5968 // If SIMD instructions are present, however, we force spilling all live SIMD
5969 // registers in full width (since the runtime only saves/restores lower part).
5970 locations->SetCustomSlowPathCallerSaves(
5971 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005972}
5973
5974void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005975 HBasicBlock* block = instruction->GetBlock();
5976 if (block->GetLoopInformation() != nullptr) {
5977 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5978 // The back edge will generate the suspend check.
5979 return;
5980 }
5981 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5982 // The goto will generate the suspend check.
5983 return;
5984 }
5985 GenerateSuspendCheck(instruction, nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08005986 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005987}
5988
Alexandre Rames67555f72014-11-18 10:55:16 +00005989void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005990 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5991 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005992 InvokeRuntimeCallingConvention calling_convention;
5993 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5994}
5995
5996void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005997 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005998 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005999}
6000
6001void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
6002 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006003 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006004 DataType::Type input_type = conversion->GetInputType();
6005 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006006 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6007 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006008 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
6009 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006010 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6011 }
6012
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006013 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006014 locations->SetInAt(0, Location::RequiresFpuRegister());
6015 } else {
6016 locations->SetInAt(0, Location::RequiresRegister());
6017 }
6018
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006019 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006020 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6021 } else {
6022 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6023 }
6024}
6025
6026void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006027 DataType::Type result_type = conversion->GetResultType();
6028 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00006029
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006030 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6031 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006032
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006033 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
6034 int result_size = DataType::Size(result_type);
6035 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00006036 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006037 Register output = OutputRegister(conversion);
6038 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006039 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01006040 // 'int' values are used directly as W registers, discarding the top
6041 // bits, so we don't need to sign-extend and can just perform a move.
6042 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
6043 // top 32 bits of the target register. We theoretically could leave those
6044 // bits unchanged, but we would have to make sure that no code uses a
6045 // 32bit input value as a 64bit value assuming that the top 32 bits are
6046 // zero.
6047 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006048 } else if (DataType::IsUnsignedType(result_type) ||
6049 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
6050 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006051 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00006052 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006053 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006054 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006055 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006056 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6057 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006058 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006059 } else if (DataType::IsFloatingPointType(result_type) &&
6060 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006061 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
6062 } else {
6063 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6064 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006065 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00006066}
Alexandre Rames67555f72014-11-18 10:55:16 +00006067
Serban Constantinescu02164b32014-11-13 14:05:07 +00006068void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
6069 HandleShift(ushr);
6070}
6071
6072void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
6073 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00006074}
6075
6076void LocationsBuilderARM64::VisitXor(HXor* instruction) {
6077 HandleBinaryOp(instruction);
6078}
6079
6080void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
6081 HandleBinaryOp(instruction);
6082}
6083
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006084void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006085 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006086 LOG(FATAL) << "Unreachable";
6087}
6088
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006089void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006090 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006091 LOG(FATAL) << "Unreachable";
6092}
6093
Mark Mendellfe57faa2015-09-18 09:26:15 -04006094// Simple implementation of packed switch - generate cascaded compare/jumps.
6095void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6096 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006097 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006098 locations->SetInAt(0, Location::RequiresRegister());
6099}
6100
6101void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6102 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08006103 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006104 Register value_reg = InputRegisterAt(switch_instr, 0);
6105 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6106
Zheng Xu3927c8b2015-11-18 17:46:25 +08006107 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01006108 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08006109 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
6110 // make sure we don't emit it if the target may run out of range.
6111 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
6112 // ranges and emit the tables only as required.
6113 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04006114
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006115 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08006116 // Current instruction id is an upper bound of the number of HIRs in the graph.
6117 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
6118 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006119 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6120 Register temp = temps.AcquireW();
6121 __ Subs(temp, value_reg, Operand(lower_bound));
6122
Zheng Xu3927c8b2015-11-18 17:46:25 +08006123 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006124 // Jump to successors[0] if value == lower_bound.
6125 __ B(eq, codegen_->GetLabelOf(successors[0]));
6126 int32_t last_index = 0;
6127 for (; num_entries - last_index > 2; last_index += 2) {
6128 __ Subs(temp, temp, Operand(2));
6129 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6130 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
6131 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6132 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
6133 }
6134 if (num_entries - last_index == 2) {
6135 // The last missing case_value.
6136 __ Cmp(temp, Operand(1));
6137 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08006138 }
6139
6140 // And the default for any other value.
6141 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6142 __ B(codegen_->GetLabelOf(default_block));
6143 }
6144 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01006145 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08006146
6147 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6148
6149 // Below instructions should use at most one blocked register. Since there are two blocked
6150 // registers, we are free to block one.
6151 Register temp_w = temps.AcquireW();
6152 Register index;
6153 // Remove the bias.
6154 if (lower_bound != 0) {
6155 index = temp_w;
6156 __ Sub(index, value_reg, Operand(lower_bound));
6157 } else {
6158 index = value_reg;
6159 }
6160
6161 // Jump to default block if index is out of the range.
6162 __ Cmp(index, Operand(num_entries));
6163 __ B(hs, codegen_->GetLabelOf(default_block));
6164
6165 // In current VIXL implementation, it won't require any blocked registers to encode the
6166 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
6167 // register pressure.
6168 Register table_base = temps.AcquireX();
6169 // Load jump offset from the table.
6170 __ Adr(table_base, jump_table->GetTableStartLabel());
6171 Register jump_offset = temp_w;
6172 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
6173
6174 // Jump to target block by branching to table_base(pc related) + offset.
6175 Register target_address = table_base;
6176 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
6177 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006178 }
6179}
6180
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006181void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
6182 HInstruction* instruction,
6183 Location out,
6184 uint32_t offset,
6185 Location maybe_temp,
6186 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006187 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006188 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006189 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006190 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006191 if (kUseBakerReadBarrier) {
6192 // Load with fast path based Baker's read barrier.
6193 // /* HeapReference<Object> */ out = *(out + offset)
6194 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6195 out,
6196 out_reg,
6197 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006198 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006199 /* needs_null_check= */ false,
6200 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006201 } else {
6202 // Load with slow path based read barrier.
6203 // Save the value of `out` into `maybe_temp` before overwriting it
6204 // in the following move operation, as we will need it for the
6205 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006206 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00006207 __ Mov(temp_reg, out_reg);
6208 // /* HeapReference<Object> */ out = *(out + offset)
6209 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6210 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6211 }
6212 } else {
6213 // Plain load with no read barrier.
6214 // /* HeapReference<Object> */ out = *(out + offset)
6215 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6216 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6217 }
6218}
6219
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006220void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
6221 HInstruction* instruction,
6222 Location out,
6223 Location obj,
6224 uint32_t offset,
6225 Location maybe_temp,
6226 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006227 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006228 Register out_reg = RegisterFrom(out, type);
6229 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006230 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006231 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006232 if (kUseBakerReadBarrier) {
6233 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00006234 // /* HeapReference<Object> */ out = *(obj + offset)
6235 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6236 out,
6237 obj_reg,
6238 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006239 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006240 /* needs_null_check= */ false,
6241 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006242 } else {
6243 // Load with slow path based read barrier.
6244 // /* HeapReference<Object> */ out = *(obj + offset)
6245 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6246 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6247 }
6248 } else {
6249 // Plain load with no read barrier.
6250 // /* HeapReference<Object> */ out = *(obj + offset)
6251 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6252 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6253 }
6254}
6255
Vladimir Markoca1e0382018-04-11 09:58:41 +00006256void CodeGeneratorARM64::GenerateGcRootFieldLoad(
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006257 HInstruction* instruction,
6258 Location root,
6259 Register obj,
6260 uint32_t offset,
6261 vixl::aarch64::Label* fixup_label,
6262 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006263 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006264 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006265 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006266 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006267 if (kUseBakerReadBarrier) {
6268 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00006269 // Baker's read barrier are used.
Roland Levillain44015862016-01-22 11:47:17 +00006270
Vladimir Marko008e09f32018-08-06 15:42:43 +01006271 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6272 // the Marking Register) to decide whether we need to enter
6273 // the slow path to mark the GC root.
6274 //
6275 // We use shared thunks for the slow path; shared within the method
6276 // for JIT, across methods for AOT. That thunk checks the reference
6277 // and jumps to the entrypoint if needed.
6278 //
6279 // lr = &return_address;
6280 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6281 // if (mr) { // Thread::Current()->GetIsGcMarking()
6282 // goto gc_root_thunk<root_reg>(lr)
6283 // }
6284 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00006285
Vladimir Marko008e09f32018-08-06 15:42:43 +01006286 UseScratchRegisterScope temps(GetVIXLAssembler());
6287 DCHECK(temps.IsAvailable(ip0));
6288 DCHECK(temps.IsAvailable(ip1));
6289 temps.Exclude(ip0, ip1);
6290 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
Roland Levillain44015862016-01-22 11:47:17 +00006291
Vladimir Marko008e09f32018-08-06 15:42:43 +01006292 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6293 vixl::aarch64::Label return_address;
6294 __ adr(lr, &return_address);
6295 if (fixup_label != nullptr) {
6296 __ bind(fixup_label);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006297 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006298 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
Vladimir Marko94796f82018-08-08 15:15:33 +01006299 "GC root LDR must be 2 instructions (8B) before the return address label.");
Vladimir Marko008e09f32018-08-06 15:42:43 +01006300 __ ldr(root_reg, MemOperand(obj.X(), offset));
6301 EmitBakerReadBarrierCbnz(custom_data);
6302 __ bind(&return_address);
Roland Levillain44015862016-01-22 11:47:17 +00006303 } else {
6304 // GC root loaded through a slow path for read barriers other
6305 // than Baker's.
6306 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006307 if (fixup_label == nullptr) {
6308 __ Add(root_reg.X(), obj.X(), offset);
6309 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006310 EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006311 }
Roland Levillain44015862016-01-22 11:47:17 +00006312 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00006313 GenerateReadBarrierForRootSlow(instruction, root, root);
Roland Levillain44015862016-01-22 11:47:17 +00006314 }
6315 } else {
6316 // Plain GC root load with no read barrier.
6317 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006318 if (fixup_label == nullptr) {
6319 __ Ldr(root_reg, MemOperand(obj, offset));
6320 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006321 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006322 }
Roland Levillain44015862016-01-22 11:47:17 +00006323 // Note that GC roots are not affected by heap poisoning, thus we
6324 // do not have to unpoison `root_reg` here.
6325 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006326 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00006327}
6328
Vladimir Marko94796f82018-08-08 15:15:33 +01006329void CodeGeneratorARM64::GenerateUnsafeCasOldValueMovWithBakerReadBarrier(
6330 vixl::aarch64::Register marked,
6331 vixl::aarch64::Register old_value) {
6332 DCHECK(kEmitCompilerReadBarrier);
6333 DCHECK(kUseBakerReadBarrier);
6334
6335 // Similar to the Baker RB path in GenerateGcRootFieldLoad(), with a MOV instead of LDR.
6336 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(marked.GetCode());
6337
6338 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6339 vixl::aarch64::Label return_address;
6340 __ adr(lr, &return_address);
6341 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6342 "GC root LDR must be 2 instructions (8B) before the return address label.");
6343 __ mov(marked, old_value);
6344 EmitBakerReadBarrierCbnz(custom_data);
6345 __ bind(&return_address);
6346}
6347
Roland Levillain44015862016-01-22 11:47:17 +00006348void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6349 Location ref,
Vladimir Marko248141f2018-08-10 10:40:07 +01006350 vixl::aarch64::Register obj,
6351 const vixl::aarch64::MemOperand& src,
Roland Levillain44015862016-01-22 11:47:17 +00006352 bool needs_null_check,
6353 bool use_load_acquire) {
6354 DCHECK(kEmitCompilerReadBarrier);
6355 DCHECK(kUseBakerReadBarrier);
6356
Vladimir Marko0ecac682018-08-07 10:40:38 +01006357 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6358 // Marking Register) to decide whether we need to enter the slow
6359 // path to mark the reference. Then, in the slow path, check the
6360 // gray bit in the lock word of the reference's holder (`obj`) to
6361 // decide whether to mark `ref` or not.
6362 //
6363 // We use shared thunks for the slow path; shared within the method
6364 // for JIT, across methods for AOT. That thunk checks the holder
6365 // and jumps to the entrypoint if needed. If the holder is not gray,
6366 // it creates a fake dependency and returns to the LDR instruction.
6367 //
6368 // lr = &gray_return_address;
6369 // if (mr) { // Thread::Current()->GetIsGcMarking()
6370 // goto field_thunk<holder_reg, base_reg, use_load_acquire>(lr)
6371 // }
6372 // not_gray_return_address:
6373 // // Original reference load. If the offset is too large to fit
6374 // // into LDR, we use an adjusted base register here.
6375 // HeapReference<mirror::Object> reference = *(obj+offset);
6376 // gray_return_address:
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006377
Vladimir Marko248141f2018-08-10 10:40:07 +01006378 DCHECK(src.GetAddrMode() == vixl::aarch64::Offset);
6379 DCHECK_ALIGNED(src.GetOffset(), sizeof(mirror::HeapReference<mirror::Object>));
6380
6381 UseScratchRegisterScope temps(GetVIXLAssembler());
6382 DCHECK(temps.IsAvailable(ip0));
6383 DCHECK(temps.IsAvailable(ip1));
6384 temps.Exclude(ip0, ip1);
6385 uint32_t custom_data = use_load_acquire
6386 ? EncodeBakerReadBarrierAcquireData(src.GetBaseRegister().GetCode(), obj.GetCode())
6387 : EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode());
6388
6389 {
6390 ExactAssemblyScope guard(GetVIXLAssembler(),
6391 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6392 vixl::aarch64::Label return_address;
6393 __ adr(lr, &return_address);
6394 EmitBakerReadBarrierCbnz(custom_data);
6395 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6396 "Field LDR must be 1 instruction (4B) before the return address label; "
6397 " 2 instructions (8B) for heap poisoning.");
6398 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
6399 if (use_load_acquire) {
6400 DCHECK_EQ(src.GetOffset(), 0);
6401 __ ldar(ref_reg, src);
6402 } else {
6403 __ ldr(ref_reg, src);
6404 }
6405 if (needs_null_check) {
6406 MaybeRecordImplicitNullCheck(instruction);
6407 }
6408 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6409 // macro instructions disallowed in ExactAssemblyScope.
6410 if (kPoisonHeapReferences) {
6411 __ neg(ref_reg, Operand(ref_reg));
6412 }
6413 __ bind(&return_address);
6414 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006415 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Vladimir Marko248141f2018-08-10 10:40:07 +01006416}
6417
6418void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6419 Location ref,
6420 Register obj,
6421 uint32_t offset,
6422 Location maybe_temp,
6423 bool needs_null_check,
6424 bool use_load_acquire) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006425 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6426 Register base = obj;
6427 if (use_load_acquire) {
6428 DCHECK(maybe_temp.IsRegister());
6429 base = WRegisterFrom(maybe_temp);
6430 __ Add(base, obj, offset);
6431 offset = 0u;
6432 } else if (offset >= kReferenceLoadMinFarOffset) {
6433 DCHECK(maybe_temp.IsRegister());
6434 base = WRegisterFrom(maybe_temp);
6435 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6436 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6437 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006438 }
Vladimir Marko248141f2018-08-10 10:40:07 +01006439 MemOperand src(base.X(), offset);
6440 GenerateFieldLoadWithBakerReadBarrier(
6441 instruction, ref, obj, src, needs_null_check, use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006442}
6443
Artem Serov0806f582018-10-11 20:14:20 +01006444void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HArrayGet* instruction,
6445 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006446 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006447 uint32_t data_offset,
6448 Location index,
Roland Levillain44015862016-01-22 11:47:17 +00006449 bool needs_null_check) {
6450 DCHECK(kEmitCompilerReadBarrier);
6451 DCHECK(kUseBakerReadBarrier);
6452
Vladimir Marko66d691d2017-04-07 17:53:39 +01006453 static_assert(
6454 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6455 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006456 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006457
Vladimir Marko008e09f32018-08-06 15:42:43 +01006458 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6459 // Marking Register) to decide whether we need to enter the slow
6460 // path to mark the reference. Then, in the slow path, check the
6461 // gray bit in the lock word of the reference's holder (`obj`) to
6462 // decide whether to mark `ref` or not.
6463 //
6464 // We use shared thunks for the slow path; shared within the method
6465 // for JIT, across methods for AOT. That thunk checks the holder
6466 // and jumps to the entrypoint if needed. If the holder is not gray,
6467 // it creates a fake dependency and returns to the LDR instruction.
6468 //
6469 // lr = &gray_return_address;
6470 // if (mr) { // Thread::Current()->GetIsGcMarking()
6471 // goto array_thunk<base_reg>(lr)
6472 // }
6473 // not_gray_return_address:
6474 // // Original reference load. If the offset is too large to fit
6475 // // into LDR, we use an adjusted base register here.
6476 // HeapReference<mirror::Object> reference = data[index];
6477 // gray_return_address:
Vladimir Marko66d691d2017-04-07 17:53:39 +01006478
Vladimir Marko008e09f32018-08-06 15:42:43 +01006479 DCHECK(index.IsValid());
6480 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
6481 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006482
Vladimir Marko008e09f32018-08-06 15:42:43 +01006483 UseScratchRegisterScope temps(GetVIXLAssembler());
6484 DCHECK(temps.IsAvailable(ip0));
6485 DCHECK(temps.IsAvailable(ip1));
6486 temps.Exclude(ip0, ip1);
Artem Serov0806f582018-10-11 20:14:20 +01006487
6488 Register temp;
6489 if (instruction->GetArray()->IsIntermediateAddress()) {
6490 // We do not need to compute the intermediate address from the array: the
6491 // input instruction has done it already. See the comment in
6492 // `TryExtractArrayAccessAddress()`.
6493 if (kIsDebugBuild) {
6494 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
6495 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
6496 }
6497 temp = obj;
6498 } else {
6499 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
6500 __ Add(temp.X(), obj.X(), Operand(data_offset));
6501 }
6502
Vladimir Marko008e09f32018-08-06 15:42:43 +01006503 uint32_t custom_data = EncodeBakerReadBarrierArrayData(temp.GetCode());
Vladimir Marko66d691d2017-04-07 17:53:39 +01006504
Vladimir Marko008e09f32018-08-06 15:42:43 +01006505 {
6506 ExactAssemblyScope guard(GetVIXLAssembler(),
6507 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6508 vixl::aarch64::Label return_address;
6509 __ adr(lr, &return_address);
6510 EmitBakerReadBarrierCbnz(custom_data);
6511 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6512 "Array LDR must be 1 instruction (4B) before the return address label; "
6513 " 2 instructions (8B) for heap poisoning.");
6514 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6515 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6516 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6517 // macro instructions disallowed in ExactAssemblyScope.
6518 if (kPoisonHeapReferences) {
6519 __ neg(ref_reg, Operand(ref_reg));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006520 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006521 __ bind(&return_address);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006522 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006523 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Roland Levillain44015862016-01-22 11:47:17 +00006524}
6525
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006526void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6527 // The following condition is a compile-time one, so it does not have a run-time cost.
6528 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
6529 // The following condition is a run-time one; it is executed after the
6530 // previous compile-time test, to avoid penalizing non-debug builds.
6531 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6532 UseScratchRegisterScope temps(GetVIXLAssembler());
6533 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6534 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6535 }
6536 }
6537}
6538
Roland Levillain44015862016-01-22 11:47:17 +00006539void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6540 Location out,
6541 Location ref,
6542 Location obj,
6543 uint32_t offset,
6544 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006545 DCHECK(kEmitCompilerReadBarrier);
6546
Roland Levillain44015862016-01-22 11:47:17 +00006547 // Insert a slow path based read barrier *after* the reference load.
6548 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006549 // If heap poisoning is enabled, the unpoisoning of the loaded
6550 // reference will be carried out by the runtime within the slow
6551 // path.
6552 //
6553 // Note that `ref` currently does not get unpoisoned (when heap
6554 // poisoning is enabled), which is alright as the `ref` argument is
6555 // not used by the artReadBarrierSlow entry point.
6556 //
6557 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006558 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006559 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6560 AddSlowPath(slow_path);
6561
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006562 __ B(slow_path->GetEntryLabel());
6563 __ Bind(slow_path->GetExitLabel());
6564}
6565
Roland Levillain44015862016-01-22 11:47:17 +00006566void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6567 Location out,
6568 Location ref,
6569 Location obj,
6570 uint32_t offset,
6571 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006572 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006573 // Baker's read barriers shall be handled by the fast path
6574 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6575 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006576 // If heap poisoning is enabled, unpoisoning will be taken care of
6577 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006578 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006579 } else if (kPoisonHeapReferences) {
6580 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6581 }
6582}
6583
Roland Levillain44015862016-01-22 11:47:17 +00006584void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6585 Location out,
6586 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006587 DCHECK(kEmitCompilerReadBarrier);
6588
Roland Levillain44015862016-01-22 11:47:17 +00006589 // Insert a slow path based read barrier *after* the GC root load.
6590 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006591 // Note that GC roots are not affected by heap poisoning, so we do
6592 // not need to do anything special for this here.
6593 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006594 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006595 AddSlowPath(slow_path);
6596
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006597 __ B(slow_path->GetEntryLabel());
6598 __ Bind(slow_path->GetExitLabel());
6599}
6600
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006601void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6602 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006603 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006604 locations->SetInAt(0, Location::RequiresRegister());
6605 locations->SetOut(Location::RequiresRegister());
6606}
6607
6608void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6609 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006610 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006611 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006612 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006613 __ Ldr(XRegisterFrom(locations->Out()),
6614 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006615 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006616 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006617 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006618 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6619 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006620 __ Ldr(XRegisterFrom(locations->Out()),
6621 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006622 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006623}
6624
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006625static void PatchJitRootUse(uint8_t* code,
6626 const uint8_t* roots_data,
6627 vixl::aarch64::Literal<uint32_t>* literal,
6628 uint64_t index_in_table) {
6629 uint32_t literal_offset = literal->GetOffset();
6630 uintptr_t address =
6631 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6632 uint8_t* data = code + literal_offset;
6633 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6634}
6635
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006636void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6637 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006638 const StringReference& string_reference = entry.first;
6639 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006640 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006641 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006642 }
6643 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006644 const TypeReference& type_reference = entry.first;
6645 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006646 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006647 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006648 }
6649}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006650
Artem Serov1a719e42019-07-18 14:24:55 +01006651MemOperand InstructionCodeGeneratorARM64::VecNeonAddress(
6652 HVecMemoryOperation* instruction,
6653 UseScratchRegisterScope* temps_scope,
6654 size_t size,
6655 bool is_string_char_at,
6656 /*out*/ Register* scratch) {
6657 LocationSummary* locations = instruction->GetLocations();
6658 Register base = InputRegisterAt(instruction, 0);
6659
6660 if (instruction->InputAt(1)->IsIntermediateAddressIndex()) {
6661 DCHECK(!is_string_char_at);
6662 return MemOperand(base.X(), InputRegisterAt(instruction, 1).X());
6663 }
6664
6665 Location index = locations->InAt(1);
6666 uint32_t offset = is_string_char_at
6667 ? mirror::String::ValueOffset().Uint32Value()
6668 : mirror::Array::DataOffset(size).Uint32Value();
6669 size_t shift = ComponentSizeShiftWidth(size);
6670
6671 // HIntermediateAddress optimization is only applied for scalar ArrayGet and ArraySet.
6672 DCHECK(!instruction->InputAt(0)->IsIntermediateAddress());
6673
6674 if (index.IsConstant()) {
6675 offset += Int64FromLocation(index) << shift;
6676 return HeapOperand(base, offset);
6677 } else {
6678 *scratch = temps_scope->AcquireSameSizeAs(base);
6679 __ Add(*scratch, base, Operand(WRegisterFrom(index), LSL, shift));
6680 return HeapOperand(*scratch, offset);
6681 }
6682}
6683
Alexandre Rames67555f72014-11-18 10:55:16 +00006684#undef __
6685#undef QUICK_ENTRY_POINT
6686
Vladimir Markoca1e0382018-04-11 09:58:41 +00006687#define __ assembler.GetVIXLAssembler()->
6688
6689static void EmitGrayCheckAndFastPath(arm64::Arm64Assembler& assembler,
6690 vixl::aarch64::Register base_reg,
6691 vixl::aarch64::MemOperand& lock_word,
Vladimir Marko7a695052018-04-12 10:26:50 +01006692 vixl::aarch64::Label* slow_path,
6693 vixl::aarch64::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006694 // Load the lock word containing the rb_state.
6695 __ Ldr(ip0.W(), lock_word);
6696 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01006697 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Vladimir Markoca1e0382018-04-11 09:58:41 +00006698 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6699 __ Tbnz(ip0.W(), LockWord::kReadBarrierStateShift, slow_path);
6700 static_assert(
6701 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET,
6702 "Field and array LDR offsets must be the same to reuse the same code.");
Vladimir Marko7a695052018-04-12 10:26:50 +01006703 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
6704 if (throw_npe != nullptr) {
6705 __ Bind(throw_npe);
6706 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006707 // Adjust the return address back to the LDR (1 instruction; 2 for heap poisoning).
6708 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6709 "Field LDR must be 1 instruction (4B) before the return address label; "
6710 " 2 instructions (8B) for heap poisoning.");
6711 __ Add(lr, lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6712 // Introduce a dependency on the lock_word including rb_state,
6713 // to prevent load-load reordering, and without using
6714 // a memory barrier (which would be more expensive).
6715 __ Add(base_reg, base_reg, Operand(ip0, LSR, 32));
6716 __ Br(lr); // And return back to the function.
6717 // Note: The fake dependency is unnecessary for the slow path.
6718}
6719
6720// Load the read barrier introspection entrypoint in register `entrypoint`.
6721static void LoadReadBarrierMarkIntrospectionEntrypoint(arm64::Arm64Assembler& assembler,
6722 vixl::aarch64::Register entrypoint) {
6723 // entrypoint = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6724 DCHECK_EQ(ip0.GetCode(), 16u);
6725 const int32_t entry_point_offset =
6726 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6727 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
6728}
6729
6730void CodeGeneratorARM64::CompileBakerReadBarrierThunk(Arm64Assembler& assembler,
6731 uint32_t encoded_data,
6732 /*out*/ std::string* debug_name) {
6733 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
6734 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006735 case BakerReadBarrierKind::kField:
6736 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006737 auto base_reg =
6738 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6739 CheckValidReg(base_reg.GetCode());
6740 auto holder_reg =
6741 Register::GetXRegFromCode(BakerReadBarrierSecondRegField::Decode(encoded_data));
6742 CheckValidReg(holder_reg.GetCode());
6743 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6744 temps.Exclude(ip0, ip1);
Roland Levillain988c3912019-09-25 19:33:35 +01006745 // In the case of a field load (with relaxed semantic), if `base_reg` differs from
6746 // `holder_reg`, the offset was too large and we must have emitted (during the construction
6747 // of the HIR graph, see `art::HInstructionBuilder::BuildInstanceFieldAccess`) and preserved
6748 // (see `art::PrepareForRegisterAllocation::VisitNullCheck`) an explicit null check before
6749 // the load. Otherwise, for implicit null checks, we need to null-check the holder as we do
6750 // not necessarily do that check before going to the thunk.
6751 //
6752 // In the case of a field load with load-acquire semantics (where `base_reg` always differs
6753 // from `holder_reg`), we also need an explicit null check when implicit null checks are
6754 // allowed, as we do not emit one before going to the thunk.
Vladimir Marko7a695052018-04-12 10:26:50 +01006755 vixl::aarch64::Label throw_npe_label;
6756 vixl::aarch64::Label* throw_npe = nullptr;
Roland Levillain988c3912019-09-25 19:33:35 +01006757 if (GetCompilerOptions().GetImplicitNullChecks() &&
6758 (holder_reg.Is(base_reg) || (kind == BakerReadBarrierKind::kAcquire))) {
Vladimir Marko7a695052018-04-12 10:26:50 +01006759 throw_npe = &throw_npe_label;
6760 __ Cbz(holder_reg.W(), throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006761 }
Vladimir Marko7a695052018-04-12 10:26:50 +01006762 // Check if the holder is gray and, if not, add fake dependency to the base register
6763 // and return to the LDR instruction to load the reference. Otherwise, use introspection
6764 // to load the reference and call the entrypoint that performs further checks on the
6765 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006766 vixl::aarch64::Label slow_path;
6767 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
Vladimir Marko7a695052018-04-12 10:26:50 +01006768 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006769 __ Bind(&slow_path);
Vladimir Marko0ecac682018-08-07 10:40:38 +01006770 if (kind == BakerReadBarrierKind::kField) {
6771 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6772 __ Ldr(ip0.W(), ldr_address); // Load the LDR (immediate) unsigned offset.
6773 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6774 __ Ubfx(ip0.W(), ip0.W(), 10, 12); // Extract the offset.
6775 __ Ldr(ip0.W(), MemOperand(base_reg, ip0, LSL, 2)); // Load the reference.
6776 } else {
6777 DCHECK(kind == BakerReadBarrierKind::kAcquire);
6778 DCHECK(!base_reg.Is(holder_reg));
6779 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6780 __ Ldar(ip0.W(), MemOperand(base_reg));
6781 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006782 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
6783 __ Br(ip1); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006784 break;
6785 }
6786 case BakerReadBarrierKind::kArray: {
6787 auto base_reg =
6788 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6789 CheckValidReg(base_reg.GetCode());
6790 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6791 BakerReadBarrierSecondRegField::Decode(encoded_data));
6792 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6793 temps.Exclude(ip0, ip1);
6794 vixl::aarch64::Label slow_path;
6795 int32_t data_offset =
6796 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
6797 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
6798 DCHECK_LT(lock_word.GetOffset(), 0);
6799 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path);
6800 __ Bind(&slow_path);
6801 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
6802 __ Ldr(ip0.W(), ldr_address); // Load the LDR (register) unsigned offset.
6803 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6804 __ Ubfx(ip0, ip0, 16, 6); // Extract the index register, plus 32 (bit 21 is set).
6805 __ Bfi(ip1, ip0, 3, 6); // Insert ip0 to the entrypoint address to create
6806 // a switch case target based on the index register.
6807 __ Mov(ip0, base_reg); // Move the base register to ip0.
6808 __ Br(ip1); // Jump to the entrypoint's array switch case.
6809 break;
6810 }
6811 case BakerReadBarrierKind::kGcRoot: {
6812 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
6813 // and it does not have a forwarding address), call the correct introspection entrypoint;
6814 // otherwise return the reference (or the extracted forwarding address).
6815 // There is no gray bit check for GC roots.
6816 auto root_reg =
6817 Register::GetWRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6818 CheckValidReg(root_reg.GetCode());
6819 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6820 BakerReadBarrierSecondRegField::Decode(encoded_data));
6821 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6822 temps.Exclude(ip0, ip1);
6823 vixl::aarch64::Label return_label, not_marked, forwarding_address;
6824 __ Cbz(root_reg, &return_label);
6825 MemOperand lock_word(root_reg.X(), mirror::Object::MonitorOffset().Int32Value());
6826 __ Ldr(ip0.W(), lock_word);
6827 __ Tbz(ip0.W(), LockWord::kMarkBitStateShift, &not_marked);
6828 __ Bind(&return_label);
6829 __ Br(lr);
6830 __ Bind(&not_marked);
6831 __ Tst(ip0.W(), Operand(ip0.W(), LSL, 1));
6832 __ B(&forwarding_address, mi);
6833 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6834 // Adjust the art_quick_read_barrier_mark_introspection address in IP1 to
6835 // art_quick_read_barrier_mark_introspection_gc_roots.
6836 __ Add(ip1, ip1, Operand(BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRYPOINT_OFFSET));
6837 __ Mov(ip0.W(), root_reg);
6838 __ Br(ip1);
6839 __ Bind(&forwarding_address);
6840 __ Lsl(root_reg, ip0.W(), LockWord::kForwardingAddressShift);
6841 __ Br(lr);
6842 break;
6843 }
6844 default:
6845 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
6846 UNREACHABLE();
6847 }
6848
Vladimir Marko966b46f2018-08-03 10:20:19 +00006849 // For JIT, the slow path is considered part of the compiled method,
Vladimir Markof91fc122020-05-13 09:21:00 +01006850 // so JIT should pass null as `debug_name`.
Vladimir Marko695348f2020-05-19 14:42:02 +01006851 DCHECK(!GetCompilerOptions().IsJitCompiler() || debug_name == nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00006852 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006853 std::ostringstream oss;
6854 oss << "BakerReadBarrierThunk";
6855 switch (kind) {
6856 case BakerReadBarrierKind::kField:
6857 oss << "Field_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
6858 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
6859 break;
Vladimir Marko0ecac682018-08-07 10:40:38 +01006860 case BakerReadBarrierKind::kAcquire:
6861 oss << "Acquire_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
6862 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
6863 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +00006864 case BakerReadBarrierKind::kArray:
6865 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
6866 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6867 BakerReadBarrierSecondRegField::Decode(encoded_data));
6868 break;
6869 case BakerReadBarrierKind::kGcRoot:
6870 oss << "GcRoot_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
6871 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6872 BakerReadBarrierSecondRegField::Decode(encoded_data));
6873 break;
6874 }
6875 *debug_name = oss.str();
6876 }
6877}
6878
6879#undef __
6880
Alexandre Rames5319def2014-10-23 10:03:10 +01006881} // namespace arm64
6882} // namespace art