blob: dba8a2d124cb31822ba295650f73c0f22ee1e712 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Vladimir Markof4f2daa2017-03-20 18:26:59 +000019#include "arch/arm64/asm_support_arm64.h"
Serban Constantinescu579885a2015-02-22 20:51:33 +000020#include "arch/arm64/instruction_set_features_arm64.h"
Vladimir Marko86c87522020-05-11 16:55:55 +010021#include "arch/arm64/jni_frame_arm64.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000022#include "art_method-inl.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070023#include "base/bit_utils.h"
24#include "base/bit_utils_iterator.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010025#include "class_table.h"
Zheng Xuc6667102015-05-15 16:08:45 +080026#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000027#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080029#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010030#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010031#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070032#include "heap_poisoning.h"
Nicolas Geoffray4313ccb2020-08-26 17:01:15 +010033#include "interpreter/mterp/nterp.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080034#include "intrinsics.h"
35#include "intrinsics_arm64.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010036#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070037#include "lock_word.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010038#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070039#include "mirror/class-inl.h"
Vladimir Marko2d98dc22020-10-01 11:21:37 +000040#include "mirror/var_handle.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000041#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010042#include "thread.h"
43#include "utils/arm64/assembler_arm64.h"
44#include "utils/assembler.h"
45#include "utils/stack_checks.h"
46
Scott Wakeling97c72b72016-06-24 16:19:36 +010047using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000048using vixl::ExactAssemblyScope;
49using vixl::CodeBufferCheckScope;
50using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010051
52#ifdef __
53#error "ARM64 Codegen VIXL macro-assembler macro already defined."
54#endif
55
Vladimir Marko0a516052019-10-14 13:00:44 +000056namespace art {
Alexandre Rames5319def2014-10-23 10:03:10 +010057
Roland Levillain22ccc3a2015-11-24 13:10:05 +000058template<class MirrorType>
59class GcRoot;
60
Alexandre Rames5319def2014-10-23 10:03:10 +010061namespace arm64 {
62
Alexandre Ramesbe919d92016-08-23 18:33:36 +010063using helpers::ARM64EncodableConstantOrRegister;
64using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080065using helpers::CPURegisterFrom;
66using helpers::DRegisterFrom;
67using helpers::FPRegisterFrom;
68using helpers::HeapOperand;
69using helpers::HeapOperandFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010070using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080071using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080072using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010073using helpers::InputRegisterAt;
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +010074using helpers::Int64FromLocation;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010075using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080076using helpers::LocationFrom;
77using helpers::OperandFromMemOperand;
78using helpers::OutputCPURegister;
79using helpers::OutputFPRegister;
80using helpers::OutputRegister;
81using helpers::RegisterFrom;
82using helpers::StackOperandFrom;
83using helpers::VIXLRegCodeFromART;
84using helpers::WRegisterFrom;
85using helpers::XRegisterFrom;
86
Vladimir Markof3e0ee22015-12-17 15:23:13 +000087// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080088// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
89// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000090static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010091
Vladimir Markof4f2daa2017-03-20 18:26:59 +000092// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
93// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
Vladimir Marko008e09f32018-08-06 15:42:43 +010094// For the Baker read barrier implementation using link-time generated thunks we need to split
Vladimir Markof4f2daa2017-03-20 18:26:59 +000095// the offset explicitly.
96constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
97
Alexandre Rames5319def2014-10-23 10:03:10 +010098inline Condition ARM64Condition(IfCondition cond) {
99 switch (cond) {
100 case kCondEQ: return eq;
101 case kCondNE: return ne;
102 case kCondLT: return lt;
103 case kCondLE: return le;
104 case kCondGT: return gt;
105 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700106 case kCondB: return lo;
107 case kCondBE: return ls;
108 case kCondA: return hi;
109 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100110 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000111 LOG(FATAL) << "Unreachable";
112 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100113}
114
Vladimir Markod6e069b2016-01-18 11:11:01 +0000115inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
116 // The ARM64 condition codes can express all the necessary branches, see the
117 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
118 // There is no dex instruction or HIR that would need the missing conditions
119 // "equal or unordered" or "not equal".
120 switch (cond) {
121 case kCondEQ: return eq;
122 case kCondNE: return ne /* unordered */;
123 case kCondLT: return gt_bias ? cc : lt /* unordered */;
124 case kCondLE: return gt_bias ? ls : le /* unordered */;
125 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
126 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
127 default:
128 LOG(FATAL) << "UNREACHABLE";
129 UNREACHABLE();
130 }
131}
132
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100133Location ARM64ReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000134 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
135 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
136 // but we use the exact registers for clarity.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100137 if (return_type == DataType::Type::kFloat32) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000138 return LocationFrom(s0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100139 } else if (return_type == DataType::Type::kFloat64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000140 return LocationFrom(d0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100141 } else if (return_type == DataType::Type::kInt64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000142 return LocationFrom(x0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100143 } else if (return_type == DataType::Type::kVoid) {
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100144 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000145 } else {
146 return LocationFrom(w0);
147 }
148}
149
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100150Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000151 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100152}
153
Vladimir Marko3232dbb2018-07-25 15:42:46 +0100154static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
155 InvokeRuntimeCallingConvention calling_convention;
156 RegisterSet caller_saves = RegisterSet::Empty();
157 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
158 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
159 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
160 DataType::Type::kReference).GetCode());
161 return caller_saves;
162}
163
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100164// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
165#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700166#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100167
Zheng Xuda403092015-04-24 17:35:39 +0800168// Calculate memory accessing operand for save/restore live registers.
169static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100170 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800171 int64_t spill_offset,
172 bool is_save) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800173 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
174 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100175 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800176 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100177 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800178 codegen->GetNumberOfFloatingPointRegisters()));
179
Vladimir Marko804b03f2016-09-14 16:26:36 +0100180 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serovc8150b52019-07-31 18:28:00 +0100181 const unsigned v_reg_size_in_bits = codegen->GetSlowPathFPWidth() * 8;
Artem Serov1a719e42019-07-18 14:24:55 +0100182 DCHECK_LE(codegen->GetSIMDRegisterWidth(), kQRegSizeInBytes);
Artem Serovc8150b52019-07-31 18:28:00 +0100183 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size_in_bits, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800184
185 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
186 UseScratchRegisterScope temps(masm);
187
188 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100189 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
190 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800191 int64_t reg_size = kXRegSizeInBytes;
192 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
193 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100194 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800195 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
196 // If the offset does not fit in the instruction's immediate field, use an alternate register
197 // to compute the base address(float point registers spill base address).
198 Register new_base = temps.AcquireSameSizeAs(base);
199 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
200 base = new_base;
201 spill_offset = -core_spill_size;
202 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
203 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
204 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
205 }
206
207 if (is_save) {
208 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
209 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
210 } else {
211 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
212 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
213 }
214}
215
216void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800217 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Andreas Gampe3db70682018-12-26 15:12:03 -0800218 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100219 for (uint32_t i : LowToHighBits(core_spills)) {
220 // If the register holds an object, update the stack mask.
221 if (locations->RegisterContainsObject(i)) {
222 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800223 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100224 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
225 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
226 saved_core_stack_offsets_[i] = stack_offset;
227 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800228 }
229
Artem Serovc8150b52019-07-31 18:28:00 +0100230 const size_t fp_reg_size = codegen->GetSlowPathFPWidth();
Andreas Gampe3db70682018-12-26 15:12:03 -0800231 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100232 for (uint32_t i : LowToHighBits(fp_spills)) {
233 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
234 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
235 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serov9df37b92019-07-23 16:41:54 +0100236 stack_offset += fp_reg_size;
Zheng Xuda403092015-04-24 17:35:39 +0800237 }
238
Vladimir Marko804b03f2016-09-14 16:26:36 +0100239 SaveRestoreLiveRegistersHelper(codegen,
240 locations,
Andreas Gampe3db70682018-12-26 15:12:03 -0800241 codegen->GetFirstRegisterSlotInSlowPath(), /* is_save= */ true);
Zheng Xuda403092015-04-24 17:35:39 +0800242}
243
244void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100245 SaveRestoreLiveRegistersHelper(codegen,
246 locations,
Andreas Gampe3db70682018-12-26 15:12:03 -0800247 codegen->GetFirstRegisterSlotInSlowPath(), /* is_save= */ false);
Zheng Xuda403092015-04-24 17:35:39 +0800248}
249
Alexandre Rames5319def2014-10-23 10:03:10 +0100250class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
251 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000252 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100253
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100254 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100255 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000256 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100257
Alexandre Rames5319def2014-10-23 10:03:10 +0100258 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000259 if (instruction_->CanThrowIntoCatchBlock()) {
260 // Live registers will be restored in the catch block if caught.
261 SaveLiveRegisters(codegen, instruction_->GetLocations());
262 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000263 // We're moving two locations to locations that could overlap, so we need a parallel
264 // move resolver.
265 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100266 codegen->EmitParallelMoves(locations->InAt(0),
267 LocationFrom(calling_convention.GetRegisterAt(0)),
268 DataType::Type::kInt32,
269 locations->InAt(1),
270 LocationFrom(calling_convention.GetRegisterAt(1)),
271 DataType::Type::kInt32);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000272 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
273 ? kQuickThrowStringBounds
274 : kQuickThrowArrayBounds;
275 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100276 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800277 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100278 }
279
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100280 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100281
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100282 const char* GetDescription() const override { return "BoundsCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100283
Alexandre Rames5319def2014-10-23 10:03:10 +0100284 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100285 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
286};
287
Alexandre Rames67555f72014-11-18 10:55:16 +0000288class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
289 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000290 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000291
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100292 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000293 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
294 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000295 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800296 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000297 }
298
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100299 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100300
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100301 const char* GetDescription() const override { return "DivZeroCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100302
Alexandre Rames67555f72014-11-18 10:55:16 +0000303 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000304 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
305};
306
307class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
308 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100309 LoadClassSlowPathARM64(HLoadClass* cls, HInstruction* at)
310 : SlowPathCodeARM64(at), cls_(cls) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000311 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100312 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Alexandre Rames67555f72014-11-18 10:55:16 +0000313 }
314
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100315 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000316 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000317 Location out = locations->Out();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100318 const uint32_t dex_pc = instruction_->GetDexPc();
319 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
320 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
Alexandre Rames67555f72014-11-18 10:55:16 +0000321
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100322 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames67555f72014-11-18 10:55:16 +0000323 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000324 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000325
Vladimir Markof3c52b42017-11-17 17:32:12 +0000326 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100327 if (must_resolve_type) {
328 DCHECK(IsSameDexFile(cls_->GetDexFile(), arm64_codegen->GetGraph()->GetDexFile()));
329 dex::TypeIndex type_index = cls_->GetTypeIndex();
330 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Vladimir Marko8f63f102020-09-28 12:10:28 +0100331 if (cls_->NeedsAccessCheck()) {
332 CheckEntrypointTypes<kQuickResolveTypeAndVerifyAccess, void*, uint32_t>();
333 arm64_codegen->InvokeRuntime(kQuickResolveTypeAndVerifyAccess, instruction_, dex_pc, this);
334 } else {
335 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
336 arm64_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
337 }
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100338 // If we also must_do_clinit, the resolved type is now in the correct register.
339 } else {
340 DCHECK(must_do_clinit);
341 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
342 arm64_codegen->MoveLocation(LocationFrom(calling_convention.GetRegisterAt(0)),
343 source,
344 cls_->GetType());
345 }
346 if (must_do_clinit) {
347 arm64_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
348 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800349 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000350
351 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000352 if (out.IsValid()) {
353 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100354 DataType::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000355 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000356 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000357 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000358 __ B(GetExitLabel());
359 }
360
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100361 const char* GetDescription() const override { return "LoadClassSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100362
Alexandre Rames67555f72014-11-18 10:55:16 +0000363 private:
364 // The class this slow path will load.
365 HLoadClass* const cls_;
366
Alexandre Rames67555f72014-11-18 10:55:16 +0000367 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
368};
369
Vladimir Markoaad75c62016-10-03 08:46:48 +0000370class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
371 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000372 explicit LoadStringSlowPathARM64(HLoadString* instruction)
373 : SlowPathCodeARM64(instruction) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000374
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100375 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000376 LocationSummary* locations = instruction_->GetLocations();
377 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
378 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
379
380 __ Bind(GetEntryLabel());
381 SaveLiveRegisters(codegen, locations);
382
Vladimir Markof3c52b42017-11-17 17:32:12 +0000383 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000384 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
385 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000386 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
387 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100388 DataType::Type type = instruction_->GetType();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000389 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
390
391 RestoreLiveRegisters(codegen, locations);
392
Vladimir Markoaad75c62016-10-03 08:46:48 +0000393 __ B(GetExitLabel());
394 }
395
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100396 const char* GetDescription() const override { return "LoadStringSlowPathARM64"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000397
398 private:
399 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
400};
401
Alexandre Rames5319def2014-10-23 10:03:10 +0100402class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
403 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000404 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100405
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100406 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000407 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100408 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000409 if (instruction_->CanThrowIntoCatchBlock()) {
410 // Live registers will be restored in the catch block if caught.
411 SaveLiveRegisters(codegen, instruction_->GetLocations());
412 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000413 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
414 instruction_,
415 instruction_->GetDexPc(),
416 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800417 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100418 }
419
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100420 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100421
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100422 const char* GetDescription() const override { return "NullCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100423
Alexandre Rames5319def2014-10-23 10:03:10 +0100424 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100425 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
426};
427
428class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
429 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100430 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000431 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100432
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100433 void EmitNativeCode(CodeGenerator* codegen) override {
Artem Serov7957d952017-04-04 15:44:09 +0100434 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000435 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100436 __ Bind(GetEntryLabel());
Artem Serov1a719e42019-07-18 14:24:55 +0100437 SaveLiveRegisters(codegen, locations); // Only saves live vector regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000438 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800439 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov1a719e42019-07-18 14:24:55 +0100440 RestoreLiveRegisters(codegen, locations); // Only restores live vector regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000441 if (successor_ == nullptr) {
442 __ B(GetReturnLabel());
443 } else {
444 __ B(arm64_codegen->GetLabelOf(successor_));
445 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100446 }
447
Scott Wakeling97c72b72016-06-24 16:19:36 +0100448 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100449 DCHECK(successor_ == nullptr);
450 return &return_label_;
451 }
452
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100453 HBasicBlock* GetSuccessor() const {
454 return successor_;
455 }
456
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100457 const char* GetDescription() const override { return "SuspendCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100458
Alexandre Rames5319def2014-10-23 10:03:10 +0100459 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100460 // If not null, the block to branch to after the suspend check.
461 HBasicBlock* const successor_;
462
463 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100464 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100465
466 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
467};
468
Alexandre Rames67555f72014-11-18 10:55:16 +0000469class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
470 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000471 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000472 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000473
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100474 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000475 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800476
Alexandre Rames3e69f162014-12-10 10:36:50 +0000477 DCHECK(instruction_->IsCheckCast()
478 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
479 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100480 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000481
Alexandre Rames67555f72014-11-18 10:55:16 +0000482 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000483
Vladimir Marko87584542017-12-12 17:47:52 +0000484 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000485 SaveLiveRegisters(codegen, locations);
486 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000487
488 // We're moving two locations to locations that could overlap, so we need a parallel
489 // move resolver.
490 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800491 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800492 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100493 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800494 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800495 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100496 DataType::Type::kReference);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000497 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000498 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800499 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100500 DataType::Type ret_type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000501 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
502 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
503 } else {
504 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800505 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
506 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000507 }
508
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000509 if (!is_fatal_) {
510 RestoreLiveRegisters(codegen, locations);
511 __ B(GetExitLabel());
512 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000513 }
514
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100515 const char* GetDescription() const override { return "TypeCheckSlowPathARM64"; }
516 bool IsFatal() const override { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100517
Alexandre Rames67555f72014-11-18 10:55:16 +0000518 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000519 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000520
Alexandre Rames67555f72014-11-18 10:55:16 +0000521 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
522};
523
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700524class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
525 public:
Aart Bik42249c32016-01-07 15:33:50 -0800526 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000527 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700528
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100529 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bik42249c32016-01-07 15:33:50 -0800530 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700531 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100532 LocationSummary* locations = instruction_->GetLocations();
533 SaveLiveRegisters(codegen, locations);
534 InvokeRuntimeCallingConvention calling_convention;
535 __ Mov(calling_convention.GetRegisterAt(0),
536 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000537 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100538 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700539 }
540
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100541 const char* GetDescription() const override { return "DeoptimizationSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100542
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700543 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700544 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
545};
546
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100547class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
548 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000549 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100550
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100551 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100552 LocationSummary* locations = instruction_->GetLocations();
553 __ Bind(GetEntryLabel());
554 SaveLiveRegisters(codegen, locations);
555
556 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100557 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100558 parallel_move.AddMove(
559 locations->InAt(0),
560 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100561 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100562 nullptr);
563 parallel_move.AddMove(
564 locations->InAt(1),
565 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100566 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100567 nullptr);
568 parallel_move.AddMove(
569 locations->InAt(2),
570 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100571 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100572 nullptr);
573 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
574
575 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000576 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100577 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
578 RestoreLiveRegisters(codegen, locations);
579 __ B(GetExitLabel());
580 }
581
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100582 const char* GetDescription() const override { return "ArraySetSlowPathARM64"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100583
584 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100585 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
586};
587
Zheng Xu3927c8b2015-11-18 17:46:25 +0800588void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
589 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000590 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800591
592 // We are about to use the assembler to place literals directly. Make sure we have enough
593 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000594 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
595 num_entries * sizeof(int32_t),
596 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800597
598 __ Bind(&table_start_);
599 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
600 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100601 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800602 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100603 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800604 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
605 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
606 Literal<int32_t> literal(jump_offset);
607 __ place(&literal);
608 }
609}
610
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000611// Slow path generating a read barrier for a heap reference.
612class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
613 public:
614 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
615 Location out,
616 Location ref,
617 Location obj,
618 uint32_t offset,
619 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000620 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000621 out_(out),
622 ref_(ref),
623 obj_(obj),
624 offset_(offset),
625 index_(index) {
626 DCHECK(kEmitCompilerReadBarrier);
627 // If `obj` is equal to `out` or `ref`, it means the initial object
628 // has been overwritten by (or after) the heap object reference load
629 // to be instrumented, e.g.:
630 //
631 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000632 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000633 //
634 // In that case, we have lost the information about the original
635 // object, and the emitted read barrier cannot work properly.
636 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
637 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
638 }
639
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100640 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000641 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
642 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100643 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000644 DCHECK(locations->CanCall());
645 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100646 DCHECK(instruction_->IsInstanceFieldGet() ||
647 instruction_->IsStaticFieldGet() ||
648 instruction_->IsArrayGet() ||
649 instruction_->IsInstanceOf() ||
650 instruction_->IsCheckCast() ||
Vladimir Markoa41ea272020-09-07 15:24:36 +0000651 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000652 << "Unexpected instruction in read barrier for heap reference slow path: "
653 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000654 // The read barrier instrumentation of object ArrayGet
655 // instructions does not support the HIntermediateAddress
656 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000657 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100658 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000659
660 __ Bind(GetEntryLabel());
661
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000662 SaveLiveRegisters(codegen, locations);
663
664 // We may have to change the index's value, but as `index_` is a
665 // constant member (like other "inputs" of this slow path),
666 // introduce a copy of it, `index`.
667 Location index = index_;
668 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100669 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000670 if (instruction_->IsArrayGet()) {
671 // Compute the actual memory offset and store it in `index`.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100672 Register index_reg = RegisterFrom(index_, DataType::Type::kInt32);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000673 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
674 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
675 // We are about to change the value of `index_reg` (see the
676 // calls to vixl::MacroAssembler::Lsl and
677 // vixl::MacroAssembler::Mov below), but it has
678 // not been saved by the previous call to
679 // art::SlowPathCode::SaveLiveRegisters, as it is a
680 // callee-save register --
681 // art::SlowPathCode::SaveLiveRegisters does not consider
682 // callee-save registers, as it has been designed with the
683 // assumption that callee-save registers are supposed to be
684 // handled by the called function. So, as a callee-save
685 // register, `index_reg` _would_ eventually be saved onto
686 // the stack, but it would be too late: we would have
687 // changed its value earlier. Therefore, we manually save
688 // it here into another freely available register,
689 // `free_reg`, chosen of course among the caller-save
690 // registers (as a callee-save `free_reg` register would
691 // exhibit the same problem).
692 //
693 // Note we could have requested a temporary register from
694 // the register allocator instead; but we prefer not to, as
695 // this is a slow path, and we know we can find a
696 // caller-save register that is available.
697 Register free_reg = FindAvailableCallerSaveRegister(codegen);
698 __ Mov(free_reg.W(), index_reg);
699 index_reg = free_reg;
700 index = LocationFrom(index_reg);
701 } else {
702 // The initial register stored in `index_` has already been
703 // saved in the call to art::SlowPathCode::SaveLiveRegisters
704 // (as it is not a callee-save register), so we can freely
705 // use it.
706 }
707 // Shifting the index value contained in `index_reg` by the scale
708 // factor (2) cannot overflow in practice, as the runtime is
709 // unable to allocate object arrays with a size larger than
710 // 2^26 - 1 (that is, 2^28 - 4 bytes).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100711 __ Lsl(index_reg, index_reg, DataType::SizeShift(type));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000712 static_assert(
713 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
714 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
715 __ Add(index_reg, index_reg, Operand(offset_));
716 } else {
Vladimir Markoa41ea272020-09-07 15:24:36 +0000717 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile/VarHandleGet
Roland Levillain3d312422016-06-23 13:53:42 +0100718 // intrinsics, `index_` is not shifted by a scale factor of 2
719 // (as in the case of ArrayGet), as it is actually an offset
720 // to an object field within an object.
721 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000722 DCHECK(instruction_->GetLocations()->Intrinsified());
Vladimir Marko2d98dc22020-10-01 11:21:37 +0000723 Intrinsics intrinsic = instruction_->AsInvoke()->GetIntrinsic();
724 DCHECK(intrinsic == Intrinsics::kUnsafeGetObject ||
725 intrinsic == Intrinsics::kUnsafeGetObjectVolatile ||
Vladimir Markoe17530a2020-11-11 17:02:26 +0000726 intrinsic == Intrinsics::kUnsafeCASObject ||
Vladimir Marko2d98dc22020-10-01 11:21:37 +0000727 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
Vladimir Marko1bff99f2020-11-02 15:07:33 +0000728 mirror::VarHandle::AccessModeTemplate::kGet ||
729 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
730 mirror::VarHandle::AccessModeTemplate::kCompareAndSet ||
731 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
732 mirror::VarHandle::AccessModeTemplate::kCompareAndExchange)
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000733 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100734 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100735 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000736 }
737 }
738
739 // We're moving two or three locations to locations that could
740 // overlap, so we need a parallel move resolver.
741 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100742 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000743 parallel_move.AddMove(ref_,
744 LocationFrom(calling_convention.GetRegisterAt(0)),
745 type,
746 nullptr);
747 parallel_move.AddMove(obj_,
748 LocationFrom(calling_convention.GetRegisterAt(1)),
749 type,
750 nullptr);
751 if (index.IsValid()) {
752 parallel_move.AddMove(index,
753 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100754 DataType::Type::kInt32,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000755 nullptr);
756 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
757 } else {
758 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
759 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
760 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000761 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000762 instruction_,
763 instruction_->GetDexPc(),
764 this);
765 CheckEntrypointTypes<
766 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
767 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
768
769 RestoreLiveRegisters(codegen, locations);
770
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000771 __ B(GetExitLabel());
772 }
773
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100774 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000775
776 private:
777 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100778 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
779 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000780 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
781 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
782 return Register(VIXLRegCodeFromART(i), kXRegSize);
783 }
784 }
785 // We shall never fail to find a free caller-save register, as
786 // there are more than two core caller-save registers on ARM64
787 // (meaning it is possible to find one which is different from
788 // `ref` and `obj`).
789 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
790 LOG(FATAL) << "Could not find a free register";
791 UNREACHABLE();
792 }
793
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000794 const Location out_;
795 const Location ref_;
796 const Location obj_;
797 const uint32_t offset_;
798 // An additional location containing an index to an array.
799 // Only used for HArrayGet and the UnsafeGetObject &
800 // UnsafeGetObjectVolatile intrinsics.
801 const Location index_;
802
803 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
804};
805
806// Slow path generating a read barrier for a GC root.
807class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
808 public:
809 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000810 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000811 DCHECK(kEmitCompilerReadBarrier);
812 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000813
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100814 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000815 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100816 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000817 DCHECK(locations->CanCall());
818 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Vladimir Markoa41ea272020-09-07 15:24:36 +0000819 DCHECK(instruction_->IsLoadClass() ||
820 instruction_->IsLoadString() ||
821 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000822 << "Unexpected instruction in read barrier for GC root slow path: "
823 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000824
825 __ Bind(GetEntryLabel());
826 SaveLiveRegisters(codegen, locations);
827
828 InvokeRuntimeCallingConvention calling_convention;
829 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
830 // The argument of the ReadBarrierForRootSlow is not a managed
831 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
832 // thus we need a 64-bit move here, and we cannot use
833 //
834 // arm64_codegen->MoveLocation(
835 // LocationFrom(calling_convention.GetRegisterAt(0)),
836 // root_,
837 // type);
838 //
839 // which would emit a 32-bit move, as `type` is a (32-bit wide)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100840 // reference type (`DataType::Type::kReference`).
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000841 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000842 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000843 instruction_,
844 instruction_->GetDexPc(),
845 this);
846 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
847 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
848
849 RestoreLiveRegisters(codegen, locations);
850 __ B(GetExitLabel());
851 }
852
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100853 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000854
855 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000856 const Location out_;
857 const Location root_;
858
859 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
860};
861
Alexandre Rames5319def2014-10-23 10:03:10 +0100862#undef __
863
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100864Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100865 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100866 if (type == DataType::Type::kVoid) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100867 LOG(FATAL) << "Unreachable type " << type;
868 }
869
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100870 if (DataType::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100871 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
872 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100873 } else if (!DataType::IsFloatingPointType(type) &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000874 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000875 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
876 } else {
877 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100878 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
879 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100880 }
881
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000882 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100883 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100884 return next_location;
885}
886
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100887Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100888 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100889}
890
Vladimir Marko86c87522020-05-11 16:55:55 +0100891Location CriticalNativeCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
892 DCHECK_NE(type, DataType::Type::kReference);
893
894 Location location = Location::NoLocation();
895 if (DataType::IsFloatingPointType(type)) {
896 if (fpr_index_ < kParameterFPRegistersLength) {
897 location = LocationFrom(kParameterFPRegisters[fpr_index_]);
898 ++fpr_index_;
899 }
900 } else {
901 // Native ABI uses the same registers as managed, except that the method register x0
902 // is a normal argument.
903 if (gpr_index_ < 1u + kParameterCoreRegistersLength) {
904 location = LocationFrom(gpr_index_ == 0u ? x0 : kParameterCoreRegisters[gpr_index_ - 1u]);
905 ++gpr_index_;
906 }
907 }
908 if (location.IsInvalid()) {
909 if (DataType::Is64BitType(type)) {
910 location = Location::DoubleStackSlot(stack_offset_);
911 } else {
912 location = Location::StackSlot(stack_offset_);
913 }
914 stack_offset_ += kFramePointerSize;
915
916 if (for_register_allocation_) {
917 location = Location::Any();
918 }
919 }
920 return location;
921}
922
923Location CriticalNativeCallingConventionVisitorARM64::GetReturnLocation(DataType::Type type) const {
924 // We perform conversion to the managed ABI return register after the call if needed.
925 InvokeDexCallingConventionVisitorARM64 dex_calling_convention;
926 return dex_calling_convention.GetReturnLocation(type);
927}
928
929Location CriticalNativeCallingConventionVisitorARM64::GetMethodLocation() const {
930 // Pass the method in the hidden argument x15.
931 return Location::RegisterLocation(x15.GetCode());
932}
933
Serban Constantinescu579885a2015-02-22 20:51:33 +0000934CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100935 const CompilerOptions& compiler_options,
936 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100937 : CodeGenerator(graph,
938 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000939 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000940 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100941 callee_saved_core_registers.GetList(),
942 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100943 compiler_options,
944 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100945 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
946 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov1a719e42019-07-18 14:24:55 +0100947 location_builder_neon_(graph, this),
948 instruction_visitor_neon_(graph, this),
949 location_builder_sve_(graph, this),
950 instruction_visitor_sve_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100951 move_resolver_(graph->GetAllocator(), this),
Artem Serovaa6f4832018-11-21 18:57:54 +0000952 assembler_(graph->GetAllocator(),
953 compiler_options.GetInstructionSetFeatures()->AsArm64InstructionSetFeatures()),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000954 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100955 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000956 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100957 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko8f63f102020-09-28 12:10:28 +0100958 public_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
959 package_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000960 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100961 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeb9eb002020-10-02 13:54:19 +0100962 boot_image_jni_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +0100963 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100964 call_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100965 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100966 uint32_literals_(std::less<uint32_t>(),
967 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
968 uint64_literals_(std::less<uint64_t>(),
969 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000970 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100971 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000972 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +0000973 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
974 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
975 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000976 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000977 AddAllocatedRegister(LocationFrom(lr));
Artem Serov1a719e42019-07-18 14:24:55 +0100978
979 bool use_sve = ShouldUseSVE();
980 if (use_sve) {
981 location_builder_ = &location_builder_sve_;
982 instruction_visitor_ = &instruction_visitor_sve_;
983 } else {
984 location_builder_ = &location_builder_neon_;
985 instruction_visitor_ = &instruction_visitor_neon_;
986 }
987}
988
989bool CodeGeneratorARM64::ShouldUseSVE() const {
990 return kArm64AllowSVE && GetInstructionSetFeatures().HasSVE();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000991}
Alexandre Rames5319def2014-10-23 10:03:10 +0100992
Alexandre Rames67555f72014-11-18 10:55:16 +0000993#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100994
Zheng Xu3927c8b2015-11-18 17:46:25 +0800995void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100996 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800997 jump_table->EmitTable(this);
998 }
999}
1000
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001001void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001002 EmitJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +00001003
1004 // Emit JIT baker read barrier slow paths.
Vladimir Marko695348f2020-05-19 14:42:02 +01001005 DCHECK(GetCompilerOptions().IsJitCompiler() || jit_baker_read_barrier_slow_paths_.empty());
Vladimir Marko966b46f2018-08-03 10:20:19 +00001006 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
1007 uint32_t encoded_data = entry.first;
1008 vixl::aarch64::Label* slow_path_entry = &entry.second.label;
1009 __ Bind(slow_path_entry);
Andreas Gampe3db70682018-12-26 15:12:03 -08001010 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name= */ nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00001011 }
1012
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001013 // Ensure we emit the literal pool.
1014 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001015
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001016 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001017
1018 // Verify Baker read barrier linker patches.
1019 if (kIsDebugBuild) {
1020 ArrayRef<const uint8_t> code = allocator->GetMemory();
1021 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
1022 DCHECK(info.label.IsBound());
1023 uint32_t literal_offset = info.label.GetLocation();
1024 DCHECK_ALIGNED(literal_offset, 4u);
1025
1026 auto GetInsn = [&code](uint32_t offset) {
1027 DCHECK_ALIGNED(offset, 4u);
1028 return
1029 (static_cast<uint32_t>(code[offset + 0]) << 0) +
1030 (static_cast<uint32_t>(code[offset + 1]) << 8) +
1031 (static_cast<uint32_t>(code[offset + 2]) << 16)+
1032 (static_cast<uint32_t>(code[offset + 3]) << 24);
1033 };
1034
1035 const uint32_t encoded_data = info.custom_data;
1036 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
1037 // Check that the next instruction matches the expected LDR.
1038 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01001039 case BakerReadBarrierKind::kField:
1040 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00001041 DCHECK_GE(code.size() - literal_offset, 8u);
1042 uint32_t next_insn = GetInsn(literal_offset + 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001043 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1044 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko0ecac682018-08-07 10:40:38 +01001045 if (kind == BakerReadBarrierKind::kField) {
1046 // LDR (immediate) with correct base_reg.
1047 CHECK_EQ(next_insn & 0xffc003e0u, 0xb9400000u | (base_reg << 5));
1048 } else {
1049 DCHECK(kind == BakerReadBarrierKind::kAcquire);
1050 // LDAR with correct base_reg.
1051 CHECK_EQ(next_insn & 0xffffffe0u, 0x88dffc00u | (base_reg << 5));
1052 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001053 break;
1054 }
1055 case BakerReadBarrierKind::kArray: {
1056 DCHECK_GE(code.size() - literal_offset, 8u);
1057 uint32_t next_insn = GetInsn(literal_offset + 4u);
1058 // LDR (register) with the correct base_reg, size=10 (32-bit), option=011 (extend = LSL),
1059 // and S=1 (shift amount = 2 for 32-bit version), i.e. LDR Wt, [Xn, Xm, LSL #2].
1060 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1061 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1062 CHECK_EQ(next_insn & 0xffe0ffe0u, 0xb8607800u | (base_reg << 5));
1063 CheckValidReg((next_insn >> 16) & 0x1f); // Check index register
1064 break;
1065 }
1066 case BakerReadBarrierKind::kGcRoot: {
1067 DCHECK_GE(literal_offset, 4u);
1068 uint32_t prev_insn = GetInsn(literal_offset - 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001069 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko94796f82018-08-08 15:15:33 +01001070 // Usually LDR (immediate) with correct root_reg but
1071 // we may have a "MOV marked, old_value" for UnsafeCASObject.
1072 if ((prev_insn & 0xffe0ffff) != (0x2a0003e0 | root_reg)) { // MOV?
1073 CHECK_EQ(prev_insn & 0xffc0001fu, 0xb9400000u | root_reg); // LDR?
1074 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001075 break;
1076 }
1077 default:
1078 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
1079 UNREACHABLE();
1080 }
1081 }
1082 }
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001083}
1084
Zheng Xuad4450e2015-04-17 18:48:56 +08001085void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1086 // Note: There are 6 kinds of moves:
1087 // 1. constant -> GPR/FPR (non-cycle)
1088 // 2. constant -> stack (non-cycle)
1089 // 3. GPR/FPR -> GPR/FPR
1090 // 4. GPR/FPR -> stack
1091 // 5. stack -> GPR/FPR
1092 // 6. stack -> stack (non-cycle)
1093 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1094 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1095 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1096 // dependency.
1097 vixl_temps_.Open(GetVIXLAssembler());
1098}
1099
1100void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1101 vixl_temps_.Close();
1102}
1103
1104Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001105 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1106 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1107 || kind == Location::kSIMDStackSlot);
1108 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1109 ? Location::kFpuRegister
1110 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001111 Location scratch = GetScratchLocation(kind);
1112 if (!scratch.Equals(Location::NoLocation())) {
1113 return scratch;
1114 }
1115 // Allocate from VIXL temp registers.
1116 if (kind == Location::kRegister) {
1117 scratch = LocationFrom(vixl_temps_.AcquireX());
1118 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001119 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serov1a719e42019-07-18 14:24:55 +01001120 scratch = codegen_->GetGraph()->HasSIMD()
1121 ? codegen_->GetInstructionCodeGeneratorArm64()->AllocateSIMDScratchLocation(&vixl_temps_)
1122 : LocationFrom(vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001123 }
1124 AddScratchLocation(scratch);
1125 return scratch;
1126}
1127
1128void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1129 if (loc.IsRegister()) {
1130 vixl_temps_.Release(XRegisterFrom(loc));
1131 } else {
1132 DCHECK(loc.IsFpuRegister());
Artem Serov1a719e42019-07-18 14:24:55 +01001133 if (codegen_->GetGraph()->HasSIMD()) {
1134 codegen_->GetInstructionCodeGeneratorArm64()->FreeSIMDScratchLocation(loc, &vixl_temps_);
1135 } else {
1136 vixl_temps_.Release(DRegisterFrom(loc));
1137 }
Zheng Xuad4450e2015-04-17 18:48:56 +08001138 }
1139 RemoveScratchLocation(loc);
1140}
1141
Alexandre Rames3e69f162014-12-10 10:36:50 +00001142void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001143 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001144 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001145}
1146
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001147void CodeGeneratorARM64::MaybeIncrementHotness(bool is_frame_entry) {
1148 MacroAssembler* masm = GetVIXLAssembler();
1149 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1150 UseScratchRegisterScope temps(masm);
1151 Register counter = temps.AcquireX();
1152 Register method = is_frame_entry ? kArtMethodRegister : temps.AcquireX();
1153 if (!is_frame_entry) {
1154 __ Ldr(method, MemOperand(sp, 0));
1155 }
1156 __ Ldrh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
1157 __ Add(counter, counter, 1);
1158 // Subtract one if the counter would overflow.
1159 __ Sub(counter, counter, Operand(counter, LSR, 16));
1160 __ Strh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
1161 }
1162
1163 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffray095dc462020-08-17 16:40:28 +01001164 ScopedProfilingInfoUse spiu(
1165 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
1166 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001167 if (info != nullptr) {
Nicolas Geoffrayc1cd1332020-01-25 13:08:24 +00001168 uint64_t address = reinterpret_cast64<uint64_t>(info);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001169 vixl::aarch64::Label done;
1170 UseScratchRegisterScope temps(masm);
1171 Register temp = temps.AcquireX();
1172 Register counter = temps.AcquireW();
1173 __ Mov(temp, address);
1174 __ Ldrh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
1175 __ Add(counter, counter, 1);
Nicolas Geoffray4313ccb2020-08-26 17:01:15 +01001176 __ And(counter, counter, interpreter::kTieredHotnessMask);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001177 __ Strh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
Nicolas Geoffray4313ccb2020-08-26 17:01:15 +01001178 __ Cbnz(counter, &done);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001179 if (is_frame_entry) {
1180 if (HasEmptyFrame()) {
Vladimir Markodec78172020-06-19 15:31:23 +01001181 // The entrypoint expects the method at the bottom of the stack. We
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001182 // claim stack space necessary for alignment.
Vladimir Markodec78172020-06-19 15:31:23 +01001183 IncreaseFrame(kStackAlignment);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001184 __ Stp(kArtMethodRegister, lr, MemOperand(sp, 0));
1185 } else if (!RequiresCurrentMethod()) {
1186 __ Str(kArtMethodRegister, MemOperand(sp, 0));
1187 }
1188 } else {
1189 CHECK(RequiresCurrentMethod());
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001190 }
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001191 uint32_t entrypoint_offset =
1192 GetThreadOffset<kArm64PointerSize>(kQuickCompileOptimized).Int32Value();
1193 __ Ldr(lr, MemOperand(tr, entrypoint_offset));
1194 // Note: we don't record the call here (and therefore don't generate a stack
1195 // map), as the entrypoint should never be suspended.
1196 __ Blr(lr);
1197 if (HasEmptyFrame()) {
1198 CHECK(is_frame_entry);
1199 __ Ldr(lr, MemOperand(sp, 8));
Vladimir Markodec78172020-06-19 15:31:23 +01001200 DecreaseFrame(kStackAlignment);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001201 }
1202 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001203 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001204 }
1205}
1206
Alexandre Rames5319def2014-10-23 10:03:10 +01001207void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001208 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001209 __ Bind(&frame_entry_label_);
1210
Vladimir Marko33bff252017-11-01 14:35:42 +00001211 bool do_overflow_check =
1212 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001213 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001214 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001215 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001216 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001217 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001218 {
1219 // Ensure that between load and RecordPcInfo there are no pools emitted.
1220 ExactAssemblyScope eas(GetVIXLAssembler(),
1221 kInstructionSize,
1222 CodeBufferCheckScope::kExactSize);
1223 __ ldr(wzr, MemOperand(temp, 0));
1224 RecordPcInfo(nullptr, 0);
1225 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001226 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001227
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001228 if (!HasEmptyFrame()) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001229 // Stack layout:
1230 // sp[frame_size - 8] : lr.
1231 // ... : other preserved core registers.
1232 // ... : other preserved fp registers.
1233 // ... : reserved frame space.
1234 // sp[0] : current method.
Vladimir Marko1a225a72019-07-05 13:37:42 +01001235 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1236 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1237 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1238 DCHECK(!preserved_core_registers.IsEmpty());
1239 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1240 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001241
Vladimir Marko1a225a72019-07-05 13:37:42 +01001242 // Save the current method if we need it, or if using STP reduces code
1243 // size. Note that we do not do this in HCurrentMethod, as the
1244 // instruction might have been removed in the SSA graph.
1245 CPURegister lowest_spill;
1246 if (core_spills_offset == kXRegSizeInBytes) {
1247 // If there is no gap between the method and the lowest core spill, use
1248 // aligned STP pre-index to store both. Max difference is 512. We do
1249 // that to reduce code size even if we do not have to save the method.
1250 DCHECK_LE(frame_size, 512); // 32 core registers are only 256 bytes.
1251 lowest_spill = preserved_core_registers.PopLowestIndex();
1252 __ Stp(kArtMethodRegister, lowest_spill, MemOperand(sp, -frame_size, PreIndex));
1253 } else if (RequiresCurrentMethod()) {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001254 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001255 } else {
1256 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001257 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001258 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Vladimir Marko1a225a72019-07-05 13:37:42 +01001259 if (lowest_spill.IsValid()) {
1260 GetAssembler()->cfi().RelOffset(DWARFReg(lowest_spill), core_spills_offset);
1261 core_spills_offset += kXRegSizeInBytes;
1262 }
1263 GetAssembler()->SpillRegisters(preserved_core_registers, core_spills_offset);
1264 GetAssembler()->SpillRegisters(preserved_fp_registers, fp_spills_offset);
Mingyao Yang063fc772016-08-02 11:02:54 -07001265
1266 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1267 // Initialize should_deoptimize flag to 0.
1268 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1269 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1270 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001271 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001272 MaybeIncrementHotness(/* is_frame_entry= */ true);
Andreas Gampe3db70682018-12-26 15:12:03 -08001273 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001274}
1275
1276void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001277 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001278 if (!HasEmptyFrame()) {
Vladimir Marko1a225a72019-07-05 13:37:42 +01001279 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1280 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1281 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1282 DCHECK(!preserved_core_registers.IsEmpty());
1283 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1284 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
1285
1286 CPURegister lowest_spill;
1287 if (core_spills_offset == kXRegSizeInBytes) {
1288 // If there is no gap between the method and the lowest core spill, use
1289 // aligned LDP pre-index to pop both. Max difference is 504. We do
1290 // that to reduce code size even though the loaded method is unused.
1291 DCHECK_LE(frame_size, 504); // 32 core registers are only 256 bytes.
1292 lowest_spill = preserved_core_registers.PopLowestIndex();
1293 core_spills_offset += kXRegSizeInBytes;
1294 }
1295 GetAssembler()->UnspillRegisters(preserved_fp_registers, fp_spills_offset);
1296 GetAssembler()->UnspillRegisters(preserved_core_registers, core_spills_offset);
1297 if (lowest_spill.IsValid()) {
1298 __ Ldp(xzr, lowest_spill, MemOperand(sp, frame_size, PostIndex));
1299 GetAssembler()->cfi().Restore(DWARFReg(lowest_spill));
1300 } else {
1301 __ Drop(frame_size);
1302 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001303 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001304 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001305 __ Ret();
1306 GetAssembler()->cfi().RestoreState();
1307 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001308}
1309
Scott Wakeling97c72b72016-06-24 16:19:36 +01001310CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001311 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001312 return CPURegList(CPURegister::kRegister, kXRegSize,
1313 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001314}
1315
Scott Wakeling97c72b72016-06-24 16:19:36 +01001316CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001317 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1318 GetNumberOfFloatingPointRegisters()));
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001319 return CPURegList(CPURegister::kVRegister, kDRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001320 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001321}
1322
Alexandre Rames5319def2014-10-23 10:03:10 +01001323void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1324 __ Bind(GetLabelOf(block));
1325}
1326
Calin Juravle175dc732015-08-25 15:42:32 +01001327void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1328 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001329 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001330}
1331
Calin Juravlee460d1d2015-09-29 04:52:17 +01001332void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1333 if (location.IsRegister()) {
1334 locations->AddTemp(location);
1335 } else {
1336 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1337 }
1338}
1339
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001340void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001341 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001342 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001343 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001344 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001345 if (value_can_be_null) {
1346 __ Cbz(value, &done);
1347 }
Roland Levillainc73f0522018-08-14 15:16:50 +01001348 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07001349 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01001350 // Calculate the offset (in the card table) of the card corresponding to
1351 // `object`.
Alexandre Rames5319def2014-10-23 10:03:10 +01001352 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Roland Levillainc73f0522018-08-14 15:16:50 +01001353 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
1354 // `object`'s card.
1355 //
1356 // Register `card` contains the address of the card table. Note that the card
1357 // table's base is biased during its creation so that it always starts at an
1358 // address whose least-significant byte is equal to `kCardDirty` (see
1359 // art::gc::accounting::CardTable::Create). Therefore the STRB instruction
1360 // below writes the `kCardDirty` (byte) value into the `object`'s card
1361 // (located at `card + object >> kCardShift`).
1362 //
1363 // This dual use of the value in register `card` (1. to calculate the location
1364 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
1365 // (no need to explicitly load `kCardDirty` as an immediate value).
Serban Constantinescu02164b32014-11-13 14:05:07 +00001366 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001367 if (value_can_be_null) {
1368 __ Bind(&done);
1369 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001370}
1371
David Brazdil58282f42016-01-14 12:45:10 +00001372void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001373 // Blocked core registers:
1374 // lr : Runtime reserved.
1375 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001376 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001377 // ip1 : VIXL core temp.
1378 // ip0 : VIXL core temp.
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001379 // x18 : Platform register.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001380 //
1381 // Blocked fp registers:
1382 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001383 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1384 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001385 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001386 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001387 }
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001388 blocked_core_registers_[X18] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001389
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001390 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001391 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001392 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001393 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001394
David Brazdil58282f42016-01-14 12:45:10 +00001395 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001396 // Stubs do not save callee-save floating point registers. If the graph
1397 // is debuggable, we need to deal with these registers differently. For
1398 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001399 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1400 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001401 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001402 }
1403 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001404}
1405
Alexandre Rames3e69f162014-12-10 10:36:50 +00001406size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1407 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1408 __ Str(reg, MemOperand(sp, stack_index));
1409 return kArm64WordSize;
1410}
1411
1412size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1413 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1414 __ Ldr(reg, MemOperand(sp, stack_index));
1415 return kArm64WordSize;
1416}
1417
Artem Serov9df37b92019-07-23 16:41:54 +01001418size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1419 uint32_t reg_id ATTRIBUTE_UNUSED) {
1420 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1421 << "use SaveRestoreLiveRegistersHelper";
1422 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001423}
1424
Artem Serov9df37b92019-07-23 16:41:54 +01001425size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1426 uint32_t reg_id ATTRIBUTE_UNUSED) {
1427 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1428 << "use SaveRestoreLiveRegistersHelper";
1429 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001430}
1431
Alexandre Rames5319def2014-10-23 10:03:10 +01001432void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001433 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001434}
1435
1436void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001437 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001438}
1439
Vladimir Markoa0431112018-06-25 09:32:54 +01001440const Arm64InstructionSetFeatures& CodeGeneratorARM64::GetInstructionSetFeatures() const {
1441 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArm64InstructionSetFeatures();
1442}
1443
Alexandre Rames67555f72014-11-18 10:55:16 +00001444void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001445 if (constant->IsIntConstant()) {
1446 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1447 } else if (constant->IsLongConstant()) {
1448 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1449 } else if (constant->IsNullConstant()) {
1450 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001451 } else if (constant->IsFloatConstant()) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001452 __ Fmov(VRegister(destination), constant->AsFloatConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001453 } else {
1454 DCHECK(constant->IsDoubleConstant());
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001455 __ Fmov(VRegister(destination), constant->AsDoubleConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001456 }
1457}
1458
Alexandre Rames3e69f162014-12-10 10:36:50 +00001459
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001460static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001461 DCHECK(constant.IsConstant());
1462 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001463 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001464 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001465 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1466 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1467 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1468 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001469}
1470
Roland Levillain952b2352017-05-03 19:49:14 +01001471// Allocate a scratch register from the VIXL pool, querying first
1472// the floating-point register pool, and then the core register
1473// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001474// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1475// using a different allocation strategy.
1476static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1477 vixl::aarch64::UseScratchRegisterScope* temps,
1478 int size_in_bits) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001479 return masm->GetScratchVRegisterList()->IsEmpty()
Roland Levillain558dea12017-01-27 19:40:44 +00001480 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1481 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1482}
1483
Calin Juravlee460d1d2015-09-29 04:52:17 +01001484void CodeGeneratorARM64::MoveLocation(Location destination,
1485 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001486 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001487 if (source.Equals(destination)) {
1488 return;
1489 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001490
1491 // A valid move can always be inferred from the destination and source
1492 // locations. When moving from and to a register, the argument type can be
1493 // used to generate 32bit instead of 64bit moves. In debug mode we also
1494 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001495 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001496
1497 if (destination.IsRegister() || destination.IsFpuRegister()) {
1498 if (unspecified_type) {
1499 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1500 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001501 (src_cst != nullptr && (src_cst->IsIntConstant()
1502 || src_cst->IsFloatConstant()
1503 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001504 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001505 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001506 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001507 // If the source is a double stack slot or a 64bit constant, a 64bit
1508 // type is appropriate. Else the source is a register, and since the
1509 // type has not been specified, we chose a 64bit type to force a 64bit
1510 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001511 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001512 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001513 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001514 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1515 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001516 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001517 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1518 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1519 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001520 } else if (source.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001521 GetInstructionCodeGeneratorArm64()->LoadSIMDRegFromStack(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001522 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001523 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001524 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001525 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001526 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001527 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001528 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001529 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001530 DataType::Type source_type = DataType::Is64BitType(dst_type)
1531 ? DataType::Type::kInt64
1532 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001533 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1534 }
1535 } else {
1536 DCHECK(source.IsFpuRegister());
1537 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001538 DataType::Type source_type = DataType::Is64BitType(dst_type)
1539 ? DataType::Type::kFloat64
1540 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001541 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1542 } else {
1543 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001544 if (GetGraph()->HasSIMD()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001545 GetInstructionCodeGeneratorArm64()->MoveSIMDRegToSIMDReg(destination, source);
Artem Serovd4bccf12017-04-03 18:47:32 +01001546 } else {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001547 __ Fmov(VRegister(dst), FPRegisterFrom(source, dst_type));
Artem Serovd4bccf12017-04-03 18:47:32 +01001548 }
1549 }
1550 }
1551 } else if (destination.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001552 GetInstructionCodeGeneratorArm64()->MoveToSIMDStackSlot(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001553 } else { // The destination is not a register. It must be a stack slot.
1554 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1555 if (source.IsRegister() || source.IsFpuRegister()) {
1556 if (unspecified_type) {
1557 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001558 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001559 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001560 dst_type =
1561 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001562 }
1563 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001564 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1565 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001566 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001567 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001568 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1569 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001570 UseScratchRegisterScope temps(GetVIXLAssembler());
1571 HConstant* src_cst = source.GetConstant();
1572 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001573 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001574 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1575 ? Register(xzr)
1576 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001577 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001578 if (src_cst->IsIntConstant()) {
1579 temp = temps.AcquireW();
1580 } else if (src_cst->IsLongConstant()) {
1581 temp = temps.AcquireX();
1582 } else if (src_cst->IsFloatConstant()) {
1583 temp = temps.AcquireS();
1584 } else {
1585 DCHECK(src_cst->IsDoubleConstant());
1586 temp = temps.AcquireD();
1587 }
1588 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001589 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001590 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001591 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001592 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001593 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001594 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001595 // Use any scratch register (a core or a floating-point one)
1596 // from VIXL scratch register pools as a temporary.
1597 //
1598 // We used to only use the FP scratch register pool, but in some
1599 // rare cases the only register from this pool (D31) would
1600 // already be used (e.g. within a ParallelMove instruction, when
1601 // a move is blocked by a another move requiring a scratch FP
1602 // register, which would reserve D31). To prevent this issue, we
1603 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001604 //
1605 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001606 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001607 // use AcquireFPOrCoreCPURegisterOfSize instead of
1608 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1609 // allocates core scratch registers first.
1610 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1611 GetVIXLAssembler(),
1612 &temps,
1613 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001614 __ Ldr(temp, StackOperandFrom(source));
1615 __ Str(temp, StackOperandFrom(destination));
1616 }
1617 }
1618}
1619
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001620void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001621 CPURegister dst,
1622 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001623 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001624 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001625 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001626 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001627 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001628 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001629 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001630 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001631 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001632 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001633 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001634 case DataType::Type::kInt16:
1635 __ Ldrsh(Register(dst), src);
1636 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001637 case DataType::Type::kInt32:
1638 case DataType::Type::kReference:
1639 case DataType::Type::kInt64:
1640 case DataType::Type::kFloat32:
1641 case DataType::Type::kFloat64:
1642 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001643 __ Ldr(dst, src);
1644 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001645 case DataType::Type::kUint32:
1646 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001647 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001648 LOG(FATAL) << "Unreachable type " << type;
1649 }
1650}
1651
Calin Juravle77520bc2015-01-12 18:45:46 +00001652void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001653 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001654 const MemOperand& src,
1655 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001656 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001657 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001658 Register temp_base = temps.AcquireX();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001659 DataType::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001660
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001661 DCHECK(!src.IsPreIndex());
1662 DCHECK(!src.IsPostIndex());
1663
1664 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001665 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001666 {
1667 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1668 MemOperand base = MemOperand(temp_base);
1669 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001670 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001671 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001672 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001673 {
1674 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1675 __ ldarb(Register(dst), base);
1676 if (needs_null_check) {
1677 MaybeRecordImplicitNullCheck(instruction);
1678 }
1679 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001680 if (type == DataType::Type::kInt8) {
1681 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001682 }
1683 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001684 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001685 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001686 {
1687 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1688 __ ldarh(Register(dst), base);
1689 if (needs_null_check) {
1690 MaybeRecordImplicitNullCheck(instruction);
1691 }
1692 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001693 if (type == DataType::Type::kInt16) {
1694 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
1695 }
Artem Serov914d7a82017-02-07 14:33:49 +00001696 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001697 case DataType::Type::kInt32:
1698 case DataType::Type::kReference:
1699 case DataType::Type::kInt64:
1700 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001701 {
1702 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1703 __ ldar(Register(dst), base);
1704 if (needs_null_check) {
1705 MaybeRecordImplicitNullCheck(instruction);
1706 }
1707 }
1708 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001709 case DataType::Type::kFloat32:
1710 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00001711 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001712 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001713
Artem Serov914d7a82017-02-07 14:33:49 +00001714 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1715 {
1716 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1717 __ ldar(temp, base);
1718 if (needs_null_check) {
1719 MaybeRecordImplicitNullCheck(instruction);
1720 }
1721 }
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001722 __ Fmov(VRegister(dst), temp);
Artem Serov914d7a82017-02-07 14:33:49 +00001723 break;
Roland Levillain44015862016-01-22 11:47:17 +00001724 }
Aart Bik66c158e2018-01-31 12:55:04 -08001725 case DataType::Type::kUint32:
1726 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001727 case DataType::Type::kVoid:
Artem Serov914d7a82017-02-07 14:33:49 +00001728 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001729 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001730 }
1731}
1732
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001733void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001734 CPURegister src,
1735 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001736 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001737 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001738 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001739 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001740 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001741 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001742 case DataType::Type::kUint16:
1743 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001744 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001745 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001746 case DataType::Type::kInt32:
1747 case DataType::Type::kReference:
1748 case DataType::Type::kInt64:
1749 case DataType::Type::kFloat32:
1750 case DataType::Type::kFloat64:
1751 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001752 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001753 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001754 case DataType::Type::kUint32:
1755 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001756 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001757 LOG(FATAL) << "Unreachable type " << type;
1758 }
1759}
1760
Artem Serov914d7a82017-02-07 14:33:49 +00001761void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001762 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001763 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00001764 const MemOperand& dst,
1765 bool needs_null_check) {
1766 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001767 UseScratchRegisterScope temps(GetVIXLAssembler());
1768 Register temp_base = temps.AcquireX();
1769
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001770 DCHECK(!dst.IsPreIndex());
1771 DCHECK(!dst.IsPostIndex());
1772
1773 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001774 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001775 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001776 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00001777 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001778 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001779 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001780 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001781 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001782 {
1783 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1784 __ stlrb(Register(src), base);
1785 if (needs_null_check) {
1786 MaybeRecordImplicitNullCheck(instruction);
1787 }
1788 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001789 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001790 case DataType::Type::kUint16:
1791 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001792 {
1793 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1794 __ stlrh(Register(src), base);
1795 if (needs_null_check) {
1796 MaybeRecordImplicitNullCheck(instruction);
1797 }
1798 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001799 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001800 case DataType::Type::kInt32:
1801 case DataType::Type::kReference:
1802 case DataType::Type::kInt64:
1803 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001804 {
1805 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1806 __ stlr(Register(src), base);
1807 if (needs_null_check) {
1808 MaybeRecordImplicitNullCheck(instruction);
1809 }
1810 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001811 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001812 case DataType::Type::kFloat32:
1813 case DataType::Type::kFloat64: {
1814 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001815 Register temp_src;
1816 if (src.IsZero()) {
1817 // The zero register is used to avoid synthesizing zero constants.
1818 temp_src = Register(src);
1819 } else {
1820 DCHECK(src.IsFPRegister());
1821 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001822 __ Fmov(temp_src, VRegister(src));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001823 }
Artem Serov914d7a82017-02-07 14:33:49 +00001824 {
1825 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1826 __ stlr(temp_src, base);
1827 if (needs_null_check) {
1828 MaybeRecordImplicitNullCheck(instruction);
1829 }
1830 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001831 break;
1832 }
Aart Bik66c158e2018-01-31 12:55:04 -08001833 case DataType::Type::kUint32:
1834 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001835 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001836 LOG(FATAL) << "Unreachable type " << type;
1837 }
1838}
1839
Calin Juravle175dc732015-08-25 15:42:32 +01001840void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1841 HInstruction* instruction,
1842 uint32_t dex_pc,
1843 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001844 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00001845
Vladimir Markof6675082019-05-17 12:05:28 +01001846 ThreadOffset64 entrypoint_offset = GetThreadOffset<kArm64PointerSize>(entrypoint);
1847 // Reduce code size for AOT by using shared trampolines for slow path runtime calls across the
1848 // entire oat file. This adds an extra branch and we do not want to slow down the main path.
1849 // For JIT, thunk sharing is per-method, so the gains would be smaller or even negative.
Vladimir Marko695348f2020-05-19 14:42:02 +01001850 if (slow_path == nullptr || GetCompilerOptions().IsJitCompiler()) {
Vladimir Markof6675082019-05-17 12:05:28 +01001851 __ Ldr(lr, MemOperand(tr, entrypoint_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00001852 // Ensure the pc position is recorded immediately after the `blr` instruction.
1853 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1854 __ blr(lr);
1855 if (EntrypointRequiresStackMap(entrypoint)) {
1856 RecordPcInfo(instruction, dex_pc, slow_path);
1857 }
Vladimir Markof6675082019-05-17 12:05:28 +01001858 } else {
1859 // Ensure the pc position is recorded immediately after the `bl` instruction.
1860 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1861 EmitEntrypointThunkCall(entrypoint_offset);
1862 if (EntrypointRequiresStackMap(entrypoint)) {
1863 RecordPcInfo(instruction, dex_pc, slow_path);
1864 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001865 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001866}
1867
Roland Levillaindec8f632016-07-22 17:10:06 +01001868void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1869 HInstruction* instruction,
1870 SlowPathCode* slow_path) {
1871 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01001872 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1873 __ Blr(lr);
1874}
1875
Alexandre Rames67555f72014-11-18 10:55:16 +00001876void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001877 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001878 UseScratchRegisterScope temps(GetVIXLAssembler());
1879 Register temp = temps.AcquireW();
Vladimir Markodc682aa2018-01-04 18:42:57 +00001880 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001881 const size_t status_byte_offset =
1882 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1883 constexpr uint32_t shifted_visibly_initialized_value =
1884 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001885
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001886 // CMP (immediate) is limited to imm12 or imm12<<12, so we would need to materialize
1887 // the constant 0xf0000000 for comparison with the full 32-bit field. To reduce the code
1888 // size, load only the high byte of the field and compare with 0xf0.
1889 // Note: The same code size could be achieved with LDR+MNV(asr #24)+CBNZ but benchmarks
1890 // show that this pattern is slower (tested on little cores).
1891 __ Ldrb(temp, HeapOperand(class_reg, status_byte_offset));
1892 __ Cmp(temp, shifted_visibly_initialized_value);
1893 __ B(lo, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001894 __ Bind(slow_path->GetExitLabel());
1895}
Alexandre Rames5319def2014-10-23 10:03:10 +01001896
Vladimir Marko175e7862018-03-27 09:03:13 +00001897void InstructionCodeGeneratorARM64::GenerateBitstringTypeCheckCompare(
1898 HTypeCheckInstruction* check, vixl::aarch64::Register temp) {
1899 uint32_t path_to_root = check->GetBitstringPathToRoot();
1900 uint32_t mask = check->GetBitstringMask();
1901 DCHECK(IsPowerOfTwo(mask + 1));
1902 size_t mask_bits = WhichPowerOf2(mask + 1);
1903
1904 if (mask_bits == 16u) {
1905 // Load only the bitstring part of the status word.
1906 __ Ldrh(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1907 } else {
1908 // /* uint32_t */ temp = temp->status_
1909 __ Ldr(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1910 // Extract the bitstring bits.
1911 __ Ubfx(temp, temp, 0, mask_bits);
1912 }
1913 // Compare the bitstring bits to `path_to_root`.
1914 __ Cmp(temp, path_to_root);
1915}
1916
Roland Levillain44015862016-01-22 11:47:17 +00001917void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001918 BarrierType type = BarrierAll;
1919
1920 switch (kind) {
1921 case MemBarrierKind::kAnyAny:
1922 case MemBarrierKind::kAnyStore: {
1923 type = BarrierAll;
1924 break;
1925 }
1926 case MemBarrierKind::kLoadAny: {
1927 type = BarrierReads;
1928 break;
1929 }
1930 case MemBarrierKind::kStoreStore: {
1931 type = BarrierWrites;
1932 break;
1933 }
1934 default:
1935 LOG(FATAL) << "Unexpected memory barrier " << kind;
1936 }
1937 __ Dmb(InnerShareable, type);
1938}
1939
Serban Constantinescu02164b32014-11-13 14:05:07 +00001940void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1941 HBasicBlock* successor) {
1942 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001943 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1944 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001945 slow_path =
1946 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001947 instruction->SetSlowPath(slow_path);
1948 codegen_->AddSlowPath(slow_path);
1949 if (successor != nullptr) {
1950 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001951 }
1952 } else {
1953 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1954 }
1955
Serban Constantinescu02164b32014-11-13 14:05:07 +00001956 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1957 Register temp = temps.AcquireW();
1958
Andreas Gampe542451c2016-07-26 09:02:02 -07001959 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001960 if (successor == nullptr) {
1961 __ Cbnz(temp, slow_path->GetEntryLabel());
1962 __ Bind(slow_path->GetReturnLabel());
1963 } else {
1964 __ Cbz(temp, codegen_->GetLabelOf(successor));
1965 __ B(slow_path->GetEntryLabel());
1966 // slow_path will return to GetLabelOf(successor).
1967 }
1968}
1969
Alexandre Rames5319def2014-10-23 10:03:10 +01001970InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1971 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001972 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001973 assembler_(codegen->GetAssembler()),
1974 codegen_(codegen) {}
1975
Alexandre Rames67555f72014-11-18 10:55:16 +00001976void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001977 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001978 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001979 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001980 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001981 case DataType::Type::kInt32:
1982 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01001983 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001984 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001985 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001986 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001987
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001988 case DataType::Type::kFloat32:
1989 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001990 locations->SetInAt(0, Location::RequiresFpuRegister());
1991 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001992 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001993 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001994
Alexandre Rames5319def2014-10-23 10:03:10 +01001995 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001996 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001997 }
1998}
1999
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002000void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2001 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002002 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2003
2004 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002005 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01002006 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002007 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2008 object_field_get_with_read_barrier
2009 ? LocationSummary::kCallOnSlowPath
2010 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002011 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002012 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko0ecac682018-08-07 10:40:38 +01002013 // We need a temporary register for the read barrier load in
2014 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2015 // only if the field is volatile or the offset is too big.
2016 if (field_info.IsVolatile() ||
2017 field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2018 locations->AddTemp(FixedTempLocation());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002019 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002020 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002021 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002022 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002023 locations->SetOut(Location::RequiresFpuRegister());
2024 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002025 // The output overlaps for an object field get when read barriers
2026 // are enabled: we do not want the load to overwrite the object's
2027 // location, as we need it to emit the read barrier.
2028 locations->SetOut(
2029 Location::RequiresRegister(),
2030 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002031 }
2032}
2033
2034void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2035 const FieldInfo& field_info) {
2036 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002037 LocationSummary* locations = instruction->GetLocations();
2038 Location base_loc = locations->InAt(0);
2039 Location out = locations->Out();
2040 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01002041 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
2042 DataType::Type load_type = instruction->GetType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002043 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002044
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002045 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01002046 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002047 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002048 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002049 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002050 Location maybe_temp =
2051 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002052 // Note that potential implicit null checks are handled in this
2053 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2054 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2055 instruction,
2056 out,
2057 base,
2058 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002059 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002060 /* needs_null_check= */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002061 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002062 } else {
2063 // General case.
2064 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002065 // Note that a potential implicit null check is handled in this
2066 // CodeGeneratorARM64::LoadAcquire call.
2067 // NB: LoadAcquire will record the pc info if needed.
2068 codegen_->LoadAcquire(
Andreas Gampe3db70682018-12-26 15:12:03 -08002069 instruction, OutputCPURegister(instruction), field, /* needs_null_check= */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002070 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002071 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2072 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01002073 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002074 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002075 }
Vladimir Marko61b92282017-10-11 13:23:17 +01002076 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002077 // If read barriers are enabled, emit read barriers other than
2078 // Baker's using a slow path (and also unpoison the loaded
2079 // reference, if heap poisoning is enabled).
2080 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2081 }
Roland Levillain4d027112015-07-01 15:41:14 +01002082 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002083}
2084
2085void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2086 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002087 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01002088 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002089 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2090 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002091 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002092 locations->SetInAt(1, Location::RequiresFpuRegister());
2093 } else {
2094 locations->SetInAt(1, Location::RequiresRegister());
2095 }
2096}
2097
2098void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002099 const FieldInfo& field_info,
2100 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002101 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2102
2103 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002104 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002105 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002106 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002107 DataType::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002108
Roland Levillain4d027112015-07-01 15:41:14 +01002109 {
2110 // We use a block to end the scratch scope before the write barrier, thus
2111 // freeing the temporary registers so they can be used in `MarkGCCard`.
2112 UseScratchRegisterScope temps(GetVIXLAssembler());
2113
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002114 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002115 DCHECK(value.IsW());
2116 Register temp = temps.AcquireW();
2117 __ Mov(temp, value.W());
2118 GetAssembler()->PoisonHeapReference(temp.W());
2119 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002120 }
Roland Levillain4d027112015-07-01 15:41:14 +01002121
2122 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002123 codegen_->StoreRelease(
Andreas Gampe3db70682018-12-26 15:12:03 -08002124 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check= */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002125 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002126 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2127 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002128 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2129 codegen_->MaybeRecordImplicitNullCheck(instruction);
2130 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002131 }
2132
2133 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002134 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002135 }
2136}
2137
Alexandre Rames67555f72014-11-18 10:55:16 +00002138void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002139 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002140
2141 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002142 case DataType::Type::kInt32:
2143 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002144 Register dst = OutputRegister(instr);
2145 Register lhs = InputRegisterAt(instr, 0);
2146 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002147 if (instr->IsAdd()) {
2148 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002149 } else if (instr->IsAnd()) {
2150 __ And(dst, lhs, rhs);
2151 } else if (instr->IsOr()) {
2152 __ Orr(dst, lhs, rhs);
2153 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002154 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002155 } else if (instr->IsRor()) {
2156 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002157 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002158 __ Ror(dst, lhs, shift);
2159 } else {
2160 // Ensure shift distance is in the same size register as the result. If
2161 // we are rotating a long and the shift comes in a w register originally,
2162 // we don't need to sxtw for use as an x since the shift distances are
2163 // all & reg_bits - 1.
2164 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2165 }
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002166 } else if (instr->IsMin() || instr->IsMax()) {
2167 __ Cmp(lhs, rhs);
2168 __ Csel(dst, lhs, rhs, instr->IsMin() ? lt : gt);
Alexandre Rames67555f72014-11-18 10:55:16 +00002169 } else {
2170 DCHECK(instr->IsXor());
2171 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002172 }
2173 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002174 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002175 case DataType::Type::kFloat32:
2176 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002177 VRegister dst = OutputFPRegister(instr);
2178 VRegister lhs = InputFPRegisterAt(instr, 0);
2179 VRegister rhs = InputFPRegisterAt(instr, 1);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002180 if (instr->IsAdd()) {
2181 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002182 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002183 __ Fsub(dst, lhs, rhs);
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002184 } else if (instr->IsMin()) {
2185 __ Fmin(dst, lhs, rhs);
2186 } else if (instr->IsMax()) {
2187 __ Fmax(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002188 } else {
2189 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002190 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002191 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002192 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002193 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002194 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002195 }
2196}
2197
Serban Constantinescu02164b32014-11-13 14:05:07 +00002198void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2199 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2200
Vladimir Markoca6fff82017-10-03 14:49:14 +01002201 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002202 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002203 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002204 case DataType::Type::kInt32:
2205 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002206 locations->SetInAt(0, Location::RequiresRegister());
2207 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002208 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002209 break;
2210 }
2211 default:
2212 LOG(FATAL) << "Unexpected shift type " << type;
2213 }
2214}
2215
2216void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2217 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2218
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002219 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002220 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002221 case DataType::Type::kInt32:
2222 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002223 Register dst = OutputRegister(instr);
2224 Register lhs = InputRegisterAt(instr, 0);
2225 Operand rhs = InputOperandAt(instr, 1);
2226 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002227 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002228 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002229 if (instr->IsShl()) {
2230 __ Lsl(dst, lhs, shift_value);
2231 } else if (instr->IsShr()) {
2232 __ Asr(dst, lhs, shift_value);
2233 } else {
2234 __ Lsr(dst, lhs, shift_value);
2235 }
2236 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002237 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002238
2239 if (instr->IsShl()) {
2240 __ Lsl(dst, lhs, rhs_reg);
2241 } else if (instr->IsShr()) {
2242 __ Asr(dst, lhs, rhs_reg);
2243 } else {
2244 __ Lsr(dst, lhs, rhs_reg);
2245 }
2246 }
2247 break;
2248 }
2249 default:
2250 LOG(FATAL) << "Unexpected shift operation type " << type;
2251 }
2252}
2253
Alexandre Rames5319def2014-10-23 10:03:10 +01002254void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002255 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002256}
2257
2258void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002259 HandleBinaryOp(instruction);
2260}
2261
2262void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2263 HandleBinaryOp(instruction);
2264}
2265
2266void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2267 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002268}
2269
Artem Serov7fc63502016-02-09 17:15:29 +00002270void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002271 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002272 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002273 locations->SetInAt(0, Location::RequiresRegister());
2274 // There is no immediate variant of negated bitwise instructions in AArch64.
2275 locations->SetInAt(1, Location::RequiresRegister());
2276 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2277}
2278
Artem Serov7fc63502016-02-09 17:15:29 +00002279void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002280 Register dst = OutputRegister(instr);
2281 Register lhs = InputRegisterAt(instr, 0);
2282 Register rhs = InputRegisterAt(instr, 1);
2283
2284 switch (instr->GetOpKind()) {
2285 case HInstruction::kAnd:
2286 __ Bic(dst, lhs, rhs);
2287 break;
2288 case HInstruction::kOr:
2289 __ Orn(dst, lhs, rhs);
2290 break;
2291 case HInstruction::kXor:
2292 __ Eon(dst, lhs, rhs);
2293 break;
2294 default:
2295 LOG(FATAL) << "Unreachable";
2296 }
2297}
2298
Anton Kirilov74234da2017-01-13 14:42:47 +00002299void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2300 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002301 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2302 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002303 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002304 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002305 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2306 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2307 } else {
2308 locations->SetInAt(0, Location::RequiresRegister());
2309 }
2310 locations->SetInAt(1, Location::RequiresRegister());
2311 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2312}
2313
Anton Kirilov74234da2017-01-13 14:42:47 +00002314void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2315 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002316 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002317 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002318 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002319 Register out = OutputRegister(instruction);
2320 Register left;
2321 if (kind != HInstruction::kNeg) {
2322 left = InputRegisterAt(instruction, 0);
2323 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002324 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002325 // shifter operand operation, the IR generating `right_reg` (input to the type
2326 // conversion) can have a different type from the current instruction's type,
2327 // so we manually indicate the type.
2328 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002329 Operand right_operand(0);
2330
Anton Kirilov74234da2017-01-13 14:42:47 +00002331 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2332 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002333 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2334 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002335 right_operand = Operand(right_reg,
2336 helpers::ShiftFromOpKind(op_kind),
2337 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002338 }
2339
2340 // Logical binary operations do not support extension operations in the
2341 // operand. Note that VIXL would still manage if it was passed by generating
2342 // the extension as a separate instruction.
2343 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2344 DCHECK(!right_operand.IsExtendedRegister() ||
2345 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2346 kind != HInstruction::kNeg));
2347 switch (kind) {
2348 case HInstruction::kAdd:
2349 __ Add(out, left, right_operand);
2350 break;
2351 case HInstruction::kAnd:
2352 __ And(out, left, right_operand);
2353 break;
2354 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002355 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002356 __ Neg(out, right_operand);
2357 break;
2358 case HInstruction::kOr:
2359 __ Orr(out, left, right_operand);
2360 break;
2361 case HInstruction::kSub:
2362 __ Sub(out, left, right_operand);
2363 break;
2364 case HInstruction::kXor:
2365 __ Eor(out, left, right_operand);
2366 break;
2367 default:
2368 LOG(FATAL) << "Unexpected operation kind: " << kind;
2369 UNREACHABLE();
2370 }
2371}
2372
Artem Serov328429f2016-07-06 16:23:04 +01002373void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002374 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002375 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002376 locations->SetInAt(0, Location::RequiresRegister());
2377 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002378 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002379}
2380
Roland Levillain19c54192016-11-04 13:44:09 +00002381void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002382 __ Add(OutputRegister(instruction),
2383 InputRegisterAt(instruction, 0),
2384 Operand(InputOperandAt(instruction, 1)));
2385}
2386
Artem Serove1811ed2017-04-27 16:50:47 +01002387void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2388 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002389 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002390
2391 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2392
2393 locations->SetInAt(0, Location::RequiresRegister());
2394 // For byte case we don't need to shift the index variable so we can encode the data offset into
2395 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2396 // data offset constant generation out of the loop and reduce the critical path length in the
2397 // loop.
2398 locations->SetInAt(1, shift->GetValue() == 0
2399 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2400 : Location::RequiresRegister());
2401 locations->SetInAt(2, Location::ConstantLocation(shift));
2402 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2403}
2404
2405void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2406 HIntermediateAddressIndex* instruction) {
2407 Register index_reg = InputRegisterAt(instruction, 0);
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002408 uint32_t shift = Int64FromLocation(instruction->GetLocations()->InAt(2));
Artem Serove1811ed2017-04-27 16:50:47 +01002409 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2410
2411 if (shift == 0) {
2412 __ Add(OutputRegister(instruction), index_reg, offset);
2413 } else {
2414 Register offset_reg = InputRegisterAt(instruction, 1);
2415 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2416 }
2417}
2418
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002419void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002420 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002421 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002422 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2423 if (instr->GetOpKind() == HInstruction::kSub &&
2424 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002425 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002426 // Don't allocate register for Mneg instruction.
2427 } else {
2428 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2429 Location::RequiresRegister());
2430 }
2431 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2432 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002433 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2434}
2435
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002436void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002437 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002438 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2439 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002440
2441 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2442 // This fixup should be carried out for all multiply-accumulate instructions:
2443 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002444 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002445 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2446 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002447 vixl::aarch64::Instruction* prev =
2448 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002449 if (prev->IsLoadOrStore()) {
2450 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002451 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002452 __ nop();
2453 }
2454 }
2455
2456 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002457 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002458 __ Madd(res, mul_left, mul_right, accumulator);
2459 } else {
2460 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002461 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002462 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002463 __ Mneg(res, mul_left, mul_right);
2464 } else {
2465 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2466 __ Msub(res, mul_left, mul_right, accumulator);
2467 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002468 }
2469}
2470
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002471void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002472 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002473 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002474 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002475 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2476 object_array_get_with_read_barrier
2477 ? LocationSummary::kCallOnSlowPath
2478 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002479 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002480 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002481 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002482 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002483 // We need a temporary register for the read barrier load in
2484 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2485 // only if the offset is too big.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002486 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2487 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002488 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002489 if (offset >= kReferenceLoadMinFarOffset) {
2490 locations->AddTemp(FixedTempLocation());
2491 }
Artem Serov0806f582018-10-11 20:14:20 +01002492 } else if (!instruction->GetArray()->IsIntermediateAddress()) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01002493 // We need a non-scratch temporary for the array data pointer in
Artem Serov0806f582018-10-11 20:14:20 +01002494 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier() for the case with no
2495 // intermediate address.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002496 locations->AddTemp(Location::RequiresRegister());
2497 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002498 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002499 locations->SetInAt(0, Location::RequiresRegister());
2500 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002501 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002502 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2503 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002504 // The output overlaps in the case of an object array get with
2505 // read barriers enabled: we do not want the move to overwrite the
2506 // array's location, as we need it to emit the read barrier.
2507 locations->SetOut(
2508 Location::RequiresRegister(),
2509 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002510 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002511}
2512
2513void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002514 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002515 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002516 LocationSummary* locations = instruction->GetLocations();
2517 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002518 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002519 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002520 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2521 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002522 MacroAssembler* masm = GetVIXLAssembler();
2523 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002524
Artem Serov0806f582018-10-11 20:14:20 +01002525 // The non-Baker read barrier instrumentation of object ArrayGet instructions
Roland Levillain19c54192016-11-04 13:44:09 +00002526 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002527 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002528 instruction->GetArray()->IsIntermediateAddress() &&
Artem Serov0806f582018-10-11 20:14:20 +01002529 kEmitCompilerReadBarrier &&
2530 !kUseBakerReadBarrier));
Roland Levillain19c54192016-11-04 13:44:09 +00002531
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002532 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002533 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002534 // Note that a potential implicit null check is handled in the
2535 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002536 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002537 if (index.IsConstant()) {
Artem Serov0806f582018-10-11 20:14:20 +01002538 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002539 // Array load with a constant index can be treated as a field load.
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002540 offset += Int64FromLocation(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002541 Location maybe_temp =
2542 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2543 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2544 out,
2545 obj.W(),
2546 offset,
2547 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002548 /* needs_null_check= */ false,
2549 /* use_load_acquire= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002550 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002551 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08002552 instruction, out, obj.W(), offset, index, /* needs_null_check= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002553 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002554 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002555 // General case.
2556 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002557 Register length;
2558 if (maybe_compressed_char_at) {
2559 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2560 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002561 {
2562 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2563 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2564
2565 if (instruction->GetArray()->IsIntermediateAddress()) {
2566 DCHECK_LT(count_offset, offset);
2567 int64_t adjusted_offset =
2568 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2569 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2570 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2571 } else {
2572 __ Ldr(length, HeapOperand(obj, count_offset));
2573 }
2574 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002575 }
jessicahandojo05765752016-09-09 19:01:32 -07002576 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002577 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002578 if (maybe_compressed_char_at) {
2579 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002580 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2581 "Expecting 0=compressed, 1=uncompressed");
2582 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002583 __ Ldrb(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002584 HeapOperand(obj, offset + Int64FromLocation(index)));
jessicahandojo05765752016-09-09 19:01:32 -07002585 __ B(&done);
2586 __ Bind(&uncompressed_load);
2587 __ Ldrh(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002588 HeapOperand(obj, offset + (Int64FromLocation(index) << 1)));
jessicahandojo05765752016-09-09 19:01:32 -07002589 __ Bind(&done);
2590 } else {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002591 offset += Int64FromLocation(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002592 source = HeapOperand(obj, offset);
2593 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002594 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002595 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002596 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002597 // We do not need to compute the intermediate address from the array: the
2598 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002599 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002600 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002601 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2602 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
Roland Levillain44015862016-01-22 11:47:17 +00002603 }
2604 temp = obj;
2605 } else {
2606 __ Add(temp, obj, offset);
2607 }
jessicahandojo05765752016-09-09 19:01:32 -07002608 if (maybe_compressed_char_at) {
2609 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002610 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2611 "Expecting 0=compressed, 1=uncompressed");
2612 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002613 __ Ldrb(Register(OutputCPURegister(instruction)),
2614 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2615 __ B(&done);
2616 __ Bind(&uncompressed_load);
2617 __ Ldrh(Register(OutputCPURegister(instruction)),
2618 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2619 __ Bind(&done);
2620 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002621 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002622 }
Roland Levillain44015862016-01-22 11:47:17 +00002623 }
jessicahandojo05765752016-09-09 19:01:32 -07002624 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002625 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2626 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002627 codegen_->Load(type, OutputCPURegister(instruction), source);
2628 codegen_->MaybeRecordImplicitNullCheck(instruction);
2629 }
Roland Levillain44015862016-01-22 11:47:17 +00002630
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002631 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002632 static_assert(
2633 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2634 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2635 Location obj_loc = locations->InAt(0);
2636 if (index.IsConstant()) {
2637 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2638 } else {
2639 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2640 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002641 }
Roland Levillain4d027112015-07-01 15:41:14 +01002642 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002643}
2644
Alexandre Rames5319def2014-10-23 10:03:10 +01002645void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002646 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002647 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002648 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002649}
2650
2651void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002652 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002653 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002654 {
2655 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2656 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2657 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2658 codegen_->MaybeRecordImplicitNullCheck(instruction);
2659 }
jessicahandojo05765752016-09-09 19:01:32 -07002660 // Mask out compression flag from String's array length.
2661 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002662 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002663 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002664}
2665
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002666void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002667 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002668
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002669 bool needs_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002670 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002671 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002672 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002673 locations->SetInAt(0, Location::RequiresRegister());
2674 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002675 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2676 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002677 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002678 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002679 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002680 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002681 }
2682}
2683
2684void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002685 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002686 LocationSummary* locations = instruction->GetLocations();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002687 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002688 bool needs_write_barrier =
2689 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002690
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002691 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002692 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002693 CPURegister source = value;
2694 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002695 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002696 MemOperand destination = HeapOperand(array);
2697 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002698
2699 if (!needs_write_barrier) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002700 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002701 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002702 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002703 destination = HeapOperand(array, offset);
2704 } else {
2705 UseScratchRegisterScope temps(masm);
2706 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002707 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002708 // We do not need to compute the intermediate address from the array: the
2709 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002710 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002711 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002712 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2713 DCHECK(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002714 }
2715 temp = array;
2716 } else {
2717 __ Add(temp, array, offset);
2718 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002719 destination = HeapOperand(temp,
2720 XRegisterFrom(index),
2721 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002722 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002723 }
Artem Serov914d7a82017-02-07 14:33:49 +00002724 {
2725 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2726 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2727 codegen_->Store(value_type, value, destination);
2728 codegen_->MaybeRecordImplicitNullCheck(instruction);
2729 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002730 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002731 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002732
2733 bool can_value_be_null = instruction->GetValueCanBeNull();
2734 vixl::aarch64::Label do_store;
2735 if (can_value_be_null) {
2736 __ Cbz(Register(value), &do_store);
2737 }
2738
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002739 SlowPathCodeARM64* slow_path = nullptr;
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002740 if (needs_type_check) {
2741 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
2742 codegen_->AddSlowPath(slow_path);
2743
2744 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2745 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2746 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2747
Alexandre Rames97833a02015-04-16 15:07:12 +01002748 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002749 Register temp = temps.AcquireSameSizeAs(array);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002750 Register temp2 = temps.AcquireSameSizeAs(array);
2751
2752 // Note that when Baker read barriers are enabled, the type
2753 // checks are performed without read barriers. This is fine,
2754 // even in the case where a class object is in the from-space
2755 // after the flip, as a comparison involving such a type would
2756 // not produce a false positive; it may of course produce a
2757 // false negative, in which case we would take the ArraySet
2758 // slow path.
2759
2760 // /* HeapReference<Class> */ temp = array->klass_
2761 {
2762 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2763 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2764 __ Ldr(temp, HeapOperand(array, class_offset));
2765 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames97833a02015-04-16 15:07:12 +01002766 }
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002767 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Alexandre Rames97833a02015-04-16 15:07:12 +01002768
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002769 // /* HeapReference<Class> */ temp = temp->component_type_
2770 __ Ldr(temp, HeapOperand(temp, component_offset));
2771 // /* HeapReference<Class> */ temp2 = value->klass_
2772 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2773 // If heap poisoning is enabled, no need to unpoison `temp`
2774 // nor `temp2`, as we are comparing two poisoned references.
2775 __ Cmp(temp, temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002776
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002777 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2778 vixl::aarch64::Label do_put;
2779 __ B(eq, &do_put);
2780 // If heap poisoning is enabled, the `temp` reference has
2781 // not been unpoisoned yet; unpoison it now.
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002782 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002783
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002784 // /* HeapReference<Class> */ temp = temp->super_class_
2785 __ Ldr(temp, HeapOperand(temp, super_offset));
2786 // If heap poisoning is enabled, no need to unpoison
2787 // `temp`, as we are comparing against null below.
2788 __ Cbnz(temp, slow_path->GetEntryLabel());
2789 __ Bind(&do_put);
Vladimir Markod1ef8732017-04-18 13:55:13 +01002790 } else {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002791 __ B(ne, slow_path->GetEntryLabel());
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002792 }
2793 }
2794
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002795 codegen_->MarkGCCard(array, value.W(), /* value_can_be_null= */ false);
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002796
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002797 if (can_value_be_null) {
2798 DCHECK(do_store.IsLinked());
2799 __ Bind(&do_store);
2800 }
2801
2802 UseScratchRegisterScope temps(masm);
2803 if (kPoisonHeapReferences) {
2804 Register temp_source = temps.AcquireSameSizeAs(array);
2805 DCHECK(value.IsW());
2806 __ Mov(temp_source, value.W());
2807 GetAssembler()->PoisonHeapReference(temp_source);
2808 source = temp_source;
2809 }
2810
2811 if (index.IsConstant()) {
2812 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
2813 destination = HeapOperand(array, offset);
2814 } else {
2815 Register temp_base = temps.AcquireSameSizeAs(array);
2816 __ Add(temp_base, array, offset);
2817 destination = HeapOperand(temp_base,
2818 XRegisterFrom(index),
2819 LSL,
2820 DataType::SizeShift(value_type));
2821 }
2822
2823 {
2824 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2825 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2826 __ Str(source, destination);
2827
2828 if (can_value_be_null || !needs_type_check) {
2829 codegen_->MaybeRecordImplicitNullCheck(instruction);
2830 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002831 }
2832
2833 if (slow_path != nullptr) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002834 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002835 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002836 }
2837}
2838
Alexandre Rames67555f72014-11-18 10:55:16 +00002839void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002840 RegisterSet caller_saves = RegisterSet::Empty();
2841 InvokeRuntimeCallingConvention calling_convention;
2842 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2843 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2844 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002845
2846 // If both index and length are constant, we can check the bounds statically and
2847 // generate code accordingly. We want to make sure we generate constant locations
2848 // in that case, regardless of whether they are encodable in the comparison or not.
2849 HInstruction* index = instruction->InputAt(0);
2850 HInstruction* length = instruction->InputAt(1);
2851 bool both_const = index->IsConstant() && length->IsConstant();
2852 locations->SetInAt(0, both_const
2853 ? Location::ConstantLocation(index->AsConstant())
2854 : ARM64EncodableConstantOrRegister(index, instruction));
2855 locations->SetInAt(1, both_const
2856 ? Location::ConstantLocation(length->AsConstant())
2857 : ARM64EncodableConstantOrRegister(length, instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002858}
2859
2860void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Georgia Kouvelibe530852019-01-17 10:46:41 +00002861 LocationSummary* locations = instruction->GetLocations();
2862 Location index_loc = locations->InAt(0);
2863 Location length_loc = locations->InAt(1);
2864
2865 int cmp_first_input = 0;
2866 int cmp_second_input = 1;
2867 Condition cond = hs;
2868
2869 if (index_loc.IsConstant()) {
2870 int64_t index = Int64FromLocation(index_loc);
2871 if (length_loc.IsConstant()) {
2872 int64_t length = Int64FromLocation(length_loc);
2873 if (index < 0 || index >= length) {
2874 BoundsCheckSlowPathARM64* slow_path =
2875 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
2876 codegen_->AddSlowPath(slow_path);
2877 __ B(slow_path->GetEntryLabel());
2878 } else {
2879 // BCE will remove the bounds check if we are guaranteed to pass.
2880 // However, some optimization after BCE may have generated this, and we should not
2881 // generate a bounds check if it is a valid range.
2882 }
2883 return;
2884 }
2885 // Only the index is constant: change the order of the operands and commute the condition
2886 // so we can use an immediate constant for the index (only the second input to a cmp
2887 // instruction can be an immediate).
2888 cmp_first_input = 1;
2889 cmp_second_input = 0;
2890 cond = ls;
2891 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002892 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002893 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002894 __ Cmp(InputRegisterAt(instruction, cmp_first_input),
2895 InputOperandAt(instruction, cmp_second_input));
Alexandre Rames67555f72014-11-18 10:55:16 +00002896 codegen_->AddSlowPath(slow_path);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002897 __ B(slow_path->GetEntryLabel(), cond);
Alexandre Rames67555f72014-11-18 10:55:16 +00002898}
2899
Alexandre Rames67555f72014-11-18 10:55:16 +00002900void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2901 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002902 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00002903 locations->SetInAt(0, Location::RequiresRegister());
2904 if (check->HasUses()) {
2905 locations->SetOut(Location::SameAsFirstInput());
2906 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01002907 // Rely on the type initialization to save everything we need.
2908 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Alexandre Rames67555f72014-11-18 10:55:16 +00002909}
2910
2911void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2912 // We assume the class is not null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01002913 SlowPathCodeARM64* slow_path =
2914 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(check->GetLoadClass(), check);
Alexandre Rames67555f72014-11-18 10:55:16 +00002915 codegen_->AddSlowPath(slow_path);
2916 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2917}
2918
Roland Levillain1a653882016-03-18 18:05:57 +00002919static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2920 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2921 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2922}
2923
2924void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002925 VRegister lhs_reg = InputFPRegisterAt(instruction, 0);
Roland Levillain1a653882016-03-18 18:05:57 +00002926 Location rhs_loc = instruction->GetLocations()->InAt(1);
2927 if (rhs_loc.IsConstant()) {
2928 // 0.0 is the only immediate that can be encoded directly in
2929 // an FCMP instruction.
2930 //
2931 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2932 // specify that in a floating-point comparison, positive zero
2933 // and negative zero are considered equal, so we can use the
2934 // literal 0.0 for both cases here.
2935 //
2936 // Note however that some methods (Float.equal, Float.compare,
2937 // Float.compareTo, Double.equal, Double.compare,
2938 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2939 // StrictMath.min) consider 0.0 to be (strictly) greater than
2940 // -0.0. So if we ever translate calls to these methods into a
2941 // HCompare instruction, we must handle the -0.0 case with
2942 // care here.
2943 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2944 __ Fcmp(lhs_reg, 0.0);
2945 } else {
2946 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2947 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002948}
2949
Serban Constantinescu02164b32014-11-13 14:05:07 +00002950void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002951 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002952 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002953 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002954 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002955 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002956 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002957 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002958 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002959 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002960 case DataType::Type::kInt32:
2961 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002962 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002963 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002964 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2965 break;
2966 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002967 case DataType::Type::kFloat32:
2968 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002969 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002970 locations->SetInAt(1,
2971 IsFloatingPointZeroConstant(compare->InputAt(1))
2972 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2973 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002974 locations->SetOut(Location::RequiresRegister());
2975 break;
2976 }
2977 default:
2978 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2979 }
2980}
2981
2982void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002983 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002984
2985 // 0 if: left == right
2986 // 1 if: left > right
2987 // -1 if: left < right
2988 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002989 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002990 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002991 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002992 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002993 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002994 case DataType::Type::kInt32:
2995 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002996 Register result = OutputRegister(compare);
2997 Register left = InputRegisterAt(compare, 0);
2998 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002999 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003000 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3001 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003002 break;
3003 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003004 case DataType::Type::kFloat32:
3005 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003006 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003007 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003008 __ Cset(result, ne);
3009 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003010 break;
3011 }
3012 default:
3013 LOG(FATAL) << "Unimplemented compare type " << in_type;
3014 }
3015}
3016
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003017void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003018 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003019
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003020 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003021 locations->SetInAt(0, Location::RequiresFpuRegister());
3022 locations->SetInAt(1,
3023 IsFloatingPointZeroConstant(instruction->InputAt(1))
3024 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3025 : Location::RequiresFpuRegister());
3026 } else {
3027 // Integer cases.
3028 locations->SetInAt(0, Location::RequiresRegister());
3029 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3030 }
3031
David Brazdilb3e773e2016-01-26 11:28:37 +00003032 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003033 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003034 }
3035}
3036
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003037void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003038 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003039 return;
3040 }
3041
3042 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003043 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003044 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003045
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003046 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003047 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003048 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003049 } else {
3050 // Integer cases.
3051 Register lhs = InputRegisterAt(instruction, 0);
3052 Operand rhs = InputOperandAt(instruction, 1);
3053 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003054 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003055 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003056}
3057
3058#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3059 M(Equal) \
3060 M(NotEqual) \
3061 M(LessThan) \
3062 M(LessThanOrEqual) \
3063 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003064 M(GreaterThanOrEqual) \
3065 M(Below) \
3066 M(BelowOrEqual) \
3067 M(Above) \
3068 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003069#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003070void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3071void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003072FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003073#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003074#undef FOR_EACH_CONDITION_INSTRUCTION
3075
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003076void InstructionCodeGeneratorARM64::GenerateIntDivForPower2Denom(HDiv* instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003077 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003078 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003079 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
3080
3081 Register out = OutputRegister(instruction);
3082 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003083
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003084 Register final_dividend;
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003085 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003086 // No need to adjust the result for non-negative dividends or the INT32_MIN/INT64_MIN dividends.
3087 // NOTE: The generated code for HDiv correctly works for the INT32_MIN/INT64_MIN dividends:
3088 // imm == 2
3089 // add out, dividend(0x80000000), dividend(0x80000000), lsr #31 => out = 0x80000001
3090 // asr out, out(0x80000001), #1 => out = 0xc0000000
3091 // This is the same as 'asr out, 0x80000000, #1'
3092 //
3093 // imm > 2
3094 // add temp, dividend(0x80000000), imm - 1 => temp = 0b10..01..1, where the number
3095 // of the rightmost 1s is ctz_imm.
3096 // cmp dividend(0x80000000), 0 => N = 1, V = 0 (lt is true)
3097 // csel out, temp(0b10..01..1), dividend(0x80000000), lt => out = 0b10..01..1
3098 // asr out, out(0b10..01..1), #ctz_imm => out = 0b1..10..0, where the number of the
3099 // leftmost 1s is ctz_imm + 1.
3100 // This is the same as 'asr out, dividend(0x80000000), #ctz_imm'.
3101 //
3102 // imm == INT32_MIN
3103 // add tmp, dividend(0x80000000), #0x7fffffff => tmp = -1
3104 // cmp dividend(0x80000000), 0 => N = 1, V = 0 (lt is true)
3105 // csel out, temp(-1), dividend(0x80000000), lt => out = -1
3106 // neg out, out(-1), asr #31 => out = 1
3107 // This is the same as 'neg out, dividend(0x80000000), asr #31'.
3108 final_dividend = dividend;
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003109 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003110 if (abs_imm == 2) {
3111 int bits = DataType::Size(instruction->GetResultType()) * kBitsPerByte;
3112 __ Add(out, dividend, Operand(dividend, LSR, bits - 1));
3113 } else {
3114 UseScratchRegisterScope temps(GetVIXLAssembler());
3115 Register temp = temps.AcquireSameSizeAs(out);
3116 __ Add(temp, dividend, abs_imm - 1);
3117 __ Cmp(dividend, 0);
3118 __ Csel(out, temp, dividend, lt);
3119 }
3120 final_dividend = out;
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003121 }
3122
Zheng Xuc6667102015-05-15 16:08:45 +08003123 int ctz_imm = CTZ(abs_imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003124 if (imm > 0) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003125 __ Asr(out, final_dividend, ctz_imm);
Zheng Xuc6667102015-05-15 16:08:45 +08003126 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003127 __ Neg(out, Operand(final_dividend, ASR, ctz_imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003128 }
3129}
3130
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003131// Return true if the magic number was modified by subtracting 2^32(Int32 div) or 2^64(Int64 div).
3132// So dividend needs to be added.
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003133static inline bool NeedToAddDividend(int64_t magic_number, int64_t divisor) {
3134 return divisor > 0 && magic_number < 0;
3135}
3136
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003137// Return true if the magic number was modified by adding 2^32(Int32 div) or 2^64(Int64 div).
3138// So dividend needs to be subtracted.
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003139static inline bool NeedToSubDividend(int64_t magic_number, int64_t divisor) {
3140 return divisor < 0 && magic_number > 0;
3141}
3142
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003143// Generate code which increments the value in register 'in' by 1 if the value is negative.
3144// It is done with 'add out, in, in, lsr #31 or #63'.
3145// If the value is a result of an operation setting the N flag, CINC MI can be used
3146// instead of ADD. 'use_cond_inc' controls this.
3147void InstructionCodeGeneratorARM64::GenerateIncrementNegativeByOne(
3148 Register out,
3149 Register in,
3150 bool use_cond_inc) {
3151 if (use_cond_inc) {
3152 __ Cinc(out, in, mi);
3153 } else {
3154 __ Add(out, in, Operand(in, LSR, in.GetSizeInBits() - 1));
3155 }
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003156}
3157
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003158// Helper to generate code producing the result of HRem with a constant divisor.
3159void InstructionCodeGeneratorARM64::GenerateResultRemWithAnyConstant(
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003160 Register out,
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003161 Register dividend,
3162 Register quotient,
3163 int64_t divisor,
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003164 UseScratchRegisterScope* temps_scope) {
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003165 Register temp_imm = temps_scope->AcquireSameSizeAs(out);
3166 __ Mov(temp_imm, divisor);
3167 __ Msub(out, quotient, temp_imm, dividend);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003168}
3169
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003170// Helper to generate code for HDiv/HRem instructions when a dividend is non-negative and
3171// a divisor is a positive constant, not power of 2.
3172void InstructionCodeGeneratorARM64::GenerateInt64UnsignedDivRemWithAnyPositiveConstant(
3173 HBinaryOperation* instruction) {
3174 DCHECK(instruction->IsDiv() || instruction->IsRem());
3175 DCHECK(instruction->GetResultType() == DataType::Type::kInt64);
3176
3177 LocationSummary* locations = instruction->GetLocations();
3178 Location second = locations->InAt(1);
3179 DCHECK(second.IsConstant());
3180
3181 Register out = OutputRegister(instruction);
3182 Register dividend = InputRegisterAt(instruction, 0);
3183 int64_t imm = Int64FromConstant(second.GetConstant());
3184 DCHECK_GT(imm, 0);
3185
3186 int64_t magic;
3187 int shift;
3188 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ true, &magic, &shift);
3189
3190 UseScratchRegisterScope temps(GetVIXLAssembler());
3191 Register temp = temps.AcquireSameSizeAs(out);
3192
3193 auto generate_unsigned_div_code = [this, magic, shift](Register out,
3194 Register dividend,
3195 Register temp) {
3196 // temp = get_high(dividend * magic)
3197 __ Mov(temp, magic);
3198 if (magic > 0 && shift == 0) {
3199 __ Smulh(out, dividend, temp);
3200 } else {
3201 __ Smulh(temp, dividend, temp);
3202 if (magic < 0) {
3203 // The negative magic means that the multiplier m is greater than INT64_MAX.
3204 // In such a case shift is never 0. See the proof in
3205 // InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant.
3206 __ Add(temp, temp, dividend);
3207 }
3208 DCHECK_NE(shift, 0);
3209 __ Lsr(out, temp, shift);
3210 }
3211 };
3212
3213 if (instruction->IsDiv()) {
3214 generate_unsigned_div_code(out, dividend, temp);
3215 } else {
3216 generate_unsigned_div_code(temp, dividend, temp);
3217 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3218 }
3219}
3220
3221// Helper to generate code for HDiv/HRem instructions for any dividend and a constant divisor
3222// (not power of 2).
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003223void InstructionCodeGeneratorARM64::GenerateInt64DivRemWithAnyConstant(
3224 HBinaryOperation* instruction) {
Zheng Xuc6667102015-05-15 16:08:45 +08003225 DCHECK(instruction->IsDiv() || instruction->IsRem());
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003226 DCHECK(instruction->GetResultType() == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003227
3228 LocationSummary* locations = instruction->GetLocations();
3229 Location second = locations->InAt(1);
3230 DCHECK(second.IsConstant());
3231
3232 Register out = OutputRegister(instruction);
3233 Register dividend = InputRegisterAt(instruction, 0);
3234 int64_t imm = Int64FromConstant(second.GetConstant());
3235
Zheng Xuc6667102015-05-15 16:08:45 +08003236 int64_t magic;
3237 int shift;
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003238 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ true, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08003239
3240 UseScratchRegisterScope temps(GetVIXLAssembler());
3241 Register temp = temps.AcquireSameSizeAs(out);
3242
3243 // temp = get_high(dividend * magic)
3244 __ Mov(temp, magic);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003245 __ Smulh(temp, dividend, temp);
3246
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003247 // The multiplication result might need some corrections to be finalized.
3248 // The last correction is to increment by 1, if the result is negative.
3249 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3250 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3251 // However if one of the corrections is ADD or SUB, the sign can be detected
3252 // with ADDS/SUBS. They set the N flag if the result is negative.
3253 // This allows to use CINC MI which has latency 1.
3254 bool use_cond_inc = false;
3255
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003256 // Some combinations of magic_number and the divisor require to correct the result.
3257 // Check whether the correction is needed.
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003258 if (NeedToAddDividend(magic, imm)) {
3259 __ Adds(temp, temp, dividend);
3260 use_cond_inc = true;
3261 } else if (NeedToSubDividend(magic, imm)) {
3262 __ Subs(temp, temp, dividend);
3263 use_cond_inc = true;
3264 }
3265
3266 if (shift != 0) {
3267 __ Asr(temp, temp, shift);
3268 }
3269
3270 if (instruction->IsRem()) {
3271 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3272 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3273 } else {
3274 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3275 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003276}
3277
3278void InstructionCodeGeneratorARM64::GenerateInt32DivRemWithAnyConstant(
3279 HBinaryOperation* instruction) {
3280 DCHECK(instruction->IsDiv() || instruction->IsRem());
3281 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
3282
3283 LocationSummary* locations = instruction->GetLocations();
3284 Location second = locations->InAt(1);
3285 DCHECK(second.IsConstant());
3286
3287 Register out = OutputRegister(instruction);
3288 Register dividend = InputRegisterAt(instruction, 0);
3289 int64_t imm = Int64FromConstant(second.GetConstant());
3290
3291 int64_t magic;
3292 int shift;
3293 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
3294 UseScratchRegisterScope temps(GetVIXLAssembler());
3295 Register temp = temps.AcquireSameSizeAs(out);
3296
3297 // temp = get_high(dividend * magic)
3298 __ Mov(temp, magic);
3299 __ Smull(temp.X(), dividend, temp);
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003300
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003301 // The multiplication result might need some corrections to be finalized.
3302 // The last correction is to increment by 1, if the result is negative.
3303 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3304 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3305 // However if one of the corrections is ADD or SUB, the sign can be detected
3306 // with ADDS/SUBS. They set the N flag if the result is negative.
3307 // This allows to use CINC MI which has latency 1.
3308 bool use_cond_inc = false;
3309
3310 // ADD/SUB correction is performed in the high 32 bits
3311 // as high 32 bits are ignored because type are kInt32.
3312 if (NeedToAddDividend(magic, imm)) {
3313 __ Adds(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3314 use_cond_inc = true;
3315 } else if (NeedToSubDividend(magic, imm)) {
3316 __ Subs(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3317 use_cond_inc = true;
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003318 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003319
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003320 // Extract the result from the high 32 bits and apply the final right shift.
3321 DCHECK_LT(shift, 32);
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003322 if (imm > 0 && HasNonNegativeInputAt(instruction, 0)) {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01003323 // No need to adjust the result for a non-negative dividend and a positive divisor.
3324 if (instruction->IsDiv()) {
3325 __ Lsr(out.X(), temp.X(), 32 + shift);
3326 } else {
3327 __ Lsr(temp.X(), temp.X(), 32 + shift);
3328 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3329 }
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003330 } else {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01003331 __ Asr(temp.X(), temp.X(), 32 + shift);
3332
3333 if (instruction->IsRem()) {
3334 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3335 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3336 } else {
3337 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3338 }
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003339 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003340}
3341
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003342void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction,
3343 int64_t divisor) {
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003344 DCHECK(instruction->IsDiv() || instruction->IsRem());
3345 if (instruction->GetResultType() == DataType::Type::kInt64) {
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003346 if (divisor > 0 && HasNonNegativeInputAt(instruction, 0)) {
3347 GenerateInt64UnsignedDivRemWithAnyPositiveConstant(instruction);
3348 } else {
3349 GenerateInt64DivRemWithAnyConstant(instruction);
3350 }
Zheng Xuc6667102015-05-15 16:08:45 +08003351 } else {
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003352 GenerateInt32DivRemWithAnyConstant(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003353 }
3354}
3355
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003356void InstructionCodeGeneratorARM64::GenerateIntDivForConstDenom(HDiv *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003357 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Zheng Xuc6667102015-05-15 16:08:45 +08003358
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003359 if (imm == 0) {
3360 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3361 return;
3362 }
Zheng Xuc6667102015-05-15 16:08:45 +08003363
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003364 if (IsPowerOfTwo(AbsOrMin(imm))) {
3365 GenerateIntDivForPower2Denom(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003366 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003367 // Cases imm == -1 or imm == 1 are handled by InstructionSimplifier.
3368 DCHECK(imm < -2 || imm > 2) << imm;
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003369 GenerateDivRemWithAnyConstant(instruction, imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003370 }
3371}
3372
3373void InstructionCodeGeneratorARM64::GenerateIntDiv(HDiv *instruction) {
3374 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
3375 << instruction->GetResultType();
3376
3377 if (instruction->GetLocations()->InAt(1).IsConstant()) {
3378 GenerateIntDivForConstDenom(instruction);
3379 } else {
3380 Register out = OutputRegister(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003381 Register dividend = InputRegisterAt(instruction, 0);
3382 Register divisor = InputRegisterAt(instruction, 1);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003383 __ Sdiv(out, dividend, divisor);
Zheng Xuc6667102015-05-15 16:08:45 +08003384 }
3385}
3386
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003387void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3388 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003389 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003390 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003391 case DataType::Type::kInt32:
3392 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003393 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003394 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003395 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3396 break;
3397
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003398 case DataType::Type::kFloat32:
3399 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003400 locations->SetInAt(0, Location::RequiresFpuRegister());
3401 locations->SetInAt(1, Location::RequiresFpuRegister());
3402 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3403 break;
3404
3405 default:
3406 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3407 }
3408}
3409
3410void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003411 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003412 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003413 case DataType::Type::kInt32:
3414 case DataType::Type::kInt64:
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003415 GenerateIntDiv(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003416 break;
3417
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003418 case DataType::Type::kFloat32:
3419 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003420 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3421 break;
3422
3423 default:
3424 LOG(FATAL) << "Unexpected div type " << type;
3425 }
3426}
3427
Alexandre Rames67555f72014-11-18 10:55:16 +00003428void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003429 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003430 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003431}
3432
3433void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3434 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003435 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003436 codegen_->AddSlowPath(slow_path);
3437 Location value = instruction->GetLocations()->InAt(0);
3438
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003439 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003440
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003441 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003442 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Elliott Hughesc1896c92018-11-29 11:33:18 -08003443 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003444 }
3445
Alexandre Rames67555f72014-11-18 10:55:16 +00003446 if (value.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003447 int64_t divisor = Int64FromLocation(value);
Alexandre Rames67555f72014-11-18 10:55:16 +00003448 if (divisor == 0) {
3449 __ B(slow_path->GetEntryLabel());
3450 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003451 // A division by a non-null constant is valid. We don't need to perform
3452 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003453 }
3454 } else {
3455 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3456 }
3457}
3458
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003459void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3460 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003461 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003462 locations->SetOut(Location::ConstantLocation(constant));
3463}
3464
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003465void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3466 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003467 // Will be generated at use site.
3468}
3469
Alexandre Rames5319def2014-10-23 10:03:10 +01003470void LocationsBuilderARM64::VisitExit(HExit* exit) {
3471 exit->SetLocations(nullptr);
3472}
3473
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003474void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003475}
3476
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003477void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3478 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003479 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003480 locations->SetOut(Location::ConstantLocation(constant));
3481}
3482
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003483void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003484 // Will be generated at use site.
3485}
3486
David Brazdilfc6a86a2015-06-26 10:33:45 +00003487void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003488 if (successor->IsExitBlock()) {
3489 DCHECK(got->GetPrevious()->AlwaysThrows());
3490 return; // no code needed
3491 }
3492
Serban Constantinescu02164b32014-11-13 14:05:07 +00003493 HBasicBlock* block = got->GetBlock();
3494 HInstruction* previous = got->GetPrevious();
3495 HLoopInformation* info = block->GetLoopInformation();
3496
David Brazdil46e2a392015-03-16 17:31:52 +00003497 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00003498 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003499 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3500 return;
3501 }
3502 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3503 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08003504 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003505 }
3506 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003507 __ B(codegen_->GetLabelOf(successor));
3508 }
3509}
3510
David Brazdilfc6a86a2015-06-26 10:33:45 +00003511void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3512 got->SetLocations(nullptr);
3513}
3514
3515void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3516 HandleGoto(got, got->GetSuccessor());
3517}
3518
3519void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3520 try_boundary->SetLocations(nullptr);
3521}
3522
3523void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3524 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3525 if (!successor->IsExitBlock()) {
3526 HandleGoto(try_boundary, successor);
3527 }
3528}
3529
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003530void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003531 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003532 vixl::aarch64::Label* true_target,
3533 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003534 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003535
David Brazdil0debae72015-11-12 18:37:00 +00003536 if (true_target == nullptr && false_target == nullptr) {
3537 // Nothing to do. The code always falls through.
3538 return;
3539 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003540 // Constant condition, statically compared against "true" (integer value 1).
3541 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003542 if (true_target != nullptr) {
3543 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003544 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003545 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003546 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003547 if (false_target != nullptr) {
3548 __ B(false_target);
3549 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003550 }
David Brazdil0debae72015-11-12 18:37:00 +00003551 return;
3552 }
3553
3554 // The following code generates these patterns:
3555 // (1) true_target == nullptr && false_target != nullptr
3556 // - opposite condition true => branch to false_target
3557 // (2) true_target != nullptr && false_target == nullptr
3558 // - condition true => branch to true_target
3559 // (3) true_target != nullptr && false_target != nullptr
3560 // - condition true => branch to true_target
3561 // - branch to false_target
3562 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003563 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003564 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003565 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003566 if (true_target == nullptr) {
3567 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3568 } else {
3569 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3570 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003571 } else {
3572 // The condition instruction has not been materialized, use its inputs as
3573 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003574 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003575
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003576 DataType::Type type = condition->InputAt(0)->GetType();
3577 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003578 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003579 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003580 IfCondition opposite_condition = condition->GetOppositeCondition();
3581 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003582 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003583 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003584 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003585 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003586 // Integer cases.
3587 Register lhs = InputRegisterAt(condition, 0);
3588 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003589
3590 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003591 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003592 if (true_target == nullptr) {
3593 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3594 non_fallthrough_target = false_target;
3595 } else {
3596 arm64_cond = ARM64Condition(condition->GetCondition());
3597 non_fallthrough_target = true_target;
3598 }
3599
Aart Bik086d27e2016-01-20 17:02:00 -08003600 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003601 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003602 switch (arm64_cond) {
3603 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003604 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003605 break;
3606 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003607 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003608 break;
3609 case lt:
3610 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003611 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003612 break;
3613 case ge:
3614 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003615 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003616 break;
3617 default:
3618 // Without the `static_cast` the compiler throws an error for
3619 // `-Werror=sign-promo`.
3620 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3621 }
3622 } else {
3623 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003624 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003625 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003626 }
3627 }
David Brazdil0debae72015-11-12 18:37:00 +00003628
3629 // If neither branch falls through (case 3), the conditional branch to `true_target`
3630 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3631 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003632 __ B(false_target);
3633 }
3634}
3635
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003636void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003637 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003638 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003639 locations->SetInAt(0, Location::RequiresRegister());
3640 }
3641}
3642
3643void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003644 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3645 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003646 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3647 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3648 true_target = nullptr;
3649 }
3650 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3651 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3652 false_target = nullptr;
3653 }
Andreas Gampe3db70682018-12-26 15:12:03 -08003654 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003655}
3656
3657void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003658 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003659 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003660 InvokeRuntimeCallingConvention calling_convention;
3661 RegisterSet caller_saves = RegisterSet::Empty();
3662 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3663 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003664 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003665 locations->SetInAt(0, Location::RequiresRegister());
3666 }
3667}
3668
3669void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003670 SlowPathCodeARM64* slow_path =
3671 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003672 GenerateTestAndBranch(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08003673 /* condition_input_index= */ 0,
David Brazdil0debae72015-11-12 18:37:00 +00003674 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08003675 /* false_target= */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003676}
3677
Mingyao Yang063fc772016-08-02 11:02:54 -07003678void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003679 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003680 LocationSummary(flag, LocationSummary::kNoCall);
3681 locations->SetOut(Location::RequiresRegister());
3682}
3683
3684void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3685 __ Ldr(OutputRegister(flag),
3686 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3687}
3688
David Brazdilc0b601b2016-02-08 14:20:45 +00003689static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3690 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003691 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003692}
3693
Alexandre Rames880f1192016-06-13 16:04:50 +01003694static inline Condition GetConditionForSelect(HCondition* condition) {
3695 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003696 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3697 : ARM64Condition(cond);
3698}
3699
David Brazdil74eb1b22015-12-14 11:44:01 +00003700void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003701 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003702 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003703 locations->SetInAt(0, Location::RequiresFpuRegister());
3704 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003705 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003706 } else {
3707 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3708 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3709 bool is_true_value_constant = cst_true_value != nullptr;
3710 bool is_false_value_constant = cst_false_value != nullptr;
3711 // Ask VIXL whether we should synthesize constants in registers.
3712 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3713 Operand true_op = is_true_value_constant ?
3714 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3715 Operand false_op = is_false_value_constant ?
3716 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3717 bool true_value_in_register = false;
3718 bool false_value_in_register = false;
3719 MacroAssembler::GetCselSynthesisInformation(
3720 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3721 true_value_in_register |= !is_true_value_constant;
3722 false_value_in_register |= !is_false_value_constant;
3723
3724 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3725 : Location::ConstantLocation(cst_true_value));
3726 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3727 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003728 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003729 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003730
David Brazdil74eb1b22015-12-14 11:44:01 +00003731 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3732 locations->SetInAt(2, Location::RequiresRegister());
3733 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003734}
3735
3736void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003737 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003738 Condition csel_cond;
3739
3740 if (IsBooleanValueOrMaterializedCondition(cond)) {
3741 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003742 // Use the condition flags set by the previous instruction.
3743 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003744 } else {
3745 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003746 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003747 }
3748 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003749 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003750 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003751 } else {
3752 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003753 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003754 }
3755
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003756 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003757 __ Fcsel(OutputFPRegister(select),
3758 InputFPRegisterAt(select, 1),
3759 InputFPRegisterAt(select, 0),
3760 csel_cond);
3761 } else {
3762 __ Csel(OutputRegister(select),
3763 InputOperandAt(select, 1),
3764 InputOperandAt(select, 0),
3765 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003766 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003767}
3768
David Srbecky0cf44932015-12-09 14:09:59 +00003769void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003770 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00003771}
3772
David Srbeckyd28f4a02016-03-14 17:14:24 +00003773void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3774 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003775}
3776
Vladimir Markodec78172020-06-19 15:31:23 +01003777void CodeGeneratorARM64::IncreaseFrame(size_t adjustment) {
3778 __ Claim(adjustment);
3779 GetAssembler()->cfi().AdjustCFAOffset(adjustment);
3780}
3781
3782void CodeGeneratorARM64::DecreaseFrame(size_t adjustment) {
3783 __ Drop(adjustment);
3784 GetAssembler()->cfi().AdjustCFAOffset(-adjustment);
3785}
3786
David Srbeckyc7098ff2016-02-09 14:30:11 +00003787void CodeGeneratorARM64::GenerateNop() {
3788 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003789}
3790
Alexandre Rames5319def2014-10-23 10:03:10 +01003791void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003792 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003793}
3794
3795void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003796 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003797}
3798
3799void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003800 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003801}
3802
3803void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003804 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003805}
3806
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003807// Temp is used for read barrier.
3808static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3809 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003810 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003811 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3812 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3813 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3814 return 1;
3815 }
3816 return 0;
3817}
3818
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003819// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003820// interface pointer, one for loading the current interface.
3821// The other checks have one temp for loading the object's class.
3822static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3823 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3824 return 3;
3825 }
3826 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003827}
3828
Alexandre Rames67555f72014-11-18 10:55:16 +00003829void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003830 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003831 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003832 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003833 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003834 case TypeCheckKind::kExactCheck:
3835 case TypeCheckKind::kAbstractClassCheck:
3836 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00003837 case TypeCheckKind::kArrayObjectCheck: {
3838 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
3839 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
3840 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003841 break;
Vladimir Marko87584542017-12-12 17:47:52 +00003842 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003843 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003844 case TypeCheckKind::kUnresolvedCheck:
3845 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003846 call_kind = LocationSummary::kCallOnSlowPath;
3847 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00003848 case TypeCheckKind::kBitstringCheck:
3849 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003850 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003851
Vladimir Markoca6fff82017-10-03 14:49:14 +01003852 LocationSummary* locations =
3853 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003854 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003855 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003856 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003857 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003858 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3859 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3860 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3861 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3862 } else {
3863 locations->SetInAt(1, Location::RequiresRegister());
3864 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003865 // The "out" register is used as a temporary, so it overlaps with the inputs.
3866 // Note that TypeCheckSlowPathARM64 uses this register too.
3867 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003868 // Add temps if necessary for read barriers.
3869 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003870}
3871
3872void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003873 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003874 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003875 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003876 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003877 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3878 ? Register()
3879 : InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003880 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003881 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003882 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3883 DCHECK_LE(num_temps, 1u);
3884 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003885 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3886 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3887 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3888 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003889
Scott Wakeling97c72b72016-06-24 16:19:36 +01003890 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003891 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003892
3893 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003894 // Avoid null check if we know `obj` is not null.
3895 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003896 __ Cbz(obj, &zero);
3897 }
3898
Roland Levillain44015862016-01-22 11:47:17 +00003899 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003900 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003901 ReadBarrierOption read_barrier_option =
3902 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003903 // /* HeapReference<Class> */ out = obj->klass_
3904 GenerateReferenceLoadTwoRegisters(instruction,
3905 out_loc,
3906 obj_loc,
3907 class_offset,
3908 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003909 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003910 __ Cmp(out, cls);
3911 __ Cset(out, eq);
3912 if (zero.IsLinked()) {
3913 __ B(&done);
3914 }
3915 break;
3916 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003917
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003918 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003919 ReadBarrierOption read_barrier_option =
3920 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003921 // /* HeapReference<Class> */ out = obj->klass_
3922 GenerateReferenceLoadTwoRegisters(instruction,
3923 out_loc,
3924 obj_loc,
3925 class_offset,
3926 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003927 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003928 // If the class is abstract, we eagerly fetch the super class of the
3929 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003930 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003931 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003932 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003933 GenerateReferenceLoadOneRegister(instruction,
3934 out_loc,
3935 super_offset,
3936 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003937 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003938 // If `out` is null, we use it for the result, and jump to `done`.
3939 __ Cbz(out, &done);
3940 __ Cmp(out, cls);
3941 __ B(ne, &loop);
3942 __ Mov(out, 1);
3943 if (zero.IsLinked()) {
3944 __ B(&done);
3945 }
3946 break;
3947 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003948
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003949 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003950 ReadBarrierOption read_barrier_option =
3951 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003952 // /* HeapReference<Class> */ out = obj->klass_
3953 GenerateReferenceLoadTwoRegisters(instruction,
3954 out_loc,
3955 obj_loc,
3956 class_offset,
3957 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003958 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003959 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003960 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003961 __ Bind(&loop);
3962 __ Cmp(out, cls);
3963 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003964 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003965 GenerateReferenceLoadOneRegister(instruction,
3966 out_loc,
3967 super_offset,
3968 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003969 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003970 __ Cbnz(out, &loop);
3971 // If `out` is null, we use it for the result, and jump to `done`.
3972 __ B(&done);
3973 __ Bind(&success);
3974 __ Mov(out, 1);
3975 if (zero.IsLinked()) {
3976 __ B(&done);
3977 }
3978 break;
3979 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003980
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003981 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003982 ReadBarrierOption read_barrier_option =
3983 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003984 // /* HeapReference<Class> */ out = obj->klass_
3985 GenerateReferenceLoadTwoRegisters(instruction,
3986 out_loc,
3987 obj_loc,
3988 class_offset,
3989 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003990 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003991 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003992 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003993 __ Cmp(out, cls);
3994 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003995 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003996 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003997 GenerateReferenceLoadOneRegister(instruction,
3998 out_loc,
3999 component_offset,
4000 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004001 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004002 // If `out` is null, we use it for the result, and jump to `done`.
4003 __ Cbz(out, &done);
4004 __ Ldrh(out, HeapOperand(out, primitive_offset));
4005 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4006 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004007 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004008 __ Mov(out, 1);
4009 __ B(&done);
4010 break;
4011 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004012
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004013 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004014 // No read barrier since the slow path will retry upon failure.
4015 // /* HeapReference<Class> */ out = obj->klass_
4016 GenerateReferenceLoadTwoRegisters(instruction,
4017 out_loc,
4018 obj_loc,
4019 class_offset,
4020 maybe_temp_loc,
4021 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004022 __ Cmp(out, cls);
4023 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004024 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08004025 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004026 codegen_->AddSlowPath(slow_path);
4027 __ B(ne, slow_path->GetEntryLabel());
4028 __ Mov(out, 1);
4029 if (zero.IsLinked()) {
4030 __ B(&done);
4031 }
4032 break;
4033 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004034
Calin Juravle98893e12015-10-02 21:05:03 +01004035 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004036 case TypeCheckKind::kInterfaceCheck: {
4037 // Note that we indeed only call on slow path, but we always go
4038 // into the slow path for the unresolved and interface check
4039 // cases.
4040 //
4041 // We cannot directly call the InstanceofNonTrivial runtime
4042 // entry point without resorting to a type checking slow path
4043 // here (i.e. by calling InvokeRuntime directly), as it would
4044 // require to assign fixed registers for the inputs of this
4045 // HInstanceOf instruction (following the runtime calling
4046 // convention), which might be cluttered by the potential first
4047 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004048 //
4049 // TODO: Introduce a new runtime entry point taking the object
4050 // to test (instead of its class) as argument, and let it deal
4051 // with the read barrier issues. This will let us refactor this
4052 // case of the `switch` code as it was previously (with a direct
4053 // call to the runtime not using a type checking slow path).
4054 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004055 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004056 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08004057 instruction, /* is_fatal= */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004058 codegen_->AddSlowPath(slow_path);
4059 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004060 if (zero.IsLinked()) {
4061 __ B(&done);
4062 }
4063 break;
4064 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004065
4066 case TypeCheckKind::kBitstringCheck: {
4067 // /* HeapReference<Class> */ temp = obj->klass_
4068 GenerateReferenceLoadTwoRegisters(instruction,
4069 out_loc,
4070 obj_loc,
4071 class_offset,
4072 maybe_temp_loc,
4073 kWithoutReadBarrier);
4074
4075 GenerateBitstringTypeCheckCompare(instruction, out);
4076 __ Cset(out, eq);
4077 if (zero.IsLinked()) {
4078 __ B(&done);
4079 }
4080 break;
4081 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004082 }
4083
4084 if (zero.IsLinked()) {
4085 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004086 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004087 }
4088
4089 if (done.IsLinked()) {
4090 __ Bind(&done);
4091 }
4092
4093 if (slow_path != nullptr) {
4094 __ Bind(slow_path->GetExitLabel());
4095 }
4096}
4097
4098void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004099 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00004100 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004101 LocationSummary* locations =
4102 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004103 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00004104 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
4105 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
4106 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
4107 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
4108 } else {
4109 locations->SetInAt(1, Location::RequiresRegister());
4110 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004111 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4112 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004113}
4114
4115void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004116 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004117 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004118 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004119 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00004120 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
4121 ? Register()
4122 : InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004123 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4124 DCHECK_GE(num_temps, 1u);
4125 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004126 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004127 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4128 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004129 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004130 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4131 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4132 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4133 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4134 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4135 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4136 const uint32_t object_array_data_offset =
4137 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004138
Vladimir Marko87584542017-12-12 17:47:52 +00004139 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004140 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004141 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4142 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004143 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004144
Scott Wakeling97c72b72016-06-24 16:19:36 +01004145 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004146 // Avoid null check if we know obj is not null.
4147 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004148 __ Cbz(obj, &done);
4149 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004150
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004151 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004152 case TypeCheckKind::kExactCheck:
4153 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004154 // /* HeapReference<Class> */ temp = obj->klass_
4155 GenerateReferenceLoadTwoRegisters(instruction,
4156 temp_loc,
4157 obj_loc,
4158 class_offset,
4159 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004160 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004161
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004162 __ Cmp(temp, cls);
4163 // Jump to slow path for throwing the exception or doing a
4164 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004165 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004166 break;
4167 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004168
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004169 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004170 // /* HeapReference<Class> */ temp = obj->klass_
4171 GenerateReferenceLoadTwoRegisters(instruction,
4172 temp_loc,
4173 obj_loc,
4174 class_offset,
4175 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004176 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004177
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004178 // If the class is abstract, we eagerly fetch the super class of the
4179 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004180 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004181 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004182 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004183 GenerateReferenceLoadOneRegister(instruction,
4184 temp_loc,
4185 super_offset,
4186 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004187 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004188
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004189 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4190 // exception.
4191 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4192 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004193 __ Cmp(temp, cls);
4194 __ B(ne, &loop);
4195 break;
4196 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004197
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004198 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004199 // /* HeapReference<Class> */ temp = obj->klass_
4200 GenerateReferenceLoadTwoRegisters(instruction,
4201 temp_loc,
4202 obj_loc,
4203 class_offset,
4204 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004205 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004206
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004207 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004208 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004209 __ Bind(&loop);
4210 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004211 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004212
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004213 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004214 GenerateReferenceLoadOneRegister(instruction,
4215 temp_loc,
4216 super_offset,
4217 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004218 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004219
4220 // If the class reference currently in `temp` is not null, jump
4221 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004222 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004223 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004224 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004225 break;
4226 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004227
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004228 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004229 // /* HeapReference<Class> */ temp = obj->klass_
4230 GenerateReferenceLoadTwoRegisters(instruction,
4231 temp_loc,
4232 obj_loc,
4233 class_offset,
4234 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004235 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004236
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004237 // Do an exact check.
4238 __ Cmp(temp, cls);
4239 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004240
4241 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004242 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004243 GenerateReferenceLoadOneRegister(instruction,
4244 temp_loc,
4245 component_offset,
4246 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004247 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004248
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004249 // If the component type is null, jump to the slow path to throw the exception.
4250 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4251 // Otherwise, the object is indeed an array. Further check that this component type is not a
4252 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004253 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4254 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004255 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004256 break;
4257 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004258
Calin Juravle98893e12015-10-02 21:05:03 +01004259 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004260 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004261 //
4262 // We cannot directly call the CheckCast runtime entry point
4263 // without resorting to a type checking slow path here (i.e. by
4264 // calling InvokeRuntime directly), as it would require to
4265 // assign fixed registers for the inputs of this HInstanceOf
4266 // instruction (following the runtime calling convention), which
4267 // might be cluttered by the potential first read barrier
4268 // emission at the beginning of this method.
4269 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004270 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004271 case TypeCheckKind::kInterfaceCheck: {
4272 // /* HeapReference<Class> */ temp = obj->klass_
4273 GenerateReferenceLoadTwoRegisters(instruction,
4274 temp_loc,
4275 obj_loc,
4276 class_offset,
4277 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004278 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004279
4280 // /* HeapReference<Class> */ temp = temp->iftable_
4281 GenerateReferenceLoadTwoRegisters(instruction,
4282 temp_loc,
4283 temp_loc,
4284 iftable_offset,
4285 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004286 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004287 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004288 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004289 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004290 vixl::aarch64::Label start_loop;
4291 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004292 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004293 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4294 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004295 // Go to next interface.
4296 __ Add(temp, temp, 2 * kHeapReferenceSize);
4297 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004298 // Compare the classes and continue the loop if they do not match.
4299 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4300 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004301 break;
4302 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004303
4304 case TypeCheckKind::kBitstringCheck: {
4305 // /* HeapReference<Class> */ temp = obj->klass_
4306 GenerateReferenceLoadTwoRegisters(instruction,
4307 temp_loc,
4308 obj_loc,
4309 class_offset,
4310 maybe_temp2_loc,
4311 kWithoutReadBarrier);
4312
4313 GenerateBitstringTypeCheckCompare(instruction, temp);
4314 __ B(ne, type_check_slow_path->GetEntryLabel());
4315 break;
4316 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004317 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004318 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004319
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004320 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004321}
4322
Alexandre Rames5319def2014-10-23 10:03:10 +01004323void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004324 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004325 locations->SetOut(Location::ConstantLocation(constant));
4326}
4327
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004328void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004329 // Will be generated at use site.
4330}
4331
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004332void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004333 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004334 locations->SetOut(Location::ConstantLocation(constant));
4335}
4336
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004337void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004338 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004339}
4340
Calin Juravle175dc732015-08-25 15:42:32 +01004341void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4342 // The trampoline uses the same calling convention as dex calling conventions,
4343 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4344 // the method_idx.
4345 HandleInvoke(invoke);
4346}
4347
4348void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4349 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004350 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004351}
4352
Alexandre Rames5319def2014-10-23 10:03:10 +01004353void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004354 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004355 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004356}
4357
Alexandre Rames67555f72014-11-18 10:55:16 +00004358void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4359 HandleInvoke(invoke);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004360 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
4361 // We cannot request ip1 as it's blocked by the register allocator.
4362 invoke->GetLocations()->SetInAt(invoke->GetNumberOfArguments() - 1, Location::Any());
4363 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004364}
4365
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004366void CodeGeneratorARM64::MaybeGenerateInlineCacheCheck(HInstruction* instruction,
4367 Register klass) {
4368 DCHECK_EQ(klass.GetCode(), 0u);
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004369 // We know the destination of an intrinsic, so no need to record inline
4370 // caches.
4371 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00004372 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004373 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004374 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
Nicolas Geoffray095dc462020-08-17 16:40:28 +01004375 ScopedProfilingInfoUse spiu(
4376 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
4377 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00004378 if (info != nullptr) {
4379 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
4380 uint64_t address = reinterpret_cast64<uint64_t>(cache);
4381 vixl::aarch64::Label done;
4382 __ Mov(x8, address);
4383 __ Ldr(x9, MemOperand(x8, InlineCache::ClassesOffset().Int32Value()));
4384 // Fast path for a monomorphic cache.
4385 __ Cmp(klass, x9);
4386 __ B(eq, &done);
4387 InvokeRuntime(kQuickUpdateInlineCache, instruction, instruction->GetDexPc());
4388 __ Bind(&done);
4389 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004390 }
4391}
4392
Alexandre Rames67555f72014-11-18 10:55:16 +00004393void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4394 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004395 LocationSummary* locations = invoke->GetLocations();
4396 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004397 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004398 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004399 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004400
Artem Serov914d7a82017-02-07 14:33:49 +00004401 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004402 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004403 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004404 {
4405 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4406 // /* HeapReference<Class> */ temp = temp->klass_
4407 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4408 codegen_->MaybeRecordImplicitNullCheck(invoke);
4409 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004410 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004411 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004412 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004413 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004414 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004415 }
Artem Serov914d7a82017-02-07 14:33:49 +00004416
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004417 // Instead of simply (possibly) unpoisoning `temp` here, we should
4418 // emit a read barrier for the previous class reference load.
4419 // However this is not required in practice, as this is an
4420 // intermediate/temporary reference and because the current
4421 // concurrent copying collector keeps the from-space memory
4422 // intact/accessible until the end of the marking phase (the
4423 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004424 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004425
4426 // If we're compiling baseline, update the inline cache.
4427 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
4428
4429 // The register ip1 is required to be used for the hidden argument in
4430 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
4431 MacroAssembler* masm = GetVIXLAssembler();
4432 UseScratchRegisterScope scratch_scope(masm);
4433 scratch_scope.Exclude(ip1);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004434 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
4435 Location interface_method = locations->InAt(invoke->GetNumberOfArguments() - 1);
4436 if (interface_method.IsStackSlot()) {
4437 __ Ldr(ip1, StackOperandFrom(receiver));
4438 } else {
4439 __ Mov(ip1, XRegisterFrom(interface_method));
4440 }
4441 } else {
4442 codegen_->LoadMethod(
4443 invoke->GetHiddenArgumentLoadKind(), Location::RegisterLocation(ip1.GetCode()), invoke);
4444 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004445
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004446 __ Ldr(temp,
4447 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4448 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004449 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004450 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004451 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004452 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004453 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004454
4455 {
4456 // Ensure the pc position is recorded immediately after the `blr` instruction.
4457 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4458
4459 // lr();
4460 __ blr(lr);
4461 DCHECK(!codegen_->IsLeafMethod());
4462 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4463 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004464
Andreas Gampe3db70682018-12-26 15:12:03 -08004465 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004466}
4467
4468void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004469 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004470 if (intrinsic.TryDispatch(invoke)) {
4471 return;
4472 }
4473
Alexandre Rames67555f72014-11-18 10:55:16 +00004474 HandleInvoke(invoke);
4475}
4476
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004477void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004478 // Explicit clinit checks triggered by static invokes must have been pruned by
4479 // art::PrepareForRegisterAllocation.
4480 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004481
Vladimir Markoca6fff82017-10-03 14:49:14 +01004482 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004483 if (intrinsic.TryDispatch(invoke)) {
4484 return;
4485 }
4486
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004487 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
Vladimir Marko86c87522020-05-11 16:55:55 +01004488 CriticalNativeCallingConventionVisitorARM64 calling_convention_visitor(
4489 /*for_register_allocation=*/ true);
4490 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4491 } else {
4492 HandleInvoke(invoke);
4493 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004494}
4495
Andreas Gampe878d58c2015-01-15 23:24:00 -08004496static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4497 if (invoke->GetLocations()->Intrinsified()) {
4498 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4499 intrinsic.Dispatch(invoke);
4500 return true;
4501 }
4502 return false;
4503}
4504
Vladimir Markodc151b22015-10-15 18:02:30 +01004505HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4506 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004507 ArtMethod* method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004508 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004509 return desired_dispatch_info;
4510}
4511
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004512void CodeGeneratorARM64::LoadMethod(MethodLoadKind load_kind, Location temp, HInvoke* invoke) {
4513 switch (load_kind) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004514 case MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01004515 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01004516 // Add ADRP with its PC-relative method patch.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004517 vixl::aarch64::Label* adrp_label =
4518 NewBootImageMethodPatch(invoke->GetResolvedMethodReference());
Vladimir Marko65979462017-05-19 17:25:12 +01004519 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4520 // Add ADD with its PC-relative method patch.
4521 vixl::aarch64::Label* add_label =
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004522 NewBootImageMethodPatch(invoke->GetResolvedMethodReference(), adrp_label);
Vladimir Marko65979462017-05-19 17:25:12 +01004523 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4524 break;
4525 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004526 case MethodLoadKind::kBootImageRelRo: {
Vladimir Markob066d432018-01-03 13:14:37 +00004527 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004528 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00004529 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_offset);
4530 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4531 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
4532 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_offset, adrp_label);
4533 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
4534 EmitLdrOffsetPlaceholder(ldr_label, WRegisterFrom(temp), XRegisterFrom(temp));
4535 break;
4536 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004537 case MethodLoadKind::kBssEntry: {
Vladimir Markob066d432018-01-03 13:14:37 +00004538 // Add ADRP with its PC-relative .bss entry patch.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004539 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(invoke->GetMethodReference());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004540 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Markob066d432018-01-03 13:14:37 +00004541 // Add LDR with its PC-relative .bss entry patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004542 vixl::aarch64::Label* ldr_label =
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004543 NewMethodBssEntryPatch(invoke->GetMethodReference(), adrp_label);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01004544 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004545 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004546 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004547 }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004548 case MethodLoadKind::kJitDirectAddress: {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004549 // Load method address from literal pool.
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004550 __ Ldr(XRegisterFrom(temp),
4551 DeduplicateUint64Literal(reinterpret_cast<uint64_t>(invoke->GetResolvedMethod())));
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004552 break;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004553 }
4554 case MethodLoadKind::kRuntimeCall: {
4555 // Test situation, don't do anything.
4556 break;
4557 }
4558 default: {
4559 LOG(FATAL) << "Load kind should have already been handled " << load_kind;
4560 UNREACHABLE();
4561 }
4562 }
4563}
4564
4565void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4566 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
4567 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
4568 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4569 switch (invoke->GetMethodLoadKind()) {
4570 case MethodLoadKind::kStringInit: {
4571 uint32_t offset =
4572 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4573 // temp = thread->string_init_entrypoint
4574 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
4575 break;
4576 }
4577 case MethodLoadKind::kRecursive: {
4578 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
4579 break;
4580 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004581 case MethodLoadKind::kRuntimeCall: {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004582 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4583 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004584 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004585 case MethodLoadKind::kBootImageLinkTimePcRelative:
4586 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
4587 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
4588 // Do not materialize the method pointer, load directly the entrypoint.
4589 // Add ADRP with its PC-relative JNI entrypoint patch.
4590 vixl::aarch64::Label* adrp_label =
4591 NewBootImageJniEntrypointPatch(invoke->GetResolvedMethodReference());
4592 EmitAdrpPlaceholder(adrp_label, lr);
4593 // Add the LDR with its PC-relative method patch.
4594 vixl::aarch64::Label* add_label =
4595 NewBootImageJniEntrypointPatch(invoke->GetResolvedMethodReference(), adrp_label);
4596 EmitLdrOffsetPlaceholder(add_label, lr, lr);
4597 break;
4598 }
4599 FALLTHROUGH_INTENDED;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004600 default: {
4601 LoadMethod(invoke->GetMethodLoadKind(), temp, invoke);
4602 break;
4603 }
Vladimir Marko58155012015-08-19 12:49:41 +00004604 }
4605
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004606 auto call_lr = [&]() {
4607 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4608 ExactAssemblyScope eas(GetVIXLAssembler(),
4609 kInstructionSize,
4610 CodeBufferCheckScope::kExactSize);
4611 // lr()
4612 __ blr(lr);
4613 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko86c87522020-05-11 16:55:55 +01004614 };
Vladimir Marko58155012015-08-19 12:49:41 +00004615 switch (invoke->GetCodePtrLocation()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004616 case CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004617 {
4618 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4619 ExactAssemblyScope eas(GetVIXLAssembler(),
4620 kInstructionSize,
4621 CodeBufferCheckScope::kExactSize);
4622 __ bl(&frame_entry_label_);
4623 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4624 }
Vladimir Marko58155012015-08-19 12:49:41 +00004625 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004626 case CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01004627 size_t out_frame_size =
4628 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorARM64,
4629 kAapcs64StackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01004630 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004631 if (invoke->GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative) {
4632 call_lr();
4633 } else {
4634 // LR = callee_method->ptr_sized_fields_.data_; // EntryPointFromJni
4635 MemberOffset offset = ArtMethod::EntryPointFromJniOffset(kArm64PointerSize);
4636 __ Ldr(lr, MemOperand(XRegisterFrom(callee_method), offset.Int32Value()));
4637 // lr()
4638 call_lr();
4639 }
Vladimir Marko86c87522020-05-11 16:55:55 +01004640 // Zero-/sign-extend the result when needed due to native and managed ABI mismatch.
4641 switch (invoke->GetType()) {
4642 case DataType::Type::kBool:
4643 __ Ubfx(w0, w0, 0, 8);
4644 break;
4645 case DataType::Type::kInt8:
4646 __ Sbfx(w0, w0, 0, 8);
4647 break;
4648 case DataType::Type::kUint16:
4649 __ Ubfx(w0, w0, 0, 16);
4650 break;
4651 case DataType::Type::kInt16:
4652 __ Sbfx(w0, w0, 0, 16);
4653 break;
4654 case DataType::Type::kInt32:
4655 case DataType::Type::kInt64:
4656 case DataType::Type::kFloat32:
4657 case DataType::Type::kFloat64:
4658 case DataType::Type::kVoid:
4659 break;
4660 default:
4661 DCHECK(false) << invoke->GetType();
4662 break;
4663 }
4664 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01004665 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01004666 }
4667 break;
4668 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004669 case CodePtrLocation::kCallArtMethod: {
4670 // LR = callee_method->ptr_sized_fields_.entry_point_from_quick_compiled_code_;
4671 MemberOffset offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
4672 __ Ldr(lr, MemOperand(XRegisterFrom(callee_method), offset.Int32Value()));
4673 // lr()
4674 call_lr();
Vladimir Marko58155012015-08-19 12:49:41 +00004675 break;
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004676 }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004677 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004678
Andreas Gampe878d58c2015-01-15 23:24:00 -08004679 DCHECK(!IsLeafMethod());
4680}
4681
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004682void CodeGeneratorARM64::GenerateVirtualCall(
4683 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004684 // Use the calling convention instead of the location of the receiver, as
4685 // intrinsics may have put the receiver in a different register. In the intrinsics
4686 // slow path, the arguments have been moved to the right place, so here we are
4687 // guaranteed that the receiver is the first register of the calling convention.
4688 InvokeDexCallingConvention calling_convention;
4689 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004690 Register temp = XRegisterFrom(temp_in);
4691 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4692 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4693 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004694 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004695
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004696 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004697
4698 {
4699 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4700 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4701 // /* HeapReference<Class> */ temp = receiver->klass_
4702 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4703 MaybeRecordImplicitNullCheck(invoke);
4704 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004705 // Instead of simply (possibly) unpoisoning `temp` here, we should
4706 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004707 // intermediate/temporary reference and because the current
4708 // concurrent copying collector keeps the from-space memory
4709 // intact/accessible until the end of the marking phase (the
4710 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004711 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004712
4713 // If we're compiling baseline, update the inline cache.
4714 MaybeGenerateInlineCacheCheck(invoke, temp);
4715
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004716 // temp = temp->GetMethodAt(method_offset);
4717 __ Ldr(temp, MemOperand(temp, method_offset));
4718 // lr = temp->GetEntryPoint();
4719 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004720 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004721 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004722 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4723 // lr();
4724 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004725 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004726 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004727}
4728
Vladimir Marko9922f002020-06-08 15:05:15 +01004729void CodeGeneratorARM64::MoveFromReturnRegister(Location trg, DataType::Type type) {
4730 if (!trg.IsValid()) {
4731 DCHECK(type == DataType::Type::kVoid);
4732 return;
4733 }
4734
4735 DCHECK_NE(type, DataType::Type::kVoid);
4736
4737 if (DataType::IsIntegralType(type) || type == DataType::Type::kReference) {
4738 Register trg_reg = RegisterFrom(trg, type);
4739 Register res_reg = RegisterFrom(ARM64ReturnLocation(type), type);
4740 __ Mov(trg_reg, res_reg, kDiscardForSameWReg);
4741 } else {
4742 VRegister trg_reg = FPRegisterFrom(trg, type);
4743 VRegister res_reg = FPRegisterFrom(ARM64ReturnLocation(type), type);
4744 __ Fmov(trg_reg, res_reg);
4745 }
4746}
4747
Orion Hodsonac141392017-01-13 11:53:47 +00004748void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00004749 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
4750 if (intrinsic.TryDispatch(invoke)) {
4751 return;
4752 }
Orion Hodsonac141392017-01-13 11:53:47 +00004753 HandleInvoke(invoke);
4754}
4755
4756void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00004757 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4758 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
4759 return;
4760 }
Orion Hodsonac141392017-01-13 11:53:47 +00004761 codegen_->GenerateInvokePolymorphicCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004762 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004763}
4764
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004765void LocationsBuilderARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4766 HandleInvoke(invoke);
4767}
4768
4769void InstructionCodeGeneratorARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4770 codegen_->GenerateInvokeCustomCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004771 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004772}
4773
Vladimir Marko6fd16062018-06-26 11:02:04 +01004774vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageIntrinsicPatch(
4775 uint32_t intrinsic_data,
4776 vixl::aarch64::Label* adrp_label) {
4777 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004778 /* dex_file= */ nullptr, intrinsic_data, adrp_label, &boot_image_other_patches_);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004779}
4780
Vladimir Markob066d432018-01-03 13:14:37 +00004781vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageRelRoPatch(
4782 uint32_t boot_image_offset,
4783 vixl::aarch64::Label* adrp_label) {
4784 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004785 /* dex_file= */ nullptr, boot_image_offset, adrp_label, &boot_image_other_patches_);
Vladimir Markob066d432018-01-03 13:14:37 +00004786}
4787
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004788vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004789 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004790 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004791 return NewPcRelativePatch(
4792 target_method.dex_file, target_method.index, adrp_label, &boot_image_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004793}
4794
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004795vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4796 MethodReference target_method,
4797 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004798 return NewPcRelativePatch(
4799 target_method.dex_file, target_method.index, adrp_label, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004800}
4801
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004802vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageTypePatch(
Scott Wakeling97c72b72016-06-24 16:19:36 +01004803 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004804 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004805 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004806 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &boot_image_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004807}
4808
Vladimir Marko1998cd02017-01-13 13:02:58 +00004809vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
Vladimir Marko8f63f102020-09-28 12:10:28 +01004810 HLoadClass* load_class,
Vladimir Marko1998cd02017-01-13 13:02:58 +00004811 vixl::aarch64::Label* adrp_label) {
Vladimir Marko8f63f102020-09-28 12:10:28 +01004812 const DexFile& dex_file = load_class->GetDexFile();
4813 dex::TypeIndex type_index = load_class->GetTypeIndex();
4814 ArenaDeque<PcRelativePatchInfo>* patches = nullptr;
4815 switch (load_class->GetLoadKind()) {
4816 case HLoadClass::LoadKind::kBssEntry:
4817 patches = &type_bss_entry_patches_;
4818 break;
4819 case HLoadClass::LoadKind::kBssEntryPublic:
4820 patches = &public_type_bss_entry_patches_;
4821 break;
4822 case HLoadClass::LoadKind::kBssEntryPackage:
4823 patches = &package_type_bss_entry_patches_;
4824 break;
4825 default:
4826 LOG(FATAL) << "Unexpected load kind: " << load_class->GetLoadKind();
4827 UNREACHABLE();
4828 }
4829 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004830}
4831
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004832vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004833 const DexFile& dex_file,
4834 dex::StringIndex string_index,
4835 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004836 return NewPcRelativePatch(
4837 &dex_file, string_index.index_, adrp_label, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01004838}
4839
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004840vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4841 const DexFile& dex_file,
4842 dex::StringIndex string_index,
4843 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004844 return NewPcRelativePatch(&dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004845}
4846
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004847vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageJniEntrypointPatch(
4848 MethodReference target_method,
4849 vixl::aarch64::Label* adrp_label) {
4850 return NewPcRelativePatch(
4851 target_method.dex_file, target_method.index, adrp_label, &boot_image_jni_entrypoint_patches_);
4852}
4853
Vladimir Markof6675082019-05-17 12:05:28 +01004854void CodeGeneratorARM64::EmitEntrypointThunkCall(ThreadOffset64 entrypoint_offset) {
4855 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01004856 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markof6675082019-05-17 12:05:28 +01004857 call_entrypoint_patches_.emplace_back(/*dex_file*/ nullptr, entrypoint_offset.Uint32Value());
4858 vixl::aarch64::Label* bl_label = &call_entrypoint_patches_.back().label;
4859 __ bind(bl_label);
4860 __ bl(static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4861}
4862
Vladimir Marko966b46f2018-08-03 10:20:19 +00004863void CodeGeneratorARM64::EmitBakerReadBarrierCbnz(uint32_t custom_data) {
Vladimir Marko94796f82018-08-08 15:15:33 +01004864 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01004865 if (GetCompilerOptions().IsJitCompiler()) {
Vladimir Marko966b46f2018-08-03 10:20:19 +00004866 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
4867 vixl::aarch64::Label* slow_path_entry = &it->second.label;
4868 __ cbnz(mr, slow_path_entry);
4869 } else {
4870 baker_read_barrier_patches_.emplace_back(custom_data);
4871 vixl::aarch64::Label* cbnz_label = &baker_read_barrier_patches_.back().label;
4872 __ bind(cbnz_label);
4873 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4874 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004875}
4876
Scott Wakeling97c72b72016-06-24 16:19:36 +01004877vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004878 const DexFile* dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004879 uint32_t offset_or_index,
4880 vixl::aarch64::Label* adrp_label,
4881 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004882 // Add a patch entry and return the label.
4883 patches->emplace_back(dex_file, offset_or_index);
4884 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004885 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004886 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4887 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4888 return label;
4889}
4890
Scott Wakeling97c72b72016-06-24 16:19:36 +01004891vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4892 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004893 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004894}
4895
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004896vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004897 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004898 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004899 return jit_string_patches_.GetOrCreate(
4900 StringReference(&dex_file, string_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004901 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004902}
4903
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004904vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004905 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004906 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004907 return jit_class_patches_.GetOrCreate(
4908 TypeReference(&dex_file, type_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004909 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004910}
4911
Vladimir Markoaad75c62016-10-03 08:46:48 +00004912void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4913 vixl::aarch64::Register reg) {
4914 DCHECK(reg.IsX());
4915 SingleEmissionCheckScope guard(GetVIXLAssembler());
4916 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004917 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004918}
4919
4920void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4921 vixl::aarch64::Register out,
4922 vixl::aarch64::Register base) {
4923 DCHECK(out.IsX());
4924 DCHECK(base.IsX());
4925 SingleEmissionCheckScope guard(GetVIXLAssembler());
4926 __ Bind(fixup_label);
4927 __ add(out, base, Operand(/* offset placeholder */ 0));
4928}
4929
4930void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4931 vixl::aarch64::Register out,
4932 vixl::aarch64::Register base) {
4933 DCHECK(base.IsX());
4934 SingleEmissionCheckScope guard(GetVIXLAssembler());
4935 __ Bind(fixup_label);
4936 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4937}
4938
Vladimir Markoeebb8212018-06-05 14:57:24 +01004939void CodeGeneratorARM64::LoadBootImageAddress(vixl::aarch64::Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01004940 uint32_t boot_image_reference) {
4941 if (GetCompilerOptions().IsBootImage()) {
4942 // Add ADRP with its PC-relative type patch.
4943 vixl::aarch64::Label* adrp_label = NewBootImageIntrinsicPatch(boot_image_reference);
4944 EmitAdrpPlaceholder(adrp_label, reg.X());
4945 // Add ADD with its PC-relative type patch.
4946 vixl::aarch64::Label* add_label = NewBootImageIntrinsicPatch(boot_image_reference, adrp_label);
4947 EmitAddPlaceholder(add_label, reg.X(), reg.X());
Vladimir Markoa2da9b92018-10-10 14:21:55 +01004948 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01004949 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004950 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004951 EmitAdrpPlaceholder(adrp_label, reg.X());
4952 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004953 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_reference, adrp_label);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004954 EmitLdrOffsetPlaceholder(ldr_label, reg.W(), reg.X());
4955 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01004956 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01004957 gc::Heap* heap = Runtime::Current()->GetHeap();
4958 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004959 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01004960 __ Ldr(reg.W(), DeduplicateBootImageAddressLiteral(reinterpret_cast<uintptr_t>(address)));
4961 }
4962}
4963
Vladimir Markode91ca92020-10-27 13:41:40 +00004964void CodeGeneratorARM64::LoadIntrinsicDeclaringClass(vixl::aarch64::Register reg, HInvoke* invoke) {
4965 DCHECK_NE(invoke->GetIntrinsic(), Intrinsics::kNone);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004966 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01004967 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004968 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01004969 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
4970 // Add ADRP with its PC-relative type patch.
4971 vixl::aarch64::Label* adrp_label = NewBootImageTypePatch(*target_method.dex_file, type_idx);
Vladimir Markode91ca92020-10-27 13:41:40 +00004972 EmitAdrpPlaceholder(adrp_label, reg.X());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004973 // Add ADD with its PC-relative type patch.
4974 vixl::aarch64::Label* add_label =
4975 NewBootImageTypePatch(*target_method.dex_file, type_idx, adrp_label);
Vladimir Markode91ca92020-10-27 13:41:40 +00004976 EmitAddPlaceholder(add_label, reg.X(), reg.X());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004977 } else {
Vladimir Markode91ca92020-10-27 13:41:40 +00004978 uint32_t boot_image_offset = GetBootImageOffsetOfIntrinsicDeclaringClass(invoke);
4979 LoadBootImageAddress(reg, boot_image_offset);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004980 }
Vladimir Marko6fd16062018-06-26 11:02:04 +01004981}
4982
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004983template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004984inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4985 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004986 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004987 for (const PcRelativePatchInfo& info : infos) {
4988 linker_patches->push_back(Factory(info.label.GetLocation(),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004989 info.target_dex_file,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004990 info.pc_insn_label->GetLocation(),
4991 info.offset_or_index));
4992 }
4993}
4994
Vladimir Marko6fd16062018-06-26 11:02:04 +01004995template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
4996linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
4997 const DexFile* target_dex_file,
4998 uint32_t pc_insn_offset,
4999 uint32_t boot_image_offset) {
5000 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5001 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005002}
5003
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005004void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005005 DCHECK(linker_patches->empty());
5006 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005007 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005008 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005009 boot_image_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005010 type_bss_entry_patches_.size() +
Vladimir Marko8f63f102020-09-28 12:10:28 +01005011 public_type_bss_entry_patches_.size() +
5012 package_type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005013 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005014 string_bss_entry_patches_.size() +
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005015 boot_image_jni_entrypoint_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005016 boot_image_other_patches_.size() +
Vladimir Markof6675082019-05-17 12:05:28 +01005017 call_entrypoint_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005018 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00005019 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005020 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005021 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005022 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005023 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005024 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005025 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005026 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01005027 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005028 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005029 DCHECK(boot_image_type_patches_.empty());
5030 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005031 }
5032 if (GetCompilerOptions().IsBootImage()) {
5033 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5034 boot_image_other_patches_, linker_patches);
5035 } else {
5036 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5037 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005038 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005039 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5040 method_bss_entry_patches_, linker_patches);
5041 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5042 type_bss_entry_patches_, linker_patches);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005043 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PublicTypeBssEntryPatch>(
5044 public_type_bss_entry_patches_, linker_patches);
5045 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PackageTypeBssEntryPatch>(
5046 package_type_bss_entry_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005047 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5048 string_bss_entry_patches_, linker_patches);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005049 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeJniEntrypointPatch>(
5050 boot_image_jni_entrypoint_patches_, linker_patches);
Vladimir Markof6675082019-05-17 12:05:28 +01005051 for (const PatchInfo<vixl::aarch64::Label>& info : call_entrypoint_patches_) {
5052 DCHECK(info.target_dex_file == nullptr);
5053 linker_patches->push_back(linker::LinkerPatch::CallEntrypointPatch(
5054 info.label.GetLocation(), info.offset_or_index));
5055 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005056 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005057 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
5058 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005059 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00005060 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005061}
5062
Vladimir Markoca1e0382018-04-11 09:58:41 +00005063bool CodeGeneratorARM64::NeedsThunkCode(const linker::LinkerPatch& patch) const {
Vladimir Markof6675082019-05-17 12:05:28 +01005064 return patch.GetType() == linker::LinkerPatch::Type::kCallEntrypoint ||
5065 patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
Vladimir Markoca1e0382018-04-11 09:58:41 +00005066 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
5067}
5068
5069void CodeGeneratorARM64::EmitThunkCode(const linker::LinkerPatch& patch,
5070 /*out*/ ArenaVector<uint8_t>* code,
5071 /*out*/ std::string* debug_name) {
5072 Arm64Assembler assembler(GetGraph()->GetAllocator());
5073 switch (patch.GetType()) {
5074 case linker::LinkerPatch::Type::kCallRelative: {
5075 // The thunk just uses the entry point in the ArtMethod. This works even for calls
5076 // to the generic JNI and interpreter trampolines.
5077 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
5078 kArm64PointerSize).Int32Value());
5079 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
5080 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
5081 *debug_name = "MethodCallThunk";
5082 }
5083 break;
5084 }
Vladimir Markof6675082019-05-17 12:05:28 +01005085 case linker::LinkerPatch::Type::kCallEntrypoint: {
5086 Offset offset(patch.EntrypointOffset());
5087 assembler.JumpTo(ManagedRegister(arm64::TR), offset, ManagedRegister(arm64::IP0));
5088 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
5089 *debug_name = "EntrypointCallThunk_" + std::to_string(offset.Uint32Value());
5090 }
5091 break;
5092 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00005093 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
5094 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
5095 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
5096 break;
5097 }
5098 default:
5099 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
5100 UNREACHABLE();
5101 }
5102
5103 // Ensure we emit the literal pool if any.
5104 assembler.FinalizeCode();
5105 code->resize(assembler.CodeSize());
5106 MemoryRegion code_region(code->data(), code->size());
5107 assembler.FinalizeInstructions(code_region);
5108}
5109
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005110vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
5111 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005112 value,
5113 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
5114}
5115
Scott Wakeling97c72b72016-06-24 16:19:36 +01005116vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005117 return uint64_literals_.GetOrCreate(
5118 value,
5119 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00005120}
5121
Andreas Gampe878d58c2015-01-15 23:24:00 -08005122void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005123 // Explicit clinit checks triggered by static invokes must have been pruned by
5124 // art::PrepareForRegisterAllocation.
5125 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01005126
Andreas Gampe878d58c2015-01-15 23:24:00 -08005127 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08005128 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08005129 return;
5130 }
5131
Vladimir Marko86c87522020-05-11 16:55:55 +01005132 LocationSummary* locations = invoke->GetLocations();
5133 codegen_->GenerateStaticOrDirectCall(
5134 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005135
Andreas Gampe3db70682018-12-26 15:12:03 -08005136 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005137}
5138
5139void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08005140 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08005141 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08005142 return;
5143 }
5144
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005145 {
5146 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
5147 // are no pools emitted.
5148 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
5149 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
5150 DCHECK(!codegen_->IsLeafMethod());
5151 }
5152
Andreas Gampe3db70682018-12-26 15:12:03 -08005153 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005154}
5155
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005156HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
5157 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005158 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005159 case HLoadClass::LoadKind::kInvalid:
5160 LOG(FATAL) << "UNREACHABLE";
5161 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005162 case HLoadClass::LoadKind::kReferrersClass:
5163 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005164 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005165 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005166 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8f63f102020-09-28 12:10:28 +01005167 case HLoadClass::LoadKind::kBssEntryPublic:
5168 case HLoadClass::LoadKind::kBssEntryPackage:
Vladimir Marko695348f2020-05-19 14:42:02 +01005169 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005170 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005171 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005172 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01005173 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005174 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005175 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005176 break;
5177 }
5178 return desired_class_load_kind;
5179}
5180
Alexandre Rames67555f72014-11-18 10:55:16 +00005181void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005182 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005183 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005184 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005185 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005186 cls,
5187 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005188 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00005189 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005190 return;
5191 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005192 DCHECK_EQ(cls->NeedsAccessCheck(),
5193 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
5194 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005195
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005196 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5197 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005198 ? LocationSummary::kCallOnSlowPath
5199 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005200 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005201 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005202 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005203 }
5204
Vladimir Marko41559982017-01-06 14:04:23 +00005205 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005206 locations->SetInAt(0, Location::RequiresRegister());
5207 }
5208 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005209 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
5210 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5211 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005212 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005213 } else {
5214 // For non-Baker read barrier we have a temp-clobbering call.
5215 }
5216 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005217}
5218
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005219// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5220// move.
5221void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005222 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005223 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005224 codegen_->GenerateLoadClassRuntimeCall(cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08005225 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01005226 return;
5227 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005228 DCHECK_EQ(cls->NeedsAccessCheck(),
5229 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
5230 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Calin Juravle580b6092015-10-06 17:35:58 +01005231
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005232 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01005233 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00005234
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005235 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5236 ? kWithoutReadBarrier
5237 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005238 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005239 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005240 case HLoadClass::LoadKind::kReferrersClass: {
5241 DCHECK(!cls->CanCallRuntime());
5242 DCHECK(!cls->MustGenerateClinitCheck());
5243 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5244 Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00005245 codegen_->GenerateGcRootFieldLoad(cls,
5246 out_loc,
5247 current_method,
5248 ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005249 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005250 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005251 break;
5252 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005253 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005254 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5255 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005256 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005257 // Add ADRP with its PC-relative type patch.
5258 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08005259 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005260 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005261 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005262 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005263 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005264 codegen_->NewBootImageTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005265 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005266 break;
5267 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005268 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005269 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markode91ca92020-10-27 13:41:40 +00005270 uint32_t boot_image_offset = CodeGenerator::GetBootImageOffset(cls);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005271 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5272 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005273 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005274 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005275 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005276 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005277 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005278 break;
5279 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005280 case HLoadClass::LoadKind::kBssEntry:
5281 case HLoadClass::LoadKind::kBssEntryPublic:
5282 case HLoadClass::LoadKind::kBssEntryPackage: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005283 // Add ADRP with its PC-relative Class .bss entry patch.
Vladimir Markof3c52b42017-11-17 17:32:12 +00005284 vixl::aarch64::Register temp = XRegisterFrom(out_loc);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005285 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(cls);
Vladimir Markof3c52b42017-11-17 17:32:12 +00005286 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005287 // Add LDR with its PC-relative Class .bss entry patch.
Vladimir Marko8f63f102020-09-28 12:10:28 +01005288 vixl::aarch64::Label* ldr_label = codegen_->NewBssEntryTypePatch(cls, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005289 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005290 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005291 codegen_->GenerateGcRootFieldLoad(cls,
5292 out_loc,
5293 temp,
5294 /* offset placeholder */ 0u,
5295 ldr_label,
5296 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005297 generate_null_check = true;
5298 break;
5299 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005300 case HLoadClass::LoadKind::kJitBootImageAddress: {
5301 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5302 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
5303 DCHECK_NE(address, 0u);
5304 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5305 break;
5306 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005307 case HLoadClass::LoadKind::kJitTableAddress: {
5308 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5309 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005310 cls->GetClass()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005311 codegen_->GenerateGcRootFieldLoad(cls,
5312 out_loc,
5313 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005314 /* offset= */ 0,
5315 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005316 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005317 break;
5318 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005319 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005320 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005321 LOG(FATAL) << "UNREACHABLE";
5322 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005323 }
5324
Vladimir Markoea4c1262017-02-06 19:59:33 +00005325 bool do_clinit = cls->MustGenerateClinitCheck();
5326 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005327 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01005328 SlowPathCodeARM64* slow_path =
5329 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005330 codegen_->AddSlowPath(slow_path);
5331 if (generate_null_check) {
5332 __ Cbz(out, slow_path->GetEntryLabel());
5333 }
5334 if (cls->MustGenerateClinitCheck()) {
5335 GenerateClassInitializationCheck(slow_path, out);
5336 } else {
5337 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005338 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005339 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005340 }
5341}
5342
Orion Hodsondbaa5c72018-05-10 08:22:46 +01005343void LocationsBuilderARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5344 InvokeRuntimeCallingConvention calling_convention;
5345 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5346 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
5347}
5348
5349void InstructionCodeGeneratorARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5350 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
5351}
5352
Orion Hodson18259d72018-04-12 11:18:23 +01005353void LocationsBuilderARM64::VisitLoadMethodType(HLoadMethodType* load) {
5354 InvokeRuntimeCallingConvention calling_convention;
5355 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5356 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
5357}
5358
5359void InstructionCodeGeneratorARM64::VisitLoadMethodType(HLoadMethodType* load) {
5360 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
5361}
5362
David Brazdilcb1c0552015-08-04 16:22:25 +01005363static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005364 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005365}
5366
Alexandre Rames67555f72014-11-18 10:55:16 +00005367void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5368 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005369 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00005370 locations->SetOut(Location::RequiresRegister());
5371}
5372
5373void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005374 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5375}
5376
5377void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005378 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005379}
5380
5381void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5382 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005383}
5384
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005385HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5386 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005387 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005388 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005389 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005390 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01005391 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005392 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005393 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005394 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01005395 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005396 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005397 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005398 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005399 }
5400 return desired_string_load_kind;
5401}
5402
Alexandre Rames67555f72014-11-18 10:55:16 +00005403void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005404 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005405 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005406 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005407 InvokeRuntimeCallingConvention calling_convention;
5408 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5409 } else {
5410 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005411 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5412 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005413 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005414 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005415 } else {
5416 // For non-Baker read barrier we have a temp-clobbering call.
5417 }
5418 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005419 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005420}
5421
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005422// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5423// move.
5424void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005425 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005426 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005427
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005428 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005429 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005430 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5431 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005432 // Add ADRP with its PC-relative String patch.
5433 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005434 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005435 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005436 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005437 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005438 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005439 codegen_->NewBootImageStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005440 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005441 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005442 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005443 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005444 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005445 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markode91ca92020-10-27 13:41:40 +00005446 uint32_t boot_image_offset = CodeGenerator::GetBootImageOffset(load);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005447 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005448 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005449 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005450 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005451 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005452 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
5453 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005454 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005455 case HLoadString::LoadKind::kBssEntry: {
5456 // Add ADRP with its PC-relative String .bss entry patch.
5457 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005458 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00005459 Register temp = XRegisterFrom(out_loc);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005460 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005461 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005462 // Add LDR with its PC-relative String .bss entry patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005463 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005464 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005465 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005466 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005467 codegen_->GenerateGcRootFieldLoad(load,
5468 out_loc,
5469 temp,
5470 /* offset placeholder */ 0u,
5471 ldr_label,
5472 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005473 SlowPathCodeARM64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005474 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005475 codegen_->AddSlowPath(slow_path);
5476 __ Cbz(out.X(), slow_path->GetEntryLabel());
5477 __ Bind(slow_path->GetExitLabel());
Andreas Gampe3db70682018-12-26 15:12:03 -08005478 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005479 return;
5480 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005481 case HLoadString::LoadKind::kJitBootImageAddress: {
5482 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
5483 DCHECK_NE(address, 0u);
5484 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5485 return;
5486 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005487 case HLoadString::LoadKind::kJitTableAddress: {
5488 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005489 load->GetStringIndex(),
5490 load->GetString()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005491 codegen_->GenerateGcRootFieldLoad(load,
5492 out_loc,
5493 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005494 /* offset= */ 0,
5495 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005496 kCompilerReadBarrierOption);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005497 return;
5498 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005499 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005500 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005501 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005502
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005503 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005504 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005505 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005506 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005507 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5508 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005509 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005510}
5511
Alexandre Rames5319def2014-10-23 10:03:10 +01005512void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005513 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01005514 locations->SetOut(Location::ConstantLocation(constant));
5515}
5516
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005517void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005518 // Will be generated at use site.
5519}
5520
Alexandre Rames67555f72014-11-18 10:55:16 +00005521void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005522 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5523 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005524 InvokeRuntimeCallingConvention calling_convention;
5525 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5526}
5527
5528void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005529 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005530 instruction,
5531 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005532 if (instruction->IsEnter()) {
5533 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5534 } else {
5535 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5536 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005537 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005538}
5539
Alexandre Rames42d641b2014-10-27 14:00:51 +00005540void LocationsBuilderARM64::VisitMul(HMul* mul) {
5541 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005542 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005543 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005544 case DataType::Type::kInt32:
5545 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005546 locations->SetInAt(0, Location::RequiresRegister());
5547 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005548 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005549 break;
5550
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005551 case DataType::Type::kFloat32:
5552 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005553 locations->SetInAt(0, Location::RequiresFpuRegister());
5554 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005555 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005556 break;
5557
5558 default:
5559 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5560 }
5561}
5562
5563void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5564 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005565 case DataType::Type::kInt32:
5566 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005567 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5568 break;
5569
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005570 case DataType::Type::kFloat32:
5571 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005572 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005573 break;
5574
5575 default:
5576 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5577 }
5578}
5579
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005580void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5581 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005582 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005583 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005584 case DataType::Type::kInt32:
5585 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005586 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005587 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005588 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005589
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005590 case DataType::Type::kFloat32:
5591 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005592 locations->SetInAt(0, Location::RequiresFpuRegister());
5593 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005594 break;
5595
5596 default:
5597 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5598 }
5599}
5600
5601void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5602 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005603 case DataType::Type::kInt32:
5604 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005605 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5606 break;
5607
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005608 case DataType::Type::kFloat32:
5609 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005610 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005611 break;
5612
5613 default:
5614 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5615 }
5616}
5617
5618void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005619 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5620 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005621 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005622 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005623 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5624 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005625}
5626
5627void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005628 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5629 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005630 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005631 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005632 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005633}
5634
Alexandre Rames5319def2014-10-23 10:03:10 +01005635void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005636 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5637 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005638 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07005639 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005640 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005641}
5642
5643void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005644 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5645 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005646 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005647}
5648
5649void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005650 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005651 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005652 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005653}
5654
5655void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005656 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005657 case DataType::Type::kInt32:
5658 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005659 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005660 break;
5661
5662 default:
5663 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5664 }
5665}
5666
David Brazdil66d126e2015-04-03 16:02:44 +01005667void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005668 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005669 locations->SetInAt(0, Location::RequiresRegister());
5670 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5671}
5672
5673void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005674 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005675}
5676
Alexandre Rames5319def2014-10-23 10:03:10 +01005677void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005678 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5679 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005680}
5681
Calin Juravle2ae48182016-03-16 14:05:09 +00005682void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5683 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005684 return;
5685 }
Artem Serov914d7a82017-02-07 14:33:49 +00005686 {
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01005687 // Ensure that between load and RecordPcInfo there are no pools emitted.
Artem Serov914d7a82017-02-07 14:33:49 +00005688 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5689 Location obj = instruction->GetLocations()->InAt(0);
5690 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5691 RecordPcInfo(instruction, instruction->GetDexPc());
5692 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005693}
5694
Calin Juravle2ae48182016-03-16 14:05:09 +00005695void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005696 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005697 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005698
5699 LocationSummary* locations = instruction->GetLocations();
5700 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005701
5702 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005703}
5704
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005705void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005706 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005707}
5708
Alexandre Rames67555f72014-11-18 10:55:16 +00005709void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5710 HandleBinaryOp(instruction);
5711}
5712
5713void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5714 HandleBinaryOp(instruction);
5715}
5716
Alexandre Rames3e69f162014-12-10 10:36:50 +00005717void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5718 LOG(FATAL) << "Unreachable";
5719}
5720
5721void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005722 if (instruction->GetNext()->IsSuspendCheck() &&
5723 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5724 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5725 // The back edge will generate the suspend check.
5726 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5727 }
5728
Alexandre Rames3e69f162014-12-10 10:36:50 +00005729 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5730}
5731
Alexandre Rames5319def2014-10-23 10:03:10 +01005732void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005733 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005734 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5735 if (location.IsStackSlot()) {
5736 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5737 } else if (location.IsDoubleStackSlot()) {
5738 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5739 }
5740 locations->SetOut(location);
5741}
5742
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005743void InstructionCodeGeneratorARM64::VisitParameterValue(
5744 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005745 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005746}
5747
5748void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5749 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005750 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005751 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005752}
5753
5754void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5755 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5756 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005757}
5758
5759void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005760 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005761 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005762 locations->SetInAt(i, Location::Any());
5763 }
5764 locations->SetOut(Location::Any());
5765}
5766
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005767void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005768 LOG(FATAL) << "Unreachable";
5769}
5770
Serban Constantinescu02164b32014-11-13 14:05:07 +00005771void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005772 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005773 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005774 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005775 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005776 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005777
5778 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005779 case DataType::Type::kInt32:
5780 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005781 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005782 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005783 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5784 break;
5785
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005786 case DataType::Type::kFloat32:
5787 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005788 InvokeRuntimeCallingConvention calling_convention;
5789 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5790 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5791 locations->SetOut(calling_convention.GetReturnLocation(type));
5792
5793 break;
5794 }
5795
Serban Constantinescu02164b32014-11-13 14:05:07 +00005796 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005797 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005798 }
5799}
5800
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005801void InstructionCodeGeneratorARM64::GenerateIntRemForPower2Denom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005802 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005803 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
5804 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
5805
5806 Register out = OutputRegister(instruction);
5807 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005808
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01005809 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01005810 // No need to adjust the result for non-negative dividends or the INT32_MIN/INT64_MIN dividends.
5811 // NOTE: The generated code for HRem correctly works for the INT32_MIN/INT64_MIN dividends.
5812 // INT*_MIN % imm must be 0 for any imm of power 2. 'and' works only with bits
5813 // 0..30 (Int32 case)/0..62 (Int64 case) of a dividend. For INT32_MIN/INT64_MIN they are zeros.
5814 // So 'and' always produces zero.
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005815 __ And(out, dividend, abs_imm - 1);
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01005816 } else {
5817 if (abs_imm == 2) {
5818 __ Cmp(dividend, 0);
5819 __ And(out, dividend, 1);
5820 __ Csneg(out, out, out, ge);
5821 } else {
5822 UseScratchRegisterScope temps(GetVIXLAssembler());
5823 Register temp = temps.AcquireSameSizeAs(out);
5824
5825 __ Negs(temp, dividend);
5826 __ And(out, dividend, abs_imm - 1);
5827 __ And(temp, temp, abs_imm - 1);
5828 __ Csneg(out, out, temp, mi);
5829 }
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005830 }
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005831}
5832
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005833void InstructionCodeGeneratorARM64::GenerateIntRemForConstDenom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005834 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005835
5836 if (imm == 0) {
5837 // Do not generate anything.
5838 // DivZeroCheck would prevent any code to be executed.
5839 return;
5840 }
5841
Evgeny Astigeevichf58dc652018-06-25 17:54:07 +01005842 if (IsPowerOfTwo(AbsOrMin(imm))) {
5843 // Cases imm == -1 or imm == 1 are handled in constant folding by
5844 // InstructionWithAbsorbingInputSimplifier.
5845 // If the cases have survided till code generation they are handled in
5846 // GenerateIntRemForPower2Denom becauses -1 and 1 are the power of 2 (2^0).
5847 // The correct code is generated for them, just more instructions.
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005848 GenerateIntRemForPower2Denom(instruction);
5849 } else {
5850 DCHECK(imm < -2 || imm > 2) << imm;
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01005851 GenerateDivRemWithAnyConstant(instruction, imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005852 }
5853}
5854
5855void InstructionCodeGeneratorARM64::GenerateIntRem(HRem* instruction) {
5856 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
5857 << instruction->GetResultType();
5858
5859 if (instruction->GetLocations()->InAt(1).IsConstant()) {
5860 GenerateIntRemForConstDenom(instruction);
5861 } else {
5862 Register out = OutputRegister(instruction);
5863 Register dividend = InputRegisterAt(instruction, 0);
5864 Register divisor = InputRegisterAt(instruction, 1);
5865 UseScratchRegisterScope temps(GetVIXLAssembler());
5866 Register temp = temps.AcquireSameSizeAs(out);
5867 __ Sdiv(temp, dividend, divisor);
5868 __ Msub(out, temp, divisor, dividend);
5869 }
5870}
5871
Serban Constantinescu02164b32014-11-13 14:05:07 +00005872void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005873 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005874
Serban Constantinescu02164b32014-11-13 14:05:07 +00005875 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005876 case DataType::Type::kInt32:
5877 case DataType::Type::kInt64: {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005878 GenerateIntRem(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005879 break;
5880 }
5881
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005882 case DataType::Type::kFloat32:
5883 case DataType::Type::kFloat64: {
5884 QuickEntrypointEnum entrypoint =
5885 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005886 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005887 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00005888 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5889 } else {
5890 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5891 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005892 break;
5893 }
5894
Serban Constantinescu02164b32014-11-13 14:05:07 +00005895 default:
5896 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005897 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005898 }
5899}
5900
Aart Bik1f8d51b2018-02-15 10:42:37 -08005901void LocationsBuilderARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005902 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005903}
5904
Aart Bik1f8d51b2018-02-15 10:42:37 -08005905void InstructionCodeGeneratorARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005906 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005907}
5908
5909void LocationsBuilderARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005910 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005911}
5912
5913void InstructionCodeGeneratorARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005914 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005915}
5916
Aart Bik3dad3412018-02-28 12:01:46 -08005917void LocationsBuilderARM64::VisitAbs(HAbs* abs) {
5918 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
5919 switch (abs->GetResultType()) {
5920 case DataType::Type::kInt32:
5921 case DataType::Type::kInt64:
5922 locations->SetInAt(0, Location::RequiresRegister());
5923 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5924 break;
5925 case DataType::Type::kFloat32:
5926 case DataType::Type::kFloat64:
5927 locations->SetInAt(0, Location::RequiresFpuRegister());
5928 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5929 break;
5930 default:
5931 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5932 }
5933}
5934
5935void InstructionCodeGeneratorARM64::VisitAbs(HAbs* abs) {
5936 switch (abs->GetResultType()) {
5937 case DataType::Type::kInt32:
5938 case DataType::Type::kInt64: {
5939 Register in_reg = InputRegisterAt(abs, 0);
5940 Register out_reg = OutputRegister(abs);
5941 __ Cmp(in_reg, Operand(0));
5942 __ Cneg(out_reg, in_reg, lt);
5943 break;
5944 }
5945 case DataType::Type::kFloat32:
5946 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01005947 VRegister in_reg = InputFPRegisterAt(abs, 0);
5948 VRegister out_reg = OutputFPRegister(abs);
Aart Bik3dad3412018-02-28 12:01:46 -08005949 __ Fabs(out_reg, in_reg);
5950 break;
5951 }
5952 default:
5953 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5954 }
5955}
5956
Igor Murashkind01745e2017-04-05 16:40:31 -07005957void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5958 constructor_fence->SetLocations(nullptr);
5959}
5960
5961void InstructionCodeGeneratorARM64::VisitConstructorFence(
5962 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5963 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5964}
5965
Calin Juravle27df7582015-04-17 19:12:31 +01005966void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5967 memory_barrier->SetLocations(nullptr);
5968}
5969
5970void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005971 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005972}
5973
Alexandre Rames5319def2014-10-23 10:03:10 +01005974void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005975 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005976 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005977 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005978}
5979
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00005980void InstructionCodeGeneratorARM64::VisitReturn(HReturn* ret) {
5981 if (GetGraph()->IsCompilingOsr()) {
5982 // To simplify callers of an OSR method, we put the return value in both
5983 // floating point and core register.
5984 switch (ret->InputAt(0)->GetType()) {
5985 case DataType::Type::kFloat32:
5986 __ Fmov(w0, s0);
5987 break;
5988 case DataType::Type::kFloat64:
5989 __ Fmov(x0, d0);
5990 break;
5991 default:
5992 break;
5993 }
5994 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005995 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005996}
5997
5998void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5999 instruction->SetLocations(nullptr);
6000}
6001
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006002void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01006003 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01006004}
6005
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006006void LocationsBuilderARM64::VisitRor(HRor* ror) {
6007 HandleBinaryOp(ror);
6008}
6009
6010void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
6011 HandleBinaryOp(ror);
6012}
6013
Serban Constantinescu02164b32014-11-13 14:05:07 +00006014void LocationsBuilderARM64::VisitShl(HShl* shl) {
6015 HandleShift(shl);
6016}
6017
6018void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
6019 HandleShift(shl);
6020}
6021
6022void LocationsBuilderARM64::VisitShr(HShr* shr) {
6023 HandleShift(shr);
6024}
6025
6026void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
6027 HandleShift(shr);
6028}
6029
Alexandre Rames5319def2014-10-23 10:03:10 +01006030void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006031 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006032}
6033
6034void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006035 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006036}
6037
Alexandre Rames67555f72014-11-18 10:55:16 +00006038void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006039 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00006040}
6041
6042void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01006043 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00006044}
6045
6046void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01006047 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006048}
6049
Alexandre Rames67555f72014-11-18 10:55:16 +00006050void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006051 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01006052}
6053
Vladimir Marko552a1342017-10-31 10:56:47 +00006054void LocationsBuilderARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6055 codegen_->CreateStringBuilderAppendLocations(instruction, LocationFrom(x0));
6056}
6057
6058void InstructionCodeGeneratorARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6059 __ Mov(w0, instruction->GetFormat()->GetValue());
6060 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
6061}
6062
Calin Juravlee460d1d2015-09-29 04:52:17 +01006063void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
6064 HUnresolvedInstanceFieldGet* instruction) {
6065 FieldAccessCallingConventionARM64 calling_convention;
6066 codegen_->CreateUnresolvedFieldLocationSummary(
6067 instruction, instruction->GetFieldType(), calling_convention);
6068}
6069
6070void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
6071 HUnresolvedInstanceFieldGet* instruction) {
6072 FieldAccessCallingConventionARM64 calling_convention;
6073 codegen_->GenerateUnresolvedFieldAccess(instruction,
6074 instruction->GetFieldType(),
6075 instruction->GetFieldIndex(),
6076 instruction->GetDexPc(),
6077 calling_convention);
6078}
6079
6080void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
6081 HUnresolvedInstanceFieldSet* instruction) {
6082 FieldAccessCallingConventionARM64 calling_convention;
6083 codegen_->CreateUnresolvedFieldLocationSummary(
6084 instruction, instruction->GetFieldType(), calling_convention);
6085}
6086
6087void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
6088 HUnresolvedInstanceFieldSet* instruction) {
6089 FieldAccessCallingConventionARM64 calling_convention;
6090 codegen_->GenerateUnresolvedFieldAccess(instruction,
6091 instruction->GetFieldType(),
6092 instruction->GetFieldIndex(),
6093 instruction->GetDexPc(),
6094 calling_convention);
6095}
6096
6097void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
6098 HUnresolvedStaticFieldGet* instruction) {
6099 FieldAccessCallingConventionARM64 calling_convention;
6100 codegen_->CreateUnresolvedFieldLocationSummary(
6101 instruction, instruction->GetFieldType(), calling_convention);
6102}
6103
6104void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
6105 HUnresolvedStaticFieldGet* instruction) {
6106 FieldAccessCallingConventionARM64 calling_convention;
6107 codegen_->GenerateUnresolvedFieldAccess(instruction,
6108 instruction->GetFieldType(),
6109 instruction->GetFieldIndex(),
6110 instruction->GetDexPc(),
6111 calling_convention);
6112}
6113
6114void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
6115 HUnresolvedStaticFieldSet* instruction) {
6116 FieldAccessCallingConventionARM64 calling_convention;
6117 codegen_->CreateUnresolvedFieldLocationSummary(
6118 instruction, instruction->GetFieldType(), calling_convention);
6119}
6120
6121void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
6122 HUnresolvedStaticFieldSet* instruction) {
6123 FieldAccessCallingConventionARM64 calling_convention;
6124 codegen_->GenerateUnresolvedFieldAccess(instruction,
6125 instruction->GetFieldType(),
6126 instruction->GetFieldIndex(),
6127 instruction->GetDexPc(),
6128 calling_convention);
6129}
6130
Alexandre Rames5319def2014-10-23 10:03:10 +01006131void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006132 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6133 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01006134 // In suspend check slow path, usually there are no caller-save registers at all.
6135 // If SIMD instructions are present, however, we force spilling all live SIMD
6136 // registers in full width (since the runtime only saves/restores lower part).
6137 locations->SetCustomSlowPathCallerSaves(
6138 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01006139}
6140
6141void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006142 HBasicBlock* block = instruction->GetBlock();
6143 if (block->GetLoopInformation() != nullptr) {
6144 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6145 // The back edge will generate the suspend check.
6146 return;
6147 }
6148 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6149 // The goto will generate the suspend check.
6150 return;
6151 }
6152 GenerateSuspendCheck(instruction, nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08006153 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01006154}
6155
Alexandre Rames67555f72014-11-18 10:55:16 +00006156void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006157 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6158 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00006159 InvokeRuntimeCallingConvention calling_convention;
6160 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
6161}
6162
6163void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00006164 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08006165 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00006166}
6167
6168void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
6169 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006170 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006171 DataType::Type input_type = conversion->GetInputType();
6172 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006173 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6174 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006175 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
6176 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006177 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6178 }
6179
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006180 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006181 locations->SetInAt(0, Location::RequiresFpuRegister());
6182 } else {
6183 locations->SetInAt(0, Location::RequiresRegister());
6184 }
6185
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006186 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006187 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6188 } else {
6189 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6190 }
6191}
6192
6193void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006194 DataType::Type result_type = conversion->GetResultType();
6195 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00006196
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006197 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6198 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006199
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006200 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
6201 int result_size = DataType::Size(result_type);
6202 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00006203 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006204 Register output = OutputRegister(conversion);
6205 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006206 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01006207 // 'int' values are used directly as W registers, discarding the top
6208 // bits, so we don't need to sign-extend and can just perform a move.
6209 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
6210 // top 32 bits of the target register. We theoretically could leave those
6211 // bits unchanged, but we would have to make sure that no code uses a
6212 // 32bit input value as a 64bit value assuming that the top 32 bits are
6213 // zero.
6214 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006215 } else if (DataType::IsUnsignedType(result_type) ||
6216 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
6217 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006218 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00006219 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006220 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006221 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006222 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006223 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6224 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006225 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006226 } else if (DataType::IsFloatingPointType(result_type) &&
6227 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006228 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
6229 } else {
6230 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6231 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006232 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00006233}
Alexandre Rames67555f72014-11-18 10:55:16 +00006234
Serban Constantinescu02164b32014-11-13 14:05:07 +00006235void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
6236 HandleShift(ushr);
6237}
6238
6239void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
6240 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00006241}
6242
6243void LocationsBuilderARM64::VisitXor(HXor* instruction) {
6244 HandleBinaryOp(instruction);
6245}
6246
6247void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
6248 HandleBinaryOp(instruction);
6249}
6250
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006251void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006252 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006253 LOG(FATAL) << "Unreachable";
6254}
6255
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006256void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006257 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006258 LOG(FATAL) << "Unreachable";
6259}
6260
Mark Mendellfe57faa2015-09-18 09:26:15 -04006261// Simple implementation of packed switch - generate cascaded compare/jumps.
6262void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6263 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006264 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006265 locations->SetInAt(0, Location::RequiresRegister());
6266}
6267
6268void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6269 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08006270 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006271 Register value_reg = InputRegisterAt(switch_instr, 0);
6272 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6273
Zheng Xu3927c8b2015-11-18 17:46:25 +08006274 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01006275 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08006276 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
6277 // make sure we don't emit it if the target may run out of range.
6278 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
6279 // ranges and emit the tables only as required.
6280 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04006281
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006282 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08006283 // Current instruction id is an upper bound of the number of HIRs in the graph.
6284 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
6285 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006286 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6287 Register temp = temps.AcquireW();
6288 __ Subs(temp, value_reg, Operand(lower_bound));
6289
Zheng Xu3927c8b2015-11-18 17:46:25 +08006290 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006291 // Jump to successors[0] if value == lower_bound.
6292 __ B(eq, codegen_->GetLabelOf(successors[0]));
6293 int32_t last_index = 0;
6294 for (; num_entries - last_index > 2; last_index += 2) {
6295 __ Subs(temp, temp, Operand(2));
6296 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6297 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
6298 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6299 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
6300 }
6301 if (num_entries - last_index == 2) {
6302 // The last missing case_value.
6303 __ Cmp(temp, Operand(1));
6304 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08006305 }
6306
6307 // And the default for any other value.
6308 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6309 __ B(codegen_->GetLabelOf(default_block));
6310 }
6311 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01006312 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08006313
6314 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6315
6316 // Below instructions should use at most one blocked register. Since there are two blocked
6317 // registers, we are free to block one.
6318 Register temp_w = temps.AcquireW();
6319 Register index;
6320 // Remove the bias.
6321 if (lower_bound != 0) {
6322 index = temp_w;
6323 __ Sub(index, value_reg, Operand(lower_bound));
6324 } else {
6325 index = value_reg;
6326 }
6327
6328 // Jump to default block if index is out of the range.
6329 __ Cmp(index, Operand(num_entries));
6330 __ B(hs, codegen_->GetLabelOf(default_block));
6331
6332 // In current VIXL implementation, it won't require any blocked registers to encode the
6333 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
6334 // register pressure.
6335 Register table_base = temps.AcquireX();
6336 // Load jump offset from the table.
6337 __ Adr(table_base, jump_table->GetTableStartLabel());
6338 Register jump_offset = temp_w;
6339 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
6340
6341 // Jump to target block by branching to table_base(pc related) + offset.
6342 Register target_address = table_base;
6343 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
6344 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006345 }
6346}
6347
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006348void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
6349 HInstruction* instruction,
6350 Location out,
6351 uint32_t offset,
6352 Location maybe_temp,
6353 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006354 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006355 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006356 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006357 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006358 if (kUseBakerReadBarrier) {
6359 // Load with fast path based Baker's read barrier.
6360 // /* HeapReference<Object> */ out = *(out + offset)
6361 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6362 out,
6363 out_reg,
6364 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006365 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006366 /* needs_null_check= */ false,
6367 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006368 } else {
6369 // Load with slow path based read barrier.
6370 // Save the value of `out` into `maybe_temp` before overwriting it
6371 // in the following move operation, as we will need it for the
6372 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006373 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00006374 __ Mov(temp_reg, out_reg);
6375 // /* HeapReference<Object> */ out = *(out + offset)
6376 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6377 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6378 }
6379 } else {
6380 // Plain load with no read barrier.
6381 // /* HeapReference<Object> */ out = *(out + offset)
6382 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6383 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6384 }
6385}
6386
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006387void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
6388 HInstruction* instruction,
6389 Location out,
6390 Location obj,
6391 uint32_t offset,
6392 Location maybe_temp,
6393 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006394 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006395 Register out_reg = RegisterFrom(out, type);
6396 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006397 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006398 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006399 if (kUseBakerReadBarrier) {
6400 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00006401 // /* HeapReference<Object> */ out = *(obj + offset)
6402 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6403 out,
6404 obj_reg,
6405 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006406 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006407 /* needs_null_check= */ false,
6408 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006409 } else {
6410 // Load with slow path based read barrier.
6411 // /* HeapReference<Object> */ out = *(obj + offset)
6412 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6413 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6414 }
6415 } else {
6416 // Plain load with no read barrier.
6417 // /* HeapReference<Object> */ out = *(obj + offset)
6418 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6419 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6420 }
6421}
6422
Vladimir Markoca1e0382018-04-11 09:58:41 +00006423void CodeGeneratorARM64::GenerateGcRootFieldLoad(
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006424 HInstruction* instruction,
6425 Location root,
6426 Register obj,
6427 uint32_t offset,
6428 vixl::aarch64::Label* fixup_label,
6429 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006430 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006431 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006432 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006433 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006434 if (kUseBakerReadBarrier) {
6435 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00006436 // Baker's read barrier are used.
Roland Levillain44015862016-01-22 11:47:17 +00006437
Vladimir Marko008e09f32018-08-06 15:42:43 +01006438 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6439 // the Marking Register) to decide whether we need to enter
6440 // the slow path to mark the GC root.
6441 //
6442 // We use shared thunks for the slow path; shared within the method
6443 // for JIT, across methods for AOT. That thunk checks the reference
6444 // and jumps to the entrypoint if needed.
6445 //
6446 // lr = &return_address;
6447 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6448 // if (mr) { // Thread::Current()->GetIsGcMarking()
6449 // goto gc_root_thunk<root_reg>(lr)
6450 // }
6451 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00006452
Vladimir Marko008e09f32018-08-06 15:42:43 +01006453 UseScratchRegisterScope temps(GetVIXLAssembler());
6454 DCHECK(temps.IsAvailable(ip0));
6455 DCHECK(temps.IsAvailable(ip1));
6456 temps.Exclude(ip0, ip1);
6457 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
Roland Levillain44015862016-01-22 11:47:17 +00006458
Vladimir Marko008e09f32018-08-06 15:42:43 +01006459 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6460 vixl::aarch64::Label return_address;
6461 __ adr(lr, &return_address);
6462 if (fixup_label != nullptr) {
6463 __ bind(fixup_label);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006464 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006465 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
Vladimir Marko94796f82018-08-08 15:15:33 +01006466 "GC root LDR must be 2 instructions (8B) before the return address label.");
Vladimir Marko008e09f32018-08-06 15:42:43 +01006467 __ ldr(root_reg, MemOperand(obj.X(), offset));
6468 EmitBakerReadBarrierCbnz(custom_data);
6469 __ bind(&return_address);
Roland Levillain44015862016-01-22 11:47:17 +00006470 } else {
6471 // GC root loaded through a slow path for read barriers other
6472 // than Baker's.
6473 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006474 if (fixup_label == nullptr) {
6475 __ Add(root_reg.X(), obj.X(), offset);
6476 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006477 EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006478 }
Roland Levillain44015862016-01-22 11:47:17 +00006479 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00006480 GenerateReadBarrierForRootSlow(instruction, root, root);
Roland Levillain44015862016-01-22 11:47:17 +00006481 }
6482 } else {
6483 // Plain GC root load with no read barrier.
6484 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006485 if (fixup_label == nullptr) {
6486 __ Ldr(root_reg, MemOperand(obj, offset));
6487 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006488 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006489 }
Roland Levillain44015862016-01-22 11:47:17 +00006490 // Note that GC roots are not affected by heap poisoning, thus we
6491 // do not have to unpoison `root_reg` here.
6492 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006493 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00006494}
6495
Vladimir Marko94796f82018-08-08 15:15:33 +01006496void CodeGeneratorARM64::GenerateUnsafeCasOldValueMovWithBakerReadBarrier(
6497 vixl::aarch64::Register marked,
6498 vixl::aarch64::Register old_value) {
6499 DCHECK(kEmitCompilerReadBarrier);
6500 DCHECK(kUseBakerReadBarrier);
6501
6502 // Similar to the Baker RB path in GenerateGcRootFieldLoad(), with a MOV instead of LDR.
6503 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(marked.GetCode());
6504
6505 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6506 vixl::aarch64::Label return_address;
6507 __ adr(lr, &return_address);
6508 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6509 "GC root LDR must be 2 instructions (8B) before the return address label.");
6510 __ mov(marked, old_value);
6511 EmitBakerReadBarrierCbnz(custom_data);
6512 __ bind(&return_address);
6513}
6514
Roland Levillain44015862016-01-22 11:47:17 +00006515void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6516 Location ref,
Vladimir Marko248141f2018-08-10 10:40:07 +01006517 vixl::aarch64::Register obj,
6518 const vixl::aarch64::MemOperand& src,
Roland Levillain44015862016-01-22 11:47:17 +00006519 bool needs_null_check,
6520 bool use_load_acquire) {
6521 DCHECK(kEmitCompilerReadBarrier);
6522 DCHECK(kUseBakerReadBarrier);
6523
Vladimir Marko0ecac682018-08-07 10:40:38 +01006524 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6525 // Marking Register) to decide whether we need to enter the slow
6526 // path to mark the reference. Then, in the slow path, check the
6527 // gray bit in the lock word of the reference's holder (`obj`) to
6528 // decide whether to mark `ref` or not.
6529 //
6530 // We use shared thunks for the slow path; shared within the method
6531 // for JIT, across methods for AOT. That thunk checks the holder
6532 // and jumps to the entrypoint if needed. If the holder is not gray,
6533 // it creates a fake dependency and returns to the LDR instruction.
6534 //
6535 // lr = &gray_return_address;
6536 // if (mr) { // Thread::Current()->GetIsGcMarking()
6537 // goto field_thunk<holder_reg, base_reg, use_load_acquire>(lr)
6538 // }
6539 // not_gray_return_address:
6540 // // Original reference load. If the offset is too large to fit
6541 // // into LDR, we use an adjusted base register here.
6542 // HeapReference<mirror::Object> reference = *(obj+offset);
6543 // gray_return_address:
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006544
Vladimir Marko248141f2018-08-10 10:40:07 +01006545 DCHECK(src.GetAddrMode() == vixl::aarch64::Offset);
6546 DCHECK_ALIGNED(src.GetOffset(), sizeof(mirror::HeapReference<mirror::Object>));
6547
6548 UseScratchRegisterScope temps(GetVIXLAssembler());
6549 DCHECK(temps.IsAvailable(ip0));
6550 DCHECK(temps.IsAvailable(ip1));
6551 temps.Exclude(ip0, ip1);
6552 uint32_t custom_data = use_load_acquire
6553 ? EncodeBakerReadBarrierAcquireData(src.GetBaseRegister().GetCode(), obj.GetCode())
6554 : EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode());
6555
6556 {
6557 ExactAssemblyScope guard(GetVIXLAssembler(),
6558 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6559 vixl::aarch64::Label return_address;
6560 __ adr(lr, &return_address);
6561 EmitBakerReadBarrierCbnz(custom_data);
6562 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6563 "Field LDR must be 1 instruction (4B) before the return address label; "
6564 " 2 instructions (8B) for heap poisoning.");
6565 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
6566 if (use_load_acquire) {
6567 DCHECK_EQ(src.GetOffset(), 0);
6568 __ ldar(ref_reg, src);
6569 } else {
6570 __ ldr(ref_reg, src);
6571 }
6572 if (needs_null_check) {
6573 MaybeRecordImplicitNullCheck(instruction);
6574 }
6575 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6576 // macro instructions disallowed in ExactAssemblyScope.
6577 if (kPoisonHeapReferences) {
6578 __ neg(ref_reg, Operand(ref_reg));
6579 }
6580 __ bind(&return_address);
6581 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006582 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Vladimir Marko248141f2018-08-10 10:40:07 +01006583}
6584
6585void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6586 Location ref,
6587 Register obj,
6588 uint32_t offset,
6589 Location maybe_temp,
6590 bool needs_null_check,
6591 bool use_load_acquire) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006592 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6593 Register base = obj;
6594 if (use_load_acquire) {
6595 DCHECK(maybe_temp.IsRegister());
6596 base = WRegisterFrom(maybe_temp);
6597 __ Add(base, obj, offset);
6598 offset = 0u;
6599 } else if (offset >= kReferenceLoadMinFarOffset) {
6600 DCHECK(maybe_temp.IsRegister());
6601 base = WRegisterFrom(maybe_temp);
6602 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6603 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6604 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006605 }
Vladimir Marko248141f2018-08-10 10:40:07 +01006606 MemOperand src(base.X(), offset);
6607 GenerateFieldLoadWithBakerReadBarrier(
6608 instruction, ref, obj, src, needs_null_check, use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006609}
6610
Artem Serov0806f582018-10-11 20:14:20 +01006611void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HArrayGet* instruction,
6612 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006613 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006614 uint32_t data_offset,
6615 Location index,
Roland Levillain44015862016-01-22 11:47:17 +00006616 bool needs_null_check) {
6617 DCHECK(kEmitCompilerReadBarrier);
6618 DCHECK(kUseBakerReadBarrier);
6619
Vladimir Marko66d691d2017-04-07 17:53:39 +01006620 static_assert(
6621 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6622 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006623 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006624
Vladimir Marko008e09f32018-08-06 15:42:43 +01006625 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6626 // Marking Register) to decide whether we need to enter the slow
6627 // path to mark the reference. Then, in the slow path, check the
6628 // gray bit in the lock word of the reference's holder (`obj`) to
6629 // decide whether to mark `ref` or not.
6630 //
6631 // We use shared thunks for the slow path; shared within the method
6632 // for JIT, across methods for AOT. That thunk checks the holder
6633 // and jumps to the entrypoint if needed. If the holder is not gray,
6634 // it creates a fake dependency and returns to the LDR instruction.
6635 //
6636 // lr = &gray_return_address;
6637 // if (mr) { // Thread::Current()->GetIsGcMarking()
6638 // goto array_thunk<base_reg>(lr)
6639 // }
6640 // not_gray_return_address:
6641 // // Original reference load. If the offset is too large to fit
6642 // // into LDR, we use an adjusted base register here.
6643 // HeapReference<mirror::Object> reference = data[index];
6644 // gray_return_address:
Vladimir Marko66d691d2017-04-07 17:53:39 +01006645
Vladimir Marko008e09f32018-08-06 15:42:43 +01006646 DCHECK(index.IsValid());
6647 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
6648 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006649
Vladimir Marko008e09f32018-08-06 15:42:43 +01006650 UseScratchRegisterScope temps(GetVIXLAssembler());
6651 DCHECK(temps.IsAvailable(ip0));
6652 DCHECK(temps.IsAvailable(ip1));
6653 temps.Exclude(ip0, ip1);
Artem Serov0806f582018-10-11 20:14:20 +01006654
6655 Register temp;
6656 if (instruction->GetArray()->IsIntermediateAddress()) {
6657 // We do not need to compute the intermediate address from the array: the
6658 // input instruction has done it already. See the comment in
6659 // `TryExtractArrayAccessAddress()`.
6660 if (kIsDebugBuild) {
6661 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
6662 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
6663 }
6664 temp = obj;
6665 } else {
6666 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
6667 __ Add(temp.X(), obj.X(), Operand(data_offset));
6668 }
6669
Vladimir Marko008e09f32018-08-06 15:42:43 +01006670 uint32_t custom_data = EncodeBakerReadBarrierArrayData(temp.GetCode());
Vladimir Marko66d691d2017-04-07 17:53:39 +01006671
Vladimir Marko008e09f32018-08-06 15:42:43 +01006672 {
6673 ExactAssemblyScope guard(GetVIXLAssembler(),
6674 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6675 vixl::aarch64::Label return_address;
6676 __ adr(lr, &return_address);
6677 EmitBakerReadBarrierCbnz(custom_data);
6678 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6679 "Array LDR must be 1 instruction (4B) before the return address label; "
6680 " 2 instructions (8B) for heap poisoning.");
6681 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6682 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6683 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6684 // macro instructions disallowed in ExactAssemblyScope.
6685 if (kPoisonHeapReferences) {
6686 __ neg(ref_reg, Operand(ref_reg));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006687 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006688 __ bind(&return_address);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006689 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006690 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Roland Levillain44015862016-01-22 11:47:17 +00006691}
6692
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006693void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6694 // The following condition is a compile-time one, so it does not have a run-time cost.
6695 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
6696 // The following condition is a run-time one; it is executed after the
6697 // previous compile-time test, to avoid penalizing non-debug builds.
6698 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6699 UseScratchRegisterScope temps(GetVIXLAssembler());
6700 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6701 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6702 }
6703 }
6704}
6705
Vladimir Marko1bff99f2020-11-02 15:07:33 +00006706SlowPathCodeARM64* CodeGeneratorARM64::AddReadBarrierSlowPath(HInstruction* instruction,
6707 Location out,
6708 Location ref,
6709 Location obj,
6710 uint32_t offset,
6711 Location index) {
6712 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
6713 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6714 AddSlowPath(slow_path);
6715 return slow_path;
6716}
6717
Roland Levillain44015862016-01-22 11:47:17 +00006718void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6719 Location out,
6720 Location ref,
6721 Location obj,
6722 uint32_t offset,
6723 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006724 DCHECK(kEmitCompilerReadBarrier);
6725
Roland Levillain44015862016-01-22 11:47:17 +00006726 // Insert a slow path based read barrier *after* the reference load.
6727 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006728 // If heap poisoning is enabled, the unpoisoning of the loaded
6729 // reference will be carried out by the runtime within the slow
6730 // path.
6731 //
6732 // Note that `ref` currently does not get unpoisoned (when heap
6733 // poisoning is enabled), which is alright as the `ref` argument is
6734 // not used by the artReadBarrierSlow entry point.
6735 //
6736 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko1bff99f2020-11-02 15:07:33 +00006737 SlowPathCodeARM64* slow_path = AddReadBarrierSlowPath(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006738
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006739 __ B(slow_path->GetEntryLabel());
6740 __ Bind(slow_path->GetExitLabel());
6741}
6742
Roland Levillain44015862016-01-22 11:47:17 +00006743void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6744 Location out,
6745 Location ref,
6746 Location obj,
6747 uint32_t offset,
6748 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006749 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006750 // Baker's read barriers shall be handled by the fast path
6751 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6752 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006753 // If heap poisoning is enabled, unpoisoning will be taken care of
6754 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006755 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006756 } else if (kPoisonHeapReferences) {
6757 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6758 }
6759}
6760
Roland Levillain44015862016-01-22 11:47:17 +00006761void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6762 Location out,
6763 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006764 DCHECK(kEmitCompilerReadBarrier);
6765
Roland Levillain44015862016-01-22 11:47:17 +00006766 // Insert a slow path based read barrier *after* the GC root load.
6767 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006768 // Note that GC roots are not affected by heap poisoning, so we do
6769 // not need to do anything special for this here.
6770 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006771 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006772 AddSlowPath(slow_path);
6773
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006774 __ B(slow_path->GetEntryLabel());
6775 __ Bind(slow_path->GetExitLabel());
6776}
6777
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006778void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6779 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006780 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006781 locations->SetInAt(0, Location::RequiresRegister());
6782 locations->SetOut(Location::RequiresRegister());
6783}
6784
6785void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6786 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006787 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006788 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006789 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006790 __ Ldr(XRegisterFrom(locations->Out()),
6791 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006792 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006793 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006794 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006795 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6796 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006797 __ Ldr(XRegisterFrom(locations->Out()),
6798 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006799 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006800}
6801
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006802static void PatchJitRootUse(uint8_t* code,
6803 const uint8_t* roots_data,
6804 vixl::aarch64::Literal<uint32_t>* literal,
6805 uint64_t index_in_table) {
6806 uint32_t literal_offset = literal->GetOffset();
6807 uintptr_t address =
6808 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6809 uint8_t* data = code + literal_offset;
6810 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6811}
6812
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006813void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6814 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006815 const StringReference& string_reference = entry.first;
6816 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006817 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006818 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006819 }
6820 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006821 const TypeReference& type_reference = entry.first;
6822 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006823 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006824 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006825 }
6826}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006827
Artem Serov1a719e42019-07-18 14:24:55 +01006828MemOperand InstructionCodeGeneratorARM64::VecNeonAddress(
6829 HVecMemoryOperation* instruction,
6830 UseScratchRegisterScope* temps_scope,
6831 size_t size,
6832 bool is_string_char_at,
6833 /*out*/ Register* scratch) {
6834 LocationSummary* locations = instruction->GetLocations();
6835 Register base = InputRegisterAt(instruction, 0);
6836
6837 if (instruction->InputAt(1)->IsIntermediateAddressIndex()) {
6838 DCHECK(!is_string_char_at);
6839 return MemOperand(base.X(), InputRegisterAt(instruction, 1).X());
6840 }
6841
6842 Location index = locations->InAt(1);
6843 uint32_t offset = is_string_char_at
6844 ? mirror::String::ValueOffset().Uint32Value()
6845 : mirror::Array::DataOffset(size).Uint32Value();
6846 size_t shift = ComponentSizeShiftWidth(size);
6847
6848 // HIntermediateAddress optimization is only applied for scalar ArrayGet and ArraySet.
6849 DCHECK(!instruction->InputAt(0)->IsIntermediateAddress());
6850
6851 if (index.IsConstant()) {
6852 offset += Int64FromLocation(index) << shift;
6853 return HeapOperand(base, offset);
6854 } else {
6855 *scratch = temps_scope->AcquireSameSizeAs(base);
6856 __ Add(*scratch, base, Operand(WRegisterFrom(index), LSL, shift));
6857 return HeapOperand(*scratch, offset);
6858 }
6859}
6860
Alexandre Rames67555f72014-11-18 10:55:16 +00006861#undef __
6862#undef QUICK_ENTRY_POINT
6863
Vladimir Markoca1e0382018-04-11 09:58:41 +00006864#define __ assembler.GetVIXLAssembler()->
6865
6866static void EmitGrayCheckAndFastPath(arm64::Arm64Assembler& assembler,
6867 vixl::aarch64::Register base_reg,
6868 vixl::aarch64::MemOperand& lock_word,
Vladimir Marko7a695052018-04-12 10:26:50 +01006869 vixl::aarch64::Label* slow_path,
6870 vixl::aarch64::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006871 // Load the lock word containing the rb_state.
6872 __ Ldr(ip0.W(), lock_word);
6873 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01006874 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Vladimir Markoca1e0382018-04-11 09:58:41 +00006875 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6876 __ Tbnz(ip0.W(), LockWord::kReadBarrierStateShift, slow_path);
6877 static_assert(
6878 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET,
6879 "Field and array LDR offsets must be the same to reuse the same code.");
Vladimir Marko7a695052018-04-12 10:26:50 +01006880 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
6881 if (throw_npe != nullptr) {
6882 __ Bind(throw_npe);
6883 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006884 // Adjust the return address back to the LDR (1 instruction; 2 for heap poisoning).
6885 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6886 "Field LDR must be 1 instruction (4B) before the return address label; "
6887 " 2 instructions (8B) for heap poisoning.");
6888 __ Add(lr, lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6889 // Introduce a dependency on the lock_word including rb_state,
6890 // to prevent load-load reordering, and without using
6891 // a memory barrier (which would be more expensive).
6892 __ Add(base_reg, base_reg, Operand(ip0, LSR, 32));
6893 __ Br(lr); // And return back to the function.
6894 // Note: The fake dependency is unnecessary for the slow path.
6895}
6896
6897// Load the read barrier introspection entrypoint in register `entrypoint`.
6898static void LoadReadBarrierMarkIntrospectionEntrypoint(arm64::Arm64Assembler& assembler,
6899 vixl::aarch64::Register entrypoint) {
6900 // entrypoint = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6901 DCHECK_EQ(ip0.GetCode(), 16u);
6902 const int32_t entry_point_offset =
6903 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6904 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
6905}
6906
6907void CodeGeneratorARM64::CompileBakerReadBarrierThunk(Arm64Assembler& assembler,
6908 uint32_t encoded_data,
6909 /*out*/ std::string* debug_name) {
6910 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
6911 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006912 case BakerReadBarrierKind::kField:
6913 case BakerReadBarrierKind::kAcquire: {
Ulyana Trafimovicheeaf47f2020-10-28 15:59:29 +00006914 auto base_reg =
6915 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00006916 CheckValidReg(base_reg.GetCode());
Ulyana Trafimovicheeaf47f2020-10-28 15:59:29 +00006917 auto holder_reg =
6918 Register::GetXRegFromCode(BakerReadBarrierSecondRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00006919 CheckValidReg(holder_reg.GetCode());
6920 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6921 temps.Exclude(ip0, ip1);
Roland Levillain988c3912019-09-25 19:33:35 +01006922 // In the case of a field load (with relaxed semantic), if `base_reg` differs from
6923 // `holder_reg`, the offset was too large and we must have emitted (during the construction
6924 // of the HIR graph, see `art::HInstructionBuilder::BuildInstanceFieldAccess`) and preserved
6925 // (see `art::PrepareForRegisterAllocation::VisitNullCheck`) an explicit null check before
6926 // the load. Otherwise, for implicit null checks, we need to null-check the holder as we do
6927 // not necessarily do that check before going to the thunk.
6928 //
6929 // In the case of a field load with load-acquire semantics (where `base_reg` always differs
6930 // from `holder_reg`), we also need an explicit null check when implicit null checks are
6931 // allowed, as we do not emit one before going to the thunk.
Vladimir Marko7a695052018-04-12 10:26:50 +01006932 vixl::aarch64::Label throw_npe_label;
6933 vixl::aarch64::Label* throw_npe = nullptr;
Roland Levillain988c3912019-09-25 19:33:35 +01006934 if (GetCompilerOptions().GetImplicitNullChecks() &&
6935 (holder_reg.Is(base_reg) || (kind == BakerReadBarrierKind::kAcquire))) {
Vladimir Marko7a695052018-04-12 10:26:50 +01006936 throw_npe = &throw_npe_label;
6937 __ Cbz(holder_reg.W(), throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006938 }
Vladimir Marko7a695052018-04-12 10:26:50 +01006939 // Check if the holder is gray and, if not, add fake dependency to the base register
6940 // and return to the LDR instruction to load the reference. Otherwise, use introspection
6941 // to load the reference and call the entrypoint that performs further checks on the
6942 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006943 vixl::aarch64::Label slow_path;
6944 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
Vladimir Marko7a695052018-04-12 10:26:50 +01006945 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006946 __ Bind(&slow_path);
Vladimir Marko0ecac682018-08-07 10:40:38 +01006947 if (kind == BakerReadBarrierKind::kField) {
6948 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6949 __ Ldr(ip0.W(), ldr_address); // Load the LDR (immediate) unsigned offset.
6950 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6951 __ Ubfx(ip0.W(), ip0.W(), 10, 12); // Extract the offset.
6952 __ Ldr(ip0.W(), MemOperand(base_reg, ip0, LSL, 2)); // Load the reference.
6953 } else {
6954 DCHECK(kind == BakerReadBarrierKind::kAcquire);
6955 DCHECK(!base_reg.Is(holder_reg));
6956 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6957 __ Ldar(ip0.W(), MemOperand(base_reg));
6958 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006959 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
6960 __ Br(ip1); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006961 break;
6962 }
6963 case BakerReadBarrierKind::kArray: {
Ulyana Trafimovicheeaf47f2020-10-28 15:59:29 +00006964 auto base_reg =
6965 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00006966 CheckValidReg(base_reg.GetCode());
6967 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6968 BakerReadBarrierSecondRegField::Decode(encoded_data));
6969 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6970 temps.Exclude(ip0, ip1);
6971 vixl::aarch64::Label slow_path;
6972 int32_t data_offset =
6973 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
6974 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
6975 DCHECK_LT(lock_word.GetOffset(), 0);
6976 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path);
6977 __ Bind(&slow_path);
6978 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
6979 __ Ldr(ip0.W(), ldr_address); // Load the LDR (register) unsigned offset.
6980 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6981 __ Ubfx(ip0, ip0, 16, 6); // Extract the index register, plus 32 (bit 21 is set).
6982 __ Bfi(ip1, ip0, 3, 6); // Insert ip0 to the entrypoint address to create
6983 // a switch case target based on the index register.
6984 __ Mov(ip0, base_reg); // Move the base register to ip0.
6985 __ Br(ip1); // Jump to the entrypoint's array switch case.
6986 break;
6987 }
6988 case BakerReadBarrierKind::kGcRoot: {
6989 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
6990 // and it does not have a forwarding address), call the correct introspection entrypoint;
6991 // otherwise return the reference (or the extracted forwarding address).
6992 // There is no gray bit check for GC roots.
Ulyana Trafimovicheeaf47f2020-10-28 15:59:29 +00006993 auto root_reg =
6994 Register::GetWRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00006995 CheckValidReg(root_reg.GetCode());
6996 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6997 BakerReadBarrierSecondRegField::Decode(encoded_data));
6998 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6999 temps.Exclude(ip0, ip1);
7000 vixl::aarch64::Label return_label, not_marked, forwarding_address;
7001 __ Cbz(root_reg, &return_label);
7002 MemOperand lock_word(root_reg.X(), mirror::Object::MonitorOffset().Int32Value());
7003 __ Ldr(ip0.W(), lock_word);
7004 __ Tbz(ip0.W(), LockWord::kMarkBitStateShift, &not_marked);
7005 __ Bind(&return_label);
7006 __ Br(lr);
7007 __ Bind(&not_marked);
7008 __ Tst(ip0.W(), Operand(ip0.W(), LSL, 1));
7009 __ B(&forwarding_address, mi);
7010 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
7011 // Adjust the art_quick_read_barrier_mark_introspection address in IP1 to
7012 // art_quick_read_barrier_mark_introspection_gc_roots.
7013 __ Add(ip1, ip1, Operand(BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRYPOINT_OFFSET));
7014 __ Mov(ip0.W(), root_reg);
7015 __ Br(ip1);
7016 __ Bind(&forwarding_address);
7017 __ Lsl(root_reg, ip0.W(), LockWord::kForwardingAddressShift);
7018 __ Br(lr);
7019 break;
7020 }
7021 default:
7022 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
7023 UNREACHABLE();
7024 }
7025
Vladimir Marko966b46f2018-08-03 10:20:19 +00007026 // For JIT, the slow path is considered part of the compiled method,
Vladimir Markof91fc122020-05-13 09:21:00 +01007027 // so JIT should pass null as `debug_name`.
Vladimir Marko695348f2020-05-19 14:42:02 +01007028 DCHECK(!GetCompilerOptions().IsJitCompiler() || debug_name == nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00007029 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00007030 std::ostringstream oss;
7031 oss << "BakerReadBarrierThunk";
7032 switch (kind) {
7033 case BakerReadBarrierKind::kField:
7034 oss << "Field_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
7035 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
7036 break;
Vladimir Marko0ecac682018-08-07 10:40:38 +01007037 case BakerReadBarrierKind::kAcquire:
7038 oss << "Acquire_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
7039 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
7040 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +00007041 case BakerReadBarrierKind::kArray:
7042 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
7043 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7044 BakerReadBarrierSecondRegField::Decode(encoded_data));
7045 break;
7046 case BakerReadBarrierKind::kGcRoot:
7047 oss << "GcRoot_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
7048 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7049 BakerReadBarrierSecondRegField::Decode(encoded_data));
7050 break;
7051 }
7052 *debug_name = oss.str();
7053 }
7054}
7055
7056#undef __
7057
Alexandre Rames5319def2014-10-23 10:03:10 +01007058} // namespace arm64
7059} // namespace art