blob: ba5b1b7c536200482f4fb7e02509b09ed402b578 [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"
Andreas Gampe878d58c2015-01-15 23:24:00 -080033#include "intrinsics.h"
34#include "intrinsics_arm64.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010035#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070036#include "lock_word.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010037#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070038#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000039#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010040#include "thread.h"
41#include "utils/arm64/assembler_arm64.h"
42#include "utils/assembler.h"
43#include "utils/stack_checks.h"
44
Scott Wakeling97c72b72016-06-24 16:19:36 +010045using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000046using vixl::ExactAssemblyScope;
47using vixl::CodeBufferCheckScope;
48using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010049
50#ifdef __
51#error "ARM64 Codegen VIXL macro-assembler macro already defined."
52#endif
53
Vladimir Marko0a516052019-10-14 13:00:44 +000054namespace art {
Alexandre Rames5319def2014-10-23 10:03:10 +010055
Roland Levillain22ccc3a2015-11-24 13:10:05 +000056template<class MirrorType>
57class GcRoot;
58
Alexandre Rames5319def2014-10-23 10:03:10 +010059namespace arm64 {
60
Alexandre Ramesbe919d92016-08-23 18:33:36 +010061using helpers::ARM64EncodableConstantOrRegister;
62using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080063using helpers::CPURegisterFrom;
64using helpers::DRegisterFrom;
65using helpers::FPRegisterFrom;
66using helpers::HeapOperand;
67using helpers::HeapOperandFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010068using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080070using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010071using helpers::InputRegisterAt;
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +010072using helpers::Int64FromLocation;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010073using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080074using helpers::LocationFrom;
75using helpers::OperandFromMemOperand;
76using helpers::OutputCPURegister;
77using helpers::OutputFPRegister;
78using helpers::OutputRegister;
79using helpers::RegisterFrom;
80using helpers::StackOperandFrom;
81using helpers::VIXLRegCodeFromART;
82using helpers::WRegisterFrom;
83using helpers::XRegisterFrom;
84
Vladimir Markof3e0ee22015-12-17 15:23:13 +000085// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080086// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
87// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000088static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010089
Vladimir Markof4f2daa2017-03-20 18:26:59 +000090// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
91// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
Vladimir Marko008e09f32018-08-06 15:42:43 +010092// For the Baker read barrier implementation using link-time generated thunks we need to split
Vladimir Markof4f2daa2017-03-20 18:26:59 +000093// the offset explicitly.
94constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
95
Alexandre Rames5319def2014-10-23 10:03:10 +010096inline Condition ARM64Condition(IfCondition cond) {
97 switch (cond) {
98 case kCondEQ: return eq;
99 case kCondNE: return ne;
100 case kCondLT: return lt;
101 case kCondLE: return le;
102 case kCondGT: return gt;
103 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700104 case kCondB: return lo;
105 case kCondBE: return ls;
106 case kCondA: return hi;
107 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100108 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000109 LOG(FATAL) << "Unreachable";
110 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100111}
112
Vladimir Markod6e069b2016-01-18 11:11:01 +0000113inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
114 // The ARM64 condition codes can express all the necessary branches, see the
115 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
116 // There is no dex instruction or HIR that would need the missing conditions
117 // "equal or unordered" or "not equal".
118 switch (cond) {
119 case kCondEQ: return eq;
120 case kCondNE: return ne /* unordered */;
121 case kCondLT: return gt_bias ? cc : lt /* unordered */;
122 case kCondLE: return gt_bias ? ls : le /* unordered */;
123 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
124 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
125 default:
126 LOG(FATAL) << "UNREACHABLE";
127 UNREACHABLE();
128 }
129}
130
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100131Location ARM64ReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000132 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
133 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
134 // but we use the exact registers for clarity.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100135 if (return_type == DataType::Type::kFloat32) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000136 return LocationFrom(s0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100137 } else if (return_type == DataType::Type::kFloat64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000138 return LocationFrom(d0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100139 } else if (return_type == DataType::Type::kInt64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000140 return LocationFrom(x0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100141 } else if (return_type == DataType::Type::kVoid) {
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100142 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000143 } else {
144 return LocationFrom(w0);
145 }
146}
147
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100148Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000149 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100150}
151
Vladimir Marko3232dbb2018-07-25 15:42:46 +0100152static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
153 InvokeRuntimeCallingConvention calling_convention;
154 RegisterSet caller_saves = RegisterSet::Empty();
155 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
156 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
157 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
158 DataType::Type::kReference).GetCode());
159 return caller_saves;
160}
161
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100162// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
163#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700164#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100165
Zheng Xuda403092015-04-24 17:35:39 +0800166// Calculate memory accessing operand for save/restore live registers.
167static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100168 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800169 int64_t spill_offset,
170 bool is_save) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800171 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
172 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100173 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800174 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100175 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800176 codegen->GetNumberOfFloatingPointRegisters()));
177
Vladimir Marko804b03f2016-09-14 16:26:36 +0100178 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serovc8150b52019-07-31 18:28:00 +0100179 const unsigned v_reg_size_in_bits = codegen->GetSlowPathFPWidth() * 8;
Artem Serov1a719e42019-07-18 14:24:55 +0100180 DCHECK_LE(codegen->GetSIMDRegisterWidth(), kQRegSizeInBytes);
Artem Serovc8150b52019-07-31 18:28:00 +0100181 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size_in_bits, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800182
183 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
184 UseScratchRegisterScope temps(masm);
185
186 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100187 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
188 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800189 int64_t reg_size = kXRegSizeInBytes;
190 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
191 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100192 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800193 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
194 // If the offset does not fit in the instruction's immediate field, use an alternate register
195 // to compute the base address(float point registers spill base address).
196 Register new_base = temps.AcquireSameSizeAs(base);
197 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
198 base = new_base;
199 spill_offset = -core_spill_size;
200 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
201 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
202 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
203 }
204
205 if (is_save) {
206 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
207 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
208 } else {
209 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
210 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
211 }
212}
213
214void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800215 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Andreas Gampe3db70682018-12-26 15:12:03 -0800216 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100217 for (uint32_t i : LowToHighBits(core_spills)) {
218 // If the register holds an object, update the stack mask.
219 if (locations->RegisterContainsObject(i)) {
220 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800221 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100222 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
223 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
224 saved_core_stack_offsets_[i] = stack_offset;
225 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800226 }
227
Artem Serovc8150b52019-07-31 18:28:00 +0100228 const size_t fp_reg_size = codegen->GetSlowPathFPWidth();
Andreas Gampe3db70682018-12-26 15:12:03 -0800229 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100230 for (uint32_t i : LowToHighBits(fp_spills)) {
231 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
232 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
233 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serov9df37b92019-07-23 16:41:54 +0100234 stack_offset += fp_reg_size;
Zheng Xuda403092015-04-24 17:35:39 +0800235 }
236
Vladimir Marko804b03f2016-09-14 16:26:36 +0100237 SaveRestoreLiveRegistersHelper(codegen,
238 locations,
Andreas Gampe3db70682018-12-26 15:12:03 -0800239 codegen->GetFirstRegisterSlotInSlowPath(), /* is_save= */ true);
Zheng Xuda403092015-04-24 17:35:39 +0800240}
241
242void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100243 SaveRestoreLiveRegistersHelper(codegen,
244 locations,
Andreas Gampe3db70682018-12-26 15:12:03 -0800245 codegen->GetFirstRegisterSlotInSlowPath(), /* is_save= */ false);
Zheng Xuda403092015-04-24 17:35:39 +0800246}
247
Alexandre Rames5319def2014-10-23 10:03:10 +0100248class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
249 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000250 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100251
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100252 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100253 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000254 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100255
Alexandre Rames5319def2014-10-23 10:03:10 +0100256 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000257 if (instruction_->CanThrowIntoCatchBlock()) {
258 // Live registers will be restored in the catch block if caught.
259 SaveLiveRegisters(codegen, instruction_->GetLocations());
260 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000261 // We're moving two locations to locations that could overlap, so we need a parallel
262 // move resolver.
263 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100264 codegen->EmitParallelMoves(locations->InAt(0),
265 LocationFrom(calling_convention.GetRegisterAt(0)),
266 DataType::Type::kInt32,
267 locations->InAt(1),
268 LocationFrom(calling_convention.GetRegisterAt(1)),
269 DataType::Type::kInt32);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000270 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
271 ? kQuickThrowStringBounds
272 : kQuickThrowArrayBounds;
273 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100274 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800275 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100276 }
277
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100278 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100279
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100280 const char* GetDescription() const override { return "BoundsCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100281
Alexandre Rames5319def2014-10-23 10:03:10 +0100282 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100283 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
284};
285
Alexandre Rames67555f72014-11-18 10:55:16 +0000286class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
287 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000288 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000289
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100290 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000291 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
292 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000293 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800294 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000295 }
296
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100297 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100298
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100299 const char* GetDescription() const override { return "DivZeroCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100300
Alexandre Rames67555f72014-11-18 10:55:16 +0000301 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000302 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
303};
304
305class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
306 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100307 LoadClassSlowPathARM64(HLoadClass* cls, HInstruction* at)
308 : SlowPathCodeARM64(at), cls_(cls) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000309 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100310 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Alexandre Rames67555f72014-11-18 10:55:16 +0000311 }
312
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100313 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000314 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000315 Location out = locations->Out();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100316 const uint32_t dex_pc = instruction_->GetDexPc();
317 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
318 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
Alexandre Rames67555f72014-11-18 10:55:16 +0000319
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100320 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames67555f72014-11-18 10:55:16 +0000321 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000322 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000323
Vladimir Markof3c52b42017-11-17 17:32:12 +0000324 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100325 if (must_resolve_type) {
326 DCHECK(IsSameDexFile(cls_->GetDexFile(), arm64_codegen->GetGraph()->GetDexFile()));
327 dex::TypeIndex type_index = cls_->GetTypeIndex();
328 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Vladimir Marko9d479252018-07-24 11:35:20 +0100329 arm64_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
330 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100331 // If we also must_do_clinit, the resolved type is now in the correct register.
332 } else {
333 DCHECK(must_do_clinit);
334 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
335 arm64_codegen->MoveLocation(LocationFrom(calling_convention.GetRegisterAt(0)),
336 source,
337 cls_->GetType());
338 }
339 if (must_do_clinit) {
340 arm64_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
341 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800342 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000343
344 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000345 if (out.IsValid()) {
346 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100347 DataType::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000348 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000349 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000350 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000351 __ B(GetExitLabel());
352 }
353
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100354 const char* GetDescription() const override { return "LoadClassSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100355
Alexandre Rames67555f72014-11-18 10:55:16 +0000356 private:
357 // The class this slow path will load.
358 HLoadClass* const cls_;
359
Alexandre Rames67555f72014-11-18 10:55:16 +0000360 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
361};
362
Vladimir Markoaad75c62016-10-03 08:46:48 +0000363class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
364 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000365 explicit LoadStringSlowPathARM64(HLoadString* instruction)
366 : SlowPathCodeARM64(instruction) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000367
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100368 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000369 LocationSummary* locations = instruction_->GetLocations();
370 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
371 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
372
373 __ Bind(GetEntryLabel());
374 SaveLiveRegisters(codegen, locations);
375
Vladimir Markof3c52b42017-11-17 17:32:12 +0000376 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000377 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
378 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000379 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
380 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100381 DataType::Type type = instruction_->GetType();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000382 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
383
384 RestoreLiveRegisters(codegen, locations);
385
Vladimir Markoaad75c62016-10-03 08:46:48 +0000386 __ B(GetExitLabel());
387 }
388
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100389 const char* GetDescription() const override { return "LoadStringSlowPathARM64"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000390
391 private:
392 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
393};
394
Alexandre Rames5319def2014-10-23 10:03:10 +0100395class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
396 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000397 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100398
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100399 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000400 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100401 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000402 if (instruction_->CanThrowIntoCatchBlock()) {
403 // Live registers will be restored in the catch block if caught.
404 SaveLiveRegisters(codegen, instruction_->GetLocations());
405 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000406 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
407 instruction_,
408 instruction_->GetDexPc(),
409 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800410 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100411 }
412
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100413 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100414
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100415 const char* GetDescription() const override { return "NullCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100416
Alexandre Rames5319def2014-10-23 10:03:10 +0100417 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100418 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
419};
420
421class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
422 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100423 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000424 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100425
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100426 void EmitNativeCode(CodeGenerator* codegen) override {
Artem Serov7957d952017-04-04 15:44:09 +0100427 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000428 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100429 __ Bind(GetEntryLabel());
Artem Serov1a719e42019-07-18 14:24:55 +0100430 SaveLiveRegisters(codegen, locations); // Only saves live vector regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000431 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800432 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov1a719e42019-07-18 14:24:55 +0100433 RestoreLiveRegisters(codegen, locations); // Only restores live vector regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000434 if (successor_ == nullptr) {
435 __ B(GetReturnLabel());
436 } else {
437 __ B(arm64_codegen->GetLabelOf(successor_));
438 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100439 }
440
Scott Wakeling97c72b72016-06-24 16:19:36 +0100441 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100442 DCHECK(successor_ == nullptr);
443 return &return_label_;
444 }
445
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100446 HBasicBlock* GetSuccessor() const {
447 return successor_;
448 }
449
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100450 const char* GetDescription() const override { return "SuspendCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100451
Alexandre Rames5319def2014-10-23 10:03:10 +0100452 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100453 // If not null, the block to branch to after the suspend check.
454 HBasicBlock* const successor_;
455
456 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100457 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100458
459 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
460};
461
Alexandre Rames67555f72014-11-18 10:55:16 +0000462class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
463 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000464 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000465 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000466
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100467 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000468 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800469
Alexandre Rames3e69f162014-12-10 10:36:50 +0000470 DCHECK(instruction_->IsCheckCast()
471 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
472 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100473 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000474
Alexandre Rames67555f72014-11-18 10:55:16 +0000475 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000476
Vladimir Marko87584542017-12-12 17:47:52 +0000477 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000478 SaveLiveRegisters(codegen, locations);
479 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000480
481 // We're moving two locations to locations that could overlap, so we need a parallel
482 // move resolver.
483 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800484 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800485 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100486 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800487 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800488 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100489 DataType::Type::kReference);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000490 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000491 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800492 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100493 DataType::Type ret_type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000494 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
495 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
496 } else {
497 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800498 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
499 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000500 }
501
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000502 if (!is_fatal_) {
503 RestoreLiveRegisters(codegen, locations);
504 __ B(GetExitLabel());
505 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000506 }
507
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100508 const char* GetDescription() const override { return "TypeCheckSlowPathARM64"; }
509 bool IsFatal() const override { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100510
Alexandre Rames67555f72014-11-18 10:55:16 +0000511 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000512 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000513
Alexandre Rames67555f72014-11-18 10:55:16 +0000514 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
515};
516
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700517class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
518 public:
Aart Bik42249c32016-01-07 15:33:50 -0800519 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000520 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700521
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100522 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bik42249c32016-01-07 15:33:50 -0800523 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700524 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100525 LocationSummary* locations = instruction_->GetLocations();
526 SaveLiveRegisters(codegen, locations);
527 InvokeRuntimeCallingConvention calling_convention;
528 __ Mov(calling_convention.GetRegisterAt(0),
529 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000530 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100531 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700532 }
533
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100534 const char* GetDescription() const override { return "DeoptimizationSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100535
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700536 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700537 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
538};
539
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100540class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
541 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000542 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100543
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100544 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100545 LocationSummary* locations = instruction_->GetLocations();
546 __ Bind(GetEntryLabel());
547 SaveLiveRegisters(codegen, locations);
548
549 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100550 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100551 parallel_move.AddMove(
552 locations->InAt(0),
553 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100554 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100555 nullptr);
556 parallel_move.AddMove(
557 locations->InAt(1),
558 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100559 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100560 nullptr);
561 parallel_move.AddMove(
562 locations->InAt(2),
563 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100564 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100565 nullptr);
566 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
567
568 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000569 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100570 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
571 RestoreLiveRegisters(codegen, locations);
572 __ B(GetExitLabel());
573 }
574
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100575 const char* GetDescription() const override { return "ArraySetSlowPathARM64"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100576
577 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100578 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
579};
580
Zheng Xu3927c8b2015-11-18 17:46:25 +0800581void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
582 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000583 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800584
585 // We are about to use the assembler to place literals directly. Make sure we have enough
586 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000587 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
588 num_entries * sizeof(int32_t),
589 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800590
591 __ Bind(&table_start_);
592 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
593 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100594 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800595 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100596 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800597 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
598 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
599 Literal<int32_t> literal(jump_offset);
600 __ place(&literal);
601 }
602}
603
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000604// Slow path generating a read barrier for a heap reference.
605class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
606 public:
607 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
608 Location out,
609 Location ref,
610 Location obj,
611 uint32_t offset,
612 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000613 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000614 out_(out),
615 ref_(ref),
616 obj_(obj),
617 offset_(offset),
618 index_(index) {
619 DCHECK(kEmitCompilerReadBarrier);
620 // If `obj` is equal to `out` or `ref`, it means the initial object
621 // has been overwritten by (or after) the heap object reference load
622 // to be instrumented, e.g.:
623 //
624 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000625 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000626 //
627 // In that case, we have lost the information about the original
628 // object, and the emitted read barrier cannot work properly.
629 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
630 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
631 }
632
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100633 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000634 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
635 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100636 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000637 DCHECK(locations->CanCall());
638 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100639 DCHECK(instruction_->IsInstanceFieldGet() ||
640 instruction_->IsStaticFieldGet() ||
641 instruction_->IsArrayGet() ||
642 instruction_->IsInstanceOf() ||
643 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700644 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000645 << "Unexpected instruction in read barrier for heap reference slow path: "
646 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000647 // The read barrier instrumentation of object ArrayGet
648 // instructions does not support the HIntermediateAddress
649 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000650 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100651 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000652
653 __ Bind(GetEntryLabel());
654
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000655 SaveLiveRegisters(codegen, locations);
656
657 // We may have to change the index's value, but as `index_` is a
658 // constant member (like other "inputs" of this slow path),
659 // introduce a copy of it, `index`.
660 Location index = index_;
661 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100662 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000663 if (instruction_->IsArrayGet()) {
664 // Compute the actual memory offset and store it in `index`.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100665 Register index_reg = RegisterFrom(index_, DataType::Type::kInt32);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000666 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
667 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
668 // We are about to change the value of `index_reg` (see the
669 // calls to vixl::MacroAssembler::Lsl and
670 // vixl::MacroAssembler::Mov below), but it has
671 // not been saved by the previous call to
672 // art::SlowPathCode::SaveLiveRegisters, as it is a
673 // callee-save register --
674 // art::SlowPathCode::SaveLiveRegisters does not consider
675 // callee-save registers, as it has been designed with the
676 // assumption that callee-save registers are supposed to be
677 // handled by the called function. So, as a callee-save
678 // register, `index_reg` _would_ eventually be saved onto
679 // the stack, but it would be too late: we would have
680 // changed its value earlier. Therefore, we manually save
681 // it here into another freely available register,
682 // `free_reg`, chosen of course among the caller-save
683 // registers (as a callee-save `free_reg` register would
684 // exhibit the same problem).
685 //
686 // Note we could have requested a temporary register from
687 // the register allocator instead; but we prefer not to, as
688 // this is a slow path, and we know we can find a
689 // caller-save register that is available.
690 Register free_reg = FindAvailableCallerSaveRegister(codegen);
691 __ Mov(free_reg.W(), index_reg);
692 index_reg = free_reg;
693 index = LocationFrom(index_reg);
694 } else {
695 // The initial register stored in `index_` has already been
696 // saved in the call to art::SlowPathCode::SaveLiveRegisters
697 // (as it is not a callee-save register), so we can freely
698 // use it.
699 }
700 // Shifting the index value contained in `index_reg` by the scale
701 // factor (2) cannot overflow in practice, as the runtime is
702 // unable to allocate object arrays with a size larger than
703 // 2^26 - 1 (that is, 2^28 - 4 bytes).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100704 __ Lsl(index_reg, index_reg, DataType::SizeShift(type));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000705 static_assert(
706 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
707 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
708 __ Add(index_reg, index_reg, Operand(offset_));
709 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100710 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
711 // intrinsics, `index_` is not shifted by a scale factor of 2
712 // (as in the case of ArrayGet), as it is actually an offset
713 // to an object field within an object.
714 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000715 DCHECK(instruction_->GetLocations()->Intrinsified());
716 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
717 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
718 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100719 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100720 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000721 }
722 }
723
724 // We're moving two or three locations to locations that could
725 // overlap, so we need a parallel move resolver.
726 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100727 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000728 parallel_move.AddMove(ref_,
729 LocationFrom(calling_convention.GetRegisterAt(0)),
730 type,
731 nullptr);
732 parallel_move.AddMove(obj_,
733 LocationFrom(calling_convention.GetRegisterAt(1)),
734 type,
735 nullptr);
736 if (index.IsValid()) {
737 parallel_move.AddMove(index,
738 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100739 DataType::Type::kInt32,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000740 nullptr);
741 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
742 } else {
743 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
744 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
745 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000746 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000747 instruction_,
748 instruction_->GetDexPc(),
749 this);
750 CheckEntrypointTypes<
751 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
752 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
753
754 RestoreLiveRegisters(codegen, locations);
755
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000756 __ B(GetExitLabel());
757 }
758
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100759 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000760
761 private:
762 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100763 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
764 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000765 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
766 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
767 return Register(VIXLRegCodeFromART(i), kXRegSize);
768 }
769 }
770 // We shall never fail to find a free caller-save register, as
771 // there are more than two core caller-save registers on ARM64
772 // (meaning it is possible to find one which is different from
773 // `ref` and `obj`).
774 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
775 LOG(FATAL) << "Could not find a free register";
776 UNREACHABLE();
777 }
778
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000779 const Location out_;
780 const Location ref_;
781 const Location obj_;
782 const uint32_t offset_;
783 // An additional location containing an index to an array.
784 // Only used for HArrayGet and the UnsafeGetObject &
785 // UnsafeGetObjectVolatile intrinsics.
786 const Location index_;
787
788 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
789};
790
791// Slow path generating a read barrier for a GC root.
792class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
793 public:
794 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000795 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000796 DCHECK(kEmitCompilerReadBarrier);
797 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000798
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100799 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000800 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100801 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000802 DCHECK(locations->CanCall());
803 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000804 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
805 << "Unexpected instruction in read barrier for GC root slow path: "
806 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000807
808 __ Bind(GetEntryLabel());
809 SaveLiveRegisters(codegen, locations);
810
811 InvokeRuntimeCallingConvention calling_convention;
812 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
813 // The argument of the ReadBarrierForRootSlow is not a managed
814 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
815 // thus we need a 64-bit move here, and we cannot use
816 //
817 // arm64_codegen->MoveLocation(
818 // LocationFrom(calling_convention.GetRegisterAt(0)),
819 // root_,
820 // type);
821 //
822 // which would emit a 32-bit move, as `type` is a (32-bit wide)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100823 // reference type (`DataType::Type::kReference`).
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000824 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000825 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000826 instruction_,
827 instruction_->GetDexPc(),
828 this);
829 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
830 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
831
832 RestoreLiveRegisters(codegen, locations);
833 __ B(GetExitLabel());
834 }
835
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100836 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000837
838 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000839 const Location out_;
840 const Location root_;
841
842 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
843};
844
Alexandre Rames5319def2014-10-23 10:03:10 +0100845#undef __
846
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100847Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100848 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100849 if (type == DataType::Type::kVoid) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100850 LOG(FATAL) << "Unreachable type " << type;
851 }
852
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100853 if (DataType::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100854 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
855 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100856 } else if (!DataType::IsFloatingPointType(type) &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000857 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000858 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
859 } else {
860 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100861 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
862 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100863 }
864
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000865 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100866 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100867 return next_location;
868}
869
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100870Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100871 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100872}
873
Vladimir Marko86c87522020-05-11 16:55:55 +0100874Location CriticalNativeCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
875 DCHECK_NE(type, DataType::Type::kReference);
876
877 Location location = Location::NoLocation();
878 if (DataType::IsFloatingPointType(type)) {
879 if (fpr_index_ < kParameterFPRegistersLength) {
880 location = LocationFrom(kParameterFPRegisters[fpr_index_]);
881 ++fpr_index_;
882 }
883 } else {
884 // Native ABI uses the same registers as managed, except that the method register x0
885 // is a normal argument.
886 if (gpr_index_ < 1u + kParameterCoreRegistersLength) {
887 location = LocationFrom(gpr_index_ == 0u ? x0 : kParameterCoreRegisters[gpr_index_ - 1u]);
888 ++gpr_index_;
889 }
890 }
891 if (location.IsInvalid()) {
892 if (DataType::Is64BitType(type)) {
893 location = Location::DoubleStackSlot(stack_offset_);
894 } else {
895 location = Location::StackSlot(stack_offset_);
896 }
897 stack_offset_ += kFramePointerSize;
898
899 if (for_register_allocation_) {
900 location = Location::Any();
901 }
902 }
903 return location;
904}
905
906Location CriticalNativeCallingConventionVisitorARM64::GetReturnLocation(DataType::Type type) const {
907 // We perform conversion to the managed ABI return register after the call if needed.
908 InvokeDexCallingConventionVisitorARM64 dex_calling_convention;
909 return dex_calling_convention.GetReturnLocation(type);
910}
911
912Location CriticalNativeCallingConventionVisitorARM64::GetMethodLocation() const {
913 // Pass the method in the hidden argument x15.
914 return Location::RegisterLocation(x15.GetCode());
915}
916
Serban Constantinescu579885a2015-02-22 20:51:33 +0000917CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100918 const CompilerOptions& compiler_options,
919 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100920 : CodeGenerator(graph,
921 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000922 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000923 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100924 callee_saved_core_registers.GetList(),
925 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100926 compiler_options,
927 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100928 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
929 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov1a719e42019-07-18 14:24:55 +0100930 location_builder_neon_(graph, this),
931 instruction_visitor_neon_(graph, this),
932 location_builder_sve_(graph, this),
933 instruction_visitor_sve_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100934 move_resolver_(graph->GetAllocator(), this),
Artem Serovaa6f4832018-11-21 18:57:54 +0000935 assembler_(graph->GetAllocator(),
936 compiler_options.GetInstructionSetFeatures()->AsArm64InstructionSetFeatures()),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000937 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100938 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000939 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100940 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000941 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100942 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +0100943 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100944 call_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100945 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100946 uint32_literals_(std::less<uint32_t>(),
947 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
948 uint64_literals_(std::less<uint64_t>(),
949 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000950 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100951 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000952 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +0000953 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
954 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
955 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000956 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000957 AddAllocatedRegister(LocationFrom(lr));
Artem Serov1a719e42019-07-18 14:24:55 +0100958
959 bool use_sve = ShouldUseSVE();
960 if (use_sve) {
961 location_builder_ = &location_builder_sve_;
962 instruction_visitor_ = &instruction_visitor_sve_;
963 } else {
964 location_builder_ = &location_builder_neon_;
965 instruction_visitor_ = &instruction_visitor_neon_;
966 }
967}
968
969bool CodeGeneratorARM64::ShouldUseSVE() const {
970 return kArm64AllowSVE && GetInstructionSetFeatures().HasSVE();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000971}
Alexandre Rames5319def2014-10-23 10:03:10 +0100972
Alexandre Rames67555f72014-11-18 10:55:16 +0000973#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100974
Zheng Xu3927c8b2015-11-18 17:46:25 +0800975void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100976 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800977 jump_table->EmitTable(this);
978 }
979}
980
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000981void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800982 EmitJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +0000983
984 // Emit JIT baker read barrier slow paths.
Vladimir Marko695348f2020-05-19 14:42:02 +0100985 DCHECK(GetCompilerOptions().IsJitCompiler() || jit_baker_read_barrier_slow_paths_.empty());
Vladimir Marko966b46f2018-08-03 10:20:19 +0000986 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
987 uint32_t encoded_data = entry.first;
988 vixl::aarch64::Label* slow_path_entry = &entry.second.label;
989 __ Bind(slow_path_entry);
Andreas Gampe3db70682018-12-26 15:12:03 -0800990 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name= */ nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +0000991 }
992
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000993 // Ensure we emit the literal pool.
994 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000995
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000996 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +0000997
998 // Verify Baker read barrier linker patches.
999 if (kIsDebugBuild) {
1000 ArrayRef<const uint8_t> code = allocator->GetMemory();
1001 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
1002 DCHECK(info.label.IsBound());
1003 uint32_t literal_offset = info.label.GetLocation();
1004 DCHECK_ALIGNED(literal_offset, 4u);
1005
1006 auto GetInsn = [&code](uint32_t offset) {
1007 DCHECK_ALIGNED(offset, 4u);
1008 return
1009 (static_cast<uint32_t>(code[offset + 0]) << 0) +
1010 (static_cast<uint32_t>(code[offset + 1]) << 8) +
1011 (static_cast<uint32_t>(code[offset + 2]) << 16)+
1012 (static_cast<uint32_t>(code[offset + 3]) << 24);
1013 };
1014
1015 const uint32_t encoded_data = info.custom_data;
1016 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
1017 // Check that the next instruction matches the expected LDR.
1018 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01001019 case BakerReadBarrierKind::kField:
1020 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00001021 DCHECK_GE(code.size() - literal_offset, 8u);
1022 uint32_t next_insn = GetInsn(literal_offset + 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001023 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1024 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko0ecac682018-08-07 10:40:38 +01001025 if (kind == BakerReadBarrierKind::kField) {
1026 // LDR (immediate) with correct base_reg.
1027 CHECK_EQ(next_insn & 0xffc003e0u, 0xb9400000u | (base_reg << 5));
1028 } else {
1029 DCHECK(kind == BakerReadBarrierKind::kAcquire);
1030 // LDAR with correct base_reg.
1031 CHECK_EQ(next_insn & 0xffffffe0u, 0x88dffc00u | (base_reg << 5));
1032 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001033 break;
1034 }
1035 case BakerReadBarrierKind::kArray: {
1036 DCHECK_GE(code.size() - literal_offset, 8u);
1037 uint32_t next_insn = GetInsn(literal_offset + 4u);
1038 // LDR (register) with the correct base_reg, size=10 (32-bit), option=011 (extend = LSL),
1039 // and S=1 (shift amount = 2 for 32-bit version), i.e. LDR Wt, [Xn, Xm, LSL #2].
1040 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1041 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1042 CHECK_EQ(next_insn & 0xffe0ffe0u, 0xb8607800u | (base_reg << 5));
1043 CheckValidReg((next_insn >> 16) & 0x1f); // Check index register
1044 break;
1045 }
1046 case BakerReadBarrierKind::kGcRoot: {
1047 DCHECK_GE(literal_offset, 4u);
1048 uint32_t prev_insn = GetInsn(literal_offset - 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001049 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko94796f82018-08-08 15:15:33 +01001050 // Usually LDR (immediate) with correct root_reg but
1051 // we may have a "MOV marked, old_value" for UnsafeCASObject.
1052 if ((prev_insn & 0xffe0ffff) != (0x2a0003e0 | root_reg)) { // MOV?
1053 CHECK_EQ(prev_insn & 0xffc0001fu, 0xb9400000u | root_reg); // LDR?
1054 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001055 break;
1056 }
1057 default:
1058 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
1059 UNREACHABLE();
1060 }
1061 }
1062 }
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001063}
1064
Zheng Xuad4450e2015-04-17 18:48:56 +08001065void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1066 // Note: There are 6 kinds of moves:
1067 // 1. constant -> GPR/FPR (non-cycle)
1068 // 2. constant -> stack (non-cycle)
1069 // 3. GPR/FPR -> GPR/FPR
1070 // 4. GPR/FPR -> stack
1071 // 5. stack -> GPR/FPR
1072 // 6. stack -> stack (non-cycle)
1073 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1074 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1075 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1076 // dependency.
1077 vixl_temps_.Open(GetVIXLAssembler());
1078}
1079
1080void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1081 vixl_temps_.Close();
1082}
1083
1084Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001085 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1086 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1087 || kind == Location::kSIMDStackSlot);
1088 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1089 ? Location::kFpuRegister
1090 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001091 Location scratch = GetScratchLocation(kind);
1092 if (!scratch.Equals(Location::NoLocation())) {
1093 return scratch;
1094 }
1095 // Allocate from VIXL temp registers.
1096 if (kind == Location::kRegister) {
1097 scratch = LocationFrom(vixl_temps_.AcquireX());
1098 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001099 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serov1a719e42019-07-18 14:24:55 +01001100 scratch = codegen_->GetGraph()->HasSIMD()
1101 ? codegen_->GetInstructionCodeGeneratorArm64()->AllocateSIMDScratchLocation(&vixl_temps_)
1102 : LocationFrom(vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001103 }
1104 AddScratchLocation(scratch);
1105 return scratch;
1106}
1107
1108void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1109 if (loc.IsRegister()) {
1110 vixl_temps_.Release(XRegisterFrom(loc));
1111 } else {
1112 DCHECK(loc.IsFpuRegister());
Artem Serov1a719e42019-07-18 14:24:55 +01001113 if (codegen_->GetGraph()->HasSIMD()) {
1114 codegen_->GetInstructionCodeGeneratorArm64()->FreeSIMDScratchLocation(loc, &vixl_temps_);
1115 } else {
1116 vixl_temps_.Release(DRegisterFrom(loc));
1117 }
Zheng Xuad4450e2015-04-17 18:48:56 +08001118 }
1119 RemoveScratchLocation(loc);
1120}
1121
Alexandre Rames3e69f162014-12-10 10:36:50 +00001122void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001123 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001124 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001125}
1126
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001127void CodeGeneratorARM64::MaybeIncrementHotness(bool is_frame_entry) {
1128 MacroAssembler* masm = GetVIXLAssembler();
1129 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1130 UseScratchRegisterScope temps(masm);
1131 Register counter = temps.AcquireX();
1132 Register method = is_frame_entry ? kArtMethodRegister : temps.AcquireX();
1133 if (!is_frame_entry) {
1134 __ Ldr(method, MemOperand(sp, 0));
1135 }
1136 __ Ldrh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
1137 __ Add(counter, counter, 1);
1138 // Subtract one if the counter would overflow.
1139 __ Sub(counter, counter, Operand(counter, LSR, 16));
1140 __ Strh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
1141 }
1142
1143 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
1144 ScopedObjectAccess soa(Thread::Current());
1145 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001146 if (info != nullptr) {
Nicolas Geoffrayc1cd1332020-01-25 13:08:24 +00001147 uint64_t address = reinterpret_cast64<uint64_t>(info);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001148 vixl::aarch64::Label done;
1149 UseScratchRegisterScope temps(masm);
1150 Register temp = temps.AcquireX();
1151 Register counter = temps.AcquireW();
1152 __ Mov(temp, address);
1153 __ Ldrh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
1154 __ Add(counter, counter, 1);
1155 __ Strh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
1156 __ Tst(counter, 0xffff);
1157 __ B(ne, &done);
1158 if (is_frame_entry) {
1159 if (HasEmptyFrame()) {
Vladimir Markodec78172020-06-19 15:31:23 +01001160 // The entrypoint expects the method at the bottom of the stack. We
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001161 // claim stack space necessary for alignment.
Vladimir Markodec78172020-06-19 15:31:23 +01001162 IncreaseFrame(kStackAlignment);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001163 __ Stp(kArtMethodRegister, lr, MemOperand(sp, 0));
1164 } else if (!RequiresCurrentMethod()) {
1165 __ Str(kArtMethodRegister, MemOperand(sp, 0));
1166 }
1167 } else {
1168 CHECK(RequiresCurrentMethod());
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001169 }
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001170 uint32_t entrypoint_offset =
1171 GetThreadOffset<kArm64PointerSize>(kQuickCompileOptimized).Int32Value();
1172 __ Ldr(lr, MemOperand(tr, entrypoint_offset));
1173 // Note: we don't record the call here (and therefore don't generate a stack
1174 // map), as the entrypoint should never be suspended.
1175 __ Blr(lr);
1176 if (HasEmptyFrame()) {
1177 CHECK(is_frame_entry);
1178 __ Ldr(lr, MemOperand(sp, 8));
Vladimir Markodec78172020-06-19 15:31:23 +01001179 DecreaseFrame(kStackAlignment);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001180 }
1181 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001182 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001183 }
1184}
1185
Alexandre Rames5319def2014-10-23 10:03:10 +01001186void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001187 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001188 __ Bind(&frame_entry_label_);
1189
Vladimir Marko33bff252017-11-01 14:35:42 +00001190 bool do_overflow_check =
1191 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001192 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001193 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001194 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001195 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001196 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001197 {
1198 // Ensure that between load and RecordPcInfo there are no pools emitted.
1199 ExactAssemblyScope eas(GetVIXLAssembler(),
1200 kInstructionSize,
1201 CodeBufferCheckScope::kExactSize);
1202 __ ldr(wzr, MemOperand(temp, 0));
1203 RecordPcInfo(nullptr, 0);
1204 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001205 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001206
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001207 if (!HasEmptyFrame()) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001208 // Stack layout:
1209 // sp[frame_size - 8] : lr.
1210 // ... : other preserved core registers.
1211 // ... : other preserved fp registers.
1212 // ... : reserved frame space.
1213 // sp[0] : current method.
Vladimir Marko1a225a72019-07-05 13:37:42 +01001214 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1215 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1216 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1217 DCHECK(!preserved_core_registers.IsEmpty());
1218 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1219 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001220
Vladimir Marko1a225a72019-07-05 13:37:42 +01001221 // Save the current method if we need it, or if using STP reduces code
1222 // size. Note that we do not do this in HCurrentMethod, as the
1223 // instruction might have been removed in the SSA graph.
1224 CPURegister lowest_spill;
1225 if (core_spills_offset == kXRegSizeInBytes) {
1226 // If there is no gap between the method and the lowest core spill, use
1227 // aligned STP pre-index to store both. Max difference is 512. We do
1228 // that to reduce code size even if we do not have to save the method.
1229 DCHECK_LE(frame_size, 512); // 32 core registers are only 256 bytes.
1230 lowest_spill = preserved_core_registers.PopLowestIndex();
1231 __ Stp(kArtMethodRegister, lowest_spill, MemOperand(sp, -frame_size, PreIndex));
1232 } else if (RequiresCurrentMethod()) {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001233 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001234 } else {
1235 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001236 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001237 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Vladimir Marko1a225a72019-07-05 13:37:42 +01001238 if (lowest_spill.IsValid()) {
1239 GetAssembler()->cfi().RelOffset(DWARFReg(lowest_spill), core_spills_offset);
1240 core_spills_offset += kXRegSizeInBytes;
1241 }
1242 GetAssembler()->SpillRegisters(preserved_core_registers, core_spills_offset);
1243 GetAssembler()->SpillRegisters(preserved_fp_registers, fp_spills_offset);
Mingyao Yang063fc772016-08-02 11:02:54 -07001244
1245 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1246 // Initialize should_deoptimize flag to 0.
1247 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1248 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1249 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001250 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001251 MaybeIncrementHotness(/* is_frame_entry= */ true);
Andreas Gampe3db70682018-12-26 15:12:03 -08001252 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001253}
1254
1255void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001256 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001257 if (!HasEmptyFrame()) {
Vladimir Marko1a225a72019-07-05 13:37:42 +01001258 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1259 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1260 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1261 DCHECK(!preserved_core_registers.IsEmpty());
1262 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1263 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
1264
1265 CPURegister lowest_spill;
1266 if (core_spills_offset == kXRegSizeInBytes) {
1267 // If there is no gap between the method and the lowest core spill, use
1268 // aligned LDP pre-index to pop both. Max difference is 504. We do
1269 // that to reduce code size even though the loaded method is unused.
1270 DCHECK_LE(frame_size, 504); // 32 core registers are only 256 bytes.
1271 lowest_spill = preserved_core_registers.PopLowestIndex();
1272 core_spills_offset += kXRegSizeInBytes;
1273 }
1274 GetAssembler()->UnspillRegisters(preserved_fp_registers, fp_spills_offset);
1275 GetAssembler()->UnspillRegisters(preserved_core_registers, core_spills_offset);
1276 if (lowest_spill.IsValid()) {
1277 __ Ldp(xzr, lowest_spill, MemOperand(sp, frame_size, PostIndex));
1278 GetAssembler()->cfi().Restore(DWARFReg(lowest_spill));
1279 } else {
1280 __ Drop(frame_size);
1281 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001282 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001283 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001284 __ Ret();
1285 GetAssembler()->cfi().RestoreState();
1286 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001287}
1288
Scott Wakeling97c72b72016-06-24 16:19:36 +01001289CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001290 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001291 return CPURegList(CPURegister::kRegister, kXRegSize,
1292 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001293}
1294
Scott Wakeling97c72b72016-06-24 16:19:36 +01001295CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001296 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1297 GetNumberOfFloatingPointRegisters()));
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001298 return CPURegList(CPURegister::kVRegister, kDRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001299 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001300}
1301
Alexandre Rames5319def2014-10-23 10:03:10 +01001302void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1303 __ Bind(GetLabelOf(block));
1304}
1305
Calin Juravle175dc732015-08-25 15:42:32 +01001306void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1307 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001308 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001309}
1310
Calin Juravlee460d1d2015-09-29 04:52:17 +01001311void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1312 if (location.IsRegister()) {
1313 locations->AddTemp(location);
1314 } else {
1315 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1316 }
1317}
1318
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001319void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001320 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001321 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001322 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001323 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001324 if (value_can_be_null) {
1325 __ Cbz(value, &done);
1326 }
Roland Levillainc73f0522018-08-14 15:16:50 +01001327 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07001328 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01001329 // Calculate the offset (in the card table) of the card corresponding to
1330 // `object`.
Alexandre Rames5319def2014-10-23 10:03:10 +01001331 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Roland Levillainc73f0522018-08-14 15:16:50 +01001332 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
1333 // `object`'s card.
1334 //
1335 // Register `card` contains the address of the card table. Note that the card
1336 // table's base is biased during its creation so that it always starts at an
1337 // address whose least-significant byte is equal to `kCardDirty` (see
1338 // art::gc::accounting::CardTable::Create). Therefore the STRB instruction
1339 // below writes the `kCardDirty` (byte) value into the `object`'s card
1340 // (located at `card + object >> kCardShift`).
1341 //
1342 // This dual use of the value in register `card` (1. to calculate the location
1343 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
1344 // (no need to explicitly load `kCardDirty` as an immediate value).
Serban Constantinescu02164b32014-11-13 14:05:07 +00001345 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001346 if (value_can_be_null) {
1347 __ Bind(&done);
1348 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001349}
1350
David Brazdil58282f42016-01-14 12:45:10 +00001351void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001352 // Blocked core registers:
1353 // lr : Runtime reserved.
1354 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001355 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001356 // ip1 : VIXL core temp.
1357 // ip0 : VIXL core temp.
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001358 // x18 : Platform register.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001359 //
1360 // Blocked fp registers:
1361 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001362 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1363 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001364 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001365 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001366 }
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001367 blocked_core_registers_[X18] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001368
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001369 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001370 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001371 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001372 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001373
David Brazdil58282f42016-01-14 12:45:10 +00001374 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001375 // Stubs do not save callee-save floating point registers. If the graph
1376 // is debuggable, we need to deal with these registers differently. For
1377 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001378 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1379 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001380 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001381 }
1382 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001383}
1384
Alexandre Rames3e69f162014-12-10 10:36:50 +00001385size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1386 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1387 __ Str(reg, MemOperand(sp, stack_index));
1388 return kArm64WordSize;
1389}
1390
1391size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1392 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1393 __ Ldr(reg, MemOperand(sp, stack_index));
1394 return kArm64WordSize;
1395}
1396
Artem Serov9df37b92019-07-23 16:41:54 +01001397size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1398 uint32_t reg_id ATTRIBUTE_UNUSED) {
1399 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1400 << "use SaveRestoreLiveRegistersHelper";
1401 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001402}
1403
Artem Serov9df37b92019-07-23 16:41:54 +01001404size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1405 uint32_t reg_id ATTRIBUTE_UNUSED) {
1406 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1407 << "use SaveRestoreLiveRegistersHelper";
1408 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001409}
1410
Alexandre Rames5319def2014-10-23 10:03:10 +01001411void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001412 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001413}
1414
1415void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001416 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001417}
1418
Vladimir Markoa0431112018-06-25 09:32:54 +01001419const Arm64InstructionSetFeatures& CodeGeneratorARM64::GetInstructionSetFeatures() const {
1420 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArm64InstructionSetFeatures();
1421}
1422
Alexandre Rames67555f72014-11-18 10:55:16 +00001423void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001424 if (constant->IsIntConstant()) {
1425 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1426 } else if (constant->IsLongConstant()) {
1427 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1428 } else if (constant->IsNullConstant()) {
1429 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001430 } else if (constant->IsFloatConstant()) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001431 __ Fmov(VRegister(destination), constant->AsFloatConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001432 } else {
1433 DCHECK(constant->IsDoubleConstant());
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001434 __ Fmov(VRegister(destination), constant->AsDoubleConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001435 }
1436}
1437
Alexandre Rames3e69f162014-12-10 10:36:50 +00001438
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001439static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001440 DCHECK(constant.IsConstant());
1441 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001442 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001443 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001444 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1445 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1446 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1447 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001448}
1449
Roland Levillain952b2352017-05-03 19:49:14 +01001450// Allocate a scratch register from the VIXL pool, querying first
1451// the floating-point register pool, and then the core register
1452// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001453// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1454// using a different allocation strategy.
1455static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1456 vixl::aarch64::UseScratchRegisterScope* temps,
1457 int size_in_bits) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001458 return masm->GetScratchVRegisterList()->IsEmpty()
Roland Levillain558dea12017-01-27 19:40:44 +00001459 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1460 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1461}
1462
Calin Juravlee460d1d2015-09-29 04:52:17 +01001463void CodeGeneratorARM64::MoveLocation(Location destination,
1464 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001465 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001466 if (source.Equals(destination)) {
1467 return;
1468 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001469
1470 // A valid move can always be inferred from the destination and source
1471 // locations. When moving from and to a register, the argument type can be
1472 // used to generate 32bit instead of 64bit moves. In debug mode we also
1473 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001474 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001475
1476 if (destination.IsRegister() || destination.IsFpuRegister()) {
1477 if (unspecified_type) {
1478 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1479 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001480 (src_cst != nullptr && (src_cst->IsIntConstant()
1481 || src_cst->IsFloatConstant()
1482 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001483 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001484 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001485 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001486 // If the source is a double stack slot or a 64bit constant, a 64bit
1487 // type is appropriate. Else the source is a register, and since the
1488 // type has not been specified, we chose a 64bit type to force a 64bit
1489 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001490 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001491 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001492 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001493 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1494 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001495 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001496 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1497 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1498 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001499 } else if (source.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001500 GetInstructionCodeGeneratorArm64()->LoadSIMDRegFromStack(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001501 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001502 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001503 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001504 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001505 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001506 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001507 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001508 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001509 DataType::Type source_type = DataType::Is64BitType(dst_type)
1510 ? DataType::Type::kInt64
1511 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001512 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1513 }
1514 } else {
1515 DCHECK(source.IsFpuRegister());
1516 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001517 DataType::Type source_type = DataType::Is64BitType(dst_type)
1518 ? DataType::Type::kFloat64
1519 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001520 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1521 } else {
1522 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001523 if (GetGraph()->HasSIMD()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001524 GetInstructionCodeGeneratorArm64()->MoveSIMDRegToSIMDReg(destination, source);
Artem Serovd4bccf12017-04-03 18:47:32 +01001525 } else {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001526 __ Fmov(VRegister(dst), FPRegisterFrom(source, dst_type));
Artem Serovd4bccf12017-04-03 18:47:32 +01001527 }
1528 }
1529 }
1530 } else if (destination.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001531 GetInstructionCodeGeneratorArm64()->MoveToSIMDStackSlot(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001532 } else { // The destination is not a register. It must be a stack slot.
1533 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1534 if (source.IsRegister() || source.IsFpuRegister()) {
1535 if (unspecified_type) {
1536 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001537 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001538 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001539 dst_type =
1540 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001541 }
1542 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001543 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1544 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001545 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001546 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001547 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1548 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001549 UseScratchRegisterScope temps(GetVIXLAssembler());
1550 HConstant* src_cst = source.GetConstant();
1551 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001552 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001553 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1554 ? Register(xzr)
1555 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001556 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001557 if (src_cst->IsIntConstant()) {
1558 temp = temps.AcquireW();
1559 } else if (src_cst->IsLongConstant()) {
1560 temp = temps.AcquireX();
1561 } else if (src_cst->IsFloatConstant()) {
1562 temp = temps.AcquireS();
1563 } else {
1564 DCHECK(src_cst->IsDoubleConstant());
1565 temp = temps.AcquireD();
1566 }
1567 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001568 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001569 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001570 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001571 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001572 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001573 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001574 // Use any scratch register (a core or a floating-point one)
1575 // from VIXL scratch register pools as a temporary.
1576 //
1577 // We used to only use the FP scratch register pool, but in some
1578 // rare cases the only register from this pool (D31) would
1579 // already be used (e.g. within a ParallelMove instruction, when
1580 // a move is blocked by a another move requiring a scratch FP
1581 // register, which would reserve D31). To prevent this issue, we
1582 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001583 //
1584 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001585 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001586 // use AcquireFPOrCoreCPURegisterOfSize instead of
1587 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1588 // allocates core scratch registers first.
1589 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1590 GetVIXLAssembler(),
1591 &temps,
1592 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001593 __ Ldr(temp, StackOperandFrom(source));
1594 __ Str(temp, StackOperandFrom(destination));
1595 }
1596 }
1597}
1598
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001599void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001600 CPURegister dst,
1601 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001602 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001603 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001604 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001605 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001606 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001607 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001608 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001609 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001610 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001611 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001612 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001613 case DataType::Type::kInt16:
1614 __ Ldrsh(Register(dst), src);
1615 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001616 case DataType::Type::kInt32:
1617 case DataType::Type::kReference:
1618 case DataType::Type::kInt64:
1619 case DataType::Type::kFloat32:
1620 case DataType::Type::kFloat64:
1621 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001622 __ Ldr(dst, src);
1623 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001624 case DataType::Type::kUint32:
1625 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001626 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001627 LOG(FATAL) << "Unreachable type " << type;
1628 }
1629}
1630
Calin Juravle77520bc2015-01-12 18:45:46 +00001631void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001632 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001633 const MemOperand& src,
1634 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001635 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001636 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001637 Register temp_base = temps.AcquireX();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001638 DataType::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001639
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001640 DCHECK(!src.IsPreIndex());
1641 DCHECK(!src.IsPostIndex());
1642
1643 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001644 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001645 {
1646 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1647 MemOperand base = MemOperand(temp_base);
1648 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001649 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001650 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001651 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001652 {
1653 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1654 __ ldarb(Register(dst), base);
1655 if (needs_null_check) {
1656 MaybeRecordImplicitNullCheck(instruction);
1657 }
1658 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001659 if (type == DataType::Type::kInt8) {
1660 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001661 }
1662 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001663 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001664 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001665 {
1666 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1667 __ ldarh(Register(dst), base);
1668 if (needs_null_check) {
1669 MaybeRecordImplicitNullCheck(instruction);
1670 }
1671 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001672 if (type == DataType::Type::kInt16) {
1673 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
1674 }
Artem Serov914d7a82017-02-07 14:33:49 +00001675 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001676 case DataType::Type::kInt32:
1677 case DataType::Type::kReference:
1678 case DataType::Type::kInt64:
1679 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001680 {
1681 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1682 __ ldar(Register(dst), base);
1683 if (needs_null_check) {
1684 MaybeRecordImplicitNullCheck(instruction);
1685 }
1686 }
1687 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001688 case DataType::Type::kFloat32:
1689 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00001690 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001691 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001692
Artem Serov914d7a82017-02-07 14:33:49 +00001693 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1694 {
1695 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1696 __ ldar(temp, base);
1697 if (needs_null_check) {
1698 MaybeRecordImplicitNullCheck(instruction);
1699 }
1700 }
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001701 __ Fmov(VRegister(dst), temp);
Artem Serov914d7a82017-02-07 14:33:49 +00001702 break;
Roland Levillain44015862016-01-22 11:47:17 +00001703 }
Aart Bik66c158e2018-01-31 12:55:04 -08001704 case DataType::Type::kUint32:
1705 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001706 case DataType::Type::kVoid:
Artem Serov914d7a82017-02-07 14:33:49 +00001707 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001708 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001709 }
1710}
1711
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001712void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001713 CPURegister src,
1714 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001715 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001716 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001717 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001718 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001719 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001720 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001721 case DataType::Type::kUint16:
1722 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001723 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001724 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001725 case DataType::Type::kInt32:
1726 case DataType::Type::kReference:
1727 case DataType::Type::kInt64:
1728 case DataType::Type::kFloat32:
1729 case DataType::Type::kFloat64:
1730 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001731 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001732 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001733 case DataType::Type::kUint32:
1734 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001735 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001736 LOG(FATAL) << "Unreachable type " << type;
1737 }
1738}
1739
Artem Serov914d7a82017-02-07 14:33:49 +00001740void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001741 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001742 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00001743 const MemOperand& dst,
1744 bool needs_null_check) {
1745 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001746 UseScratchRegisterScope temps(GetVIXLAssembler());
1747 Register temp_base = temps.AcquireX();
1748
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001749 DCHECK(!dst.IsPreIndex());
1750 DCHECK(!dst.IsPostIndex());
1751
1752 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001753 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001754 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001755 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00001756 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001757 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001758 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001759 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001760 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001761 {
1762 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1763 __ stlrb(Register(src), base);
1764 if (needs_null_check) {
1765 MaybeRecordImplicitNullCheck(instruction);
1766 }
1767 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001768 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001769 case DataType::Type::kUint16:
1770 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001771 {
1772 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1773 __ stlrh(Register(src), base);
1774 if (needs_null_check) {
1775 MaybeRecordImplicitNullCheck(instruction);
1776 }
1777 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001778 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001779 case DataType::Type::kInt32:
1780 case DataType::Type::kReference:
1781 case DataType::Type::kInt64:
1782 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001783 {
1784 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1785 __ stlr(Register(src), base);
1786 if (needs_null_check) {
1787 MaybeRecordImplicitNullCheck(instruction);
1788 }
1789 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001790 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001791 case DataType::Type::kFloat32:
1792 case DataType::Type::kFloat64: {
1793 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001794 Register temp_src;
1795 if (src.IsZero()) {
1796 // The zero register is used to avoid synthesizing zero constants.
1797 temp_src = Register(src);
1798 } else {
1799 DCHECK(src.IsFPRegister());
1800 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001801 __ Fmov(temp_src, VRegister(src));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001802 }
Artem Serov914d7a82017-02-07 14:33:49 +00001803 {
1804 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1805 __ stlr(temp_src, base);
1806 if (needs_null_check) {
1807 MaybeRecordImplicitNullCheck(instruction);
1808 }
1809 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001810 break;
1811 }
Aart Bik66c158e2018-01-31 12:55:04 -08001812 case DataType::Type::kUint32:
1813 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001814 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001815 LOG(FATAL) << "Unreachable type " << type;
1816 }
1817}
1818
Calin Juravle175dc732015-08-25 15:42:32 +01001819void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1820 HInstruction* instruction,
1821 uint32_t dex_pc,
1822 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001823 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00001824
Vladimir Markof6675082019-05-17 12:05:28 +01001825 ThreadOffset64 entrypoint_offset = GetThreadOffset<kArm64PointerSize>(entrypoint);
1826 // Reduce code size for AOT by using shared trampolines for slow path runtime calls across the
1827 // entire oat file. This adds an extra branch and we do not want to slow down the main path.
1828 // For JIT, thunk sharing is per-method, so the gains would be smaller or even negative.
Vladimir Marko695348f2020-05-19 14:42:02 +01001829 if (slow_path == nullptr || GetCompilerOptions().IsJitCompiler()) {
Vladimir Markof6675082019-05-17 12:05:28 +01001830 __ Ldr(lr, MemOperand(tr, entrypoint_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00001831 // Ensure the pc position is recorded immediately after the `blr` instruction.
1832 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1833 __ blr(lr);
1834 if (EntrypointRequiresStackMap(entrypoint)) {
1835 RecordPcInfo(instruction, dex_pc, slow_path);
1836 }
Vladimir Markof6675082019-05-17 12:05:28 +01001837 } else {
1838 // Ensure the pc position is recorded immediately after the `bl` instruction.
1839 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1840 EmitEntrypointThunkCall(entrypoint_offset);
1841 if (EntrypointRequiresStackMap(entrypoint)) {
1842 RecordPcInfo(instruction, dex_pc, slow_path);
1843 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001844 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001845}
1846
Roland Levillaindec8f632016-07-22 17:10:06 +01001847void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1848 HInstruction* instruction,
1849 SlowPathCode* slow_path) {
1850 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01001851 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1852 __ Blr(lr);
1853}
1854
Alexandre Rames67555f72014-11-18 10:55:16 +00001855void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001856 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001857 UseScratchRegisterScope temps(GetVIXLAssembler());
1858 Register temp = temps.AcquireW();
Vladimir Markodc682aa2018-01-04 18:42:57 +00001859 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001860 const size_t status_byte_offset =
1861 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1862 constexpr uint32_t shifted_visibly_initialized_value =
1863 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001864
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001865 // CMP (immediate) is limited to imm12 or imm12<<12, so we would need to materialize
1866 // the constant 0xf0000000 for comparison with the full 32-bit field. To reduce the code
1867 // size, load only the high byte of the field and compare with 0xf0.
1868 // Note: The same code size could be achieved with LDR+MNV(asr #24)+CBNZ but benchmarks
1869 // show that this pattern is slower (tested on little cores).
1870 __ Ldrb(temp, HeapOperand(class_reg, status_byte_offset));
1871 __ Cmp(temp, shifted_visibly_initialized_value);
1872 __ B(lo, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001873 __ Bind(slow_path->GetExitLabel());
1874}
Alexandre Rames5319def2014-10-23 10:03:10 +01001875
Vladimir Marko175e7862018-03-27 09:03:13 +00001876void InstructionCodeGeneratorARM64::GenerateBitstringTypeCheckCompare(
1877 HTypeCheckInstruction* check, vixl::aarch64::Register temp) {
1878 uint32_t path_to_root = check->GetBitstringPathToRoot();
1879 uint32_t mask = check->GetBitstringMask();
1880 DCHECK(IsPowerOfTwo(mask + 1));
1881 size_t mask_bits = WhichPowerOf2(mask + 1);
1882
1883 if (mask_bits == 16u) {
1884 // Load only the bitstring part of the status word.
1885 __ Ldrh(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1886 } else {
1887 // /* uint32_t */ temp = temp->status_
1888 __ Ldr(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1889 // Extract the bitstring bits.
1890 __ Ubfx(temp, temp, 0, mask_bits);
1891 }
1892 // Compare the bitstring bits to `path_to_root`.
1893 __ Cmp(temp, path_to_root);
1894}
1895
Roland Levillain44015862016-01-22 11:47:17 +00001896void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001897 BarrierType type = BarrierAll;
1898
1899 switch (kind) {
1900 case MemBarrierKind::kAnyAny:
1901 case MemBarrierKind::kAnyStore: {
1902 type = BarrierAll;
1903 break;
1904 }
1905 case MemBarrierKind::kLoadAny: {
1906 type = BarrierReads;
1907 break;
1908 }
1909 case MemBarrierKind::kStoreStore: {
1910 type = BarrierWrites;
1911 break;
1912 }
1913 default:
1914 LOG(FATAL) << "Unexpected memory barrier " << kind;
1915 }
1916 __ Dmb(InnerShareable, type);
1917}
1918
Serban Constantinescu02164b32014-11-13 14:05:07 +00001919void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1920 HBasicBlock* successor) {
1921 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001922 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1923 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001924 slow_path =
1925 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001926 instruction->SetSlowPath(slow_path);
1927 codegen_->AddSlowPath(slow_path);
1928 if (successor != nullptr) {
1929 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001930 }
1931 } else {
1932 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1933 }
1934
Serban Constantinescu02164b32014-11-13 14:05:07 +00001935 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1936 Register temp = temps.AcquireW();
1937
Andreas Gampe542451c2016-07-26 09:02:02 -07001938 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001939 if (successor == nullptr) {
1940 __ Cbnz(temp, slow_path->GetEntryLabel());
1941 __ Bind(slow_path->GetReturnLabel());
1942 } else {
1943 __ Cbz(temp, codegen_->GetLabelOf(successor));
1944 __ B(slow_path->GetEntryLabel());
1945 // slow_path will return to GetLabelOf(successor).
1946 }
1947}
1948
Alexandre Rames5319def2014-10-23 10:03:10 +01001949InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1950 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001951 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001952 assembler_(codegen->GetAssembler()),
1953 codegen_(codegen) {}
1954
Alexandre Rames67555f72014-11-18 10:55:16 +00001955void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001956 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001957 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001958 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001959 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001960 case DataType::Type::kInt32:
1961 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01001962 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001963 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001964 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001965 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001966
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001967 case DataType::Type::kFloat32:
1968 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001969 locations->SetInAt(0, Location::RequiresFpuRegister());
1970 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001971 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001972 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001973
Alexandre Rames5319def2014-10-23 10:03:10 +01001974 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001975 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001976 }
1977}
1978
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001979void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
1980 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001981 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1982
1983 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001984 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01001985 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001986 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
1987 object_field_get_with_read_barrier
1988 ? LocationSummary::kCallOnSlowPath
1989 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001990 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001991 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko0ecac682018-08-07 10:40:38 +01001992 // We need a temporary register for the read barrier load in
1993 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
1994 // only if the field is volatile or the offset is too big.
1995 if (field_info.IsVolatile() ||
1996 field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
1997 locations->AddTemp(FixedTempLocation());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001998 }
Vladimir Marko70e97462016-08-09 11:04:26 +01001999 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002000 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002001 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002002 locations->SetOut(Location::RequiresFpuRegister());
2003 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002004 // The output overlaps for an object field get when read barriers
2005 // are enabled: we do not want the load to overwrite the object's
2006 // location, as we need it to emit the read barrier.
2007 locations->SetOut(
2008 Location::RequiresRegister(),
2009 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002010 }
2011}
2012
2013void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2014 const FieldInfo& field_info) {
2015 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002016 LocationSummary* locations = instruction->GetLocations();
2017 Location base_loc = locations->InAt(0);
2018 Location out = locations->Out();
2019 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01002020 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
2021 DataType::Type load_type = instruction->GetType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002022 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002023
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002024 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01002025 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002026 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002027 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002028 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002029 Location maybe_temp =
2030 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002031 // Note that potential implicit null checks are handled in this
2032 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2033 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2034 instruction,
2035 out,
2036 base,
2037 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002038 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002039 /* needs_null_check= */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002040 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002041 } else {
2042 // General case.
2043 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002044 // Note that a potential implicit null check is handled in this
2045 // CodeGeneratorARM64::LoadAcquire call.
2046 // NB: LoadAcquire will record the pc info if needed.
2047 codegen_->LoadAcquire(
Andreas Gampe3db70682018-12-26 15:12:03 -08002048 instruction, OutputCPURegister(instruction), field, /* needs_null_check= */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002049 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002050 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2051 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01002052 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002053 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002054 }
Vladimir Marko61b92282017-10-11 13:23:17 +01002055 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002056 // If read barriers are enabled, emit read barriers other than
2057 // Baker's using a slow path (and also unpoison the loaded
2058 // reference, if heap poisoning is enabled).
2059 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2060 }
Roland Levillain4d027112015-07-01 15:41:14 +01002061 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002062}
2063
2064void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2065 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002066 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01002067 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002068 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2069 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002070 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002071 locations->SetInAt(1, Location::RequiresFpuRegister());
2072 } else {
2073 locations->SetInAt(1, Location::RequiresRegister());
2074 }
2075}
2076
2077void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002078 const FieldInfo& field_info,
2079 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002080 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2081
2082 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002083 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002084 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002085 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002086 DataType::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002087
Roland Levillain4d027112015-07-01 15:41:14 +01002088 {
2089 // We use a block to end the scratch scope before the write barrier, thus
2090 // freeing the temporary registers so they can be used in `MarkGCCard`.
2091 UseScratchRegisterScope temps(GetVIXLAssembler());
2092
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002093 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002094 DCHECK(value.IsW());
2095 Register temp = temps.AcquireW();
2096 __ Mov(temp, value.W());
2097 GetAssembler()->PoisonHeapReference(temp.W());
2098 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002099 }
Roland Levillain4d027112015-07-01 15:41:14 +01002100
2101 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002102 codegen_->StoreRelease(
Andreas Gampe3db70682018-12-26 15:12:03 -08002103 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check= */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002104 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002105 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2106 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002107 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2108 codegen_->MaybeRecordImplicitNullCheck(instruction);
2109 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002110 }
2111
2112 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002113 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002114 }
2115}
2116
Alexandre Rames67555f72014-11-18 10:55:16 +00002117void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002118 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002119
2120 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002121 case DataType::Type::kInt32:
2122 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002123 Register dst = OutputRegister(instr);
2124 Register lhs = InputRegisterAt(instr, 0);
2125 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002126 if (instr->IsAdd()) {
2127 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002128 } else if (instr->IsAnd()) {
2129 __ And(dst, lhs, rhs);
2130 } else if (instr->IsOr()) {
2131 __ Orr(dst, lhs, rhs);
2132 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002133 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002134 } else if (instr->IsRor()) {
2135 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002136 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002137 __ Ror(dst, lhs, shift);
2138 } else {
2139 // Ensure shift distance is in the same size register as the result. If
2140 // we are rotating a long and the shift comes in a w register originally,
2141 // we don't need to sxtw for use as an x since the shift distances are
2142 // all & reg_bits - 1.
2143 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2144 }
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002145 } else if (instr->IsMin() || instr->IsMax()) {
2146 __ Cmp(lhs, rhs);
2147 __ Csel(dst, lhs, rhs, instr->IsMin() ? lt : gt);
Alexandre Rames67555f72014-11-18 10:55:16 +00002148 } else {
2149 DCHECK(instr->IsXor());
2150 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002151 }
2152 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002153 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002154 case DataType::Type::kFloat32:
2155 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002156 VRegister dst = OutputFPRegister(instr);
2157 VRegister lhs = InputFPRegisterAt(instr, 0);
2158 VRegister rhs = InputFPRegisterAt(instr, 1);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002159 if (instr->IsAdd()) {
2160 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002161 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002162 __ Fsub(dst, lhs, rhs);
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002163 } else if (instr->IsMin()) {
2164 __ Fmin(dst, lhs, rhs);
2165 } else if (instr->IsMax()) {
2166 __ Fmax(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002167 } else {
2168 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002169 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002170 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002171 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002172 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002173 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002174 }
2175}
2176
Serban Constantinescu02164b32014-11-13 14:05:07 +00002177void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2178 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2179
Vladimir Markoca6fff82017-10-03 14:49:14 +01002180 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002181 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002182 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002183 case DataType::Type::kInt32:
2184 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002185 locations->SetInAt(0, Location::RequiresRegister());
2186 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002187 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002188 break;
2189 }
2190 default:
2191 LOG(FATAL) << "Unexpected shift type " << type;
2192 }
2193}
2194
2195void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2196 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2197
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002198 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002199 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002200 case DataType::Type::kInt32:
2201 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002202 Register dst = OutputRegister(instr);
2203 Register lhs = InputRegisterAt(instr, 0);
2204 Operand rhs = InputOperandAt(instr, 1);
2205 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002206 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002207 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002208 if (instr->IsShl()) {
2209 __ Lsl(dst, lhs, shift_value);
2210 } else if (instr->IsShr()) {
2211 __ Asr(dst, lhs, shift_value);
2212 } else {
2213 __ Lsr(dst, lhs, shift_value);
2214 }
2215 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002216 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002217
2218 if (instr->IsShl()) {
2219 __ Lsl(dst, lhs, rhs_reg);
2220 } else if (instr->IsShr()) {
2221 __ Asr(dst, lhs, rhs_reg);
2222 } else {
2223 __ Lsr(dst, lhs, rhs_reg);
2224 }
2225 }
2226 break;
2227 }
2228 default:
2229 LOG(FATAL) << "Unexpected shift operation type " << type;
2230 }
2231}
2232
Alexandre Rames5319def2014-10-23 10:03:10 +01002233void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002234 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002235}
2236
2237void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002238 HandleBinaryOp(instruction);
2239}
2240
2241void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2242 HandleBinaryOp(instruction);
2243}
2244
2245void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2246 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002247}
2248
Artem Serov7fc63502016-02-09 17:15:29 +00002249void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002250 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002251 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002252 locations->SetInAt(0, Location::RequiresRegister());
2253 // There is no immediate variant of negated bitwise instructions in AArch64.
2254 locations->SetInAt(1, Location::RequiresRegister());
2255 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2256}
2257
Artem Serov7fc63502016-02-09 17:15:29 +00002258void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002259 Register dst = OutputRegister(instr);
2260 Register lhs = InputRegisterAt(instr, 0);
2261 Register rhs = InputRegisterAt(instr, 1);
2262
2263 switch (instr->GetOpKind()) {
2264 case HInstruction::kAnd:
2265 __ Bic(dst, lhs, rhs);
2266 break;
2267 case HInstruction::kOr:
2268 __ Orn(dst, lhs, rhs);
2269 break;
2270 case HInstruction::kXor:
2271 __ Eon(dst, lhs, rhs);
2272 break;
2273 default:
2274 LOG(FATAL) << "Unreachable";
2275 }
2276}
2277
Anton Kirilov74234da2017-01-13 14:42:47 +00002278void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2279 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002280 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2281 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002282 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002283 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002284 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2285 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2286 } else {
2287 locations->SetInAt(0, Location::RequiresRegister());
2288 }
2289 locations->SetInAt(1, Location::RequiresRegister());
2290 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2291}
2292
Anton Kirilov74234da2017-01-13 14:42:47 +00002293void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2294 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002295 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002296 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002297 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002298 Register out = OutputRegister(instruction);
2299 Register left;
2300 if (kind != HInstruction::kNeg) {
2301 left = InputRegisterAt(instruction, 0);
2302 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002303 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002304 // shifter operand operation, the IR generating `right_reg` (input to the type
2305 // conversion) can have a different type from the current instruction's type,
2306 // so we manually indicate the type.
2307 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002308 Operand right_operand(0);
2309
Anton Kirilov74234da2017-01-13 14:42:47 +00002310 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2311 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002312 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2313 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002314 right_operand = Operand(right_reg,
2315 helpers::ShiftFromOpKind(op_kind),
2316 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002317 }
2318
2319 // Logical binary operations do not support extension operations in the
2320 // operand. Note that VIXL would still manage if it was passed by generating
2321 // the extension as a separate instruction.
2322 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2323 DCHECK(!right_operand.IsExtendedRegister() ||
2324 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2325 kind != HInstruction::kNeg));
2326 switch (kind) {
2327 case HInstruction::kAdd:
2328 __ Add(out, left, right_operand);
2329 break;
2330 case HInstruction::kAnd:
2331 __ And(out, left, right_operand);
2332 break;
2333 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002334 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002335 __ Neg(out, right_operand);
2336 break;
2337 case HInstruction::kOr:
2338 __ Orr(out, left, right_operand);
2339 break;
2340 case HInstruction::kSub:
2341 __ Sub(out, left, right_operand);
2342 break;
2343 case HInstruction::kXor:
2344 __ Eor(out, left, right_operand);
2345 break;
2346 default:
2347 LOG(FATAL) << "Unexpected operation kind: " << kind;
2348 UNREACHABLE();
2349 }
2350}
2351
Artem Serov328429f2016-07-06 16:23:04 +01002352void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002353 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002354 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002355 locations->SetInAt(0, Location::RequiresRegister());
2356 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002357 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002358}
2359
Roland Levillain19c54192016-11-04 13:44:09 +00002360void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002361 __ Add(OutputRegister(instruction),
2362 InputRegisterAt(instruction, 0),
2363 Operand(InputOperandAt(instruction, 1)));
2364}
2365
Artem Serove1811ed2017-04-27 16:50:47 +01002366void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2367 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002368 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002369
2370 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2371
2372 locations->SetInAt(0, Location::RequiresRegister());
2373 // For byte case we don't need to shift the index variable so we can encode the data offset into
2374 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2375 // data offset constant generation out of the loop and reduce the critical path length in the
2376 // loop.
2377 locations->SetInAt(1, shift->GetValue() == 0
2378 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2379 : Location::RequiresRegister());
2380 locations->SetInAt(2, Location::ConstantLocation(shift));
2381 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2382}
2383
2384void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2385 HIntermediateAddressIndex* instruction) {
2386 Register index_reg = InputRegisterAt(instruction, 0);
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002387 uint32_t shift = Int64FromLocation(instruction->GetLocations()->InAt(2));
Artem Serove1811ed2017-04-27 16:50:47 +01002388 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2389
2390 if (shift == 0) {
2391 __ Add(OutputRegister(instruction), index_reg, offset);
2392 } else {
2393 Register offset_reg = InputRegisterAt(instruction, 1);
2394 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2395 }
2396}
2397
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002398void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002399 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002400 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002401 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2402 if (instr->GetOpKind() == HInstruction::kSub &&
2403 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002404 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002405 // Don't allocate register for Mneg instruction.
2406 } else {
2407 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2408 Location::RequiresRegister());
2409 }
2410 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2411 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002412 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2413}
2414
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002415void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002416 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002417 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2418 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002419
2420 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2421 // This fixup should be carried out for all multiply-accumulate instructions:
2422 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002423 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002424 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2425 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002426 vixl::aarch64::Instruction* prev =
2427 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002428 if (prev->IsLoadOrStore()) {
2429 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002430 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002431 __ nop();
2432 }
2433 }
2434
2435 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002436 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002437 __ Madd(res, mul_left, mul_right, accumulator);
2438 } else {
2439 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002440 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002441 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002442 __ Mneg(res, mul_left, mul_right);
2443 } else {
2444 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2445 __ Msub(res, mul_left, mul_right, accumulator);
2446 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002447 }
2448}
2449
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002450void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002451 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002452 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002453 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002454 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2455 object_array_get_with_read_barrier
2456 ? LocationSummary::kCallOnSlowPath
2457 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002458 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002459 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002460 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002461 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002462 // We need a temporary register for the read barrier load in
2463 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2464 // only if the offset is too big.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002465 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2466 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002467 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002468 if (offset >= kReferenceLoadMinFarOffset) {
2469 locations->AddTemp(FixedTempLocation());
2470 }
Artem Serov0806f582018-10-11 20:14:20 +01002471 } else if (!instruction->GetArray()->IsIntermediateAddress()) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01002472 // We need a non-scratch temporary for the array data pointer in
Artem Serov0806f582018-10-11 20:14:20 +01002473 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier() for the case with no
2474 // intermediate address.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002475 locations->AddTemp(Location::RequiresRegister());
2476 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002477 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002478 locations->SetInAt(0, Location::RequiresRegister());
2479 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002480 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002481 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2482 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002483 // The output overlaps in the case of an object array get with
2484 // read barriers enabled: we do not want the move to overwrite the
2485 // array's location, as we need it to emit the read barrier.
2486 locations->SetOut(
2487 Location::RequiresRegister(),
2488 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002489 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002490}
2491
2492void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002493 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002494 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002495 LocationSummary* locations = instruction->GetLocations();
2496 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002497 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002498 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002499 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2500 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002501 MacroAssembler* masm = GetVIXLAssembler();
2502 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002503
Artem Serov0806f582018-10-11 20:14:20 +01002504 // The non-Baker read barrier instrumentation of object ArrayGet instructions
Roland Levillain19c54192016-11-04 13:44:09 +00002505 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002506 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002507 instruction->GetArray()->IsIntermediateAddress() &&
Artem Serov0806f582018-10-11 20:14:20 +01002508 kEmitCompilerReadBarrier &&
2509 !kUseBakerReadBarrier));
Roland Levillain19c54192016-11-04 13:44:09 +00002510
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002511 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002512 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002513 // Note that a potential implicit null check is handled in the
2514 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002515 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002516 if (index.IsConstant()) {
Artem Serov0806f582018-10-11 20:14:20 +01002517 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002518 // Array load with a constant index can be treated as a field load.
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002519 offset += Int64FromLocation(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002520 Location maybe_temp =
2521 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2522 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2523 out,
2524 obj.W(),
2525 offset,
2526 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002527 /* needs_null_check= */ false,
2528 /* use_load_acquire= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002529 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002530 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08002531 instruction, out, obj.W(), offset, index, /* needs_null_check= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002532 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002533 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002534 // General case.
2535 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002536 Register length;
2537 if (maybe_compressed_char_at) {
2538 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2539 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002540 {
2541 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2542 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2543
2544 if (instruction->GetArray()->IsIntermediateAddress()) {
2545 DCHECK_LT(count_offset, offset);
2546 int64_t adjusted_offset =
2547 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2548 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2549 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2550 } else {
2551 __ Ldr(length, HeapOperand(obj, count_offset));
2552 }
2553 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002554 }
jessicahandojo05765752016-09-09 19:01:32 -07002555 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002556 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002557 if (maybe_compressed_char_at) {
2558 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002559 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2560 "Expecting 0=compressed, 1=uncompressed");
2561 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002562 __ Ldrb(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002563 HeapOperand(obj, offset + Int64FromLocation(index)));
jessicahandojo05765752016-09-09 19:01:32 -07002564 __ B(&done);
2565 __ Bind(&uncompressed_load);
2566 __ Ldrh(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002567 HeapOperand(obj, offset + (Int64FromLocation(index) << 1)));
jessicahandojo05765752016-09-09 19:01:32 -07002568 __ Bind(&done);
2569 } else {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002570 offset += Int64FromLocation(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002571 source = HeapOperand(obj, offset);
2572 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002573 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002574 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002575 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002576 // We do not need to compute the intermediate address from the array: the
2577 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002578 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002579 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002580 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2581 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
Roland Levillain44015862016-01-22 11:47:17 +00002582 }
2583 temp = obj;
2584 } else {
2585 __ Add(temp, obj, offset);
2586 }
jessicahandojo05765752016-09-09 19:01:32 -07002587 if (maybe_compressed_char_at) {
2588 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002589 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2590 "Expecting 0=compressed, 1=uncompressed");
2591 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002592 __ Ldrb(Register(OutputCPURegister(instruction)),
2593 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2594 __ B(&done);
2595 __ Bind(&uncompressed_load);
2596 __ Ldrh(Register(OutputCPURegister(instruction)),
2597 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2598 __ Bind(&done);
2599 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002600 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002601 }
Roland Levillain44015862016-01-22 11:47:17 +00002602 }
jessicahandojo05765752016-09-09 19:01:32 -07002603 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002604 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2605 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002606 codegen_->Load(type, OutputCPURegister(instruction), source);
2607 codegen_->MaybeRecordImplicitNullCheck(instruction);
2608 }
Roland Levillain44015862016-01-22 11:47:17 +00002609
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002610 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002611 static_assert(
2612 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2613 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2614 Location obj_loc = locations->InAt(0);
2615 if (index.IsConstant()) {
2616 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2617 } else {
2618 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2619 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002620 }
Roland Levillain4d027112015-07-01 15:41:14 +01002621 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002622}
2623
Alexandre Rames5319def2014-10-23 10:03:10 +01002624void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002625 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002626 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002627 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002628}
2629
2630void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002631 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002632 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002633 {
2634 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2635 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2636 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2637 codegen_->MaybeRecordImplicitNullCheck(instruction);
2638 }
jessicahandojo05765752016-09-09 19:01:32 -07002639 // Mask out compression flag from String's array length.
2640 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002641 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002642 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002643}
2644
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002645void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002646 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002647
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002648 bool needs_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002649 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002650 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002651 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002652 locations->SetInAt(0, Location::RequiresRegister());
2653 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002654 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2655 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002656 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002657 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002658 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002659 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002660 }
2661}
2662
2663void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002664 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002665 LocationSummary* locations = instruction->GetLocations();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002666 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002667 bool needs_write_barrier =
2668 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002669
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002670 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002671 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002672 CPURegister source = value;
2673 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002674 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002675 MemOperand destination = HeapOperand(array);
2676 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002677
2678 if (!needs_write_barrier) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002679 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002680 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002681 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002682 destination = HeapOperand(array, offset);
2683 } else {
2684 UseScratchRegisterScope temps(masm);
2685 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002686 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002687 // We do not need to compute the intermediate address from the array: the
2688 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002689 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002690 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002691 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2692 DCHECK(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002693 }
2694 temp = array;
2695 } else {
2696 __ Add(temp, array, offset);
2697 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002698 destination = HeapOperand(temp,
2699 XRegisterFrom(index),
2700 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002701 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002702 }
Artem Serov914d7a82017-02-07 14:33:49 +00002703 {
2704 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2705 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2706 codegen_->Store(value_type, value, destination);
2707 codegen_->MaybeRecordImplicitNullCheck(instruction);
2708 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002709 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002710 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002711
2712 bool can_value_be_null = instruction->GetValueCanBeNull();
2713 vixl::aarch64::Label do_store;
2714 if (can_value_be_null) {
2715 __ Cbz(Register(value), &do_store);
2716 }
2717
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002718 SlowPathCodeARM64* slow_path = nullptr;
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002719 if (needs_type_check) {
2720 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
2721 codegen_->AddSlowPath(slow_path);
2722
2723 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2724 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2725 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2726
Alexandre Rames97833a02015-04-16 15:07:12 +01002727 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002728 Register temp = temps.AcquireSameSizeAs(array);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002729 Register temp2 = temps.AcquireSameSizeAs(array);
2730
2731 // Note that when Baker read barriers are enabled, the type
2732 // checks are performed without read barriers. This is fine,
2733 // even in the case where a class object is in the from-space
2734 // after the flip, as a comparison involving such a type would
2735 // not produce a false positive; it may of course produce a
2736 // false negative, in which case we would take the ArraySet
2737 // slow path.
2738
2739 // /* HeapReference<Class> */ temp = array->klass_
2740 {
2741 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2742 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2743 __ Ldr(temp, HeapOperand(array, class_offset));
2744 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames97833a02015-04-16 15:07:12 +01002745 }
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002746 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Alexandre Rames97833a02015-04-16 15:07:12 +01002747
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002748 // /* HeapReference<Class> */ temp = temp->component_type_
2749 __ Ldr(temp, HeapOperand(temp, component_offset));
2750 // /* HeapReference<Class> */ temp2 = value->klass_
2751 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2752 // If heap poisoning is enabled, no need to unpoison `temp`
2753 // nor `temp2`, as we are comparing two poisoned references.
2754 __ Cmp(temp, temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002755
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002756 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2757 vixl::aarch64::Label do_put;
2758 __ B(eq, &do_put);
2759 // If heap poisoning is enabled, the `temp` reference has
2760 // not been unpoisoned yet; unpoison it now.
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002761 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002762
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002763 // /* HeapReference<Class> */ temp = temp->super_class_
2764 __ Ldr(temp, HeapOperand(temp, super_offset));
2765 // If heap poisoning is enabled, no need to unpoison
2766 // `temp`, as we are comparing against null below.
2767 __ Cbnz(temp, slow_path->GetEntryLabel());
2768 __ Bind(&do_put);
Vladimir Markod1ef8732017-04-18 13:55:13 +01002769 } else {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002770 __ B(ne, slow_path->GetEntryLabel());
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002771 }
2772 }
2773
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002774 codegen_->MarkGCCard(array, value.W(), /* value_can_be_null= */ false);
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002775
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002776 if (can_value_be_null) {
2777 DCHECK(do_store.IsLinked());
2778 __ Bind(&do_store);
2779 }
2780
2781 UseScratchRegisterScope temps(masm);
2782 if (kPoisonHeapReferences) {
2783 Register temp_source = temps.AcquireSameSizeAs(array);
2784 DCHECK(value.IsW());
2785 __ Mov(temp_source, value.W());
2786 GetAssembler()->PoisonHeapReference(temp_source);
2787 source = temp_source;
2788 }
2789
2790 if (index.IsConstant()) {
2791 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
2792 destination = HeapOperand(array, offset);
2793 } else {
2794 Register temp_base = temps.AcquireSameSizeAs(array);
2795 __ Add(temp_base, array, offset);
2796 destination = HeapOperand(temp_base,
2797 XRegisterFrom(index),
2798 LSL,
2799 DataType::SizeShift(value_type));
2800 }
2801
2802 {
2803 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2804 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2805 __ Str(source, destination);
2806
2807 if (can_value_be_null || !needs_type_check) {
2808 codegen_->MaybeRecordImplicitNullCheck(instruction);
2809 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002810 }
2811
2812 if (slow_path != nullptr) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002813 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002814 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002815 }
2816}
2817
Alexandre Rames67555f72014-11-18 10:55:16 +00002818void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002819 RegisterSet caller_saves = RegisterSet::Empty();
2820 InvokeRuntimeCallingConvention calling_convention;
2821 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2822 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2823 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002824
2825 // If both index and length are constant, we can check the bounds statically and
2826 // generate code accordingly. We want to make sure we generate constant locations
2827 // in that case, regardless of whether they are encodable in the comparison or not.
2828 HInstruction* index = instruction->InputAt(0);
2829 HInstruction* length = instruction->InputAt(1);
2830 bool both_const = index->IsConstant() && length->IsConstant();
2831 locations->SetInAt(0, both_const
2832 ? Location::ConstantLocation(index->AsConstant())
2833 : ARM64EncodableConstantOrRegister(index, instruction));
2834 locations->SetInAt(1, both_const
2835 ? Location::ConstantLocation(length->AsConstant())
2836 : ARM64EncodableConstantOrRegister(length, instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002837}
2838
2839void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Georgia Kouvelibe530852019-01-17 10:46:41 +00002840 LocationSummary* locations = instruction->GetLocations();
2841 Location index_loc = locations->InAt(0);
2842 Location length_loc = locations->InAt(1);
2843
2844 int cmp_first_input = 0;
2845 int cmp_second_input = 1;
2846 Condition cond = hs;
2847
2848 if (index_loc.IsConstant()) {
2849 int64_t index = Int64FromLocation(index_loc);
2850 if (length_loc.IsConstant()) {
2851 int64_t length = Int64FromLocation(length_loc);
2852 if (index < 0 || index >= length) {
2853 BoundsCheckSlowPathARM64* slow_path =
2854 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
2855 codegen_->AddSlowPath(slow_path);
2856 __ B(slow_path->GetEntryLabel());
2857 } else {
2858 // BCE will remove the bounds check if we are guaranteed to pass.
2859 // However, some optimization after BCE may have generated this, and we should not
2860 // generate a bounds check if it is a valid range.
2861 }
2862 return;
2863 }
2864 // Only the index is constant: change the order of the operands and commute the condition
2865 // so we can use an immediate constant for the index (only the second input to a cmp
2866 // instruction can be an immediate).
2867 cmp_first_input = 1;
2868 cmp_second_input = 0;
2869 cond = ls;
2870 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002871 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002872 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002873 __ Cmp(InputRegisterAt(instruction, cmp_first_input),
2874 InputOperandAt(instruction, cmp_second_input));
Alexandre Rames67555f72014-11-18 10:55:16 +00002875 codegen_->AddSlowPath(slow_path);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002876 __ B(slow_path->GetEntryLabel(), cond);
Alexandre Rames67555f72014-11-18 10:55:16 +00002877}
2878
Alexandre Rames67555f72014-11-18 10:55:16 +00002879void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2880 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002881 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00002882 locations->SetInAt(0, Location::RequiresRegister());
2883 if (check->HasUses()) {
2884 locations->SetOut(Location::SameAsFirstInput());
2885 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01002886 // Rely on the type initialization to save everything we need.
2887 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Alexandre Rames67555f72014-11-18 10:55:16 +00002888}
2889
2890void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2891 // We assume the class is not null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01002892 SlowPathCodeARM64* slow_path =
2893 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(check->GetLoadClass(), check);
Alexandre Rames67555f72014-11-18 10:55:16 +00002894 codegen_->AddSlowPath(slow_path);
2895 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2896}
2897
Roland Levillain1a653882016-03-18 18:05:57 +00002898static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2899 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2900 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2901}
2902
2903void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002904 VRegister lhs_reg = InputFPRegisterAt(instruction, 0);
Roland Levillain1a653882016-03-18 18:05:57 +00002905 Location rhs_loc = instruction->GetLocations()->InAt(1);
2906 if (rhs_loc.IsConstant()) {
2907 // 0.0 is the only immediate that can be encoded directly in
2908 // an FCMP instruction.
2909 //
2910 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2911 // specify that in a floating-point comparison, positive zero
2912 // and negative zero are considered equal, so we can use the
2913 // literal 0.0 for both cases here.
2914 //
2915 // Note however that some methods (Float.equal, Float.compare,
2916 // Float.compareTo, Double.equal, Double.compare,
2917 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2918 // StrictMath.min) consider 0.0 to be (strictly) greater than
2919 // -0.0. So if we ever translate calls to these methods into a
2920 // HCompare instruction, we must handle the -0.0 case with
2921 // care here.
2922 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2923 __ Fcmp(lhs_reg, 0.0);
2924 } else {
2925 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2926 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002927}
2928
Serban Constantinescu02164b32014-11-13 14:05:07 +00002929void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002930 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002931 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002932 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002933 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002934 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002935 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002936 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002937 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002938 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002939 case DataType::Type::kInt32:
2940 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002941 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002942 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002943 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2944 break;
2945 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002946 case DataType::Type::kFloat32:
2947 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002948 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002949 locations->SetInAt(1,
2950 IsFloatingPointZeroConstant(compare->InputAt(1))
2951 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2952 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002953 locations->SetOut(Location::RequiresRegister());
2954 break;
2955 }
2956 default:
2957 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2958 }
2959}
2960
2961void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002962 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002963
2964 // 0 if: left == right
2965 // 1 if: left > right
2966 // -1 if: left < right
2967 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002968 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002969 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002970 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002971 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002972 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002973 case DataType::Type::kInt32:
2974 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002975 Register result = OutputRegister(compare);
2976 Register left = InputRegisterAt(compare, 0);
2977 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002978 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002979 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2980 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002981 break;
2982 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002983 case DataType::Type::kFloat32:
2984 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002985 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002986 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002987 __ Cset(result, ne);
2988 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002989 break;
2990 }
2991 default:
2992 LOG(FATAL) << "Unimplemented compare type " << in_type;
2993 }
2994}
2995
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002996void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002997 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002998
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002999 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003000 locations->SetInAt(0, Location::RequiresFpuRegister());
3001 locations->SetInAt(1,
3002 IsFloatingPointZeroConstant(instruction->InputAt(1))
3003 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3004 : Location::RequiresFpuRegister());
3005 } else {
3006 // Integer cases.
3007 locations->SetInAt(0, Location::RequiresRegister());
3008 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3009 }
3010
David Brazdilb3e773e2016-01-26 11:28:37 +00003011 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003012 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003013 }
3014}
3015
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003016void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003017 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003018 return;
3019 }
3020
3021 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003022 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003023 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003024
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003025 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003026 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003027 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003028 } else {
3029 // Integer cases.
3030 Register lhs = InputRegisterAt(instruction, 0);
3031 Operand rhs = InputOperandAt(instruction, 1);
3032 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003033 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003034 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003035}
3036
3037#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3038 M(Equal) \
3039 M(NotEqual) \
3040 M(LessThan) \
3041 M(LessThanOrEqual) \
3042 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003043 M(GreaterThanOrEqual) \
3044 M(Below) \
3045 M(BelowOrEqual) \
3046 M(Above) \
3047 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003048#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003049void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3050void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003051FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003052#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003053#undef FOR_EACH_CONDITION_INSTRUCTION
3054
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003055void InstructionCodeGeneratorARM64::GenerateIntDivForPower2Denom(HDiv* instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003056 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003057 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003058 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
3059
3060 Register out = OutputRegister(instruction);
3061 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003062
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003063 Register final_dividend;
3064 if (HasNonNegativeResultOrMinInt(instruction->GetLeft())) {
3065 // No need to adjust the result for non-negative dividends or the INT32_MIN/INT64_MIN dividends.
3066 // NOTE: The generated code for HDiv correctly works for the INT32_MIN/INT64_MIN dividends:
3067 // imm == 2
3068 // add out, dividend(0x80000000), dividend(0x80000000), lsr #31 => out = 0x80000001
3069 // asr out, out(0x80000001), #1 => out = 0xc0000000
3070 // This is the same as 'asr out, 0x80000000, #1'
3071 //
3072 // imm > 2
3073 // add temp, dividend(0x80000000), imm - 1 => temp = 0b10..01..1, where the number
3074 // of the rightmost 1s is ctz_imm.
3075 // cmp dividend(0x80000000), 0 => N = 1, V = 0 (lt is true)
3076 // csel out, temp(0b10..01..1), dividend(0x80000000), lt => out = 0b10..01..1
3077 // asr out, out(0b10..01..1), #ctz_imm => out = 0b1..10..0, where the number of the
3078 // leftmost 1s is ctz_imm + 1.
3079 // This is the same as 'asr out, dividend(0x80000000), #ctz_imm'.
3080 //
3081 // imm == INT32_MIN
3082 // add tmp, dividend(0x80000000), #0x7fffffff => tmp = -1
3083 // cmp dividend(0x80000000), 0 => N = 1, V = 0 (lt is true)
3084 // csel out, temp(-1), dividend(0x80000000), lt => out = -1
3085 // neg out, out(-1), asr #31 => out = 1
3086 // This is the same as 'neg out, dividend(0x80000000), asr #31'.
3087 final_dividend = dividend;
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003088 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003089 if (abs_imm == 2) {
3090 int bits = DataType::Size(instruction->GetResultType()) * kBitsPerByte;
3091 __ Add(out, dividend, Operand(dividend, LSR, bits - 1));
3092 } else {
3093 UseScratchRegisterScope temps(GetVIXLAssembler());
3094 Register temp = temps.AcquireSameSizeAs(out);
3095 __ Add(temp, dividend, abs_imm - 1);
3096 __ Cmp(dividend, 0);
3097 __ Csel(out, temp, dividend, lt);
3098 }
3099 final_dividend = out;
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003100 }
3101
Zheng Xuc6667102015-05-15 16:08:45 +08003102 int ctz_imm = CTZ(abs_imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003103 if (imm > 0) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003104 __ Asr(out, final_dividend, ctz_imm);
Zheng Xuc6667102015-05-15 16:08:45 +08003105 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003106 __ Neg(out, Operand(final_dividend, ASR, ctz_imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003107 }
3108}
3109
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003110// Return true if the magic number was modified by subtracting 2^32. So dividend needs to be added.
3111static inline bool NeedToAddDividend(int64_t magic_number, int64_t divisor) {
3112 return divisor > 0 && magic_number < 0;
3113}
3114
3115// Return true if the magic number was modified by adding 2^32. So dividend needs to be subtracted.
3116static inline bool NeedToSubDividend(int64_t magic_number, int64_t divisor) {
3117 return divisor < 0 && magic_number > 0;
3118}
3119
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003120// Generate code which increments the value in register 'in' by 1 if the value is negative.
3121// It is done with 'add out, in, in, lsr #31 or #63'.
3122// If the value is a result of an operation setting the N flag, CINC MI can be used
3123// instead of ADD. 'use_cond_inc' controls this.
3124void InstructionCodeGeneratorARM64::GenerateIncrementNegativeByOne(
3125 Register out,
3126 Register in,
3127 bool use_cond_inc) {
3128 if (use_cond_inc) {
3129 __ Cinc(out, in, mi);
3130 } else {
3131 __ Add(out, in, Operand(in, LSR, in.GetSizeInBits() - 1));
3132 }
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003133}
3134
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003135// Helper to generate code producing the result of HRem with a constant divisor.
3136void InstructionCodeGeneratorARM64::GenerateResultRemWithAnyConstant(
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003137 Register out,
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003138 Register dividend,
3139 Register quotient,
3140 int64_t divisor,
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003141 UseScratchRegisterScope* temps_scope) {
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003142 Register temp_imm = temps_scope->AcquireSameSizeAs(out);
3143 __ Mov(temp_imm, divisor);
3144 __ Msub(out, quotient, temp_imm, dividend);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003145}
3146
3147void InstructionCodeGeneratorARM64::GenerateInt64DivRemWithAnyConstant(
3148 HBinaryOperation* instruction) {
Zheng Xuc6667102015-05-15 16:08:45 +08003149 DCHECK(instruction->IsDiv() || instruction->IsRem());
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003150 DCHECK(instruction->GetResultType() == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003151
3152 LocationSummary* locations = instruction->GetLocations();
3153 Location second = locations->InAt(1);
3154 DCHECK(second.IsConstant());
3155
3156 Register out = OutputRegister(instruction);
3157 Register dividend = InputRegisterAt(instruction, 0);
3158 int64_t imm = Int64FromConstant(second.GetConstant());
3159
Zheng Xuc6667102015-05-15 16:08:45 +08003160 int64_t magic;
3161 int shift;
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003162 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ true, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08003163
3164 UseScratchRegisterScope temps(GetVIXLAssembler());
3165 Register temp = temps.AcquireSameSizeAs(out);
3166
3167 // temp = get_high(dividend * magic)
3168 __ Mov(temp, magic);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003169 __ Smulh(temp, dividend, temp);
3170
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003171 // The multiplication result might need some corrections to be finalized.
3172 // The last correction is to increment by 1, if the result is negative.
3173 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3174 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3175 // However if one of the corrections is ADD or SUB, the sign can be detected
3176 // with ADDS/SUBS. They set the N flag if the result is negative.
3177 // This allows to use CINC MI which has latency 1.
3178 bool use_cond_inc = false;
3179
3180 // As magic_number can be modified to fit into 32 bits, check whether the correction is needed.
3181 if (NeedToAddDividend(magic, imm)) {
3182 __ Adds(temp, temp, dividend);
3183 use_cond_inc = true;
3184 } else if (NeedToSubDividend(magic, imm)) {
3185 __ Subs(temp, temp, dividend);
3186 use_cond_inc = true;
3187 }
3188
3189 if (shift != 0) {
3190 __ Asr(temp, temp, shift);
3191 }
3192
3193 if (instruction->IsRem()) {
3194 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3195 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3196 } else {
3197 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3198 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003199}
3200
3201void InstructionCodeGeneratorARM64::GenerateInt32DivRemWithAnyConstant(
3202 HBinaryOperation* instruction) {
3203 DCHECK(instruction->IsDiv() || instruction->IsRem());
3204 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
3205
3206 LocationSummary* locations = instruction->GetLocations();
3207 Location second = locations->InAt(1);
3208 DCHECK(second.IsConstant());
3209
3210 Register out = OutputRegister(instruction);
3211 Register dividend = InputRegisterAt(instruction, 0);
3212 int64_t imm = Int64FromConstant(second.GetConstant());
3213
3214 int64_t magic;
3215 int shift;
3216 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
3217 UseScratchRegisterScope temps(GetVIXLAssembler());
3218 Register temp = temps.AcquireSameSizeAs(out);
3219
3220 // temp = get_high(dividend * magic)
3221 __ Mov(temp, magic);
3222 __ Smull(temp.X(), dividend, temp);
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003223
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003224 // The multiplication result might need some corrections to be finalized.
3225 // The last correction is to increment by 1, if the result is negative.
3226 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3227 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3228 // However if one of the corrections is ADD or SUB, the sign can be detected
3229 // with ADDS/SUBS. They set the N flag if the result is negative.
3230 // This allows to use CINC MI which has latency 1.
3231 bool use_cond_inc = false;
3232
3233 // ADD/SUB correction is performed in the high 32 bits
3234 // as high 32 bits are ignored because type are kInt32.
3235 if (NeedToAddDividend(magic, imm)) {
3236 __ Adds(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3237 use_cond_inc = true;
3238 } else if (NeedToSubDividend(magic, imm)) {
3239 __ Subs(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3240 use_cond_inc = true;
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003241 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003242
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003243 // Extract the result from the high 32 bits and apply the final right shift.
3244 DCHECK_LT(shift, 32);
3245 __ Asr(temp.X(), temp.X(), 32 + shift);
3246
3247 if (instruction->IsRem()) {
3248 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3249 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3250 } else {
3251 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3252 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003253}
3254
3255void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3256 DCHECK(instruction->IsDiv() || instruction->IsRem());
3257 if (instruction->GetResultType() == DataType::Type::kInt64) {
3258 GenerateInt64DivRemWithAnyConstant(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003259 } else {
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003260 GenerateInt32DivRemWithAnyConstant(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003261 }
3262}
3263
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003264void InstructionCodeGeneratorARM64::GenerateIntDivForConstDenom(HDiv *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003265 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Zheng Xuc6667102015-05-15 16:08:45 +08003266
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003267 if (imm == 0) {
3268 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3269 return;
3270 }
Zheng Xuc6667102015-05-15 16:08:45 +08003271
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003272 if (IsPowerOfTwo(AbsOrMin(imm))) {
3273 GenerateIntDivForPower2Denom(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003274 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003275 // Cases imm == -1 or imm == 1 are handled by InstructionSimplifier.
3276 DCHECK(imm < -2 || imm > 2) << imm;
3277 GenerateDivRemWithAnyConstant(instruction);
3278 }
3279}
3280
3281void InstructionCodeGeneratorARM64::GenerateIntDiv(HDiv *instruction) {
3282 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
3283 << instruction->GetResultType();
3284
3285 if (instruction->GetLocations()->InAt(1).IsConstant()) {
3286 GenerateIntDivForConstDenom(instruction);
3287 } else {
3288 Register out = OutputRegister(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003289 Register dividend = InputRegisterAt(instruction, 0);
3290 Register divisor = InputRegisterAt(instruction, 1);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003291 __ Sdiv(out, dividend, divisor);
Zheng Xuc6667102015-05-15 16:08:45 +08003292 }
3293}
3294
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003295void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3296 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003297 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003298 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003299 case DataType::Type::kInt32:
3300 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003301 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003302 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003303 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3304 break;
3305
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003306 case DataType::Type::kFloat32:
3307 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003308 locations->SetInAt(0, Location::RequiresFpuRegister());
3309 locations->SetInAt(1, Location::RequiresFpuRegister());
3310 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3311 break;
3312
3313 default:
3314 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3315 }
3316}
3317
3318void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003319 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003320 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003321 case DataType::Type::kInt32:
3322 case DataType::Type::kInt64:
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003323 GenerateIntDiv(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003324 break;
3325
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003326 case DataType::Type::kFloat32:
3327 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003328 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3329 break;
3330
3331 default:
3332 LOG(FATAL) << "Unexpected div type " << type;
3333 }
3334}
3335
Alexandre Rames67555f72014-11-18 10:55:16 +00003336void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003337 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003338 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003339}
3340
3341void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3342 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003343 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003344 codegen_->AddSlowPath(slow_path);
3345 Location value = instruction->GetLocations()->InAt(0);
3346
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003347 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003348
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003349 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003350 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Elliott Hughesc1896c92018-11-29 11:33:18 -08003351 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003352 }
3353
Alexandre Rames67555f72014-11-18 10:55:16 +00003354 if (value.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003355 int64_t divisor = Int64FromLocation(value);
Alexandre Rames67555f72014-11-18 10:55:16 +00003356 if (divisor == 0) {
3357 __ B(slow_path->GetEntryLabel());
3358 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003359 // A division by a non-null constant is valid. We don't need to perform
3360 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003361 }
3362 } else {
3363 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3364 }
3365}
3366
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003367void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3368 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003369 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003370 locations->SetOut(Location::ConstantLocation(constant));
3371}
3372
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003373void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3374 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003375 // Will be generated at use site.
3376}
3377
Alexandre Rames5319def2014-10-23 10:03:10 +01003378void LocationsBuilderARM64::VisitExit(HExit* exit) {
3379 exit->SetLocations(nullptr);
3380}
3381
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003382void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003383}
3384
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003385void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3386 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003387 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003388 locations->SetOut(Location::ConstantLocation(constant));
3389}
3390
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003391void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003392 // Will be generated at use site.
3393}
3394
David Brazdilfc6a86a2015-06-26 10:33:45 +00003395void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003396 if (successor->IsExitBlock()) {
3397 DCHECK(got->GetPrevious()->AlwaysThrows());
3398 return; // no code needed
3399 }
3400
Serban Constantinescu02164b32014-11-13 14:05:07 +00003401 HBasicBlock* block = got->GetBlock();
3402 HInstruction* previous = got->GetPrevious();
3403 HLoopInformation* info = block->GetLoopInformation();
3404
David Brazdil46e2a392015-03-16 17:31:52 +00003405 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00003406 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003407 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3408 return;
3409 }
3410 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3411 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08003412 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003413 }
3414 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003415 __ B(codegen_->GetLabelOf(successor));
3416 }
3417}
3418
David Brazdilfc6a86a2015-06-26 10:33:45 +00003419void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3420 got->SetLocations(nullptr);
3421}
3422
3423void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3424 HandleGoto(got, got->GetSuccessor());
3425}
3426
3427void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3428 try_boundary->SetLocations(nullptr);
3429}
3430
3431void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3432 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3433 if (!successor->IsExitBlock()) {
3434 HandleGoto(try_boundary, successor);
3435 }
3436}
3437
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003438void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003439 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003440 vixl::aarch64::Label* true_target,
3441 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003442 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003443
David Brazdil0debae72015-11-12 18:37:00 +00003444 if (true_target == nullptr && false_target == nullptr) {
3445 // Nothing to do. The code always falls through.
3446 return;
3447 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003448 // Constant condition, statically compared against "true" (integer value 1).
3449 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003450 if (true_target != nullptr) {
3451 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003452 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003453 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003454 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003455 if (false_target != nullptr) {
3456 __ B(false_target);
3457 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003458 }
David Brazdil0debae72015-11-12 18:37:00 +00003459 return;
3460 }
3461
3462 // The following code generates these patterns:
3463 // (1) true_target == nullptr && false_target != nullptr
3464 // - opposite condition true => branch to false_target
3465 // (2) true_target != nullptr && false_target == nullptr
3466 // - condition true => branch to true_target
3467 // (3) true_target != nullptr && false_target != nullptr
3468 // - condition true => branch to true_target
3469 // - branch to false_target
3470 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003471 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003472 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003473 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003474 if (true_target == nullptr) {
3475 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3476 } else {
3477 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3478 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003479 } else {
3480 // The condition instruction has not been materialized, use its inputs as
3481 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003482 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003483
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003484 DataType::Type type = condition->InputAt(0)->GetType();
3485 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003486 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003487 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003488 IfCondition opposite_condition = condition->GetOppositeCondition();
3489 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003490 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003491 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003492 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003493 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003494 // Integer cases.
3495 Register lhs = InputRegisterAt(condition, 0);
3496 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003497
3498 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003499 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003500 if (true_target == nullptr) {
3501 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3502 non_fallthrough_target = false_target;
3503 } else {
3504 arm64_cond = ARM64Condition(condition->GetCondition());
3505 non_fallthrough_target = true_target;
3506 }
3507
Aart Bik086d27e2016-01-20 17:02:00 -08003508 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003509 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003510 switch (arm64_cond) {
3511 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003512 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003513 break;
3514 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003515 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003516 break;
3517 case lt:
3518 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003519 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003520 break;
3521 case ge:
3522 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003523 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003524 break;
3525 default:
3526 // Without the `static_cast` the compiler throws an error for
3527 // `-Werror=sign-promo`.
3528 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3529 }
3530 } else {
3531 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003532 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003533 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003534 }
3535 }
David Brazdil0debae72015-11-12 18:37:00 +00003536
3537 // If neither branch falls through (case 3), the conditional branch to `true_target`
3538 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3539 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003540 __ B(false_target);
3541 }
3542}
3543
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003544void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003545 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003546 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003547 locations->SetInAt(0, Location::RequiresRegister());
3548 }
3549}
3550
3551void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003552 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3553 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003554 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3555 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3556 true_target = nullptr;
3557 }
3558 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3559 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3560 false_target = nullptr;
3561 }
Andreas Gampe3db70682018-12-26 15:12:03 -08003562 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003563}
3564
3565void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003566 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003567 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003568 InvokeRuntimeCallingConvention calling_convention;
3569 RegisterSet caller_saves = RegisterSet::Empty();
3570 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3571 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003572 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003573 locations->SetInAt(0, Location::RequiresRegister());
3574 }
3575}
3576
3577void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003578 SlowPathCodeARM64* slow_path =
3579 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003580 GenerateTestAndBranch(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08003581 /* condition_input_index= */ 0,
David Brazdil0debae72015-11-12 18:37:00 +00003582 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08003583 /* false_target= */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003584}
3585
Mingyao Yang063fc772016-08-02 11:02:54 -07003586void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003587 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003588 LocationSummary(flag, LocationSummary::kNoCall);
3589 locations->SetOut(Location::RequiresRegister());
3590}
3591
3592void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3593 __ Ldr(OutputRegister(flag),
3594 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3595}
3596
David Brazdilc0b601b2016-02-08 14:20:45 +00003597static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3598 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003599 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003600}
3601
Alexandre Rames880f1192016-06-13 16:04:50 +01003602static inline Condition GetConditionForSelect(HCondition* condition) {
3603 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003604 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3605 : ARM64Condition(cond);
3606}
3607
David Brazdil74eb1b22015-12-14 11:44:01 +00003608void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003609 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003610 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003611 locations->SetInAt(0, Location::RequiresFpuRegister());
3612 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003613 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003614 } else {
3615 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3616 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3617 bool is_true_value_constant = cst_true_value != nullptr;
3618 bool is_false_value_constant = cst_false_value != nullptr;
3619 // Ask VIXL whether we should synthesize constants in registers.
3620 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3621 Operand true_op = is_true_value_constant ?
3622 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3623 Operand false_op = is_false_value_constant ?
3624 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3625 bool true_value_in_register = false;
3626 bool false_value_in_register = false;
3627 MacroAssembler::GetCselSynthesisInformation(
3628 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3629 true_value_in_register |= !is_true_value_constant;
3630 false_value_in_register |= !is_false_value_constant;
3631
3632 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3633 : Location::ConstantLocation(cst_true_value));
3634 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3635 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003636 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003637 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003638
David Brazdil74eb1b22015-12-14 11:44:01 +00003639 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3640 locations->SetInAt(2, Location::RequiresRegister());
3641 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003642}
3643
3644void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003645 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003646 Condition csel_cond;
3647
3648 if (IsBooleanValueOrMaterializedCondition(cond)) {
3649 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003650 // Use the condition flags set by the previous instruction.
3651 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003652 } else {
3653 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003654 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003655 }
3656 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003657 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003658 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003659 } else {
3660 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003661 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003662 }
3663
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003664 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003665 __ Fcsel(OutputFPRegister(select),
3666 InputFPRegisterAt(select, 1),
3667 InputFPRegisterAt(select, 0),
3668 csel_cond);
3669 } else {
3670 __ Csel(OutputRegister(select),
3671 InputOperandAt(select, 1),
3672 InputOperandAt(select, 0),
3673 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003674 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003675}
3676
David Srbecky0cf44932015-12-09 14:09:59 +00003677void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003678 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00003679}
3680
David Srbeckyd28f4a02016-03-14 17:14:24 +00003681void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3682 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003683}
3684
Vladimir Markodec78172020-06-19 15:31:23 +01003685void CodeGeneratorARM64::IncreaseFrame(size_t adjustment) {
3686 __ Claim(adjustment);
3687 GetAssembler()->cfi().AdjustCFAOffset(adjustment);
3688}
3689
3690void CodeGeneratorARM64::DecreaseFrame(size_t adjustment) {
3691 __ Drop(adjustment);
3692 GetAssembler()->cfi().AdjustCFAOffset(-adjustment);
3693}
3694
David Srbeckyc7098ff2016-02-09 14:30:11 +00003695void CodeGeneratorARM64::GenerateNop() {
3696 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003697}
3698
Alexandre Rames5319def2014-10-23 10:03:10 +01003699void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003700 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003701}
3702
3703void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003704 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003705}
3706
3707void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003708 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003709}
3710
3711void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003712 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003713}
3714
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003715// Temp is used for read barrier.
3716static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3717 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003718 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003719 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3720 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3721 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3722 return 1;
3723 }
3724 return 0;
3725}
3726
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003727// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003728// interface pointer, one for loading the current interface.
3729// The other checks have one temp for loading the object's class.
3730static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3731 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3732 return 3;
3733 }
3734 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003735}
3736
Alexandre Rames67555f72014-11-18 10:55:16 +00003737void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003738 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003739 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003740 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003741 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003742 case TypeCheckKind::kExactCheck:
3743 case TypeCheckKind::kAbstractClassCheck:
3744 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00003745 case TypeCheckKind::kArrayObjectCheck: {
3746 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
3747 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
3748 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003749 break;
Vladimir Marko87584542017-12-12 17:47:52 +00003750 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003751 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003752 case TypeCheckKind::kUnresolvedCheck:
3753 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003754 call_kind = LocationSummary::kCallOnSlowPath;
3755 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00003756 case TypeCheckKind::kBitstringCheck:
3757 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003758 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003759
Vladimir Markoca6fff82017-10-03 14:49:14 +01003760 LocationSummary* locations =
3761 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003762 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003763 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003764 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003765 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003766 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3767 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3768 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3769 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3770 } else {
3771 locations->SetInAt(1, Location::RequiresRegister());
3772 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003773 // The "out" register is used as a temporary, so it overlaps with the inputs.
3774 // Note that TypeCheckSlowPathARM64 uses this register too.
3775 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003776 // Add temps if necessary for read barriers.
3777 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003778}
3779
3780void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003781 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003782 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003783 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003784 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003785 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3786 ? Register()
3787 : InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003788 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003789 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003790 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3791 DCHECK_LE(num_temps, 1u);
3792 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003793 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3794 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3795 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3796 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003797
Scott Wakeling97c72b72016-06-24 16:19:36 +01003798 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003799 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003800
3801 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003802 // Avoid null check if we know `obj` is not null.
3803 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003804 __ Cbz(obj, &zero);
3805 }
3806
Roland Levillain44015862016-01-22 11:47:17 +00003807 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003808 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003809 ReadBarrierOption read_barrier_option =
3810 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003811 // /* HeapReference<Class> */ out = obj->klass_
3812 GenerateReferenceLoadTwoRegisters(instruction,
3813 out_loc,
3814 obj_loc,
3815 class_offset,
3816 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003817 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003818 __ Cmp(out, cls);
3819 __ Cset(out, eq);
3820 if (zero.IsLinked()) {
3821 __ B(&done);
3822 }
3823 break;
3824 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003825
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003826 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003827 ReadBarrierOption read_barrier_option =
3828 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003829 // /* HeapReference<Class> */ out = obj->klass_
3830 GenerateReferenceLoadTwoRegisters(instruction,
3831 out_loc,
3832 obj_loc,
3833 class_offset,
3834 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003835 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003836 // If the class is abstract, we eagerly fetch the super class of the
3837 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003838 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003839 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003840 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003841 GenerateReferenceLoadOneRegister(instruction,
3842 out_loc,
3843 super_offset,
3844 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003845 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003846 // If `out` is null, we use it for the result, and jump to `done`.
3847 __ Cbz(out, &done);
3848 __ Cmp(out, cls);
3849 __ B(ne, &loop);
3850 __ Mov(out, 1);
3851 if (zero.IsLinked()) {
3852 __ B(&done);
3853 }
3854 break;
3855 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003856
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003857 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003858 ReadBarrierOption read_barrier_option =
3859 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003860 // /* HeapReference<Class> */ out = obj->klass_
3861 GenerateReferenceLoadTwoRegisters(instruction,
3862 out_loc,
3863 obj_loc,
3864 class_offset,
3865 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003866 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003867 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003868 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003869 __ Bind(&loop);
3870 __ Cmp(out, cls);
3871 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003872 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003873 GenerateReferenceLoadOneRegister(instruction,
3874 out_loc,
3875 super_offset,
3876 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003877 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003878 __ Cbnz(out, &loop);
3879 // If `out` is null, we use it for the result, and jump to `done`.
3880 __ B(&done);
3881 __ Bind(&success);
3882 __ Mov(out, 1);
3883 if (zero.IsLinked()) {
3884 __ B(&done);
3885 }
3886 break;
3887 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003888
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003889 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003890 ReadBarrierOption read_barrier_option =
3891 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003892 // /* HeapReference<Class> */ out = obj->klass_
3893 GenerateReferenceLoadTwoRegisters(instruction,
3894 out_loc,
3895 obj_loc,
3896 class_offset,
3897 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003898 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003899 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003900 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003901 __ Cmp(out, cls);
3902 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003903 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003904 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003905 GenerateReferenceLoadOneRegister(instruction,
3906 out_loc,
3907 component_offset,
3908 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003909 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003910 // If `out` is null, we use it for the result, and jump to `done`.
3911 __ Cbz(out, &done);
3912 __ Ldrh(out, HeapOperand(out, primitive_offset));
3913 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3914 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003915 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003916 __ Mov(out, 1);
3917 __ B(&done);
3918 break;
3919 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003920
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003921 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003922 // No read barrier since the slow path will retry upon failure.
3923 // /* HeapReference<Class> */ out = obj->klass_
3924 GenerateReferenceLoadTwoRegisters(instruction,
3925 out_loc,
3926 obj_loc,
3927 class_offset,
3928 maybe_temp_loc,
3929 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003930 __ Cmp(out, cls);
3931 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01003932 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08003933 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003934 codegen_->AddSlowPath(slow_path);
3935 __ B(ne, slow_path->GetEntryLabel());
3936 __ Mov(out, 1);
3937 if (zero.IsLinked()) {
3938 __ B(&done);
3939 }
3940 break;
3941 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003942
Calin Juravle98893e12015-10-02 21:05:03 +01003943 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003944 case TypeCheckKind::kInterfaceCheck: {
3945 // Note that we indeed only call on slow path, but we always go
3946 // into the slow path for the unresolved and interface check
3947 // cases.
3948 //
3949 // We cannot directly call the InstanceofNonTrivial runtime
3950 // entry point without resorting to a type checking slow path
3951 // here (i.e. by calling InvokeRuntime directly), as it would
3952 // require to assign fixed registers for the inputs of this
3953 // HInstanceOf instruction (following the runtime calling
3954 // convention), which might be cluttered by the potential first
3955 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003956 //
3957 // TODO: Introduce a new runtime entry point taking the object
3958 // to test (instead of its class) as argument, and let it deal
3959 // with the read barrier issues. This will let us refactor this
3960 // case of the `switch` code as it was previously (with a direct
3961 // call to the runtime not using a type checking slow path).
3962 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003963 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01003964 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08003965 instruction, /* is_fatal= */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003966 codegen_->AddSlowPath(slow_path);
3967 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003968 if (zero.IsLinked()) {
3969 __ B(&done);
3970 }
3971 break;
3972 }
Vladimir Marko175e7862018-03-27 09:03:13 +00003973
3974 case TypeCheckKind::kBitstringCheck: {
3975 // /* HeapReference<Class> */ temp = obj->klass_
3976 GenerateReferenceLoadTwoRegisters(instruction,
3977 out_loc,
3978 obj_loc,
3979 class_offset,
3980 maybe_temp_loc,
3981 kWithoutReadBarrier);
3982
3983 GenerateBitstringTypeCheckCompare(instruction, out);
3984 __ Cset(out, eq);
3985 if (zero.IsLinked()) {
3986 __ B(&done);
3987 }
3988 break;
3989 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003990 }
3991
3992 if (zero.IsLinked()) {
3993 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003994 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003995 }
3996
3997 if (done.IsLinked()) {
3998 __ Bind(&done);
3999 }
4000
4001 if (slow_path != nullptr) {
4002 __ Bind(slow_path->GetExitLabel());
4003 }
4004}
4005
4006void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004007 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00004008 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004009 LocationSummary* locations =
4010 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004011 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00004012 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
4013 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
4014 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
4015 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
4016 } else {
4017 locations->SetInAt(1, Location::RequiresRegister());
4018 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004019 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4020 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004021}
4022
4023void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004024 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004025 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004026 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004027 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00004028 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
4029 ? Register()
4030 : InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004031 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4032 DCHECK_GE(num_temps, 1u);
4033 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004034 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004035 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4036 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004037 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004038 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4039 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4040 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4041 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4042 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4043 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4044 const uint32_t object_array_data_offset =
4045 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004046
Vladimir Marko87584542017-12-12 17:47:52 +00004047 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004048 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004049 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4050 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004051 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004052
Scott Wakeling97c72b72016-06-24 16:19:36 +01004053 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004054 // Avoid null check if we know obj is not null.
4055 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004056 __ Cbz(obj, &done);
4057 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004058
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004059 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004060 case TypeCheckKind::kExactCheck:
4061 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004062 // /* HeapReference<Class> */ temp = obj->klass_
4063 GenerateReferenceLoadTwoRegisters(instruction,
4064 temp_loc,
4065 obj_loc,
4066 class_offset,
4067 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004068 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004069
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004070 __ Cmp(temp, cls);
4071 // Jump to slow path for throwing the exception or doing a
4072 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004073 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004074 break;
4075 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004076
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004077 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004078 // /* HeapReference<Class> */ temp = obj->klass_
4079 GenerateReferenceLoadTwoRegisters(instruction,
4080 temp_loc,
4081 obj_loc,
4082 class_offset,
4083 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004084 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004085
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004086 // If the class is abstract, we eagerly fetch the super class of the
4087 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004088 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004089 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004090 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004091 GenerateReferenceLoadOneRegister(instruction,
4092 temp_loc,
4093 super_offset,
4094 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004095 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004096
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004097 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4098 // exception.
4099 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4100 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004101 __ Cmp(temp, cls);
4102 __ B(ne, &loop);
4103 break;
4104 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004105
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004106 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004107 // /* HeapReference<Class> */ temp = obj->klass_
4108 GenerateReferenceLoadTwoRegisters(instruction,
4109 temp_loc,
4110 obj_loc,
4111 class_offset,
4112 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004113 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004114
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004115 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004116 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004117 __ Bind(&loop);
4118 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004119 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004120
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004121 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004122 GenerateReferenceLoadOneRegister(instruction,
4123 temp_loc,
4124 super_offset,
4125 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004126 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004127
4128 // If the class reference currently in `temp` is not null, jump
4129 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004130 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004131 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004132 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004133 break;
4134 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004135
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004136 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004137 // /* HeapReference<Class> */ temp = obj->klass_
4138 GenerateReferenceLoadTwoRegisters(instruction,
4139 temp_loc,
4140 obj_loc,
4141 class_offset,
4142 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004143 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004144
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004145 // Do an exact check.
4146 __ Cmp(temp, cls);
4147 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004148
4149 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004150 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004151 GenerateReferenceLoadOneRegister(instruction,
4152 temp_loc,
4153 component_offset,
4154 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004155 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004156
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004157 // If the component type is null, jump to the slow path to throw the exception.
4158 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4159 // Otherwise, the object is indeed an array. Further check that this component type is not a
4160 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004161 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4162 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004163 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004164 break;
4165 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004166
Calin Juravle98893e12015-10-02 21:05:03 +01004167 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004168 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004169 //
4170 // We cannot directly call the CheckCast runtime entry point
4171 // without resorting to a type checking slow path here (i.e. by
4172 // calling InvokeRuntime directly), as it would require to
4173 // assign fixed registers for the inputs of this HInstanceOf
4174 // instruction (following the runtime calling convention), which
4175 // might be cluttered by the potential first read barrier
4176 // emission at the beginning of this method.
4177 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004178 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004179 case TypeCheckKind::kInterfaceCheck: {
4180 // /* HeapReference<Class> */ temp = obj->klass_
4181 GenerateReferenceLoadTwoRegisters(instruction,
4182 temp_loc,
4183 obj_loc,
4184 class_offset,
4185 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004186 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004187
4188 // /* HeapReference<Class> */ temp = temp->iftable_
4189 GenerateReferenceLoadTwoRegisters(instruction,
4190 temp_loc,
4191 temp_loc,
4192 iftable_offset,
4193 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004194 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004195 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004196 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004197 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004198 vixl::aarch64::Label start_loop;
4199 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004200 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004201 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4202 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004203 // Go to next interface.
4204 __ Add(temp, temp, 2 * kHeapReferenceSize);
4205 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004206 // Compare the classes and continue the loop if they do not match.
4207 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4208 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004209 break;
4210 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004211
4212 case TypeCheckKind::kBitstringCheck: {
4213 // /* HeapReference<Class> */ temp = obj->klass_
4214 GenerateReferenceLoadTwoRegisters(instruction,
4215 temp_loc,
4216 obj_loc,
4217 class_offset,
4218 maybe_temp2_loc,
4219 kWithoutReadBarrier);
4220
4221 GenerateBitstringTypeCheckCompare(instruction, temp);
4222 __ B(ne, type_check_slow_path->GetEntryLabel());
4223 break;
4224 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004225 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004226 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004227
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004228 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004229}
4230
Alexandre Rames5319def2014-10-23 10:03:10 +01004231void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004232 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004233 locations->SetOut(Location::ConstantLocation(constant));
4234}
4235
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004236void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004237 // Will be generated at use site.
4238}
4239
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004240void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004241 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004242 locations->SetOut(Location::ConstantLocation(constant));
4243}
4244
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004245void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004246 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004247}
4248
Calin Juravle175dc732015-08-25 15:42:32 +01004249void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4250 // The trampoline uses the same calling convention as dex calling conventions,
4251 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4252 // the method_idx.
4253 HandleInvoke(invoke);
4254}
4255
4256void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4257 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004258 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004259}
4260
Alexandre Rames5319def2014-10-23 10:03:10 +01004261void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004262 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004263 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004264}
4265
Alexandre Rames67555f72014-11-18 10:55:16 +00004266void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4267 HandleInvoke(invoke);
4268}
4269
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004270void CodeGeneratorARM64::MaybeGenerateInlineCacheCheck(HInstruction* instruction,
4271 Register klass) {
4272 DCHECK_EQ(klass.GetCode(), 0u);
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004273 // We know the destination of an intrinsic, so no need to record inline
4274 // caches.
4275 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00004276 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004277 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004278 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
4279 ScopedObjectAccess soa(Thread::Current());
4280 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00004281 if (info != nullptr) {
4282 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
4283 uint64_t address = reinterpret_cast64<uint64_t>(cache);
4284 vixl::aarch64::Label done;
4285 __ Mov(x8, address);
4286 __ Ldr(x9, MemOperand(x8, InlineCache::ClassesOffset().Int32Value()));
4287 // Fast path for a monomorphic cache.
4288 __ Cmp(klass, x9);
4289 __ B(eq, &done);
4290 InvokeRuntime(kQuickUpdateInlineCache, instruction, instruction->GetDexPc());
4291 __ Bind(&done);
4292 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004293 }
4294}
4295
Alexandre Rames67555f72014-11-18 10:55:16 +00004296void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4297 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004298 LocationSummary* locations = invoke->GetLocations();
4299 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004300 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004301 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004302 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004303
Artem Serov914d7a82017-02-07 14:33:49 +00004304 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004305 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004306 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004307 {
4308 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4309 // /* HeapReference<Class> */ temp = temp->klass_
4310 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4311 codegen_->MaybeRecordImplicitNullCheck(invoke);
4312 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004313 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004314 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004315 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004316 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004317 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004318 }
Artem Serov914d7a82017-02-07 14:33:49 +00004319
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004320 // Instead of simply (possibly) unpoisoning `temp` here, we should
4321 // emit a read barrier for the previous class reference load.
4322 // However this is not required in practice, as this is an
4323 // intermediate/temporary reference and because the current
4324 // concurrent copying collector keeps the from-space memory
4325 // intact/accessible until the end of the marking phase (the
4326 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004327 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004328
4329 // If we're compiling baseline, update the inline cache.
4330 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
4331
4332 // The register ip1 is required to be used for the hidden argument in
4333 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
4334 MacroAssembler* masm = GetVIXLAssembler();
4335 UseScratchRegisterScope scratch_scope(masm);
4336 scratch_scope.Exclude(ip1);
4337 __ Mov(ip1, invoke->GetDexMethodIndex());
4338
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004339 __ Ldr(temp,
4340 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4341 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004342 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004343 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004344 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004345 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004346 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004347
4348 {
4349 // Ensure the pc position is recorded immediately after the `blr` instruction.
4350 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4351
4352 // lr();
4353 __ blr(lr);
4354 DCHECK(!codegen_->IsLeafMethod());
4355 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4356 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004357
Andreas Gampe3db70682018-12-26 15:12:03 -08004358 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004359}
4360
4361void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004362 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004363 if (intrinsic.TryDispatch(invoke)) {
4364 return;
4365 }
4366
Alexandre Rames67555f72014-11-18 10:55:16 +00004367 HandleInvoke(invoke);
4368}
4369
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004370void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004371 // Explicit clinit checks triggered by static invokes must have been pruned by
4372 // art::PrepareForRegisterAllocation.
4373 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004374
Vladimir Markoca6fff82017-10-03 14:49:14 +01004375 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004376 if (intrinsic.TryDispatch(invoke)) {
4377 return;
4378 }
4379
Vladimir Marko86c87522020-05-11 16:55:55 +01004380 if (invoke->GetCodePtrLocation() == HInvokeStaticOrDirect::CodePtrLocation::kCallCriticalNative) {
4381 CriticalNativeCallingConventionVisitorARM64 calling_convention_visitor(
4382 /*for_register_allocation=*/ true);
4383 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4384 } else {
4385 HandleInvoke(invoke);
4386 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004387}
4388
Andreas Gampe878d58c2015-01-15 23:24:00 -08004389static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4390 if (invoke->GetLocations()->Intrinsified()) {
4391 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4392 intrinsic.Dispatch(invoke);
4393 return true;
4394 }
4395 return false;
4396}
4397
Vladimir Markodc151b22015-10-15 18:02:30 +01004398HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4399 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004400 ArtMethod* method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004401 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004402 return desired_dispatch_info;
4403}
4404
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004405void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4406 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004407 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004408 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4409 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004410 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4411 uint32_t offset =
4412 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004413 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004414 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004415 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004416 }
Vladimir Marko58155012015-08-19 12:49:41 +00004417 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Marko86c87522020-05-11 16:55:55 +01004418 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004419 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004420 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01004421 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01004422 // Add ADRP with its PC-relative method patch.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004423 vixl::aarch64::Label* adrp_label = NewBootImageMethodPatch(invoke->GetTargetMethod());
Vladimir Marko65979462017-05-19 17:25:12 +01004424 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4425 // Add ADD with its PC-relative method patch.
4426 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004427 NewBootImageMethodPatch(invoke->GetTargetMethod(), adrp_label);
Vladimir Marko65979462017-05-19 17:25:12 +01004428 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4429 break;
4430 }
Vladimir Markob066d432018-01-03 13:14:37 +00004431 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4432 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004433 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00004434 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_offset);
4435 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4436 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
4437 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_offset, adrp_label);
4438 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
4439 EmitLdrOffsetPlaceholder(ldr_label, WRegisterFrom(temp), XRegisterFrom(temp));
4440 break;
4441 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004442 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Markob066d432018-01-03 13:14:37 +00004443 // Add ADRP with its PC-relative .bss entry patch.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004444 MethodReference target_method(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4445 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(target_method);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004446 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Markob066d432018-01-03 13:14:37 +00004447 // Add LDR with its PC-relative .bss entry patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004448 vixl::aarch64::Label* ldr_label =
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004449 NewMethodBssEntryPatch(target_method, adrp_label);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01004450 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004451 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004452 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004453 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004454 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
4455 // Load method address from literal pool.
4456 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
4457 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004458 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4459 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4460 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004461 }
4462 }
4463
Vladimir Marko86c87522020-05-11 16:55:55 +01004464 auto call_code_pointer_member = [&](MemberOffset offset) {
4465 // LR = callee_method->member;
4466 __ Ldr(lr, MemOperand(XRegisterFrom(callee_method), offset.Int32Value()));
4467 {
4468 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4469 ExactAssemblyScope eas(GetVIXLAssembler(),
4470 kInstructionSize,
4471 CodeBufferCheckScope::kExactSize);
4472 // lr()
4473 __ blr(lr);
4474 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4475 }
4476 };
Vladimir Marko58155012015-08-19 12:49:41 +00004477 switch (invoke->GetCodePtrLocation()) {
4478 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004479 {
4480 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4481 ExactAssemblyScope eas(GetVIXLAssembler(),
4482 kInstructionSize,
4483 CodeBufferCheckScope::kExactSize);
4484 __ bl(&frame_entry_label_);
4485 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4486 }
Vladimir Marko58155012015-08-19 12:49:41 +00004487 break;
Vladimir Marko86c87522020-05-11 16:55:55 +01004488 case HInvokeStaticOrDirect::CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01004489 size_t out_frame_size =
4490 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorARM64,
4491 kAapcs64StackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01004492 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Marko86c87522020-05-11 16:55:55 +01004493 call_code_pointer_member(ArtMethod::EntryPointFromJniOffset(kArm64PointerSize));
4494 // Zero-/sign-extend the result when needed due to native and managed ABI mismatch.
4495 switch (invoke->GetType()) {
4496 case DataType::Type::kBool:
4497 __ Ubfx(w0, w0, 0, 8);
4498 break;
4499 case DataType::Type::kInt8:
4500 __ Sbfx(w0, w0, 0, 8);
4501 break;
4502 case DataType::Type::kUint16:
4503 __ Ubfx(w0, w0, 0, 16);
4504 break;
4505 case DataType::Type::kInt16:
4506 __ Sbfx(w0, w0, 0, 16);
4507 break;
4508 case DataType::Type::kInt32:
4509 case DataType::Type::kInt64:
4510 case DataType::Type::kFloat32:
4511 case DataType::Type::kFloat64:
4512 case DataType::Type::kVoid:
4513 break;
4514 default:
4515 DCHECK(false) << invoke->GetType();
4516 break;
4517 }
4518 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01004519 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01004520 }
4521 break;
4522 }
4523 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4524 call_code_pointer_member(ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize));
Vladimir Marko58155012015-08-19 12:49:41 +00004525 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004526 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004527
Andreas Gampe878d58c2015-01-15 23:24:00 -08004528 DCHECK(!IsLeafMethod());
4529}
4530
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004531void CodeGeneratorARM64::GenerateVirtualCall(
4532 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004533 // Use the calling convention instead of the location of the receiver, as
4534 // intrinsics may have put the receiver in a different register. In the intrinsics
4535 // slow path, the arguments have been moved to the right place, so here we are
4536 // guaranteed that the receiver is the first register of the calling convention.
4537 InvokeDexCallingConvention calling_convention;
4538 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004539 Register temp = XRegisterFrom(temp_in);
4540 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4541 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4542 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004543 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004544
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004545 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004546
4547 {
4548 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4549 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4550 // /* HeapReference<Class> */ temp = receiver->klass_
4551 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4552 MaybeRecordImplicitNullCheck(invoke);
4553 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004554 // Instead of simply (possibly) unpoisoning `temp` here, we should
4555 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004556 // intermediate/temporary reference and because the current
4557 // concurrent copying collector keeps the from-space memory
4558 // intact/accessible until the end of the marking phase (the
4559 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004560 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004561
4562 // If we're compiling baseline, update the inline cache.
4563 MaybeGenerateInlineCacheCheck(invoke, temp);
4564
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004565 // temp = temp->GetMethodAt(method_offset);
4566 __ Ldr(temp, MemOperand(temp, method_offset));
4567 // lr = temp->GetEntryPoint();
4568 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004569 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004570 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004571 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4572 // lr();
4573 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004574 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004575 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004576}
4577
Vladimir Marko9922f002020-06-08 15:05:15 +01004578void CodeGeneratorARM64::MoveFromReturnRegister(Location trg, DataType::Type type) {
4579 if (!trg.IsValid()) {
4580 DCHECK(type == DataType::Type::kVoid);
4581 return;
4582 }
4583
4584 DCHECK_NE(type, DataType::Type::kVoid);
4585
4586 if (DataType::IsIntegralType(type) || type == DataType::Type::kReference) {
4587 Register trg_reg = RegisterFrom(trg, type);
4588 Register res_reg = RegisterFrom(ARM64ReturnLocation(type), type);
4589 __ Mov(trg_reg, res_reg, kDiscardForSameWReg);
4590 } else {
4591 VRegister trg_reg = FPRegisterFrom(trg, type);
4592 VRegister res_reg = FPRegisterFrom(ARM64ReturnLocation(type), type);
4593 __ Fmov(trg_reg, res_reg);
4594 }
4595}
4596
Orion Hodsonac141392017-01-13 11:53:47 +00004597void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4598 HandleInvoke(invoke);
4599}
4600
4601void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4602 codegen_->GenerateInvokePolymorphicCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004603 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004604}
4605
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004606void LocationsBuilderARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4607 HandleInvoke(invoke);
4608}
4609
4610void InstructionCodeGeneratorARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4611 codegen_->GenerateInvokeCustomCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004612 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004613}
4614
Vladimir Marko6fd16062018-06-26 11:02:04 +01004615vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageIntrinsicPatch(
4616 uint32_t intrinsic_data,
4617 vixl::aarch64::Label* adrp_label) {
4618 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004619 /* dex_file= */ nullptr, intrinsic_data, adrp_label, &boot_image_other_patches_);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004620}
4621
Vladimir Markob066d432018-01-03 13:14:37 +00004622vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageRelRoPatch(
4623 uint32_t boot_image_offset,
4624 vixl::aarch64::Label* adrp_label) {
4625 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004626 /* dex_file= */ nullptr, boot_image_offset, adrp_label, &boot_image_other_patches_);
Vladimir Markob066d432018-01-03 13:14:37 +00004627}
4628
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004629vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004630 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004631 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004632 return NewPcRelativePatch(
4633 target_method.dex_file, target_method.index, adrp_label, &boot_image_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004634}
4635
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004636vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4637 MethodReference target_method,
4638 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004639 return NewPcRelativePatch(
4640 target_method.dex_file, target_method.index, adrp_label, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004641}
4642
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004643vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageTypePatch(
Scott Wakeling97c72b72016-06-24 16:19:36 +01004644 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004645 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004646 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004647 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &boot_image_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004648}
4649
Vladimir Marko1998cd02017-01-13 13:02:58 +00004650vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4651 const DexFile& dex_file,
4652 dex::TypeIndex type_index,
4653 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004654 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004655}
4656
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004657vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004658 const DexFile& dex_file,
4659 dex::StringIndex string_index,
4660 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004661 return NewPcRelativePatch(
4662 &dex_file, string_index.index_, adrp_label, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01004663}
4664
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004665vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4666 const DexFile& dex_file,
4667 dex::StringIndex string_index,
4668 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004669 return NewPcRelativePatch(&dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004670}
4671
Vladimir Markof6675082019-05-17 12:05:28 +01004672void CodeGeneratorARM64::EmitEntrypointThunkCall(ThreadOffset64 entrypoint_offset) {
4673 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01004674 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markof6675082019-05-17 12:05:28 +01004675 call_entrypoint_patches_.emplace_back(/*dex_file*/ nullptr, entrypoint_offset.Uint32Value());
4676 vixl::aarch64::Label* bl_label = &call_entrypoint_patches_.back().label;
4677 __ bind(bl_label);
4678 __ bl(static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4679}
4680
Vladimir Marko966b46f2018-08-03 10:20:19 +00004681void CodeGeneratorARM64::EmitBakerReadBarrierCbnz(uint32_t custom_data) {
Vladimir Marko94796f82018-08-08 15:15:33 +01004682 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01004683 if (GetCompilerOptions().IsJitCompiler()) {
Vladimir Marko966b46f2018-08-03 10:20:19 +00004684 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
4685 vixl::aarch64::Label* slow_path_entry = &it->second.label;
4686 __ cbnz(mr, slow_path_entry);
4687 } else {
4688 baker_read_barrier_patches_.emplace_back(custom_data);
4689 vixl::aarch64::Label* cbnz_label = &baker_read_barrier_patches_.back().label;
4690 __ bind(cbnz_label);
4691 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4692 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004693}
4694
Scott Wakeling97c72b72016-06-24 16:19:36 +01004695vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004696 const DexFile* dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004697 uint32_t offset_or_index,
4698 vixl::aarch64::Label* adrp_label,
4699 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004700 // Add a patch entry and return the label.
4701 patches->emplace_back(dex_file, offset_or_index);
4702 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004703 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004704 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4705 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4706 return label;
4707}
4708
Scott Wakeling97c72b72016-06-24 16:19:36 +01004709vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4710 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004711 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004712}
4713
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004714vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004715 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004716 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004717 return jit_string_patches_.GetOrCreate(
4718 StringReference(&dex_file, string_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004719 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004720}
4721
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004722vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004723 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004724 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004725 return jit_class_patches_.GetOrCreate(
4726 TypeReference(&dex_file, type_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004727 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004728}
4729
Vladimir Markoaad75c62016-10-03 08:46:48 +00004730void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4731 vixl::aarch64::Register reg) {
4732 DCHECK(reg.IsX());
4733 SingleEmissionCheckScope guard(GetVIXLAssembler());
4734 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004735 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004736}
4737
4738void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4739 vixl::aarch64::Register out,
4740 vixl::aarch64::Register base) {
4741 DCHECK(out.IsX());
4742 DCHECK(base.IsX());
4743 SingleEmissionCheckScope guard(GetVIXLAssembler());
4744 __ Bind(fixup_label);
4745 __ add(out, base, Operand(/* offset placeholder */ 0));
4746}
4747
4748void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4749 vixl::aarch64::Register out,
4750 vixl::aarch64::Register base) {
4751 DCHECK(base.IsX());
4752 SingleEmissionCheckScope guard(GetVIXLAssembler());
4753 __ Bind(fixup_label);
4754 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4755}
4756
Vladimir Markoeebb8212018-06-05 14:57:24 +01004757void CodeGeneratorARM64::LoadBootImageAddress(vixl::aarch64::Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01004758 uint32_t boot_image_reference) {
4759 if (GetCompilerOptions().IsBootImage()) {
4760 // Add ADRP with its PC-relative type patch.
4761 vixl::aarch64::Label* adrp_label = NewBootImageIntrinsicPatch(boot_image_reference);
4762 EmitAdrpPlaceholder(adrp_label, reg.X());
4763 // Add ADD with its PC-relative type patch.
4764 vixl::aarch64::Label* add_label = NewBootImageIntrinsicPatch(boot_image_reference, adrp_label);
4765 EmitAddPlaceholder(add_label, reg.X(), reg.X());
Vladimir Markoa2da9b92018-10-10 14:21:55 +01004766 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01004767 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004768 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004769 EmitAdrpPlaceholder(adrp_label, reg.X());
4770 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004771 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_reference, adrp_label);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004772 EmitLdrOffsetPlaceholder(ldr_label, reg.W(), reg.X());
4773 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01004774 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01004775 gc::Heap* heap = Runtime::Current()->GetHeap();
4776 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004777 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01004778 __ Ldr(reg.W(), DeduplicateBootImageAddressLiteral(reinterpret_cast<uintptr_t>(address)));
4779 }
4780}
4781
Vladimir Marko6fd16062018-06-26 11:02:04 +01004782void CodeGeneratorARM64::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
4783 uint32_t boot_image_offset) {
4784 DCHECK(invoke->IsStatic());
4785 InvokeRuntimeCallingConvention calling_convention;
4786 Register argument = calling_convention.GetRegisterAt(0);
4787 if (GetCompilerOptions().IsBootImage()) {
4788 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
4789 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
4790 MethodReference target_method = invoke->GetTargetMethod();
4791 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
4792 // Add ADRP with its PC-relative type patch.
4793 vixl::aarch64::Label* adrp_label = NewBootImageTypePatch(*target_method.dex_file, type_idx);
4794 EmitAdrpPlaceholder(adrp_label, argument.X());
4795 // Add ADD with its PC-relative type patch.
4796 vixl::aarch64::Label* add_label =
4797 NewBootImageTypePatch(*target_method.dex_file, type_idx, adrp_label);
4798 EmitAddPlaceholder(add_label, argument.X(), argument.X());
4799 } else {
4800 LoadBootImageAddress(argument, boot_image_offset);
4801 }
4802 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
4803 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4804}
4805
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004806template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004807inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4808 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004809 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004810 for (const PcRelativePatchInfo& info : infos) {
4811 linker_patches->push_back(Factory(info.label.GetLocation(),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004812 info.target_dex_file,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004813 info.pc_insn_label->GetLocation(),
4814 info.offset_or_index));
4815 }
4816}
4817
Vladimir Marko6fd16062018-06-26 11:02:04 +01004818template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
4819linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
4820 const DexFile* target_dex_file,
4821 uint32_t pc_insn_offset,
4822 uint32_t boot_image_offset) {
4823 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
4824 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00004825}
4826
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004827void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004828 DCHECK(linker_patches->empty());
4829 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004830 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004831 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004832 boot_image_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004833 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004834 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004835 string_bss_entry_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01004836 boot_image_other_patches_.size() +
Vladimir Markof6675082019-05-17 12:05:28 +01004837 call_entrypoint_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004838 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004839 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01004840 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004841 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004842 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004843 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004844 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004845 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004846 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01004847 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01004848 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004849 DCHECK(boot_image_type_patches_.empty());
4850 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01004851 }
4852 if (GetCompilerOptions().IsBootImage()) {
4853 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
4854 boot_image_other_patches_, linker_patches);
4855 } else {
4856 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
4857 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004858 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004859 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4860 method_bss_entry_patches_, linker_patches);
4861 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4862 type_bss_entry_patches_, linker_patches);
4863 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4864 string_bss_entry_patches_, linker_patches);
Vladimir Markof6675082019-05-17 12:05:28 +01004865 for (const PatchInfo<vixl::aarch64::Label>& info : call_entrypoint_patches_) {
4866 DCHECK(info.target_dex_file == nullptr);
4867 linker_patches->push_back(linker::LinkerPatch::CallEntrypointPatch(
4868 info.label.GetLocation(), info.offset_or_index));
4869 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004870 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004871 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
4872 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004873 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004874 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004875}
4876
Vladimir Markoca1e0382018-04-11 09:58:41 +00004877bool CodeGeneratorARM64::NeedsThunkCode(const linker::LinkerPatch& patch) const {
Vladimir Markof6675082019-05-17 12:05:28 +01004878 return patch.GetType() == linker::LinkerPatch::Type::kCallEntrypoint ||
4879 patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
Vladimir Markoca1e0382018-04-11 09:58:41 +00004880 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
4881}
4882
4883void CodeGeneratorARM64::EmitThunkCode(const linker::LinkerPatch& patch,
4884 /*out*/ ArenaVector<uint8_t>* code,
4885 /*out*/ std::string* debug_name) {
4886 Arm64Assembler assembler(GetGraph()->GetAllocator());
4887 switch (patch.GetType()) {
4888 case linker::LinkerPatch::Type::kCallRelative: {
4889 // The thunk just uses the entry point in the ArtMethod. This works even for calls
4890 // to the generic JNI and interpreter trampolines.
4891 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4892 kArm64PointerSize).Int32Value());
4893 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
4894 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
4895 *debug_name = "MethodCallThunk";
4896 }
4897 break;
4898 }
Vladimir Markof6675082019-05-17 12:05:28 +01004899 case linker::LinkerPatch::Type::kCallEntrypoint: {
4900 Offset offset(patch.EntrypointOffset());
4901 assembler.JumpTo(ManagedRegister(arm64::TR), offset, ManagedRegister(arm64::IP0));
4902 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
4903 *debug_name = "EntrypointCallThunk_" + std::to_string(offset.Uint32Value());
4904 }
4905 break;
4906 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00004907 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
4908 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
4909 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
4910 break;
4911 }
4912 default:
4913 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
4914 UNREACHABLE();
4915 }
4916
4917 // Ensure we emit the literal pool if any.
4918 assembler.FinalizeCode();
4919 code->resize(assembler.CodeSize());
4920 MemoryRegion code_region(code->data(), code->size());
4921 assembler.FinalizeInstructions(code_region);
4922}
4923
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004924vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
4925 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004926 value,
4927 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4928}
4929
Scott Wakeling97c72b72016-06-24 16:19:36 +01004930vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004931 return uint64_literals_.GetOrCreate(
4932 value,
4933 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004934}
4935
Andreas Gampe878d58c2015-01-15 23:24:00 -08004936void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004937 // Explicit clinit checks triggered by static invokes must have been pruned by
4938 // art::PrepareForRegisterAllocation.
4939 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004940
Andreas Gampe878d58c2015-01-15 23:24:00 -08004941 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08004942 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004943 return;
4944 }
4945
Vladimir Marko86c87522020-05-11 16:55:55 +01004946 LocationSummary* locations = invoke->GetLocations();
4947 codegen_->GenerateStaticOrDirectCall(
4948 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004949
Andreas Gampe3db70682018-12-26 15:12:03 -08004950 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004951}
4952
4953void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004954 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08004955 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004956 return;
4957 }
4958
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004959 {
4960 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4961 // are no pools emitted.
4962 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4963 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
4964 DCHECK(!codegen_->IsLeafMethod());
4965 }
4966
Andreas Gampe3db70682018-12-26 15:12:03 -08004967 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004968}
4969
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004970HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4971 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004972 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004973 case HLoadClass::LoadKind::kInvalid:
4974 LOG(FATAL) << "UNREACHABLE";
4975 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004976 case HLoadClass::LoadKind::kReferrersClass:
4977 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004978 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004979 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004980 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01004981 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004982 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004983 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004984 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01004985 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004986 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004987 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004988 break;
4989 }
4990 return desired_class_load_kind;
4991}
4992
Alexandre Rames67555f72014-11-18 10:55:16 +00004993void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004994 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004995 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004996 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004997 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004998 cls,
4999 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005000 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00005001 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005002 return;
5003 }
Vladimir Marko41559982017-01-06 14:04:23 +00005004 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005005
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005006 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5007 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005008 ? LocationSummary::kCallOnSlowPath
5009 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005010 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005011 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005012 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005013 }
5014
Vladimir Marko41559982017-01-06 14:04:23 +00005015 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005016 locations->SetInAt(0, Location::RequiresRegister());
5017 }
5018 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005019 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
5020 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5021 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005022 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005023 } else {
5024 // For non-Baker read barrier we have a temp-clobbering call.
5025 }
5026 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005027}
5028
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005029// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5030// move.
5031void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005032 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005033 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005034 codegen_->GenerateLoadClassRuntimeCall(cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08005035 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01005036 return;
5037 }
Vladimir Marko41559982017-01-06 14:04:23 +00005038 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005039
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005040 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01005041 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00005042
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005043 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5044 ? kWithoutReadBarrier
5045 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005046 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005047 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005048 case HLoadClass::LoadKind::kReferrersClass: {
5049 DCHECK(!cls->CanCallRuntime());
5050 DCHECK(!cls->MustGenerateClinitCheck());
5051 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5052 Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00005053 codegen_->GenerateGcRootFieldLoad(cls,
5054 out_loc,
5055 current_method,
5056 ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005057 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005058 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005059 break;
5060 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005061 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005062 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5063 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005064 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005065 // Add ADRP with its PC-relative type patch.
5066 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08005067 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005068 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005069 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005070 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005071 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005072 codegen_->NewBootImageTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005073 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005074 break;
5075 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005076 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005077 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005078 uint32_t boot_image_offset = codegen_->GetBootImageOffset(cls);
5079 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5080 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005081 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005082 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005083 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005084 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005085 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005086 break;
5087 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005088 case HLoadClass::LoadKind::kBssEntry: {
5089 // Add ADRP with its PC-relative Class .bss entry patch.
5090 const DexFile& dex_file = cls->GetDexFile();
5091 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00005092 vixl::aarch64::Register temp = XRegisterFrom(out_loc);
5093 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
5094 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005095 // Add LDR with its PC-relative Class .bss entry patch.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005096 vixl::aarch64::Label* ldr_label =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005097 codegen_->NewBssEntryTypePatch(dex_file, type_index, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005098 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005099 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005100 codegen_->GenerateGcRootFieldLoad(cls,
5101 out_loc,
5102 temp,
5103 /* offset placeholder */ 0u,
5104 ldr_label,
5105 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005106 generate_null_check = true;
5107 break;
5108 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005109 case HLoadClass::LoadKind::kJitBootImageAddress: {
5110 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5111 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
5112 DCHECK_NE(address, 0u);
5113 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5114 break;
5115 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005116 case HLoadClass::LoadKind::kJitTableAddress: {
5117 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5118 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005119 cls->GetClass()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005120 codegen_->GenerateGcRootFieldLoad(cls,
5121 out_loc,
5122 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005123 /* offset= */ 0,
5124 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005125 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005126 break;
5127 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005128 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005129 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005130 LOG(FATAL) << "UNREACHABLE";
5131 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005132 }
5133
Vladimir Markoea4c1262017-02-06 19:59:33 +00005134 bool do_clinit = cls->MustGenerateClinitCheck();
5135 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005136 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01005137 SlowPathCodeARM64* slow_path =
5138 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005139 codegen_->AddSlowPath(slow_path);
5140 if (generate_null_check) {
5141 __ Cbz(out, slow_path->GetEntryLabel());
5142 }
5143 if (cls->MustGenerateClinitCheck()) {
5144 GenerateClassInitializationCheck(slow_path, out);
5145 } else {
5146 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005147 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005148 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005149 }
5150}
5151
Orion Hodsondbaa5c72018-05-10 08:22:46 +01005152void LocationsBuilderARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5153 InvokeRuntimeCallingConvention calling_convention;
5154 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5155 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
5156}
5157
5158void InstructionCodeGeneratorARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5159 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
5160}
5161
Orion Hodson18259d72018-04-12 11:18:23 +01005162void LocationsBuilderARM64::VisitLoadMethodType(HLoadMethodType* load) {
5163 InvokeRuntimeCallingConvention calling_convention;
5164 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5165 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
5166}
5167
5168void InstructionCodeGeneratorARM64::VisitLoadMethodType(HLoadMethodType* load) {
5169 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
5170}
5171
David Brazdilcb1c0552015-08-04 16:22:25 +01005172static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005173 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005174}
5175
Alexandre Rames67555f72014-11-18 10:55:16 +00005176void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5177 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005178 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00005179 locations->SetOut(Location::RequiresRegister());
5180}
5181
5182void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005183 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5184}
5185
5186void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005187 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005188}
5189
5190void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5191 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005192}
5193
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005194HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5195 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005196 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005197 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005198 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005199 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01005200 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005201 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005202 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005203 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01005204 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005205 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005206 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005207 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005208 }
5209 return desired_string_load_kind;
5210}
5211
Alexandre Rames67555f72014-11-18 10:55:16 +00005212void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005213 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005214 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005215 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005216 InvokeRuntimeCallingConvention calling_convention;
5217 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5218 } else {
5219 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005220 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5221 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005222 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005223 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005224 } else {
5225 // For non-Baker read barrier we have a temp-clobbering call.
5226 }
5227 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005228 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005229}
5230
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005231// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5232// move.
5233void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005234 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005235 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005236
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005237 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005238 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005239 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5240 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005241 // Add ADRP with its PC-relative String patch.
5242 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005243 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005244 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005245 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005246 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005247 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005248 codegen_->NewBootImageStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005249 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005250 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005251 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005252 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005253 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005254 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5255 uint32_t boot_image_offset = codegen_->GetBootImageOffset(load);
5256 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005257 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005258 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005259 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005260 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005261 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
5262 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005263 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005264 case HLoadString::LoadKind::kBssEntry: {
5265 // Add ADRP with its PC-relative String .bss entry patch.
5266 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005267 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00005268 Register temp = XRegisterFrom(out_loc);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005269 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005270 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005271 // Add LDR with its PC-relative String .bss entry patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005272 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005273 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005274 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005275 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005276 codegen_->GenerateGcRootFieldLoad(load,
5277 out_loc,
5278 temp,
5279 /* offset placeholder */ 0u,
5280 ldr_label,
5281 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005282 SlowPathCodeARM64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005283 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005284 codegen_->AddSlowPath(slow_path);
5285 __ Cbz(out.X(), slow_path->GetEntryLabel());
5286 __ Bind(slow_path->GetExitLabel());
Andreas Gampe3db70682018-12-26 15:12:03 -08005287 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005288 return;
5289 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005290 case HLoadString::LoadKind::kJitBootImageAddress: {
5291 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
5292 DCHECK_NE(address, 0u);
5293 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5294 return;
5295 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005296 case HLoadString::LoadKind::kJitTableAddress: {
5297 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005298 load->GetStringIndex(),
5299 load->GetString()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005300 codegen_->GenerateGcRootFieldLoad(load,
5301 out_loc,
5302 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005303 /* offset= */ 0,
5304 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005305 kCompilerReadBarrierOption);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005306 return;
5307 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005308 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005309 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005310 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005311
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005312 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005313 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005314 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005315 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005316 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5317 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005318 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005319}
5320
Alexandre Rames5319def2014-10-23 10:03:10 +01005321void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005322 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01005323 locations->SetOut(Location::ConstantLocation(constant));
5324}
5325
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005326void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005327 // Will be generated at use site.
5328}
5329
Alexandre Rames67555f72014-11-18 10:55:16 +00005330void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005331 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5332 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005333 InvokeRuntimeCallingConvention calling_convention;
5334 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5335}
5336
5337void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005338 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005339 instruction,
5340 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005341 if (instruction->IsEnter()) {
5342 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5343 } else {
5344 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5345 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005346 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005347}
5348
Alexandre Rames42d641b2014-10-27 14:00:51 +00005349void LocationsBuilderARM64::VisitMul(HMul* mul) {
5350 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005351 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005352 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005353 case DataType::Type::kInt32:
5354 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005355 locations->SetInAt(0, Location::RequiresRegister());
5356 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005357 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005358 break;
5359
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005360 case DataType::Type::kFloat32:
5361 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005362 locations->SetInAt(0, Location::RequiresFpuRegister());
5363 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005364 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005365 break;
5366
5367 default:
5368 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5369 }
5370}
5371
5372void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5373 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005374 case DataType::Type::kInt32:
5375 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005376 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5377 break;
5378
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005379 case DataType::Type::kFloat32:
5380 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005381 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005382 break;
5383
5384 default:
5385 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5386 }
5387}
5388
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005389void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5390 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005391 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005392 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005393 case DataType::Type::kInt32:
5394 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005395 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005396 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005397 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005398
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005399 case DataType::Type::kFloat32:
5400 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005401 locations->SetInAt(0, Location::RequiresFpuRegister());
5402 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005403 break;
5404
5405 default:
5406 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5407 }
5408}
5409
5410void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5411 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005412 case DataType::Type::kInt32:
5413 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005414 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5415 break;
5416
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005417 case DataType::Type::kFloat32:
5418 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005419 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005420 break;
5421
5422 default:
5423 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5424 }
5425}
5426
5427void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005428 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5429 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005430 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005431 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005432 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5433 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005434}
5435
5436void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005437 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5438 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005439 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005440 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005441 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005442}
5443
Alexandre Rames5319def2014-10-23 10:03:10 +01005444void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005445 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5446 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005447 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07005448 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005449 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005450}
5451
5452void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005453 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5454 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005455 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005456}
5457
5458void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005459 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005460 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005461 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005462}
5463
5464void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005465 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005466 case DataType::Type::kInt32:
5467 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005468 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005469 break;
5470
5471 default:
5472 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5473 }
5474}
5475
David Brazdil66d126e2015-04-03 16:02:44 +01005476void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005477 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005478 locations->SetInAt(0, Location::RequiresRegister());
5479 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5480}
5481
5482void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005483 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005484}
5485
Alexandre Rames5319def2014-10-23 10:03:10 +01005486void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005487 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5488 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005489}
5490
Calin Juravle2ae48182016-03-16 14:05:09 +00005491void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5492 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005493 return;
5494 }
Artem Serov914d7a82017-02-07 14:33:49 +00005495 {
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01005496 // Ensure that between load and RecordPcInfo there are no pools emitted.
Artem Serov914d7a82017-02-07 14:33:49 +00005497 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5498 Location obj = instruction->GetLocations()->InAt(0);
5499 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5500 RecordPcInfo(instruction, instruction->GetDexPc());
5501 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005502}
5503
Calin Juravle2ae48182016-03-16 14:05:09 +00005504void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005505 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005506 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005507
5508 LocationSummary* locations = instruction->GetLocations();
5509 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005510
5511 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005512}
5513
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005514void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005515 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005516}
5517
Alexandre Rames67555f72014-11-18 10:55:16 +00005518void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5519 HandleBinaryOp(instruction);
5520}
5521
5522void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5523 HandleBinaryOp(instruction);
5524}
5525
Alexandre Rames3e69f162014-12-10 10:36:50 +00005526void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5527 LOG(FATAL) << "Unreachable";
5528}
5529
5530void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005531 if (instruction->GetNext()->IsSuspendCheck() &&
5532 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5533 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5534 // The back edge will generate the suspend check.
5535 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5536 }
5537
Alexandre Rames3e69f162014-12-10 10:36:50 +00005538 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5539}
5540
Alexandre Rames5319def2014-10-23 10:03:10 +01005541void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005542 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005543 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5544 if (location.IsStackSlot()) {
5545 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5546 } else if (location.IsDoubleStackSlot()) {
5547 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5548 }
5549 locations->SetOut(location);
5550}
5551
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005552void InstructionCodeGeneratorARM64::VisitParameterValue(
5553 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005554 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005555}
5556
5557void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5558 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005559 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005560 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005561}
5562
5563void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5564 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5565 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005566}
5567
5568void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005569 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005570 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005571 locations->SetInAt(i, Location::Any());
5572 }
5573 locations->SetOut(Location::Any());
5574}
5575
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005576void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005577 LOG(FATAL) << "Unreachable";
5578}
5579
Serban Constantinescu02164b32014-11-13 14:05:07 +00005580void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005581 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005582 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005583 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005584 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005585 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005586
5587 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005588 case DataType::Type::kInt32:
5589 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005590 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005591 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005592 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5593 break;
5594
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005595 case DataType::Type::kFloat32:
5596 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005597 InvokeRuntimeCallingConvention calling_convention;
5598 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5599 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5600 locations->SetOut(calling_convention.GetReturnLocation(type));
5601
5602 break;
5603 }
5604
Serban Constantinescu02164b32014-11-13 14:05:07 +00005605 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005606 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005607 }
5608}
5609
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005610void InstructionCodeGeneratorARM64::GenerateIntRemForPower2Denom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005611 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005612 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
5613 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
5614
5615 Register out = OutputRegister(instruction);
5616 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005617
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01005618 if (HasNonNegativeResultOrMinInt(instruction->GetLeft())) {
5619 // No need to adjust the result for non-negative dividends or the INT32_MIN/INT64_MIN dividends.
5620 // NOTE: The generated code for HRem correctly works for the INT32_MIN/INT64_MIN dividends.
5621 // INT*_MIN % imm must be 0 for any imm of power 2. 'and' works only with bits
5622 // 0..30 (Int32 case)/0..62 (Int64 case) of a dividend. For INT32_MIN/INT64_MIN they are zeros.
5623 // So 'and' always produces zero.
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005624 __ And(out, dividend, abs_imm - 1);
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01005625 } else {
5626 if (abs_imm == 2) {
5627 __ Cmp(dividend, 0);
5628 __ And(out, dividend, 1);
5629 __ Csneg(out, out, out, ge);
5630 } else {
5631 UseScratchRegisterScope temps(GetVIXLAssembler());
5632 Register temp = temps.AcquireSameSizeAs(out);
5633
5634 __ Negs(temp, dividend);
5635 __ And(out, dividend, abs_imm - 1);
5636 __ And(temp, temp, abs_imm - 1);
5637 __ Csneg(out, out, temp, mi);
5638 }
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005639 }
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005640}
5641
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005642void InstructionCodeGeneratorARM64::GenerateIntRemForConstDenom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005643 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005644
5645 if (imm == 0) {
5646 // Do not generate anything.
5647 // DivZeroCheck would prevent any code to be executed.
5648 return;
5649 }
5650
Evgeny Astigeevichf58dc652018-06-25 17:54:07 +01005651 if (IsPowerOfTwo(AbsOrMin(imm))) {
5652 // Cases imm == -1 or imm == 1 are handled in constant folding by
5653 // InstructionWithAbsorbingInputSimplifier.
5654 // If the cases have survided till code generation they are handled in
5655 // GenerateIntRemForPower2Denom becauses -1 and 1 are the power of 2 (2^0).
5656 // The correct code is generated for them, just more instructions.
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005657 GenerateIntRemForPower2Denom(instruction);
5658 } else {
5659 DCHECK(imm < -2 || imm > 2) << imm;
5660 GenerateDivRemWithAnyConstant(instruction);
5661 }
5662}
5663
5664void InstructionCodeGeneratorARM64::GenerateIntRem(HRem* instruction) {
5665 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
5666 << instruction->GetResultType();
5667
5668 if (instruction->GetLocations()->InAt(1).IsConstant()) {
5669 GenerateIntRemForConstDenom(instruction);
5670 } else {
5671 Register out = OutputRegister(instruction);
5672 Register dividend = InputRegisterAt(instruction, 0);
5673 Register divisor = InputRegisterAt(instruction, 1);
5674 UseScratchRegisterScope temps(GetVIXLAssembler());
5675 Register temp = temps.AcquireSameSizeAs(out);
5676 __ Sdiv(temp, dividend, divisor);
5677 __ Msub(out, temp, divisor, dividend);
5678 }
5679}
5680
Serban Constantinescu02164b32014-11-13 14:05:07 +00005681void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005682 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005683
Serban Constantinescu02164b32014-11-13 14:05:07 +00005684 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005685 case DataType::Type::kInt32:
5686 case DataType::Type::kInt64: {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005687 GenerateIntRem(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005688 break;
5689 }
5690
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005691 case DataType::Type::kFloat32:
5692 case DataType::Type::kFloat64: {
5693 QuickEntrypointEnum entrypoint =
5694 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005695 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005696 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00005697 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5698 } else {
5699 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5700 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005701 break;
5702 }
5703
Serban Constantinescu02164b32014-11-13 14:05:07 +00005704 default:
5705 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005706 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005707 }
5708}
5709
Aart Bik1f8d51b2018-02-15 10:42:37 -08005710void LocationsBuilderARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005711 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005712}
5713
Aart Bik1f8d51b2018-02-15 10:42:37 -08005714void InstructionCodeGeneratorARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005715 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005716}
5717
5718void LocationsBuilderARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005719 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005720}
5721
5722void InstructionCodeGeneratorARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005723 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005724}
5725
Aart Bik3dad3412018-02-28 12:01:46 -08005726void LocationsBuilderARM64::VisitAbs(HAbs* abs) {
5727 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
5728 switch (abs->GetResultType()) {
5729 case DataType::Type::kInt32:
5730 case DataType::Type::kInt64:
5731 locations->SetInAt(0, Location::RequiresRegister());
5732 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5733 break;
5734 case DataType::Type::kFloat32:
5735 case DataType::Type::kFloat64:
5736 locations->SetInAt(0, Location::RequiresFpuRegister());
5737 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5738 break;
5739 default:
5740 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5741 }
5742}
5743
5744void InstructionCodeGeneratorARM64::VisitAbs(HAbs* abs) {
5745 switch (abs->GetResultType()) {
5746 case DataType::Type::kInt32:
5747 case DataType::Type::kInt64: {
5748 Register in_reg = InputRegisterAt(abs, 0);
5749 Register out_reg = OutputRegister(abs);
5750 __ Cmp(in_reg, Operand(0));
5751 __ Cneg(out_reg, in_reg, lt);
5752 break;
5753 }
5754 case DataType::Type::kFloat32:
5755 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01005756 VRegister in_reg = InputFPRegisterAt(abs, 0);
5757 VRegister out_reg = OutputFPRegister(abs);
Aart Bik3dad3412018-02-28 12:01:46 -08005758 __ Fabs(out_reg, in_reg);
5759 break;
5760 }
5761 default:
5762 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5763 }
5764}
5765
Igor Murashkind01745e2017-04-05 16:40:31 -07005766void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5767 constructor_fence->SetLocations(nullptr);
5768}
5769
5770void InstructionCodeGeneratorARM64::VisitConstructorFence(
5771 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5772 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5773}
5774
Calin Juravle27df7582015-04-17 19:12:31 +01005775void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5776 memory_barrier->SetLocations(nullptr);
5777}
5778
5779void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005780 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005781}
5782
Alexandre Rames5319def2014-10-23 10:03:10 +01005783void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005784 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005785 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005786 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005787}
5788
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00005789void InstructionCodeGeneratorARM64::VisitReturn(HReturn* ret) {
5790 if (GetGraph()->IsCompilingOsr()) {
5791 // To simplify callers of an OSR method, we put the return value in both
5792 // floating point and core register.
5793 switch (ret->InputAt(0)->GetType()) {
5794 case DataType::Type::kFloat32:
5795 __ Fmov(w0, s0);
5796 break;
5797 case DataType::Type::kFloat64:
5798 __ Fmov(x0, d0);
5799 break;
5800 default:
5801 break;
5802 }
5803 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005804 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005805}
5806
5807void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5808 instruction->SetLocations(nullptr);
5809}
5810
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005811void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005812 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005813}
5814
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005815void LocationsBuilderARM64::VisitRor(HRor* ror) {
5816 HandleBinaryOp(ror);
5817}
5818
5819void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5820 HandleBinaryOp(ror);
5821}
5822
Serban Constantinescu02164b32014-11-13 14:05:07 +00005823void LocationsBuilderARM64::VisitShl(HShl* shl) {
5824 HandleShift(shl);
5825}
5826
5827void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5828 HandleShift(shl);
5829}
5830
5831void LocationsBuilderARM64::VisitShr(HShr* shr) {
5832 HandleShift(shr);
5833}
5834
5835void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5836 HandleShift(shr);
5837}
5838
Alexandre Rames5319def2014-10-23 10:03:10 +01005839void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005840 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005841}
5842
5843void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005844 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005845}
5846
Alexandre Rames67555f72014-11-18 10:55:16 +00005847void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005848 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005849}
5850
5851void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005852 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005853}
5854
5855void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005856 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005857}
5858
Alexandre Rames67555f72014-11-18 10:55:16 +00005859void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005860 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005861}
5862
Vladimir Marko552a1342017-10-31 10:56:47 +00005863void LocationsBuilderARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5864 codegen_->CreateStringBuilderAppendLocations(instruction, LocationFrom(x0));
5865}
5866
5867void InstructionCodeGeneratorARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5868 __ Mov(w0, instruction->GetFormat()->GetValue());
5869 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
5870}
5871
Calin Juravlee460d1d2015-09-29 04:52:17 +01005872void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5873 HUnresolvedInstanceFieldGet* instruction) {
5874 FieldAccessCallingConventionARM64 calling_convention;
5875 codegen_->CreateUnresolvedFieldLocationSummary(
5876 instruction, instruction->GetFieldType(), calling_convention);
5877}
5878
5879void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5880 HUnresolvedInstanceFieldGet* instruction) {
5881 FieldAccessCallingConventionARM64 calling_convention;
5882 codegen_->GenerateUnresolvedFieldAccess(instruction,
5883 instruction->GetFieldType(),
5884 instruction->GetFieldIndex(),
5885 instruction->GetDexPc(),
5886 calling_convention);
5887}
5888
5889void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5890 HUnresolvedInstanceFieldSet* instruction) {
5891 FieldAccessCallingConventionARM64 calling_convention;
5892 codegen_->CreateUnresolvedFieldLocationSummary(
5893 instruction, instruction->GetFieldType(), calling_convention);
5894}
5895
5896void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5897 HUnresolvedInstanceFieldSet* instruction) {
5898 FieldAccessCallingConventionARM64 calling_convention;
5899 codegen_->GenerateUnresolvedFieldAccess(instruction,
5900 instruction->GetFieldType(),
5901 instruction->GetFieldIndex(),
5902 instruction->GetDexPc(),
5903 calling_convention);
5904}
5905
5906void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5907 HUnresolvedStaticFieldGet* instruction) {
5908 FieldAccessCallingConventionARM64 calling_convention;
5909 codegen_->CreateUnresolvedFieldLocationSummary(
5910 instruction, instruction->GetFieldType(), calling_convention);
5911}
5912
5913void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5914 HUnresolvedStaticFieldGet* instruction) {
5915 FieldAccessCallingConventionARM64 calling_convention;
5916 codegen_->GenerateUnresolvedFieldAccess(instruction,
5917 instruction->GetFieldType(),
5918 instruction->GetFieldIndex(),
5919 instruction->GetDexPc(),
5920 calling_convention);
5921}
5922
5923void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5924 HUnresolvedStaticFieldSet* instruction) {
5925 FieldAccessCallingConventionARM64 calling_convention;
5926 codegen_->CreateUnresolvedFieldLocationSummary(
5927 instruction, instruction->GetFieldType(), calling_convention);
5928}
5929
5930void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5931 HUnresolvedStaticFieldSet* instruction) {
5932 FieldAccessCallingConventionARM64 calling_convention;
5933 codegen_->GenerateUnresolvedFieldAccess(instruction,
5934 instruction->GetFieldType(),
5935 instruction->GetFieldIndex(),
5936 instruction->GetDexPc(),
5937 calling_convention);
5938}
5939
Alexandre Rames5319def2014-10-23 10:03:10 +01005940void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005941 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5942 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005943 // In suspend check slow path, usually there are no caller-save registers at all.
5944 // If SIMD instructions are present, however, we force spilling all live SIMD
5945 // registers in full width (since the runtime only saves/restores lower part).
5946 locations->SetCustomSlowPathCallerSaves(
5947 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005948}
5949
5950void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005951 HBasicBlock* block = instruction->GetBlock();
5952 if (block->GetLoopInformation() != nullptr) {
5953 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5954 // The back edge will generate the suspend check.
5955 return;
5956 }
5957 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5958 // The goto will generate the suspend check.
5959 return;
5960 }
5961 GenerateSuspendCheck(instruction, nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08005962 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005963}
5964
Alexandre Rames67555f72014-11-18 10:55:16 +00005965void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005966 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5967 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005968 InvokeRuntimeCallingConvention calling_convention;
5969 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5970}
5971
5972void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005973 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005974 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005975}
5976
5977void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5978 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005979 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005980 DataType::Type input_type = conversion->GetInputType();
5981 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005982 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5983 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005984 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
5985 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005986 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5987 }
5988
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005989 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005990 locations->SetInAt(0, Location::RequiresFpuRegister());
5991 } else {
5992 locations->SetInAt(0, Location::RequiresRegister());
5993 }
5994
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005995 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005996 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5997 } else {
5998 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5999 }
6000}
6001
6002void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006003 DataType::Type result_type = conversion->GetResultType();
6004 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00006005
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006006 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6007 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006008
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006009 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
6010 int result_size = DataType::Size(result_type);
6011 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00006012 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006013 Register output = OutputRegister(conversion);
6014 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006015 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01006016 // 'int' values are used directly as W registers, discarding the top
6017 // bits, so we don't need to sign-extend and can just perform a move.
6018 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
6019 // top 32 bits of the target register. We theoretically could leave those
6020 // bits unchanged, but we would have to make sure that no code uses a
6021 // 32bit input value as a 64bit value assuming that the top 32 bits are
6022 // zero.
6023 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006024 } else if (DataType::IsUnsignedType(result_type) ||
6025 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
6026 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006027 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00006028 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006029 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006030 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006031 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006032 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6033 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006034 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006035 } else if (DataType::IsFloatingPointType(result_type) &&
6036 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006037 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
6038 } else {
6039 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6040 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006041 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00006042}
Alexandre Rames67555f72014-11-18 10:55:16 +00006043
Serban Constantinescu02164b32014-11-13 14:05:07 +00006044void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
6045 HandleShift(ushr);
6046}
6047
6048void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
6049 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00006050}
6051
6052void LocationsBuilderARM64::VisitXor(HXor* instruction) {
6053 HandleBinaryOp(instruction);
6054}
6055
6056void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
6057 HandleBinaryOp(instruction);
6058}
6059
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006060void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006061 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006062 LOG(FATAL) << "Unreachable";
6063}
6064
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006065void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006066 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006067 LOG(FATAL) << "Unreachable";
6068}
6069
Mark Mendellfe57faa2015-09-18 09:26:15 -04006070// Simple implementation of packed switch - generate cascaded compare/jumps.
6071void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6072 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006073 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006074 locations->SetInAt(0, Location::RequiresRegister());
6075}
6076
6077void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6078 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08006079 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006080 Register value_reg = InputRegisterAt(switch_instr, 0);
6081 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6082
Zheng Xu3927c8b2015-11-18 17:46:25 +08006083 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01006084 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08006085 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
6086 // make sure we don't emit it if the target may run out of range.
6087 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
6088 // ranges and emit the tables only as required.
6089 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04006090
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006091 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08006092 // Current instruction id is an upper bound of the number of HIRs in the graph.
6093 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
6094 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006095 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6096 Register temp = temps.AcquireW();
6097 __ Subs(temp, value_reg, Operand(lower_bound));
6098
Zheng Xu3927c8b2015-11-18 17:46:25 +08006099 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006100 // Jump to successors[0] if value == lower_bound.
6101 __ B(eq, codegen_->GetLabelOf(successors[0]));
6102 int32_t last_index = 0;
6103 for (; num_entries - last_index > 2; last_index += 2) {
6104 __ Subs(temp, temp, Operand(2));
6105 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6106 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
6107 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6108 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
6109 }
6110 if (num_entries - last_index == 2) {
6111 // The last missing case_value.
6112 __ Cmp(temp, Operand(1));
6113 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08006114 }
6115
6116 // And the default for any other value.
6117 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6118 __ B(codegen_->GetLabelOf(default_block));
6119 }
6120 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01006121 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08006122
6123 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6124
6125 // Below instructions should use at most one blocked register. Since there are two blocked
6126 // registers, we are free to block one.
6127 Register temp_w = temps.AcquireW();
6128 Register index;
6129 // Remove the bias.
6130 if (lower_bound != 0) {
6131 index = temp_w;
6132 __ Sub(index, value_reg, Operand(lower_bound));
6133 } else {
6134 index = value_reg;
6135 }
6136
6137 // Jump to default block if index is out of the range.
6138 __ Cmp(index, Operand(num_entries));
6139 __ B(hs, codegen_->GetLabelOf(default_block));
6140
6141 // In current VIXL implementation, it won't require any blocked registers to encode the
6142 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
6143 // register pressure.
6144 Register table_base = temps.AcquireX();
6145 // Load jump offset from the table.
6146 __ Adr(table_base, jump_table->GetTableStartLabel());
6147 Register jump_offset = temp_w;
6148 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
6149
6150 // Jump to target block by branching to table_base(pc related) + offset.
6151 Register target_address = table_base;
6152 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
6153 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006154 }
6155}
6156
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006157void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
6158 HInstruction* instruction,
6159 Location out,
6160 uint32_t offset,
6161 Location maybe_temp,
6162 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006163 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006164 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006165 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006166 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006167 if (kUseBakerReadBarrier) {
6168 // Load with fast path based Baker's read barrier.
6169 // /* HeapReference<Object> */ out = *(out + offset)
6170 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6171 out,
6172 out_reg,
6173 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006174 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006175 /* needs_null_check= */ false,
6176 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006177 } else {
6178 // Load with slow path based read barrier.
6179 // Save the value of `out` into `maybe_temp` before overwriting it
6180 // in the following move operation, as we will need it for the
6181 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006182 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00006183 __ Mov(temp_reg, out_reg);
6184 // /* HeapReference<Object> */ out = *(out + offset)
6185 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6186 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6187 }
6188 } else {
6189 // Plain load with no read barrier.
6190 // /* HeapReference<Object> */ out = *(out + offset)
6191 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6192 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6193 }
6194}
6195
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006196void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
6197 HInstruction* instruction,
6198 Location out,
6199 Location obj,
6200 uint32_t offset,
6201 Location maybe_temp,
6202 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006203 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006204 Register out_reg = RegisterFrom(out, type);
6205 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006206 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006207 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006208 if (kUseBakerReadBarrier) {
6209 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00006210 // /* HeapReference<Object> */ out = *(obj + offset)
6211 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6212 out,
6213 obj_reg,
6214 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006215 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006216 /* needs_null_check= */ false,
6217 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006218 } else {
6219 // Load with slow path based read barrier.
6220 // /* HeapReference<Object> */ out = *(obj + offset)
6221 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6222 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6223 }
6224 } else {
6225 // Plain load with no read barrier.
6226 // /* HeapReference<Object> */ out = *(obj + offset)
6227 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6228 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6229 }
6230}
6231
Vladimir Markoca1e0382018-04-11 09:58:41 +00006232void CodeGeneratorARM64::GenerateGcRootFieldLoad(
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006233 HInstruction* instruction,
6234 Location root,
6235 Register obj,
6236 uint32_t offset,
6237 vixl::aarch64::Label* fixup_label,
6238 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006239 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006240 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006241 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006242 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006243 if (kUseBakerReadBarrier) {
6244 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00006245 // Baker's read barrier are used.
Roland Levillain44015862016-01-22 11:47:17 +00006246
Vladimir Marko008e09f32018-08-06 15:42:43 +01006247 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6248 // the Marking Register) to decide whether we need to enter
6249 // the slow path to mark the GC root.
6250 //
6251 // We use shared thunks for the slow path; shared within the method
6252 // for JIT, across methods for AOT. That thunk checks the reference
6253 // and jumps to the entrypoint if needed.
6254 //
6255 // lr = &return_address;
6256 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6257 // if (mr) { // Thread::Current()->GetIsGcMarking()
6258 // goto gc_root_thunk<root_reg>(lr)
6259 // }
6260 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00006261
Vladimir Marko008e09f32018-08-06 15:42:43 +01006262 UseScratchRegisterScope temps(GetVIXLAssembler());
6263 DCHECK(temps.IsAvailable(ip0));
6264 DCHECK(temps.IsAvailable(ip1));
6265 temps.Exclude(ip0, ip1);
6266 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
Roland Levillain44015862016-01-22 11:47:17 +00006267
Vladimir Marko008e09f32018-08-06 15:42:43 +01006268 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6269 vixl::aarch64::Label return_address;
6270 __ adr(lr, &return_address);
6271 if (fixup_label != nullptr) {
6272 __ bind(fixup_label);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006273 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006274 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
Vladimir Marko94796f82018-08-08 15:15:33 +01006275 "GC root LDR must be 2 instructions (8B) before the return address label.");
Vladimir Marko008e09f32018-08-06 15:42:43 +01006276 __ ldr(root_reg, MemOperand(obj.X(), offset));
6277 EmitBakerReadBarrierCbnz(custom_data);
6278 __ bind(&return_address);
Roland Levillain44015862016-01-22 11:47:17 +00006279 } else {
6280 // GC root loaded through a slow path for read barriers other
6281 // than Baker's.
6282 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006283 if (fixup_label == nullptr) {
6284 __ Add(root_reg.X(), obj.X(), offset);
6285 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006286 EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006287 }
Roland Levillain44015862016-01-22 11:47:17 +00006288 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00006289 GenerateReadBarrierForRootSlow(instruction, root, root);
Roland Levillain44015862016-01-22 11:47:17 +00006290 }
6291 } else {
6292 // Plain GC root load with no read barrier.
6293 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006294 if (fixup_label == nullptr) {
6295 __ Ldr(root_reg, MemOperand(obj, offset));
6296 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006297 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006298 }
Roland Levillain44015862016-01-22 11:47:17 +00006299 // Note that GC roots are not affected by heap poisoning, thus we
6300 // do not have to unpoison `root_reg` here.
6301 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006302 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00006303}
6304
Vladimir Marko94796f82018-08-08 15:15:33 +01006305void CodeGeneratorARM64::GenerateUnsafeCasOldValueMovWithBakerReadBarrier(
6306 vixl::aarch64::Register marked,
6307 vixl::aarch64::Register old_value) {
6308 DCHECK(kEmitCompilerReadBarrier);
6309 DCHECK(kUseBakerReadBarrier);
6310
6311 // Similar to the Baker RB path in GenerateGcRootFieldLoad(), with a MOV instead of LDR.
6312 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(marked.GetCode());
6313
6314 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6315 vixl::aarch64::Label return_address;
6316 __ adr(lr, &return_address);
6317 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6318 "GC root LDR must be 2 instructions (8B) before the return address label.");
6319 __ mov(marked, old_value);
6320 EmitBakerReadBarrierCbnz(custom_data);
6321 __ bind(&return_address);
6322}
6323
Roland Levillain44015862016-01-22 11:47:17 +00006324void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6325 Location ref,
Vladimir Marko248141f2018-08-10 10:40:07 +01006326 vixl::aarch64::Register obj,
6327 const vixl::aarch64::MemOperand& src,
Roland Levillain44015862016-01-22 11:47:17 +00006328 bool needs_null_check,
6329 bool use_load_acquire) {
6330 DCHECK(kEmitCompilerReadBarrier);
6331 DCHECK(kUseBakerReadBarrier);
6332
Vladimir Marko0ecac682018-08-07 10:40:38 +01006333 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6334 // Marking Register) to decide whether we need to enter the slow
6335 // path to mark the reference. Then, in the slow path, check the
6336 // gray bit in the lock word of the reference's holder (`obj`) to
6337 // decide whether to mark `ref` or not.
6338 //
6339 // We use shared thunks for the slow path; shared within the method
6340 // for JIT, across methods for AOT. That thunk checks the holder
6341 // and jumps to the entrypoint if needed. If the holder is not gray,
6342 // it creates a fake dependency and returns to the LDR instruction.
6343 //
6344 // lr = &gray_return_address;
6345 // if (mr) { // Thread::Current()->GetIsGcMarking()
6346 // goto field_thunk<holder_reg, base_reg, use_load_acquire>(lr)
6347 // }
6348 // not_gray_return_address:
6349 // // Original reference load. If the offset is too large to fit
6350 // // into LDR, we use an adjusted base register here.
6351 // HeapReference<mirror::Object> reference = *(obj+offset);
6352 // gray_return_address:
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006353
Vladimir Marko248141f2018-08-10 10:40:07 +01006354 DCHECK(src.GetAddrMode() == vixl::aarch64::Offset);
6355 DCHECK_ALIGNED(src.GetOffset(), sizeof(mirror::HeapReference<mirror::Object>));
6356
6357 UseScratchRegisterScope temps(GetVIXLAssembler());
6358 DCHECK(temps.IsAvailable(ip0));
6359 DCHECK(temps.IsAvailable(ip1));
6360 temps.Exclude(ip0, ip1);
6361 uint32_t custom_data = use_load_acquire
6362 ? EncodeBakerReadBarrierAcquireData(src.GetBaseRegister().GetCode(), obj.GetCode())
6363 : EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode());
6364
6365 {
6366 ExactAssemblyScope guard(GetVIXLAssembler(),
6367 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6368 vixl::aarch64::Label return_address;
6369 __ adr(lr, &return_address);
6370 EmitBakerReadBarrierCbnz(custom_data);
6371 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6372 "Field LDR must be 1 instruction (4B) before the return address label; "
6373 " 2 instructions (8B) for heap poisoning.");
6374 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
6375 if (use_load_acquire) {
6376 DCHECK_EQ(src.GetOffset(), 0);
6377 __ ldar(ref_reg, src);
6378 } else {
6379 __ ldr(ref_reg, src);
6380 }
6381 if (needs_null_check) {
6382 MaybeRecordImplicitNullCheck(instruction);
6383 }
6384 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6385 // macro instructions disallowed in ExactAssemblyScope.
6386 if (kPoisonHeapReferences) {
6387 __ neg(ref_reg, Operand(ref_reg));
6388 }
6389 __ bind(&return_address);
6390 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006391 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Vladimir Marko248141f2018-08-10 10:40:07 +01006392}
6393
6394void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6395 Location ref,
6396 Register obj,
6397 uint32_t offset,
6398 Location maybe_temp,
6399 bool needs_null_check,
6400 bool use_load_acquire) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006401 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6402 Register base = obj;
6403 if (use_load_acquire) {
6404 DCHECK(maybe_temp.IsRegister());
6405 base = WRegisterFrom(maybe_temp);
6406 __ Add(base, obj, offset);
6407 offset = 0u;
6408 } else if (offset >= kReferenceLoadMinFarOffset) {
6409 DCHECK(maybe_temp.IsRegister());
6410 base = WRegisterFrom(maybe_temp);
6411 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6412 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6413 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006414 }
Vladimir Marko248141f2018-08-10 10:40:07 +01006415 MemOperand src(base.X(), offset);
6416 GenerateFieldLoadWithBakerReadBarrier(
6417 instruction, ref, obj, src, needs_null_check, use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006418}
6419
Artem Serov0806f582018-10-11 20:14:20 +01006420void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HArrayGet* instruction,
6421 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006422 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006423 uint32_t data_offset,
6424 Location index,
Roland Levillain44015862016-01-22 11:47:17 +00006425 bool needs_null_check) {
6426 DCHECK(kEmitCompilerReadBarrier);
6427 DCHECK(kUseBakerReadBarrier);
6428
Vladimir Marko66d691d2017-04-07 17:53:39 +01006429 static_assert(
6430 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6431 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006432 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006433
Vladimir Marko008e09f32018-08-06 15:42:43 +01006434 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6435 // Marking Register) to decide whether we need to enter the slow
6436 // path to mark the reference. Then, in the slow path, check the
6437 // gray bit in the lock word of the reference's holder (`obj`) to
6438 // decide whether to mark `ref` or not.
6439 //
6440 // We use shared thunks for the slow path; shared within the method
6441 // for JIT, across methods for AOT. That thunk checks the holder
6442 // and jumps to the entrypoint if needed. If the holder is not gray,
6443 // it creates a fake dependency and returns to the LDR instruction.
6444 //
6445 // lr = &gray_return_address;
6446 // if (mr) { // Thread::Current()->GetIsGcMarking()
6447 // goto array_thunk<base_reg>(lr)
6448 // }
6449 // not_gray_return_address:
6450 // // Original reference load. If the offset is too large to fit
6451 // // into LDR, we use an adjusted base register here.
6452 // HeapReference<mirror::Object> reference = data[index];
6453 // gray_return_address:
Vladimir Marko66d691d2017-04-07 17:53:39 +01006454
Vladimir Marko008e09f32018-08-06 15:42:43 +01006455 DCHECK(index.IsValid());
6456 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
6457 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006458
Vladimir Marko008e09f32018-08-06 15:42:43 +01006459 UseScratchRegisterScope temps(GetVIXLAssembler());
6460 DCHECK(temps.IsAvailable(ip0));
6461 DCHECK(temps.IsAvailable(ip1));
6462 temps.Exclude(ip0, ip1);
Artem Serov0806f582018-10-11 20:14:20 +01006463
6464 Register temp;
6465 if (instruction->GetArray()->IsIntermediateAddress()) {
6466 // We do not need to compute the intermediate address from the array: the
6467 // input instruction has done it already. See the comment in
6468 // `TryExtractArrayAccessAddress()`.
6469 if (kIsDebugBuild) {
6470 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
6471 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
6472 }
6473 temp = obj;
6474 } else {
6475 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
6476 __ Add(temp.X(), obj.X(), Operand(data_offset));
6477 }
6478
Vladimir Marko008e09f32018-08-06 15:42:43 +01006479 uint32_t custom_data = EncodeBakerReadBarrierArrayData(temp.GetCode());
Vladimir Marko66d691d2017-04-07 17:53:39 +01006480
Vladimir Marko008e09f32018-08-06 15:42:43 +01006481 {
6482 ExactAssemblyScope guard(GetVIXLAssembler(),
6483 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6484 vixl::aarch64::Label return_address;
6485 __ adr(lr, &return_address);
6486 EmitBakerReadBarrierCbnz(custom_data);
6487 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6488 "Array LDR must be 1 instruction (4B) before the return address label; "
6489 " 2 instructions (8B) for heap poisoning.");
6490 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6491 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6492 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6493 // macro instructions disallowed in ExactAssemblyScope.
6494 if (kPoisonHeapReferences) {
6495 __ neg(ref_reg, Operand(ref_reg));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006496 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006497 __ bind(&return_address);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006498 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006499 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Roland Levillain44015862016-01-22 11:47:17 +00006500}
6501
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006502void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6503 // The following condition is a compile-time one, so it does not have a run-time cost.
6504 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
6505 // The following condition is a run-time one; it is executed after the
6506 // previous compile-time test, to avoid penalizing non-debug builds.
6507 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6508 UseScratchRegisterScope temps(GetVIXLAssembler());
6509 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6510 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6511 }
6512 }
6513}
6514
Roland Levillain44015862016-01-22 11:47:17 +00006515void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6516 Location out,
6517 Location ref,
6518 Location obj,
6519 uint32_t offset,
6520 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006521 DCHECK(kEmitCompilerReadBarrier);
6522
Roland Levillain44015862016-01-22 11:47:17 +00006523 // Insert a slow path based read barrier *after* the reference load.
6524 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006525 // If heap poisoning is enabled, the unpoisoning of the loaded
6526 // reference will be carried out by the runtime within the slow
6527 // path.
6528 //
6529 // Note that `ref` currently does not get unpoisoned (when heap
6530 // poisoning is enabled), which is alright as the `ref` argument is
6531 // not used by the artReadBarrierSlow entry point.
6532 //
6533 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006534 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006535 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6536 AddSlowPath(slow_path);
6537
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006538 __ B(slow_path->GetEntryLabel());
6539 __ Bind(slow_path->GetExitLabel());
6540}
6541
Roland Levillain44015862016-01-22 11:47:17 +00006542void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6543 Location out,
6544 Location ref,
6545 Location obj,
6546 uint32_t offset,
6547 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006548 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006549 // Baker's read barriers shall be handled by the fast path
6550 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6551 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006552 // If heap poisoning is enabled, unpoisoning will be taken care of
6553 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006554 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006555 } else if (kPoisonHeapReferences) {
6556 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6557 }
6558}
6559
Roland Levillain44015862016-01-22 11:47:17 +00006560void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6561 Location out,
6562 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006563 DCHECK(kEmitCompilerReadBarrier);
6564
Roland Levillain44015862016-01-22 11:47:17 +00006565 // Insert a slow path based read barrier *after* the GC root load.
6566 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006567 // Note that GC roots are not affected by heap poisoning, so we do
6568 // not need to do anything special for this here.
6569 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006570 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006571 AddSlowPath(slow_path);
6572
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006573 __ B(slow_path->GetEntryLabel());
6574 __ Bind(slow_path->GetExitLabel());
6575}
6576
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006577void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6578 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006579 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006580 locations->SetInAt(0, Location::RequiresRegister());
6581 locations->SetOut(Location::RequiresRegister());
6582}
6583
6584void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6585 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006586 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006587 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006588 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006589 __ Ldr(XRegisterFrom(locations->Out()),
6590 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006591 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006592 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006593 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006594 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6595 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006596 __ Ldr(XRegisterFrom(locations->Out()),
6597 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006598 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006599}
6600
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006601static void PatchJitRootUse(uint8_t* code,
6602 const uint8_t* roots_data,
6603 vixl::aarch64::Literal<uint32_t>* literal,
6604 uint64_t index_in_table) {
6605 uint32_t literal_offset = literal->GetOffset();
6606 uintptr_t address =
6607 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6608 uint8_t* data = code + literal_offset;
6609 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6610}
6611
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006612void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6613 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006614 const StringReference& string_reference = entry.first;
6615 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006616 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006617 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006618 }
6619 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006620 const TypeReference& type_reference = entry.first;
6621 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006622 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006623 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006624 }
6625}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006626
Artem Serov1a719e42019-07-18 14:24:55 +01006627MemOperand InstructionCodeGeneratorARM64::VecNeonAddress(
6628 HVecMemoryOperation* instruction,
6629 UseScratchRegisterScope* temps_scope,
6630 size_t size,
6631 bool is_string_char_at,
6632 /*out*/ Register* scratch) {
6633 LocationSummary* locations = instruction->GetLocations();
6634 Register base = InputRegisterAt(instruction, 0);
6635
6636 if (instruction->InputAt(1)->IsIntermediateAddressIndex()) {
6637 DCHECK(!is_string_char_at);
6638 return MemOperand(base.X(), InputRegisterAt(instruction, 1).X());
6639 }
6640
6641 Location index = locations->InAt(1);
6642 uint32_t offset = is_string_char_at
6643 ? mirror::String::ValueOffset().Uint32Value()
6644 : mirror::Array::DataOffset(size).Uint32Value();
6645 size_t shift = ComponentSizeShiftWidth(size);
6646
6647 // HIntermediateAddress optimization is only applied for scalar ArrayGet and ArraySet.
6648 DCHECK(!instruction->InputAt(0)->IsIntermediateAddress());
6649
6650 if (index.IsConstant()) {
6651 offset += Int64FromLocation(index) << shift;
6652 return HeapOperand(base, offset);
6653 } else {
6654 *scratch = temps_scope->AcquireSameSizeAs(base);
6655 __ Add(*scratch, base, Operand(WRegisterFrom(index), LSL, shift));
6656 return HeapOperand(*scratch, offset);
6657 }
6658}
6659
Alexandre Rames67555f72014-11-18 10:55:16 +00006660#undef __
6661#undef QUICK_ENTRY_POINT
6662
Vladimir Markoca1e0382018-04-11 09:58:41 +00006663#define __ assembler.GetVIXLAssembler()->
6664
6665static void EmitGrayCheckAndFastPath(arm64::Arm64Assembler& assembler,
6666 vixl::aarch64::Register base_reg,
6667 vixl::aarch64::MemOperand& lock_word,
Vladimir Marko7a695052018-04-12 10:26:50 +01006668 vixl::aarch64::Label* slow_path,
6669 vixl::aarch64::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006670 // Load the lock word containing the rb_state.
6671 __ Ldr(ip0.W(), lock_word);
6672 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01006673 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Vladimir Markoca1e0382018-04-11 09:58:41 +00006674 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6675 __ Tbnz(ip0.W(), LockWord::kReadBarrierStateShift, slow_path);
6676 static_assert(
6677 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET,
6678 "Field and array LDR offsets must be the same to reuse the same code.");
Vladimir Marko7a695052018-04-12 10:26:50 +01006679 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
6680 if (throw_npe != nullptr) {
6681 __ Bind(throw_npe);
6682 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006683 // Adjust the return address back to the LDR (1 instruction; 2 for heap poisoning).
6684 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6685 "Field LDR must be 1 instruction (4B) before the return address label; "
6686 " 2 instructions (8B) for heap poisoning.");
6687 __ Add(lr, lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6688 // Introduce a dependency on the lock_word including rb_state,
6689 // to prevent load-load reordering, and without using
6690 // a memory barrier (which would be more expensive).
6691 __ Add(base_reg, base_reg, Operand(ip0, LSR, 32));
6692 __ Br(lr); // And return back to the function.
6693 // Note: The fake dependency is unnecessary for the slow path.
6694}
6695
6696// Load the read barrier introspection entrypoint in register `entrypoint`.
6697static void LoadReadBarrierMarkIntrospectionEntrypoint(arm64::Arm64Assembler& assembler,
6698 vixl::aarch64::Register entrypoint) {
6699 // entrypoint = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6700 DCHECK_EQ(ip0.GetCode(), 16u);
6701 const int32_t entry_point_offset =
6702 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6703 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
6704}
6705
6706void CodeGeneratorARM64::CompileBakerReadBarrierThunk(Arm64Assembler& assembler,
6707 uint32_t encoded_data,
6708 /*out*/ std::string* debug_name) {
6709 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
6710 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006711 case BakerReadBarrierKind::kField:
6712 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006713 auto base_reg =
6714 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6715 CheckValidReg(base_reg.GetCode());
6716 auto holder_reg =
6717 Register::GetXRegFromCode(BakerReadBarrierSecondRegField::Decode(encoded_data));
6718 CheckValidReg(holder_reg.GetCode());
6719 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6720 temps.Exclude(ip0, ip1);
Roland Levillain988c3912019-09-25 19:33:35 +01006721 // In the case of a field load (with relaxed semantic), if `base_reg` differs from
6722 // `holder_reg`, the offset was too large and we must have emitted (during the construction
6723 // of the HIR graph, see `art::HInstructionBuilder::BuildInstanceFieldAccess`) and preserved
6724 // (see `art::PrepareForRegisterAllocation::VisitNullCheck`) an explicit null check before
6725 // the load. Otherwise, for implicit null checks, we need to null-check the holder as we do
6726 // not necessarily do that check before going to the thunk.
6727 //
6728 // In the case of a field load with load-acquire semantics (where `base_reg` always differs
6729 // from `holder_reg`), we also need an explicit null check when implicit null checks are
6730 // allowed, as we do not emit one before going to the thunk.
Vladimir Marko7a695052018-04-12 10:26:50 +01006731 vixl::aarch64::Label throw_npe_label;
6732 vixl::aarch64::Label* throw_npe = nullptr;
Roland Levillain988c3912019-09-25 19:33:35 +01006733 if (GetCompilerOptions().GetImplicitNullChecks() &&
6734 (holder_reg.Is(base_reg) || (kind == BakerReadBarrierKind::kAcquire))) {
Vladimir Marko7a695052018-04-12 10:26:50 +01006735 throw_npe = &throw_npe_label;
6736 __ Cbz(holder_reg.W(), throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006737 }
Vladimir Marko7a695052018-04-12 10:26:50 +01006738 // Check if the holder is gray and, if not, add fake dependency to the base register
6739 // and return to the LDR instruction to load the reference. Otherwise, use introspection
6740 // to load the reference and call the entrypoint that performs further checks on the
6741 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006742 vixl::aarch64::Label slow_path;
6743 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
Vladimir Marko7a695052018-04-12 10:26:50 +01006744 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006745 __ Bind(&slow_path);
Vladimir Marko0ecac682018-08-07 10:40:38 +01006746 if (kind == BakerReadBarrierKind::kField) {
6747 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6748 __ Ldr(ip0.W(), ldr_address); // Load the LDR (immediate) unsigned offset.
6749 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6750 __ Ubfx(ip0.W(), ip0.W(), 10, 12); // Extract the offset.
6751 __ Ldr(ip0.W(), MemOperand(base_reg, ip0, LSL, 2)); // Load the reference.
6752 } else {
6753 DCHECK(kind == BakerReadBarrierKind::kAcquire);
6754 DCHECK(!base_reg.Is(holder_reg));
6755 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6756 __ Ldar(ip0.W(), MemOperand(base_reg));
6757 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006758 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
6759 __ Br(ip1); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006760 break;
6761 }
6762 case BakerReadBarrierKind::kArray: {
6763 auto base_reg =
6764 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6765 CheckValidReg(base_reg.GetCode());
6766 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6767 BakerReadBarrierSecondRegField::Decode(encoded_data));
6768 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6769 temps.Exclude(ip0, ip1);
6770 vixl::aarch64::Label slow_path;
6771 int32_t data_offset =
6772 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
6773 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
6774 DCHECK_LT(lock_word.GetOffset(), 0);
6775 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path);
6776 __ Bind(&slow_path);
6777 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
6778 __ Ldr(ip0.W(), ldr_address); // Load the LDR (register) unsigned offset.
6779 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6780 __ Ubfx(ip0, ip0, 16, 6); // Extract the index register, plus 32 (bit 21 is set).
6781 __ Bfi(ip1, ip0, 3, 6); // Insert ip0 to the entrypoint address to create
6782 // a switch case target based on the index register.
6783 __ Mov(ip0, base_reg); // Move the base register to ip0.
6784 __ Br(ip1); // Jump to the entrypoint's array switch case.
6785 break;
6786 }
6787 case BakerReadBarrierKind::kGcRoot: {
6788 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
6789 // and it does not have a forwarding address), call the correct introspection entrypoint;
6790 // otherwise return the reference (or the extracted forwarding address).
6791 // There is no gray bit check for GC roots.
6792 auto root_reg =
6793 Register::GetWRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6794 CheckValidReg(root_reg.GetCode());
6795 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6796 BakerReadBarrierSecondRegField::Decode(encoded_data));
6797 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6798 temps.Exclude(ip0, ip1);
6799 vixl::aarch64::Label return_label, not_marked, forwarding_address;
6800 __ Cbz(root_reg, &return_label);
6801 MemOperand lock_word(root_reg.X(), mirror::Object::MonitorOffset().Int32Value());
6802 __ Ldr(ip0.W(), lock_word);
6803 __ Tbz(ip0.W(), LockWord::kMarkBitStateShift, &not_marked);
6804 __ Bind(&return_label);
6805 __ Br(lr);
6806 __ Bind(&not_marked);
6807 __ Tst(ip0.W(), Operand(ip0.W(), LSL, 1));
6808 __ B(&forwarding_address, mi);
6809 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6810 // Adjust the art_quick_read_barrier_mark_introspection address in IP1 to
6811 // art_quick_read_barrier_mark_introspection_gc_roots.
6812 __ Add(ip1, ip1, Operand(BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRYPOINT_OFFSET));
6813 __ Mov(ip0.W(), root_reg);
6814 __ Br(ip1);
6815 __ Bind(&forwarding_address);
6816 __ Lsl(root_reg, ip0.W(), LockWord::kForwardingAddressShift);
6817 __ Br(lr);
6818 break;
6819 }
6820 default:
6821 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
6822 UNREACHABLE();
6823 }
6824
Vladimir Marko966b46f2018-08-03 10:20:19 +00006825 // For JIT, the slow path is considered part of the compiled method,
Vladimir Markof91fc122020-05-13 09:21:00 +01006826 // so JIT should pass null as `debug_name`.
Vladimir Marko695348f2020-05-19 14:42:02 +01006827 DCHECK(!GetCompilerOptions().IsJitCompiler() || debug_name == nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00006828 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006829 std::ostringstream oss;
6830 oss << "BakerReadBarrierThunk";
6831 switch (kind) {
6832 case BakerReadBarrierKind::kField:
6833 oss << "Field_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
6834 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
6835 break;
Vladimir Marko0ecac682018-08-07 10:40:38 +01006836 case BakerReadBarrierKind::kAcquire:
6837 oss << "Acquire_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
6838 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
6839 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +00006840 case BakerReadBarrierKind::kArray:
6841 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
6842 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6843 BakerReadBarrierSecondRegField::Decode(encoded_data));
6844 break;
6845 case BakerReadBarrierKind::kGcRoot:
6846 oss << "GcRoot_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
6847 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6848 BakerReadBarrierSecondRegField::Decode(encoded_data));
6849 break;
6850 }
6851 *debug_name = oss.str();
6852 }
6853}
6854
6855#undef __
6856
Alexandre Rames5319def2014-10-23 10:03:10 +01006857} // namespace arm64
6858} // namespace art