blob: 47c62f9366627e8cf0f7a423ab5f39d08afe850e [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"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000021#include "art_method-inl.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070022#include "base/bit_utils.h"
23#include "base/bit_utils_iterator.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010024#include "class_table.h"
Zheng Xuc6667102015-05-15 16:08:45 +080025#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000026#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010027#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080028#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010029#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010030#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070031#include "heap_poisoning.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080032#include "intrinsics.h"
33#include "intrinsics_arm64.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010034#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "lock_word.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010036#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070037#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000038#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010039#include "thread.h"
40#include "utils/arm64/assembler_arm64.h"
41#include "utils/assembler.h"
42#include "utils/stack_checks.h"
43
Scott Wakeling97c72b72016-06-24 16:19:36 +010044using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000045using vixl::ExactAssemblyScope;
46using vixl::CodeBufferCheckScope;
47using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010048
49#ifdef __
50#error "ARM64 Codegen VIXL macro-assembler macro already defined."
51#endif
52
Vladimir Marko0a516052019-10-14 13:00:44 +000053namespace art {
Alexandre Rames5319def2014-10-23 10:03:10 +010054
Roland Levillain22ccc3a2015-11-24 13:10:05 +000055template<class MirrorType>
56class GcRoot;
57
Alexandre Rames5319def2014-10-23 10:03:10 +010058namespace arm64 {
59
Alexandre Ramesbe919d92016-08-23 18:33:36 +010060using helpers::ARM64EncodableConstantOrRegister;
61using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080062using helpers::CPURegisterFrom;
63using helpers::DRegisterFrom;
64using helpers::FPRegisterFrom;
65using helpers::HeapOperand;
66using helpers::HeapOperandFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010067using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080068using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010070using helpers::InputRegisterAt;
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +010071using helpers::Int64FromLocation;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010072using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080073using helpers::LocationFrom;
74using helpers::OperandFromMemOperand;
75using helpers::OutputCPURegister;
76using helpers::OutputFPRegister;
77using helpers::OutputRegister;
Artem Serovd4bccf12017-04-03 18:47:32 +010078using helpers::QRegisterFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080079using 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 Serov7957d952017-04-04 15:44:09 +0100179 unsigned v_reg_size = codegen->GetGraph()->HasSIMD() ? kQRegSize : kDRegSize;
180 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800181
182 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
183 UseScratchRegisterScope temps(masm);
184
185 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100186 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
187 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800188 int64_t reg_size = kXRegSizeInBytes;
189 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
190 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100191 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800192 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
193 // If the offset does not fit in the instruction's immediate field, use an alternate register
194 // to compute the base address(float point registers spill base address).
195 Register new_base = temps.AcquireSameSizeAs(base);
196 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
197 base = new_base;
198 spill_offset = -core_spill_size;
199 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
200 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
201 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
202 }
203
204 if (is_save) {
205 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
206 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
207 } else {
208 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
209 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
210 }
211}
212
213void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800214 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Andreas Gampe3db70682018-12-26 15:12:03 -0800215 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100216 for (uint32_t i : LowToHighBits(core_spills)) {
217 // If the register holds an object, update the stack mask.
218 if (locations->RegisterContainsObject(i)) {
219 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800220 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100221 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
222 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
223 saved_core_stack_offsets_[i] = stack_offset;
224 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800225 }
226
Artem Serov9df37b92019-07-23 16:41:54 +0100227 const size_t fp_reg_size = codegen->GetGraph()->HasSIMD() ? kQRegSizeInBytes : kDRegSizeInBytes;
Andreas Gampe3db70682018-12-26 15:12:03 -0800228 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100229 for (uint32_t i : LowToHighBits(fp_spills)) {
230 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
231 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
232 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serov9df37b92019-07-23 16:41:54 +0100233 stack_offset += fp_reg_size;
Zheng Xuda403092015-04-24 17:35:39 +0800234 }
235
Vladimir Marko804b03f2016-09-14 16:26:36 +0100236 SaveRestoreLiveRegistersHelper(codegen,
237 locations,
Andreas Gampe3db70682018-12-26 15:12:03 -0800238 codegen->GetFirstRegisterSlotInSlowPath(), /* is_save= */ true);
Zheng Xuda403092015-04-24 17:35:39 +0800239}
240
241void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100242 SaveRestoreLiveRegistersHelper(codegen,
243 locations,
Andreas Gampe3db70682018-12-26 15:12:03 -0800244 codegen->GetFirstRegisterSlotInSlowPath(), /* is_save= */ false);
Zheng Xuda403092015-04-24 17:35:39 +0800245}
246
Alexandre Rames5319def2014-10-23 10:03:10 +0100247class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
248 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000249 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100250
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100251 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100252 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000253 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100254
Alexandre Rames5319def2014-10-23 10:03:10 +0100255 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000256 if (instruction_->CanThrowIntoCatchBlock()) {
257 // Live registers will be restored in the catch block if caught.
258 SaveLiveRegisters(codegen, instruction_->GetLocations());
259 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000260 // We're moving two locations to locations that could overlap, so we need a parallel
261 // move resolver.
262 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100263 codegen->EmitParallelMoves(locations->InAt(0),
264 LocationFrom(calling_convention.GetRegisterAt(0)),
265 DataType::Type::kInt32,
266 locations->InAt(1),
267 LocationFrom(calling_convention.GetRegisterAt(1)),
268 DataType::Type::kInt32);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000269 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
270 ? kQuickThrowStringBounds
271 : kQuickThrowArrayBounds;
272 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100273 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800274 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100275 }
276
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100277 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100278
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100279 const char* GetDescription() const override { return "BoundsCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100280
Alexandre Rames5319def2014-10-23 10:03:10 +0100281 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100282 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
283};
284
Alexandre Rames67555f72014-11-18 10:55:16 +0000285class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
286 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000287 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000288
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100289 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000290 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
291 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000292 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800293 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000294 }
295
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100296 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100297
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100298 const char* GetDescription() const override { return "DivZeroCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100299
Alexandre Rames67555f72014-11-18 10:55:16 +0000300 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000301 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
302};
303
304class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
305 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100306 LoadClassSlowPathARM64(HLoadClass* cls, HInstruction* at)
307 : SlowPathCodeARM64(at), cls_(cls) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000308 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100309 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Alexandre Rames67555f72014-11-18 10:55:16 +0000310 }
311
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100312 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000313 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000314 Location out = locations->Out();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100315 const uint32_t dex_pc = instruction_->GetDexPc();
316 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
317 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
Alexandre Rames67555f72014-11-18 10:55:16 +0000318
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100319 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames67555f72014-11-18 10:55:16 +0000320 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000321 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000322
Vladimir Markof3c52b42017-11-17 17:32:12 +0000323 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100324 if (must_resolve_type) {
325 DCHECK(IsSameDexFile(cls_->GetDexFile(), arm64_codegen->GetGraph()->GetDexFile()));
326 dex::TypeIndex type_index = cls_->GetTypeIndex();
327 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Vladimir Marko9d479252018-07-24 11:35:20 +0100328 arm64_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
329 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100330 // If we also must_do_clinit, the resolved type is now in the correct register.
331 } else {
332 DCHECK(must_do_clinit);
333 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
334 arm64_codegen->MoveLocation(LocationFrom(calling_convention.GetRegisterAt(0)),
335 source,
336 cls_->GetType());
337 }
338 if (must_do_clinit) {
339 arm64_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
340 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800341 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000342
343 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000344 if (out.IsValid()) {
345 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100346 DataType::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000347 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000348 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000349 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000350 __ B(GetExitLabel());
351 }
352
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100353 const char* GetDescription() const override { return "LoadClassSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100354
Alexandre Rames67555f72014-11-18 10:55:16 +0000355 private:
356 // The class this slow path will load.
357 HLoadClass* const cls_;
358
Alexandre Rames67555f72014-11-18 10:55:16 +0000359 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
360};
361
Vladimir Markoaad75c62016-10-03 08:46:48 +0000362class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
363 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000364 explicit LoadStringSlowPathARM64(HLoadString* instruction)
365 : SlowPathCodeARM64(instruction) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000366
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100367 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000368 LocationSummary* locations = instruction_->GetLocations();
369 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
370 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
371
372 __ Bind(GetEntryLabel());
373 SaveLiveRegisters(codegen, locations);
374
Vladimir Markof3c52b42017-11-17 17:32:12 +0000375 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000376 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
377 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000378 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
379 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100380 DataType::Type type = instruction_->GetType();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000381 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
382
383 RestoreLiveRegisters(codegen, locations);
384
Vladimir Markoaad75c62016-10-03 08:46:48 +0000385 __ B(GetExitLabel());
386 }
387
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100388 const char* GetDescription() const override { return "LoadStringSlowPathARM64"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000389
390 private:
391 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
392};
393
Alexandre Rames5319def2014-10-23 10:03:10 +0100394class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
395 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000396 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100397
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100398 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000399 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100400 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000401 if (instruction_->CanThrowIntoCatchBlock()) {
402 // Live registers will be restored in the catch block if caught.
403 SaveLiveRegisters(codegen, instruction_->GetLocations());
404 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000405 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
406 instruction_,
407 instruction_->GetDexPc(),
408 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800409 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100410 }
411
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100412 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100413
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100414 const char* GetDescription() const override { return "NullCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100415
Alexandre Rames5319def2014-10-23 10:03:10 +0100416 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100417 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
418};
419
420class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
421 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100422 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000423 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100424
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100425 void EmitNativeCode(CodeGenerator* codegen) override {
Artem Serov7957d952017-04-04 15:44:09 +0100426 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000427 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100428 __ Bind(GetEntryLabel());
Artem Serov7957d952017-04-04 15:44:09 +0100429 SaveLiveRegisters(codegen, locations); // Only saves live 128-bit regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000430 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800431 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov7957d952017-04-04 15:44:09 +0100432 RestoreLiveRegisters(codegen, locations); // Only restores live 128-bit regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000433 if (successor_ == nullptr) {
434 __ B(GetReturnLabel());
435 } else {
436 __ B(arm64_codegen->GetLabelOf(successor_));
437 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100438 }
439
Scott Wakeling97c72b72016-06-24 16:19:36 +0100440 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100441 DCHECK(successor_ == nullptr);
442 return &return_label_;
443 }
444
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100445 HBasicBlock* GetSuccessor() const {
446 return successor_;
447 }
448
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100449 const char* GetDescription() const override { return "SuspendCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100450
Alexandre Rames5319def2014-10-23 10:03:10 +0100451 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100452 // If not null, the block to branch to after the suspend check.
453 HBasicBlock* const successor_;
454
455 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100456 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100457
458 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
459};
460
Alexandre Rames67555f72014-11-18 10:55:16 +0000461class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
462 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000463 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000464 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000465
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100466 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000467 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800468
Alexandre Rames3e69f162014-12-10 10:36:50 +0000469 DCHECK(instruction_->IsCheckCast()
470 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
471 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100472 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000473
Alexandre Rames67555f72014-11-18 10:55:16 +0000474 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000475
Vladimir Marko87584542017-12-12 17:47:52 +0000476 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000477 SaveLiveRegisters(codegen, locations);
478 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000479
480 // We're moving two locations to locations that could overlap, so we need a parallel
481 // move resolver.
482 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800483 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800484 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100485 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800486 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800487 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100488 DataType::Type::kReference);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000489 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000490 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800491 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100492 DataType::Type ret_type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000493 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
494 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
495 } else {
496 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800497 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
498 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000499 }
500
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000501 if (!is_fatal_) {
502 RestoreLiveRegisters(codegen, locations);
503 __ B(GetExitLabel());
504 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000505 }
506
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100507 const char* GetDescription() const override { return "TypeCheckSlowPathARM64"; }
508 bool IsFatal() const override { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100509
Alexandre Rames67555f72014-11-18 10:55:16 +0000510 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000511 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000512
Alexandre Rames67555f72014-11-18 10:55:16 +0000513 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
514};
515
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700516class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
517 public:
Aart Bik42249c32016-01-07 15:33:50 -0800518 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000519 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700520
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100521 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bik42249c32016-01-07 15:33:50 -0800522 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700523 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100524 LocationSummary* locations = instruction_->GetLocations();
525 SaveLiveRegisters(codegen, locations);
526 InvokeRuntimeCallingConvention calling_convention;
527 __ Mov(calling_convention.GetRegisterAt(0),
528 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000529 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100530 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700531 }
532
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100533 const char* GetDescription() const override { return "DeoptimizationSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100534
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700535 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700536 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
537};
538
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100539class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
540 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000541 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100542
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100543 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100544 LocationSummary* locations = instruction_->GetLocations();
545 __ Bind(GetEntryLabel());
546 SaveLiveRegisters(codegen, locations);
547
548 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100549 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100550 parallel_move.AddMove(
551 locations->InAt(0),
552 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100553 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100554 nullptr);
555 parallel_move.AddMove(
556 locations->InAt(1),
557 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100558 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100559 nullptr);
560 parallel_move.AddMove(
561 locations->InAt(2),
562 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100563 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100564 nullptr);
565 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
566
567 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000568 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100569 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
570 RestoreLiveRegisters(codegen, locations);
571 __ B(GetExitLabel());
572 }
573
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100574 const char* GetDescription() const override { return "ArraySetSlowPathARM64"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100575
576 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100577 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
578};
579
Zheng Xu3927c8b2015-11-18 17:46:25 +0800580void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
581 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000582 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800583
584 // We are about to use the assembler to place literals directly. Make sure we have enough
585 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000586 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
587 num_entries * sizeof(int32_t),
588 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800589
590 __ Bind(&table_start_);
591 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
592 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100593 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800594 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100595 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800596 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
597 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
598 Literal<int32_t> literal(jump_offset);
599 __ place(&literal);
600 }
601}
602
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000603// Slow path generating a read barrier for a heap reference.
604class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
605 public:
606 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
607 Location out,
608 Location ref,
609 Location obj,
610 uint32_t offset,
611 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000612 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000613 out_(out),
614 ref_(ref),
615 obj_(obj),
616 offset_(offset),
617 index_(index) {
618 DCHECK(kEmitCompilerReadBarrier);
619 // If `obj` is equal to `out` or `ref`, it means the initial object
620 // has been overwritten by (or after) the heap object reference load
621 // to be instrumented, e.g.:
622 //
623 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000624 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000625 //
626 // In that case, we have lost the information about the original
627 // object, and the emitted read barrier cannot work properly.
628 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
629 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
630 }
631
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100632 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000633 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
634 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100635 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000636 DCHECK(locations->CanCall());
637 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100638 DCHECK(instruction_->IsInstanceFieldGet() ||
639 instruction_->IsStaticFieldGet() ||
640 instruction_->IsArrayGet() ||
641 instruction_->IsInstanceOf() ||
642 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700643 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000644 << "Unexpected instruction in read barrier for heap reference slow path: "
645 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000646 // The read barrier instrumentation of object ArrayGet
647 // instructions does not support the HIntermediateAddress
648 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000649 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100650 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000651
652 __ Bind(GetEntryLabel());
653
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000654 SaveLiveRegisters(codegen, locations);
655
656 // We may have to change the index's value, but as `index_` is a
657 // constant member (like other "inputs" of this slow path),
658 // introduce a copy of it, `index`.
659 Location index = index_;
660 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100661 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000662 if (instruction_->IsArrayGet()) {
663 // Compute the actual memory offset and store it in `index`.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100664 Register index_reg = RegisterFrom(index_, DataType::Type::kInt32);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000665 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
666 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
667 // We are about to change the value of `index_reg` (see the
668 // calls to vixl::MacroAssembler::Lsl and
669 // vixl::MacroAssembler::Mov below), but it has
670 // not been saved by the previous call to
671 // art::SlowPathCode::SaveLiveRegisters, as it is a
672 // callee-save register --
673 // art::SlowPathCode::SaveLiveRegisters does not consider
674 // callee-save registers, as it has been designed with the
675 // assumption that callee-save registers are supposed to be
676 // handled by the called function. So, as a callee-save
677 // register, `index_reg` _would_ eventually be saved onto
678 // the stack, but it would be too late: we would have
679 // changed its value earlier. Therefore, we manually save
680 // it here into another freely available register,
681 // `free_reg`, chosen of course among the caller-save
682 // registers (as a callee-save `free_reg` register would
683 // exhibit the same problem).
684 //
685 // Note we could have requested a temporary register from
686 // the register allocator instead; but we prefer not to, as
687 // this is a slow path, and we know we can find a
688 // caller-save register that is available.
689 Register free_reg = FindAvailableCallerSaveRegister(codegen);
690 __ Mov(free_reg.W(), index_reg);
691 index_reg = free_reg;
692 index = LocationFrom(index_reg);
693 } else {
694 // The initial register stored in `index_` has already been
695 // saved in the call to art::SlowPathCode::SaveLiveRegisters
696 // (as it is not a callee-save register), so we can freely
697 // use it.
698 }
699 // Shifting the index value contained in `index_reg` by the scale
700 // factor (2) cannot overflow in practice, as the runtime is
701 // unable to allocate object arrays with a size larger than
702 // 2^26 - 1 (that is, 2^28 - 4 bytes).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100703 __ Lsl(index_reg, index_reg, DataType::SizeShift(type));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000704 static_assert(
705 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
706 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
707 __ Add(index_reg, index_reg, Operand(offset_));
708 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100709 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
710 // intrinsics, `index_` is not shifted by a scale factor of 2
711 // (as in the case of ArrayGet), as it is actually an offset
712 // to an object field within an object.
713 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000714 DCHECK(instruction_->GetLocations()->Intrinsified());
715 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
716 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
717 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100718 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100719 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000720 }
721 }
722
723 // We're moving two or three locations to locations that could
724 // overlap, so we need a parallel move resolver.
725 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100726 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000727 parallel_move.AddMove(ref_,
728 LocationFrom(calling_convention.GetRegisterAt(0)),
729 type,
730 nullptr);
731 parallel_move.AddMove(obj_,
732 LocationFrom(calling_convention.GetRegisterAt(1)),
733 type,
734 nullptr);
735 if (index.IsValid()) {
736 parallel_move.AddMove(index,
737 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100738 DataType::Type::kInt32,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000739 nullptr);
740 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
741 } else {
742 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
743 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
744 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000745 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000746 instruction_,
747 instruction_->GetDexPc(),
748 this);
749 CheckEntrypointTypes<
750 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
751 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
752
753 RestoreLiveRegisters(codegen, locations);
754
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000755 __ B(GetExitLabel());
756 }
757
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100758 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000759
760 private:
761 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100762 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
763 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000764 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
765 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
766 return Register(VIXLRegCodeFromART(i), kXRegSize);
767 }
768 }
769 // We shall never fail to find a free caller-save register, as
770 // there are more than two core caller-save registers on ARM64
771 // (meaning it is possible to find one which is different from
772 // `ref` and `obj`).
773 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
774 LOG(FATAL) << "Could not find a free register";
775 UNREACHABLE();
776 }
777
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000778 const Location out_;
779 const Location ref_;
780 const Location obj_;
781 const uint32_t offset_;
782 // An additional location containing an index to an array.
783 // Only used for HArrayGet and the UnsafeGetObject &
784 // UnsafeGetObjectVolatile intrinsics.
785 const Location index_;
786
787 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
788};
789
790// Slow path generating a read barrier for a GC root.
791class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
792 public:
793 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000794 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000795 DCHECK(kEmitCompilerReadBarrier);
796 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000797
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100798 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000799 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100800 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000801 DCHECK(locations->CanCall());
802 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000803 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
804 << "Unexpected instruction in read barrier for GC root slow path: "
805 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000806
807 __ Bind(GetEntryLabel());
808 SaveLiveRegisters(codegen, locations);
809
810 InvokeRuntimeCallingConvention calling_convention;
811 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
812 // The argument of the ReadBarrierForRootSlow is not a managed
813 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
814 // thus we need a 64-bit move here, and we cannot use
815 //
816 // arm64_codegen->MoveLocation(
817 // LocationFrom(calling_convention.GetRegisterAt(0)),
818 // root_,
819 // type);
820 //
821 // which would emit a 32-bit move, as `type` is a (32-bit wide)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100822 // reference type (`DataType::Type::kReference`).
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000823 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000824 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000825 instruction_,
826 instruction_->GetDexPc(),
827 this);
828 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
829 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
830
831 RestoreLiveRegisters(codegen, locations);
832 __ B(GetExitLabel());
833 }
834
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100835 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000836
837 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000838 const Location out_;
839 const Location root_;
840
841 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
842};
843
Alexandre Rames5319def2014-10-23 10:03:10 +0100844#undef __
845
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100846Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100847 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100848 if (type == DataType::Type::kVoid) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100849 LOG(FATAL) << "Unreachable type " << type;
850 }
851
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100852 if (DataType::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100853 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
854 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100855 } else if (!DataType::IsFloatingPointType(type) &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000856 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000857 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
858 } else {
859 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100860 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
861 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100862 }
863
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000864 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100865 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100866 return next_location;
867}
868
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100869Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100870 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100871}
872
Serban Constantinescu579885a2015-02-22 20:51:33 +0000873CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100874 const CompilerOptions& compiler_options,
875 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100876 : CodeGenerator(graph,
877 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000878 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000879 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100880 callee_saved_core_registers.GetList(),
881 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100882 compiler_options,
883 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100884 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
885 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100886 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000887 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100888 move_resolver_(graph->GetAllocator(), this),
Artem Serovaa6f4832018-11-21 18:57:54 +0000889 assembler_(graph->GetAllocator(),
890 compiler_options.GetInstructionSetFeatures()->AsArm64InstructionSetFeatures()),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000891 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100892 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000893 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100894 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000895 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100896 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +0100897 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100898 call_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100899 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100900 uint32_literals_(std::less<uint32_t>(),
901 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
902 uint64_literals_(std::less<uint64_t>(),
903 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000904 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100905 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000906 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +0000907 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
908 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
909 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000910 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000911 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000912}
Alexandre Rames5319def2014-10-23 10:03:10 +0100913
Alexandre Rames67555f72014-11-18 10:55:16 +0000914#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100915
Zheng Xu3927c8b2015-11-18 17:46:25 +0800916void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100917 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800918 jump_table->EmitTable(this);
919 }
920}
921
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000922void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800923 EmitJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +0000924
925 // Emit JIT baker read barrier slow paths.
926 DCHECK(Runtime::Current()->UseJitCompilation() || jit_baker_read_barrier_slow_paths_.empty());
927 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
928 uint32_t encoded_data = entry.first;
929 vixl::aarch64::Label* slow_path_entry = &entry.second.label;
930 __ Bind(slow_path_entry);
Andreas Gampe3db70682018-12-26 15:12:03 -0800931 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name= */ nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +0000932 }
933
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000934 // Ensure we emit the literal pool.
935 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000936
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000937 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +0000938
939 // Verify Baker read barrier linker patches.
940 if (kIsDebugBuild) {
941 ArrayRef<const uint8_t> code = allocator->GetMemory();
942 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
943 DCHECK(info.label.IsBound());
944 uint32_t literal_offset = info.label.GetLocation();
945 DCHECK_ALIGNED(literal_offset, 4u);
946
947 auto GetInsn = [&code](uint32_t offset) {
948 DCHECK_ALIGNED(offset, 4u);
949 return
950 (static_cast<uint32_t>(code[offset + 0]) << 0) +
951 (static_cast<uint32_t>(code[offset + 1]) << 8) +
952 (static_cast<uint32_t>(code[offset + 2]) << 16)+
953 (static_cast<uint32_t>(code[offset + 3]) << 24);
954 };
955
956 const uint32_t encoded_data = info.custom_data;
957 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
958 // Check that the next instruction matches the expected LDR.
959 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +0100960 case BakerReadBarrierKind::kField:
961 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +0000962 DCHECK_GE(code.size() - literal_offset, 8u);
963 uint32_t next_insn = GetInsn(literal_offset + 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +0000964 CheckValidReg(next_insn & 0x1fu); // Check destination register.
965 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko0ecac682018-08-07 10:40:38 +0100966 if (kind == BakerReadBarrierKind::kField) {
967 // LDR (immediate) with correct base_reg.
968 CHECK_EQ(next_insn & 0xffc003e0u, 0xb9400000u | (base_reg << 5));
969 } else {
970 DCHECK(kind == BakerReadBarrierKind::kAcquire);
971 // LDAR with correct base_reg.
972 CHECK_EQ(next_insn & 0xffffffe0u, 0x88dffc00u | (base_reg << 5));
973 }
Vladimir Markoca1e0382018-04-11 09:58:41 +0000974 break;
975 }
976 case BakerReadBarrierKind::kArray: {
977 DCHECK_GE(code.size() - literal_offset, 8u);
978 uint32_t next_insn = GetInsn(literal_offset + 4u);
979 // LDR (register) with the correct base_reg, size=10 (32-bit), option=011 (extend = LSL),
980 // and S=1 (shift amount = 2 for 32-bit version), i.e. LDR Wt, [Xn, Xm, LSL #2].
981 CheckValidReg(next_insn & 0x1fu); // Check destination register.
982 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
983 CHECK_EQ(next_insn & 0xffe0ffe0u, 0xb8607800u | (base_reg << 5));
984 CheckValidReg((next_insn >> 16) & 0x1f); // Check index register
985 break;
986 }
987 case BakerReadBarrierKind::kGcRoot: {
988 DCHECK_GE(literal_offset, 4u);
989 uint32_t prev_insn = GetInsn(literal_offset - 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +0000990 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko94796f82018-08-08 15:15:33 +0100991 // Usually LDR (immediate) with correct root_reg but
992 // we may have a "MOV marked, old_value" for UnsafeCASObject.
993 if ((prev_insn & 0xffe0ffff) != (0x2a0003e0 | root_reg)) { // MOV?
994 CHECK_EQ(prev_insn & 0xffc0001fu, 0xb9400000u | root_reg); // LDR?
995 }
Vladimir Markoca1e0382018-04-11 09:58:41 +0000996 break;
997 }
998 default:
999 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
1000 UNREACHABLE();
1001 }
1002 }
1003 }
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001004}
1005
Zheng Xuad4450e2015-04-17 18:48:56 +08001006void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1007 // Note: There are 6 kinds of moves:
1008 // 1. constant -> GPR/FPR (non-cycle)
1009 // 2. constant -> stack (non-cycle)
1010 // 3. GPR/FPR -> GPR/FPR
1011 // 4. GPR/FPR -> stack
1012 // 5. stack -> GPR/FPR
1013 // 6. stack -> stack (non-cycle)
1014 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1015 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1016 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1017 // dependency.
1018 vixl_temps_.Open(GetVIXLAssembler());
1019}
1020
1021void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1022 vixl_temps_.Close();
1023}
1024
1025Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001026 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1027 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1028 || kind == Location::kSIMDStackSlot);
1029 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1030 ? Location::kFpuRegister
1031 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001032 Location scratch = GetScratchLocation(kind);
1033 if (!scratch.Equals(Location::NoLocation())) {
1034 return scratch;
1035 }
1036 // Allocate from VIXL temp registers.
1037 if (kind == Location::kRegister) {
1038 scratch = LocationFrom(vixl_temps_.AcquireX());
1039 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001040 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001041 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1042 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1043 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001044 }
1045 AddScratchLocation(scratch);
1046 return scratch;
1047}
1048
1049void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1050 if (loc.IsRegister()) {
1051 vixl_temps_.Release(XRegisterFrom(loc));
1052 } else {
1053 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001054 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001055 }
1056 RemoveScratchLocation(loc);
1057}
1058
Alexandre Rames3e69f162014-12-10 10:36:50 +00001059void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001060 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001061 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001062}
1063
Alexandre Rames5319def2014-10-23 10:03:10 +01001064void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001065 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001066 __ Bind(&frame_entry_label_);
1067
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001068 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1069 UseScratchRegisterScope temps(masm);
1070 Register temp = temps.AcquireX();
Nicolas Geoffray7ab07772019-08-30 08:26:59 +00001071 __ Ldrh(temp, MemOperand(kArtMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
1072 __ Add(temp, temp, 1);
Mathieu Chartier7f8678e2019-08-30 16:22:28 -07001073 // Subtract one if the counter would overflow.
1074 __ Sub(temp, temp, Operand(temp, LSR, 16));
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001075 __ Strh(temp, MemOperand(kArtMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
1076 }
1077
Vladimir Marko33bff252017-11-01 14:35:42 +00001078 bool do_overflow_check =
1079 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001080 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001081 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001082 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001083 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001084 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001085 {
1086 // Ensure that between load and RecordPcInfo there are no pools emitted.
1087 ExactAssemblyScope eas(GetVIXLAssembler(),
1088 kInstructionSize,
1089 CodeBufferCheckScope::kExactSize);
1090 __ ldr(wzr, MemOperand(temp, 0));
1091 RecordPcInfo(nullptr, 0);
1092 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001093 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001094
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001095 if (!HasEmptyFrame()) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001096 // Stack layout:
1097 // sp[frame_size - 8] : lr.
1098 // ... : other preserved core registers.
1099 // ... : other preserved fp registers.
1100 // ... : reserved frame space.
1101 // sp[0] : current method.
Vladimir Marko1a225a72019-07-05 13:37:42 +01001102 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1103 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1104 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1105 DCHECK(!preserved_core_registers.IsEmpty());
1106 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1107 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001108
Vladimir Marko1a225a72019-07-05 13:37:42 +01001109 // Save the current method if we need it, or if using STP reduces code
1110 // size. Note that we do not do this in HCurrentMethod, as the
1111 // instruction might have been removed in the SSA graph.
1112 CPURegister lowest_spill;
1113 if (core_spills_offset == kXRegSizeInBytes) {
1114 // If there is no gap between the method and the lowest core spill, use
1115 // aligned STP pre-index to store both. Max difference is 512. We do
1116 // that to reduce code size even if we do not have to save the method.
1117 DCHECK_LE(frame_size, 512); // 32 core registers are only 256 bytes.
1118 lowest_spill = preserved_core_registers.PopLowestIndex();
1119 __ Stp(kArtMethodRegister, lowest_spill, MemOperand(sp, -frame_size, PreIndex));
1120 } else if (RequiresCurrentMethod()) {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001121 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001122 } else {
1123 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001124 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001125 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Vladimir Marko1a225a72019-07-05 13:37:42 +01001126 if (lowest_spill.IsValid()) {
1127 GetAssembler()->cfi().RelOffset(DWARFReg(lowest_spill), core_spills_offset);
1128 core_spills_offset += kXRegSizeInBytes;
1129 }
1130 GetAssembler()->SpillRegisters(preserved_core_registers, core_spills_offset);
1131 GetAssembler()->SpillRegisters(preserved_fp_registers, fp_spills_offset);
Mingyao Yang063fc772016-08-02 11:02:54 -07001132
1133 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1134 // Initialize should_deoptimize flag to 0.
1135 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1136 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1137 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001138 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01001139
Andreas Gampe3db70682018-12-26 15:12:03 -08001140 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001141}
1142
1143void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001144 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001145 if (!HasEmptyFrame()) {
Vladimir Marko1a225a72019-07-05 13:37:42 +01001146 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1147 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1148 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1149 DCHECK(!preserved_core_registers.IsEmpty());
1150 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1151 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
1152
1153 CPURegister lowest_spill;
1154 if (core_spills_offset == kXRegSizeInBytes) {
1155 // If there is no gap between the method and the lowest core spill, use
1156 // aligned LDP pre-index to pop both. Max difference is 504. We do
1157 // that to reduce code size even though the loaded method is unused.
1158 DCHECK_LE(frame_size, 504); // 32 core registers are only 256 bytes.
1159 lowest_spill = preserved_core_registers.PopLowestIndex();
1160 core_spills_offset += kXRegSizeInBytes;
1161 }
1162 GetAssembler()->UnspillRegisters(preserved_fp_registers, fp_spills_offset);
1163 GetAssembler()->UnspillRegisters(preserved_core_registers, core_spills_offset);
1164 if (lowest_spill.IsValid()) {
1165 __ Ldp(xzr, lowest_spill, MemOperand(sp, frame_size, PostIndex));
1166 GetAssembler()->cfi().Restore(DWARFReg(lowest_spill));
1167 } else {
1168 __ Drop(frame_size);
1169 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001170 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001171 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001172 __ Ret();
1173 GetAssembler()->cfi().RestoreState();
1174 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001175}
1176
Scott Wakeling97c72b72016-06-24 16:19:36 +01001177CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001178 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001179 return CPURegList(CPURegister::kRegister, kXRegSize,
1180 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001181}
1182
Scott Wakeling97c72b72016-06-24 16:19:36 +01001183CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001184 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1185 GetNumberOfFloatingPointRegisters()));
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001186 return CPURegList(CPURegister::kVRegister, kDRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001187 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001188}
1189
Alexandre Rames5319def2014-10-23 10:03:10 +01001190void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1191 __ Bind(GetLabelOf(block));
1192}
1193
Calin Juravle175dc732015-08-25 15:42:32 +01001194void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1195 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001196 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001197}
1198
Calin Juravlee460d1d2015-09-29 04:52:17 +01001199void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1200 if (location.IsRegister()) {
1201 locations->AddTemp(location);
1202 } else {
1203 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1204 }
1205}
1206
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001207void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001208 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001209 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001210 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001211 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001212 if (value_can_be_null) {
1213 __ Cbz(value, &done);
1214 }
Roland Levillainc73f0522018-08-14 15:16:50 +01001215 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07001216 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01001217 // Calculate the offset (in the card table) of the card corresponding to
1218 // `object`.
Alexandre Rames5319def2014-10-23 10:03:10 +01001219 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Roland Levillainc73f0522018-08-14 15:16:50 +01001220 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
1221 // `object`'s card.
1222 //
1223 // Register `card` contains the address of the card table. Note that the card
1224 // table's base is biased during its creation so that it always starts at an
1225 // address whose least-significant byte is equal to `kCardDirty` (see
1226 // art::gc::accounting::CardTable::Create). Therefore the STRB instruction
1227 // below writes the `kCardDirty` (byte) value into the `object`'s card
1228 // (located at `card + object >> kCardShift`).
1229 //
1230 // This dual use of the value in register `card` (1. to calculate the location
1231 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
1232 // (no need to explicitly load `kCardDirty` as an immediate value).
Serban Constantinescu02164b32014-11-13 14:05:07 +00001233 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001234 if (value_can_be_null) {
1235 __ Bind(&done);
1236 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001237}
1238
David Brazdil58282f42016-01-14 12:45:10 +00001239void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001240 // Blocked core registers:
1241 // lr : Runtime reserved.
1242 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001243 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001244 // ip1 : VIXL core temp.
1245 // ip0 : VIXL core temp.
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001246 // x18 : Platform register.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001247 //
1248 // Blocked fp registers:
1249 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001250 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1251 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001252 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001253 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001254 }
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001255 blocked_core_registers_[X18] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001256
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001257 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001258 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001259 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001260 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001261
David Brazdil58282f42016-01-14 12:45:10 +00001262 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001263 // Stubs do not save callee-save floating point registers. If the graph
1264 // is debuggable, we need to deal with these registers differently. For
1265 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001266 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1267 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001268 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001269 }
1270 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001271}
1272
Alexandre Rames3e69f162014-12-10 10:36:50 +00001273size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1274 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1275 __ Str(reg, MemOperand(sp, stack_index));
1276 return kArm64WordSize;
1277}
1278
1279size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1280 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1281 __ Ldr(reg, MemOperand(sp, stack_index));
1282 return kArm64WordSize;
1283}
1284
Artem Serov9df37b92019-07-23 16:41:54 +01001285size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1286 uint32_t reg_id ATTRIBUTE_UNUSED) {
1287 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1288 << "use SaveRestoreLiveRegistersHelper";
1289 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001290}
1291
Artem Serov9df37b92019-07-23 16:41:54 +01001292size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1293 uint32_t reg_id ATTRIBUTE_UNUSED) {
1294 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1295 << "use SaveRestoreLiveRegistersHelper";
1296 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001297}
1298
Alexandre Rames5319def2014-10-23 10:03:10 +01001299void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001300 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001301}
1302
1303void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001304 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001305}
1306
Vladimir Markoa0431112018-06-25 09:32:54 +01001307const Arm64InstructionSetFeatures& CodeGeneratorARM64::GetInstructionSetFeatures() const {
1308 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArm64InstructionSetFeatures();
1309}
1310
Alexandre Rames67555f72014-11-18 10:55:16 +00001311void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001312 if (constant->IsIntConstant()) {
1313 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1314 } else if (constant->IsLongConstant()) {
1315 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1316 } else if (constant->IsNullConstant()) {
1317 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001318 } else if (constant->IsFloatConstant()) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001319 __ Fmov(VRegister(destination), constant->AsFloatConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001320 } else {
1321 DCHECK(constant->IsDoubleConstant());
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001322 __ Fmov(VRegister(destination), constant->AsDoubleConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001323 }
1324}
1325
Alexandre Rames3e69f162014-12-10 10:36:50 +00001326
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001327static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001328 DCHECK(constant.IsConstant());
1329 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001330 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001331 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001332 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1333 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1334 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1335 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001336}
1337
Roland Levillain952b2352017-05-03 19:49:14 +01001338// Allocate a scratch register from the VIXL pool, querying first
1339// the floating-point register pool, and then the core register
1340// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001341// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1342// using a different allocation strategy.
1343static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1344 vixl::aarch64::UseScratchRegisterScope* temps,
1345 int size_in_bits) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001346 return masm->GetScratchVRegisterList()->IsEmpty()
Roland Levillain558dea12017-01-27 19:40:44 +00001347 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1348 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1349}
1350
Calin Juravlee460d1d2015-09-29 04:52:17 +01001351void CodeGeneratorARM64::MoveLocation(Location destination,
1352 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001353 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001354 if (source.Equals(destination)) {
1355 return;
1356 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001357
1358 // A valid move can always be inferred from the destination and source
1359 // locations. When moving from and to a register, the argument type can be
1360 // used to generate 32bit instead of 64bit moves. In debug mode we also
1361 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001362 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001363
1364 if (destination.IsRegister() || destination.IsFpuRegister()) {
1365 if (unspecified_type) {
1366 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1367 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001368 (src_cst != nullptr && (src_cst->IsIntConstant()
1369 || src_cst->IsFloatConstant()
1370 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001371 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001372 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001373 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001374 // If the source is a double stack slot or a 64bit constant, a 64bit
1375 // type is appropriate. Else the source is a register, and since the
1376 // type has not been specified, we chose a 64bit type to force a 64bit
1377 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001378 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001379 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001380 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001381 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1382 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001383 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001384 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1385 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1386 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001387 } else if (source.IsSIMDStackSlot()) {
1388 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001389 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001390 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001391 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001392 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001393 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001394 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001395 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001396 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001397 DataType::Type source_type = DataType::Is64BitType(dst_type)
1398 ? DataType::Type::kInt64
1399 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001400 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1401 }
1402 } else {
1403 DCHECK(source.IsFpuRegister());
1404 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001405 DataType::Type source_type = DataType::Is64BitType(dst_type)
1406 ? DataType::Type::kFloat64
1407 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001408 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1409 } else {
1410 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001411 if (GetGraph()->HasSIMD()) {
1412 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1413 } else {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001414 __ Fmov(VRegister(dst), FPRegisterFrom(source, dst_type));
Artem Serovd4bccf12017-04-03 18:47:32 +01001415 }
1416 }
1417 }
1418 } else if (destination.IsSIMDStackSlot()) {
1419 if (source.IsFpuRegister()) {
1420 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1421 } else {
1422 DCHECK(source.IsSIMDStackSlot());
1423 UseScratchRegisterScope temps(GetVIXLAssembler());
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001424 if (GetVIXLAssembler()->GetScratchVRegisterList()->IsEmpty()) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001425 Register temp = temps.AcquireX();
1426 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1427 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1428 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1429 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1430 } else {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001431 VRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
Artem Serovd4bccf12017-04-03 18:47:32 +01001432 __ Ldr(temp, StackOperandFrom(source));
1433 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001434 }
1435 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001436 } else { // The destination is not a register. It must be a stack slot.
1437 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1438 if (source.IsRegister() || source.IsFpuRegister()) {
1439 if (unspecified_type) {
1440 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001441 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001442 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001443 dst_type =
1444 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001445 }
1446 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001447 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1448 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001449 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001450 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001451 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1452 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001453 UseScratchRegisterScope temps(GetVIXLAssembler());
1454 HConstant* src_cst = source.GetConstant();
1455 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001456 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001457 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1458 ? Register(xzr)
1459 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001460 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001461 if (src_cst->IsIntConstant()) {
1462 temp = temps.AcquireW();
1463 } else if (src_cst->IsLongConstant()) {
1464 temp = temps.AcquireX();
1465 } else if (src_cst->IsFloatConstant()) {
1466 temp = temps.AcquireS();
1467 } else {
1468 DCHECK(src_cst->IsDoubleConstant());
1469 temp = temps.AcquireD();
1470 }
1471 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001472 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001473 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001474 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001475 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001476 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001477 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001478 // Use any scratch register (a core or a floating-point one)
1479 // from VIXL scratch register pools as a temporary.
1480 //
1481 // We used to only use the FP scratch register pool, but in some
1482 // rare cases the only register from this pool (D31) would
1483 // already be used (e.g. within a ParallelMove instruction, when
1484 // a move is blocked by a another move requiring a scratch FP
1485 // register, which would reserve D31). To prevent this issue, we
1486 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001487 //
1488 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001489 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001490 // use AcquireFPOrCoreCPURegisterOfSize instead of
1491 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1492 // allocates core scratch registers first.
1493 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1494 GetVIXLAssembler(),
1495 &temps,
1496 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001497 __ Ldr(temp, StackOperandFrom(source));
1498 __ Str(temp, StackOperandFrom(destination));
1499 }
1500 }
1501}
1502
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001503void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001504 CPURegister dst,
1505 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001506 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001507 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001508 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001509 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001510 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001511 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001512 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001513 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001514 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001515 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001516 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001517 case DataType::Type::kInt16:
1518 __ Ldrsh(Register(dst), src);
1519 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001520 case DataType::Type::kInt32:
1521 case DataType::Type::kReference:
1522 case DataType::Type::kInt64:
1523 case DataType::Type::kFloat32:
1524 case DataType::Type::kFloat64:
1525 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001526 __ Ldr(dst, src);
1527 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001528 case DataType::Type::kUint32:
1529 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001530 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001531 LOG(FATAL) << "Unreachable type " << type;
1532 }
1533}
1534
Calin Juravle77520bc2015-01-12 18:45:46 +00001535void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001536 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001537 const MemOperand& src,
1538 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001539 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001540 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001541 Register temp_base = temps.AcquireX();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001542 DataType::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001543
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001544 DCHECK(!src.IsPreIndex());
1545 DCHECK(!src.IsPostIndex());
1546
1547 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001548 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001549 {
1550 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1551 MemOperand base = MemOperand(temp_base);
1552 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001553 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001554 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001555 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001556 {
1557 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1558 __ ldarb(Register(dst), base);
1559 if (needs_null_check) {
1560 MaybeRecordImplicitNullCheck(instruction);
1561 }
1562 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001563 if (type == DataType::Type::kInt8) {
1564 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001565 }
1566 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001567 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001568 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001569 {
1570 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1571 __ ldarh(Register(dst), base);
1572 if (needs_null_check) {
1573 MaybeRecordImplicitNullCheck(instruction);
1574 }
1575 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001576 if (type == DataType::Type::kInt16) {
1577 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
1578 }
Artem Serov914d7a82017-02-07 14:33:49 +00001579 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001580 case DataType::Type::kInt32:
1581 case DataType::Type::kReference:
1582 case DataType::Type::kInt64:
1583 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001584 {
1585 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1586 __ ldar(Register(dst), base);
1587 if (needs_null_check) {
1588 MaybeRecordImplicitNullCheck(instruction);
1589 }
1590 }
1591 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001592 case DataType::Type::kFloat32:
1593 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00001594 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001595 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001596
Artem Serov914d7a82017-02-07 14:33:49 +00001597 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1598 {
1599 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1600 __ ldar(temp, base);
1601 if (needs_null_check) {
1602 MaybeRecordImplicitNullCheck(instruction);
1603 }
1604 }
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001605 __ Fmov(VRegister(dst), temp);
Artem Serov914d7a82017-02-07 14:33:49 +00001606 break;
Roland Levillain44015862016-01-22 11:47:17 +00001607 }
Aart Bik66c158e2018-01-31 12:55:04 -08001608 case DataType::Type::kUint32:
1609 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001610 case DataType::Type::kVoid:
Artem Serov914d7a82017-02-07 14:33:49 +00001611 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001612 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001613 }
1614}
1615
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001616void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001617 CPURegister src,
1618 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001619 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001620 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001621 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001622 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001623 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001624 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001625 case DataType::Type::kUint16:
1626 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001627 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001628 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001629 case DataType::Type::kInt32:
1630 case DataType::Type::kReference:
1631 case DataType::Type::kInt64:
1632 case DataType::Type::kFloat32:
1633 case DataType::Type::kFloat64:
1634 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001635 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001636 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001637 case DataType::Type::kUint32:
1638 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001639 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001640 LOG(FATAL) << "Unreachable type " << type;
1641 }
1642}
1643
Artem Serov914d7a82017-02-07 14:33:49 +00001644void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001645 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001646 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00001647 const MemOperand& dst,
1648 bool needs_null_check) {
1649 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001650 UseScratchRegisterScope temps(GetVIXLAssembler());
1651 Register temp_base = temps.AcquireX();
1652
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001653 DCHECK(!dst.IsPreIndex());
1654 DCHECK(!dst.IsPostIndex());
1655
1656 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001657 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001658 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001659 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00001660 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001661 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001662 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001663 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001664 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001665 {
1666 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1667 __ stlrb(Register(src), base);
1668 if (needs_null_check) {
1669 MaybeRecordImplicitNullCheck(instruction);
1670 }
1671 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001672 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001673 case DataType::Type::kUint16:
1674 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001675 {
1676 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1677 __ stlrh(Register(src), base);
1678 if (needs_null_check) {
1679 MaybeRecordImplicitNullCheck(instruction);
1680 }
1681 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001682 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001683 case DataType::Type::kInt32:
1684 case DataType::Type::kReference:
1685 case DataType::Type::kInt64:
1686 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001687 {
1688 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1689 __ stlr(Register(src), base);
1690 if (needs_null_check) {
1691 MaybeRecordImplicitNullCheck(instruction);
1692 }
1693 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001694 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001695 case DataType::Type::kFloat32:
1696 case DataType::Type::kFloat64: {
1697 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001698 Register temp_src;
1699 if (src.IsZero()) {
1700 // The zero register is used to avoid synthesizing zero constants.
1701 temp_src = Register(src);
1702 } else {
1703 DCHECK(src.IsFPRegister());
1704 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001705 __ Fmov(temp_src, VRegister(src));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001706 }
Artem Serov914d7a82017-02-07 14:33:49 +00001707 {
1708 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1709 __ stlr(temp_src, base);
1710 if (needs_null_check) {
1711 MaybeRecordImplicitNullCheck(instruction);
1712 }
1713 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001714 break;
1715 }
Aart Bik66c158e2018-01-31 12:55:04 -08001716 case DataType::Type::kUint32:
1717 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001718 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001719 LOG(FATAL) << "Unreachable type " << type;
1720 }
1721}
1722
Calin Juravle175dc732015-08-25 15:42:32 +01001723void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1724 HInstruction* instruction,
1725 uint32_t dex_pc,
1726 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001727 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00001728
Vladimir Markof6675082019-05-17 12:05:28 +01001729 ThreadOffset64 entrypoint_offset = GetThreadOffset<kArm64PointerSize>(entrypoint);
1730 // Reduce code size for AOT by using shared trampolines for slow path runtime calls across the
1731 // entire oat file. This adds an extra branch and we do not want to slow down the main path.
1732 // For JIT, thunk sharing is per-method, so the gains would be smaller or even negative.
1733 if (slow_path == nullptr || Runtime::Current()->UseJitCompilation()) {
1734 __ Ldr(lr, MemOperand(tr, entrypoint_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00001735 // Ensure the pc position is recorded immediately after the `blr` instruction.
1736 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1737 __ blr(lr);
1738 if (EntrypointRequiresStackMap(entrypoint)) {
1739 RecordPcInfo(instruction, dex_pc, slow_path);
1740 }
Vladimir Markof6675082019-05-17 12:05:28 +01001741 } else {
1742 // Ensure the pc position is recorded immediately after the `bl` instruction.
1743 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1744 EmitEntrypointThunkCall(entrypoint_offset);
1745 if (EntrypointRequiresStackMap(entrypoint)) {
1746 RecordPcInfo(instruction, dex_pc, slow_path);
1747 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001748 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001749}
1750
Roland Levillaindec8f632016-07-22 17:10:06 +01001751void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1752 HInstruction* instruction,
1753 SlowPathCode* slow_path) {
1754 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01001755 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1756 __ Blr(lr);
1757}
1758
Alexandre Rames67555f72014-11-18 10:55:16 +00001759void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001760 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001761 UseScratchRegisterScope temps(GetVIXLAssembler());
1762 Register temp = temps.AcquireW();
Vladimir Markodc682aa2018-01-04 18:42:57 +00001763 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001764 const size_t status_byte_offset =
1765 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1766 constexpr uint32_t shifted_visibly_initialized_value =
1767 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001768
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001769 // CMP (immediate) is limited to imm12 or imm12<<12, so we would need to materialize
1770 // the constant 0xf0000000 for comparison with the full 32-bit field. To reduce the code
1771 // size, load only the high byte of the field and compare with 0xf0.
1772 // Note: The same code size could be achieved with LDR+MNV(asr #24)+CBNZ but benchmarks
1773 // show that this pattern is slower (tested on little cores).
1774 __ Ldrb(temp, HeapOperand(class_reg, status_byte_offset));
1775 __ Cmp(temp, shifted_visibly_initialized_value);
1776 __ B(lo, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001777 __ Bind(slow_path->GetExitLabel());
1778}
Alexandre Rames5319def2014-10-23 10:03:10 +01001779
Vladimir Marko175e7862018-03-27 09:03:13 +00001780void InstructionCodeGeneratorARM64::GenerateBitstringTypeCheckCompare(
1781 HTypeCheckInstruction* check, vixl::aarch64::Register temp) {
1782 uint32_t path_to_root = check->GetBitstringPathToRoot();
1783 uint32_t mask = check->GetBitstringMask();
1784 DCHECK(IsPowerOfTwo(mask + 1));
1785 size_t mask_bits = WhichPowerOf2(mask + 1);
1786
1787 if (mask_bits == 16u) {
1788 // Load only the bitstring part of the status word.
1789 __ Ldrh(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1790 } else {
1791 // /* uint32_t */ temp = temp->status_
1792 __ Ldr(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1793 // Extract the bitstring bits.
1794 __ Ubfx(temp, temp, 0, mask_bits);
1795 }
1796 // Compare the bitstring bits to `path_to_root`.
1797 __ Cmp(temp, path_to_root);
1798}
1799
Roland Levillain44015862016-01-22 11:47:17 +00001800void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001801 BarrierType type = BarrierAll;
1802
1803 switch (kind) {
1804 case MemBarrierKind::kAnyAny:
1805 case MemBarrierKind::kAnyStore: {
1806 type = BarrierAll;
1807 break;
1808 }
1809 case MemBarrierKind::kLoadAny: {
1810 type = BarrierReads;
1811 break;
1812 }
1813 case MemBarrierKind::kStoreStore: {
1814 type = BarrierWrites;
1815 break;
1816 }
1817 default:
1818 LOG(FATAL) << "Unexpected memory barrier " << kind;
1819 }
1820 __ Dmb(InnerShareable, type);
1821}
1822
Serban Constantinescu02164b32014-11-13 14:05:07 +00001823void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1824 HBasicBlock* successor) {
1825 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001826 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1827 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001828 slow_path =
1829 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001830 instruction->SetSlowPath(slow_path);
1831 codegen_->AddSlowPath(slow_path);
1832 if (successor != nullptr) {
1833 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001834 }
1835 } else {
1836 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1837 }
1838
Serban Constantinescu02164b32014-11-13 14:05:07 +00001839 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1840 Register temp = temps.AcquireW();
1841
Andreas Gampe542451c2016-07-26 09:02:02 -07001842 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001843 if (successor == nullptr) {
1844 __ Cbnz(temp, slow_path->GetEntryLabel());
1845 __ Bind(slow_path->GetReturnLabel());
1846 } else {
1847 __ Cbz(temp, codegen_->GetLabelOf(successor));
1848 __ B(slow_path->GetEntryLabel());
1849 // slow_path will return to GetLabelOf(successor).
1850 }
1851}
1852
Alexandre Rames5319def2014-10-23 10:03:10 +01001853InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1854 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001855 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001856 assembler_(codegen->GetAssembler()),
1857 codegen_(codegen) {}
1858
Alexandre Rames67555f72014-11-18 10:55:16 +00001859void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001860 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001861 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001862 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001863 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001864 case DataType::Type::kInt32:
1865 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01001866 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001867 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001868 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001869 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001870
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001871 case DataType::Type::kFloat32:
1872 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001873 locations->SetInAt(0, Location::RequiresFpuRegister());
1874 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001875 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001876 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001877
Alexandre Rames5319def2014-10-23 10:03:10 +01001878 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001879 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001880 }
1881}
1882
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001883void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
1884 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001885 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1886
1887 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001888 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01001889 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001890 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
1891 object_field_get_with_read_barrier
1892 ? LocationSummary::kCallOnSlowPath
1893 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001894 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001895 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko0ecac682018-08-07 10:40:38 +01001896 // We need a temporary register for the read barrier load in
1897 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
1898 // only if the field is volatile or the offset is too big.
1899 if (field_info.IsVolatile() ||
1900 field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
1901 locations->AddTemp(FixedTempLocation());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001902 }
Vladimir Marko70e97462016-08-09 11:04:26 +01001903 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001904 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001905 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001906 locations->SetOut(Location::RequiresFpuRegister());
1907 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001908 // The output overlaps for an object field get when read barriers
1909 // are enabled: we do not want the load to overwrite the object's
1910 // location, as we need it to emit the read barrier.
1911 locations->SetOut(
1912 Location::RequiresRegister(),
1913 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001914 }
1915}
1916
1917void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1918 const FieldInfo& field_info) {
1919 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001920 LocationSummary* locations = instruction->GetLocations();
1921 Location base_loc = locations->InAt(0);
1922 Location out = locations->Out();
1923 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01001924 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
1925 DataType::Type load_type = instruction->GetType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001926 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001927
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001928 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01001929 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00001930 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00001931 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001932 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001933 Location maybe_temp =
1934 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00001935 // Note that potential implicit null checks are handled in this
1936 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1937 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1938 instruction,
1939 out,
1940 base,
1941 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001942 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08001943 /* needs_null_check= */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001944 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001945 } else {
1946 // General case.
1947 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001948 // Note that a potential implicit null check is handled in this
1949 // CodeGeneratorARM64::LoadAcquire call.
1950 // NB: LoadAcquire will record the pc info if needed.
1951 codegen_->LoadAcquire(
Andreas Gampe3db70682018-12-26 15:12:03 -08001952 instruction, OutputCPURegister(instruction), field, /* needs_null_check= */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001953 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00001954 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1955 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01001956 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001957 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001958 }
Vladimir Marko61b92282017-10-11 13:23:17 +01001959 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00001960 // If read barriers are enabled, emit read barriers other than
1961 // Baker's using a slow path (and also unpoison the loaded
1962 // reference, if heap poisoning is enabled).
1963 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1964 }
Roland Levillain4d027112015-07-01 15:41:14 +01001965 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001966}
1967
1968void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1969 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001970 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01001971 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001972 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1973 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001974 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001975 locations->SetInAt(1, Location::RequiresFpuRegister());
1976 } else {
1977 locations->SetInAt(1, Location::RequiresRegister());
1978 }
1979}
1980
1981void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001982 const FieldInfo& field_info,
1983 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001984 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
1985
1986 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001987 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001988 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001989 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001990 DataType::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001991
Roland Levillain4d027112015-07-01 15:41:14 +01001992 {
1993 // We use a block to end the scratch scope before the write barrier, thus
1994 // freeing the temporary registers so they can be used in `MarkGCCard`.
1995 UseScratchRegisterScope temps(GetVIXLAssembler());
1996
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001997 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01001998 DCHECK(value.IsW());
1999 Register temp = temps.AcquireW();
2000 __ Mov(temp, value.W());
2001 GetAssembler()->PoisonHeapReference(temp.W());
2002 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002003 }
Roland Levillain4d027112015-07-01 15:41:14 +01002004
2005 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002006 codegen_->StoreRelease(
Andreas Gampe3db70682018-12-26 15:12:03 -08002007 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check= */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002008 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002009 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2010 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002011 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2012 codegen_->MaybeRecordImplicitNullCheck(instruction);
2013 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002014 }
2015
2016 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002017 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002018 }
2019}
2020
Alexandre Rames67555f72014-11-18 10:55:16 +00002021void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002022 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002023
2024 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002025 case DataType::Type::kInt32:
2026 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002027 Register dst = OutputRegister(instr);
2028 Register lhs = InputRegisterAt(instr, 0);
2029 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002030 if (instr->IsAdd()) {
2031 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002032 } else if (instr->IsAnd()) {
2033 __ And(dst, lhs, rhs);
2034 } else if (instr->IsOr()) {
2035 __ Orr(dst, lhs, rhs);
2036 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002037 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002038 } else if (instr->IsRor()) {
2039 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002040 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002041 __ Ror(dst, lhs, shift);
2042 } else {
2043 // Ensure shift distance is in the same size register as the result. If
2044 // we are rotating a long and the shift comes in a w register originally,
2045 // we don't need to sxtw for use as an x since the shift distances are
2046 // all & reg_bits - 1.
2047 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2048 }
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002049 } else if (instr->IsMin() || instr->IsMax()) {
2050 __ Cmp(lhs, rhs);
2051 __ Csel(dst, lhs, rhs, instr->IsMin() ? lt : gt);
Alexandre Rames67555f72014-11-18 10:55:16 +00002052 } else {
2053 DCHECK(instr->IsXor());
2054 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002055 }
2056 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002057 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002058 case DataType::Type::kFloat32:
2059 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002060 VRegister dst = OutputFPRegister(instr);
2061 VRegister lhs = InputFPRegisterAt(instr, 0);
2062 VRegister rhs = InputFPRegisterAt(instr, 1);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002063 if (instr->IsAdd()) {
2064 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002065 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002066 __ Fsub(dst, lhs, rhs);
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002067 } else if (instr->IsMin()) {
2068 __ Fmin(dst, lhs, rhs);
2069 } else if (instr->IsMax()) {
2070 __ Fmax(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002071 } else {
2072 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002073 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002074 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002075 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002076 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002077 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002078 }
2079}
2080
Serban Constantinescu02164b32014-11-13 14:05:07 +00002081void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2082 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2083
Vladimir Markoca6fff82017-10-03 14:49:14 +01002084 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002085 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002086 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002087 case DataType::Type::kInt32:
2088 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002089 locations->SetInAt(0, Location::RequiresRegister());
2090 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002091 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002092 break;
2093 }
2094 default:
2095 LOG(FATAL) << "Unexpected shift type " << type;
2096 }
2097}
2098
2099void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2100 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2101
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002102 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002103 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002104 case DataType::Type::kInt32:
2105 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002106 Register dst = OutputRegister(instr);
2107 Register lhs = InputRegisterAt(instr, 0);
2108 Operand rhs = InputOperandAt(instr, 1);
2109 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002110 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002111 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002112 if (instr->IsShl()) {
2113 __ Lsl(dst, lhs, shift_value);
2114 } else if (instr->IsShr()) {
2115 __ Asr(dst, lhs, shift_value);
2116 } else {
2117 __ Lsr(dst, lhs, shift_value);
2118 }
2119 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002120 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002121
2122 if (instr->IsShl()) {
2123 __ Lsl(dst, lhs, rhs_reg);
2124 } else if (instr->IsShr()) {
2125 __ Asr(dst, lhs, rhs_reg);
2126 } else {
2127 __ Lsr(dst, lhs, rhs_reg);
2128 }
2129 }
2130 break;
2131 }
2132 default:
2133 LOG(FATAL) << "Unexpected shift operation type " << type;
2134 }
2135}
2136
Alexandre Rames5319def2014-10-23 10:03:10 +01002137void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002138 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002139}
2140
2141void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002142 HandleBinaryOp(instruction);
2143}
2144
2145void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2146 HandleBinaryOp(instruction);
2147}
2148
2149void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2150 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002151}
2152
Artem Serov7fc63502016-02-09 17:15:29 +00002153void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002154 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002155 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002156 locations->SetInAt(0, Location::RequiresRegister());
2157 // There is no immediate variant of negated bitwise instructions in AArch64.
2158 locations->SetInAt(1, Location::RequiresRegister());
2159 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2160}
2161
Artem Serov7fc63502016-02-09 17:15:29 +00002162void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002163 Register dst = OutputRegister(instr);
2164 Register lhs = InputRegisterAt(instr, 0);
2165 Register rhs = InputRegisterAt(instr, 1);
2166
2167 switch (instr->GetOpKind()) {
2168 case HInstruction::kAnd:
2169 __ Bic(dst, lhs, rhs);
2170 break;
2171 case HInstruction::kOr:
2172 __ Orn(dst, lhs, rhs);
2173 break;
2174 case HInstruction::kXor:
2175 __ Eon(dst, lhs, rhs);
2176 break;
2177 default:
2178 LOG(FATAL) << "Unreachable";
2179 }
2180}
2181
Anton Kirilov74234da2017-01-13 14:42:47 +00002182void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2183 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002184 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2185 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002186 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002187 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002188 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2189 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2190 } else {
2191 locations->SetInAt(0, Location::RequiresRegister());
2192 }
2193 locations->SetInAt(1, Location::RequiresRegister());
2194 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2195}
2196
Anton Kirilov74234da2017-01-13 14:42:47 +00002197void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2198 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002199 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002200 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002201 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002202 Register out = OutputRegister(instruction);
2203 Register left;
2204 if (kind != HInstruction::kNeg) {
2205 left = InputRegisterAt(instruction, 0);
2206 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002207 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002208 // shifter operand operation, the IR generating `right_reg` (input to the type
2209 // conversion) can have a different type from the current instruction's type,
2210 // so we manually indicate the type.
2211 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002212 Operand right_operand(0);
2213
Anton Kirilov74234da2017-01-13 14:42:47 +00002214 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2215 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002216 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2217 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002218 right_operand = Operand(right_reg,
2219 helpers::ShiftFromOpKind(op_kind),
2220 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002221 }
2222
2223 // Logical binary operations do not support extension operations in the
2224 // operand. Note that VIXL would still manage if it was passed by generating
2225 // the extension as a separate instruction.
2226 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2227 DCHECK(!right_operand.IsExtendedRegister() ||
2228 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2229 kind != HInstruction::kNeg));
2230 switch (kind) {
2231 case HInstruction::kAdd:
2232 __ Add(out, left, right_operand);
2233 break;
2234 case HInstruction::kAnd:
2235 __ And(out, left, right_operand);
2236 break;
2237 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002238 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002239 __ Neg(out, right_operand);
2240 break;
2241 case HInstruction::kOr:
2242 __ Orr(out, left, right_operand);
2243 break;
2244 case HInstruction::kSub:
2245 __ Sub(out, left, right_operand);
2246 break;
2247 case HInstruction::kXor:
2248 __ Eor(out, left, right_operand);
2249 break;
2250 default:
2251 LOG(FATAL) << "Unexpected operation kind: " << kind;
2252 UNREACHABLE();
2253 }
2254}
2255
Artem Serov328429f2016-07-06 16:23:04 +01002256void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002257 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002258 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002259 locations->SetInAt(0, Location::RequiresRegister());
2260 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002261 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002262}
2263
Roland Levillain19c54192016-11-04 13:44:09 +00002264void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002265 __ Add(OutputRegister(instruction),
2266 InputRegisterAt(instruction, 0),
2267 Operand(InputOperandAt(instruction, 1)));
2268}
2269
Artem Serove1811ed2017-04-27 16:50:47 +01002270void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2271 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002272 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002273
2274 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2275
2276 locations->SetInAt(0, Location::RequiresRegister());
2277 // For byte case we don't need to shift the index variable so we can encode the data offset into
2278 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2279 // data offset constant generation out of the loop and reduce the critical path length in the
2280 // loop.
2281 locations->SetInAt(1, shift->GetValue() == 0
2282 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2283 : Location::RequiresRegister());
2284 locations->SetInAt(2, Location::ConstantLocation(shift));
2285 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2286}
2287
2288void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2289 HIntermediateAddressIndex* instruction) {
2290 Register index_reg = InputRegisterAt(instruction, 0);
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002291 uint32_t shift = Int64FromLocation(instruction->GetLocations()->InAt(2));
Artem Serove1811ed2017-04-27 16:50:47 +01002292 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2293
2294 if (shift == 0) {
2295 __ Add(OutputRegister(instruction), index_reg, offset);
2296 } else {
2297 Register offset_reg = InputRegisterAt(instruction, 1);
2298 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2299 }
2300}
2301
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002302void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002303 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002304 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002305 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2306 if (instr->GetOpKind() == HInstruction::kSub &&
2307 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002308 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002309 // Don't allocate register for Mneg instruction.
2310 } else {
2311 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2312 Location::RequiresRegister());
2313 }
2314 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2315 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002316 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2317}
2318
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002319void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002320 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002321 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2322 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002323
2324 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2325 // This fixup should be carried out for all multiply-accumulate instructions:
2326 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002327 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002328 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2329 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002330 vixl::aarch64::Instruction* prev =
2331 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002332 if (prev->IsLoadOrStore()) {
2333 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002334 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002335 __ nop();
2336 }
2337 }
2338
2339 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002340 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002341 __ Madd(res, mul_left, mul_right, accumulator);
2342 } else {
2343 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002344 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002345 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002346 __ Mneg(res, mul_left, mul_right);
2347 } else {
2348 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2349 __ Msub(res, mul_left, mul_right, accumulator);
2350 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002351 }
2352}
2353
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002354void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002355 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002356 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002357 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002358 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2359 object_array_get_with_read_barrier
2360 ? LocationSummary::kCallOnSlowPath
2361 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002362 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002363 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002364 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002365 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002366 // We need a temporary register for the read barrier load in
2367 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2368 // only if the offset is too big.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002369 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2370 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002371 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002372 if (offset >= kReferenceLoadMinFarOffset) {
2373 locations->AddTemp(FixedTempLocation());
2374 }
Artem Serov0806f582018-10-11 20:14:20 +01002375 } else if (!instruction->GetArray()->IsIntermediateAddress()) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01002376 // We need a non-scratch temporary for the array data pointer in
Artem Serov0806f582018-10-11 20:14:20 +01002377 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier() for the case with no
2378 // intermediate address.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002379 locations->AddTemp(Location::RequiresRegister());
2380 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002381 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002382 locations->SetInAt(0, Location::RequiresRegister());
2383 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002384 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002385 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2386 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002387 // The output overlaps in the case of an object array get with
2388 // read barriers enabled: we do not want the move to overwrite the
2389 // array's location, as we need it to emit the read barrier.
2390 locations->SetOut(
2391 Location::RequiresRegister(),
2392 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002393 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002394}
2395
2396void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002397 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002398 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002399 LocationSummary* locations = instruction->GetLocations();
2400 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002401 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002402 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002403 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2404 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002405 MacroAssembler* masm = GetVIXLAssembler();
2406 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002407
Artem Serov0806f582018-10-11 20:14:20 +01002408 // The non-Baker read barrier instrumentation of object ArrayGet instructions
Roland Levillain19c54192016-11-04 13:44:09 +00002409 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002410 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002411 instruction->GetArray()->IsIntermediateAddress() &&
Artem Serov0806f582018-10-11 20:14:20 +01002412 kEmitCompilerReadBarrier &&
2413 !kUseBakerReadBarrier));
Roland Levillain19c54192016-11-04 13:44:09 +00002414
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002415 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002416 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002417 // Note that a potential implicit null check is handled in the
2418 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002419 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002420 if (index.IsConstant()) {
Artem Serov0806f582018-10-11 20:14:20 +01002421 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002422 // Array load with a constant index can be treated as a field load.
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002423 offset += Int64FromLocation(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002424 Location maybe_temp =
2425 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2426 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2427 out,
2428 obj.W(),
2429 offset,
2430 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002431 /* needs_null_check= */ false,
2432 /* use_load_acquire= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002433 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002434 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08002435 instruction, out, obj.W(), offset, index, /* needs_null_check= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002436 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002437 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002438 // General case.
2439 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002440 Register length;
2441 if (maybe_compressed_char_at) {
2442 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2443 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002444 {
2445 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2446 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2447
2448 if (instruction->GetArray()->IsIntermediateAddress()) {
2449 DCHECK_LT(count_offset, offset);
2450 int64_t adjusted_offset =
2451 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2452 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2453 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2454 } else {
2455 __ Ldr(length, HeapOperand(obj, count_offset));
2456 }
2457 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002458 }
jessicahandojo05765752016-09-09 19:01:32 -07002459 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002460 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002461 if (maybe_compressed_char_at) {
2462 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002463 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2464 "Expecting 0=compressed, 1=uncompressed");
2465 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002466 __ Ldrb(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002467 HeapOperand(obj, offset + Int64FromLocation(index)));
jessicahandojo05765752016-09-09 19:01:32 -07002468 __ B(&done);
2469 __ Bind(&uncompressed_load);
2470 __ Ldrh(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002471 HeapOperand(obj, offset + (Int64FromLocation(index) << 1)));
jessicahandojo05765752016-09-09 19:01:32 -07002472 __ Bind(&done);
2473 } else {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002474 offset += Int64FromLocation(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002475 source = HeapOperand(obj, offset);
2476 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002477 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002478 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002479 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002480 // We do not need to compute the intermediate address from the array: the
2481 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002482 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002483 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002484 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2485 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
Roland Levillain44015862016-01-22 11:47:17 +00002486 }
2487 temp = obj;
2488 } else {
2489 __ Add(temp, obj, offset);
2490 }
jessicahandojo05765752016-09-09 19:01:32 -07002491 if (maybe_compressed_char_at) {
2492 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002493 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2494 "Expecting 0=compressed, 1=uncompressed");
2495 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002496 __ Ldrb(Register(OutputCPURegister(instruction)),
2497 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2498 __ B(&done);
2499 __ Bind(&uncompressed_load);
2500 __ Ldrh(Register(OutputCPURegister(instruction)),
2501 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2502 __ Bind(&done);
2503 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002504 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002505 }
Roland Levillain44015862016-01-22 11:47:17 +00002506 }
jessicahandojo05765752016-09-09 19:01:32 -07002507 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002508 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2509 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002510 codegen_->Load(type, OutputCPURegister(instruction), source);
2511 codegen_->MaybeRecordImplicitNullCheck(instruction);
2512 }
Roland Levillain44015862016-01-22 11:47:17 +00002513
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002514 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002515 static_assert(
2516 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2517 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2518 Location obj_loc = locations->InAt(0);
2519 if (index.IsConstant()) {
2520 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2521 } else {
2522 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2523 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002524 }
Roland Levillain4d027112015-07-01 15:41:14 +01002525 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002526}
2527
Alexandre Rames5319def2014-10-23 10:03:10 +01002528void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002529 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002530 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002531 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002532}
2533
2534void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002535 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002536 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002537 {
2538 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2539 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2540 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2541 codegen_->MaybeRecordImplicitNullCheck(instruction);
2542 }
jessicahandojo05765752016-09-09 19:01:32 -07002543 // Mask out compression flag from String's array length.
2544 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002545 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002546 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002547}
2548
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002549void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002550 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002551
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002552 bool needs_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002553 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002554 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002555 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002556 locations->SetInAt(0, Location::RequiresRegister());
2557 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002558 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2559 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002560 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002561 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002562 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002563 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002564 }
2565}
2566
2567void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002568 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002569 LocationSummary* locations = instruction->GetLocations();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002570 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002571 bool needs_write_barrier =
2572 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002573
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002574 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002575 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002576 CPURegister source = value;
2577 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002578 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002579 MemOperand destination = HeapOperand(array);
2580 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002581
2582 if (!needs_write_barrier) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002583 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002584 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002585 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002586 destination = HeapOperand(array, offset);
2587 } else {
2588 UseScratchRegisterScope temps(masm);
2589 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002590 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002591 // We do not need to compute the intermediate address from the array: the
2592 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002593 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002594 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002595 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2596 DCHECK(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002597 }
2598 temp = array;
2599 } else {
2600 __ Add(temp, array, offset);
2601 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002602 destination = HeapOperand(temp,
2603 XRegisterFrom(index),
2604 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002605 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002606 }
Artem Serov914d7a82017-02-07 14:33:49 +00002607 {
2608 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2609 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2610 codegen_->Store(value_type, value, destination);
2611 codegen_->MaybeRecordImplicitNullCheck(instruction);
2612 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002613 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002614 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002615
2616 bool can_value_be_null = instruction->GetValueCanBeNull();
2617 vixl::aarch64::Label do_store;
2618 if (can_value_be_null) {
2619 __ Cbz(Register(value), &do_store);
2620 }
2621
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002622 SlowPathCodeARM64* slow_path = nullptr;
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002623 if (needs_type_check) {
2624 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
2625 codegen_->AddSlowPath(slow_path);
2626
2627 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2628 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2629 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2630
Alexandre Rames97833a02015-04-16 15:07:12 +01002631 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002632 Register temp = temps.AcquireSameSizeAs(array);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002633 Register temp2 = temps.AcquireSameSizeAs(array);
2634
2635 // Note that when Baker read barriers are enabled, the type
2636 // checks are performed without read barriers. This is fine,
2637 // even in the case where a class object is in the from-space
2638 // after the flip, as a comparison involving such a type would
2639 // not produce a false positive; it may of course produce a
2640 // false negative, in which case we would take the ArraySet
2641 // slow path.
2642
2643 // /* HeapReference<Class> */ temp = array->klass_
2644 {
2645 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2646 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2647 __ Ldr(temp, HeapOperand(array, class_offset));
2648 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames97833a02015-04-16 15:07:12 +01002649 }
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002650 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Alexandre Rames97833a02015-04-16 15:07:12 +01002651
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002652 // /* HeapReference<Class> */ temp = temp->component_type_
2653 __ Ldr(temp, HeapOperand(temp, component_offset));
2654 // /* HeapReference<Class> */ temp2 = value->klass_
2655 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2656 // If heap poisoning is enabled, no need to unpoison `temp`
2657 // nor `temp2`, as we are comparing two poisoned references.
2658 __ Cmp(temp, temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002659
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002660 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2661 vixl::aarch64::Label do_put;
2662 __ B(eq, &do_put);
2663 // If heap poisoning is enabled, the `temp` reference has
2664 // not been unpoisoned yet; unpoison it now.
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002665 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002666
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002667 // /* HeapReference<Class> */ temp = temp->super_class_
2668 __ Ldr(temp, HeapOperand(temp, super_offset));
2669 // If heap poisoning is enabled, no need to unpoison
2670 // `temp`, as we are comparing against null below.
2671 __ Cbnz(temp, slow_path->GetEntryLabel());
2672 __ Bind(&do_put);
Vladimir Markod1ef8732017-04-18 13:55:13 +01002673 } else {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002674 __ B(ne, slow_path->GetEntryLabel());
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002675 }
2676 }
2677
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002678 codegen_->MarkGCCard(array, value.W(), /* value_can_be_null= */ false);
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002679
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002680 if (can_value_be_null) {
2681 DCHECK(do_store.IsLinked());
2682 __ Bind(&do_store);
2683 }
2684
2685 UseScratchRegisterScope temps(masm);
2686 if (kPoisonHeapReferences) {
2687 Register temp_source = temps.AcquireSameSizeAs(array);
2688 DCHECK(value.IsW());
2689 __ Mov(temp_source, value.W());
2690 GetAssembler()->PoisonHeapReference(temp_source);
2691 source = temp_source;
2692 }
2693
2694 if (index.IsConstant()) {
2695 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
2696 destination = HeapOperand(array, offset);
2697 } else {
2698 Register temp_base = temps.AcquireSameSizeAs(array);
2699 __ Add(temp_base, array, offset);
2700 destination = HeapOperand(temp_base,
2701 XRegisterFrom(index),
2702 LSL,
2703 DataType::SizeShift(value_type));
2704 }
2705
2706 {
2707 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2708 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2709 __ Str(source, destination);
2710
2711 if (can_value_be_null || !needs_type_check) {
2712 codegen_->MaybeRecordImplicitNullCheck(instruction);
2713 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002714 }
2715
2716 if (slow_path != nullptr) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002717 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002718 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002719 }
2720}
2721
Alexandre Rames67555f72014-11-18 10:55:16 +00002722void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002723 RegisterSet caller_saves = RegisterSet::Empty();
2724 InvokeRuntimeCallingConvention calling_convention;
2725 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2726 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2727 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002728
2729 // If both index and length are constant, we can check the bounds statically and
2730 // generate code accordingly. We want to make sure we generate constant locations
2731 // in that case, regardless of whether they are encodable in the comparison or not.
2732 HInstruction* index = instruction->InputAt(0);
2733 HInstruction* length = instruction->InputAt(1);
2734 bool both_const = index->IsConstant() && length->IsConstant();
2735 locations->SetInAt(0, both_const
2736 ? Location::ConstantLocation(index->AsConstant())
2737 : ARM64EncodableConstantOrRegister(index, instruction));
2738 locations->SetInAt(1, both_const
2739 ? Location::ConstantLocation(length->AsConstant())
2740 : ARM64EncodableConstantOrRegister(length, instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002741}
2742
2743void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Georgia Kouvelibe530852019-01-17 10:46:41 +00002744 LocationSummary* locations = instruction->GetLocations();
2745 Location index_loc = locations->InAt(0);
2746 Location length_loc = locations->InAt(1);
2747
2748 int cmp_first_input = 0;
2749 int cmp_second_input = 1;
2750 Condition cond = hs;
2751
2752 if (index_loc.IsConstant()) {
2753 int64_t index = Int64FromLocation(index_loc);
2754 if (length_loc.IsConstant()) {
2755 int64_t length = Int64FromLocation(length_loc);
2756 if (index < 0 || index >= length) {
2757 BoundsCheckSlowPathARM64* slow_path =
2758 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
2759 codegen_->AddSlowPath(slow_path);
2760 __ B(slow_path->GetEntryLabel());
2761 } else {
2762 // BCE will remove the bounds check if we are guaranteed to pass.
2763 // However, some optimization after BCE may have generated this, and we should not
2764 // generate a bounds check if it is a valid range.
2765 }
2766 return;
2767 }
2768 // Only the index is constant: change the order of the operands and commute the condition
2769 // so we can use an immediate constant for the index (only the second input to a cmp
2770 // instruction can be an immediate).
2771 cmp_first_input = 1;
2772 cmp_second_input = 0;
2773 cond = ls;
2774 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002775 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002776 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002777 __ Cmp(InputRegisterAt(instruction, cmp_first_input),
2778 InputOperandAt(instruction, cmp_second_input));
Alexandre Rames67555f72014-11-18 10:55:16 +00002779 codegen_->AddSlowPath(slow_path);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002780 __ B(slow_path->GetEntryLabel(), cond);
Alexandre Rames67555f72014-11-18 10:55:16 +00002781}
2782
Alexandre Rames67555f72014-11-18 10:55:16 +00002783void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2784 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002785 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00002786 locations->SetInAt(0, Location::RequiresRegister());
2787 if (check->HasUses()) {
2788 locations->SetOut(Location::SameAsFirstInput());
2789 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01002790 // Rely on the type initialization to save everything we need.
2791 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Alexandre Rames67555f72014-11-18 10:55:16 +00002792}
2793
2794void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2795 // We assume the class is not null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01002796 SlowPathCodeARM64* slow_path =
2797 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(check->GetLoadClass(), check);
Alexandre Rames67555f72014-11-18 10:55:16 +00002798 codegen_->AddSlowPath(slow_path);
2799 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2800}
2801
Roland Levillain1a653882016-03-18 18:05:57 +00002802static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2803 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2804 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2805}
2806
2807void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002808 VRegister lhs_reg = InputFPRegisterAt(instruction, 0);
Roland Levillain1a653882016-03-18 18:05:57 +00002809 Location rhs_loc = instruction->GetLocations()->InAt(1);
2810 if (rhs_loc.IsConstant()) {
2811 // 0.0 is the only immediate that can be encoded directly in
2812 // an FCMP instruction.
2813 //
2814 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2815 // specify that in a floating-point comparison, positive zero
2816 // and negative zero are considered equal, so we can use the
2817 // literal 0.0 for both cases here.
2818 //
2819 // Note however that some methods (Float.equal, Float.compare,
2820 // Float.compareTo, Double.equal, Double.compare,
2821 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2822 // StrictMath.min) consider 0.0 to be (strictly) greater than
2823 // -0.0. So if we ever translate calls to these methods into a
2824 // HCompare instruction, we must handle the -0.0 case with
2825 // care here.
2826 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2827 __ Fcmp(lhs_reg, 0.0);
2828 } else {
2829 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2830 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002831}
2832
Serban Constantinescu02164b32014-11-13 14:05:07 +00002833void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002834 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002835 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002836 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002837 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002838 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002839 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002840 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002841 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002842 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002843 case DataType::Type::kInt32:
2844 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002845 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002846 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002847 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2848 break;
2849 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002850 case DataType::Type::kFloat32:
2851 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002852 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002853 locations->SetInAt(1,
2854 IsFloatingPointZeroConstant(compare->InputAt(1))
2855 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2856 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002857 locations->SetOut(Location::RequiresRegister());
2858 break;
2859 }
2860 default:
2861 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2862 }
2863}
2864
2865void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002866 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002867
2868 // 0 if: left == right
2869 // 1 if: left > right
2870 // -1 if: left < right
2871 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002872 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002873 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002874 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002875 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002876 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002877 case DataType::Type::kInt32:
2878 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002879 Register result = OutputRegister(compare);
2880 Register left = InputRegisterAt(compare, 0);
2881 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002882 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002883 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2884 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002885 break;
2886 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002887 case DataType::Type::kFloat32:
2888 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002889 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002890 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002891 __ Cset(result, ne);
2892 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002893 break;
2894 }
2895 default:
2896 LOG(FATAL) << "Unimplemented compare type " << in_type;
2897 }
2898}
2899
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002900void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002901 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002902
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002903 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002904 locations->SetInAt(0, Location::RequiresFpuRegister());
2905 locations->SetInAt(1,
2906 IsFloatingPointZeroConstant(instruction->InputAt(1))
2907 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2908 : Location::RequiresFpuRegister());
2909 } else {
2910 // Integer cases.
2911 locations->SetInAt(0, Location::RequiresRegister());
2912 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2913 }
2914
David Brazdilb3e773e2016-01-26 11:28:37 +00002915 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002916 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002917 }
2918}
2919
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002920void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002921 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002922 return;
2923 }
2924
2925 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002926 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002927 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002928
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002929 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002930 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002931 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002932 } else {
2933 // Integer cases.
2934 Register lhs = InputRegisterAt(instruction, 0);
2935 Operand rhs = InputOperandAt(instruction, 1);
2936 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002937 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002938 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002939}
2940
2941#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2942 M(Equal) \
2943 M(NotEqual) \
2944 M(LessThan) \
2945 M(LessThanOrEqual) \
2946 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002947 M(GreaterThanOrEqual) \
2948 M(Below) \
2949 M(BelowOrEqual) \
2950 M(Above) \
2951 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002952#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002953void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2954void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002955FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002956#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002957#undef FOR_EACH_CONDITION_INSTRUCTION
2958
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01002959void InstructionCodeGeneratorARM64::GenerateIntDivForPower2Denom(HDiv* instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002960 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002961 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01002962 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
2963
2964 Register out = OutputRegister(instruction);
2965 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01002966
2967 if (abs_imm == 2) {
2968 int bits = DataType::Size(instruction->GetResultType()) * kBitsPerByte;
2969 __ Add(out, dividend, Operand(dividend, LSR, bits - 1));
2970 } else {
2971 UseScratchRegisterScope temps(GetVIXLAssembler());
2972 Register temp = temps.AcquireSameSizeAs(out);
2973 __ Add(temp, dividend, abs_imm - 1);
2974 __ Cmp(dividend, 0);
2975 __ Csel(out, temp, dividend, lt);
2976 }
2977
Zheng Xuc6667102015-05-15 16:08:45 +08002978 int ctz_imm = CTZ(abs_imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01002979 if (imm > 0) {
2980 __ Asr(out, out, ctz_imm);
Zheng Xuc6667102015-05-15 16:08:45 +08002981 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01002982 __ Neg(out, Operand(out, ASR, ctz_imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002983 }
2984}
2985
2986void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2987 DCHECK(instruction->IsDiv() || instruction->IsRem());
2988
2989 LocationSummary* locations = instruction->GetLocations();
2990 Location second = locations->InAt(1);
2991 DCHECK(second.IsConstant());
2992
2993 Register out = OutputRegister(instruction);
2994 Register dividend = InputRegisterAt(instruction, 0);
2995 int64_t imm = Int64FromConstant(second.GetConstant());
2996
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002997 DataType::Type type = instruction->GetResultType();
2998 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08002999
3000 int64_t magic;
3001 int shift;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003002 CalculateMagicAndShiftForDivRem(
Andreas Gampe3db70682018-12-26 15:12:03 -08003003 imm, /* is_long= */ type == DataType::Type::kInt64, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08003004
3005 UseScratchRegisterScope temps(GetVIXLAssembler());
3006 Register temp = temps.AcquireSameSizeAs(out);
3007
3008 // temp = get_high(dividend * magic)
3009 __ Mov(temp, magic);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003010 if (type == DataType::Type::kInt64) {
Zheng Xuc6667102015-05-15 16:08:45 +08003011 __ Smulh(temp, dividend, temp);
3012 } else {
3013 __ Smull(temp.X(), dividend, temp);
3014 __ Lsr(temp.X(), temp.X(), 32);
3015 }
3016
3017 if (imm > 0 && magic < 0) {
3018 __ Add(temp, temp, dividend);
3019 } else if (imm < 0 && magic > 0) {
3020 __ Sub(temp, temp, dividend);
3021 }
3022
3023 if (shift != 0) {
3024 __ Asr(temp, temp, shift);
3025 }
3026
3027 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003028 __ Sub(out, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08003029 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003030 __ Sub(temp, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08003031 // TODO: Strength reduction for msub.
3032 Register temp_imm = temps.AcquireSameSizeAs(out);
3033 __ Mov(temp_imm, imm);
3034 __ Msub(out, temp, temp_imm, dividend);
3035 }
3036}
3037
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003038void InstructionCodeGeneratorARM64::GenerateIntDivForConstDenom(HDiv *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003039 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Zheng Xuc6667102015-05-15 16:08:45 +08003040
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003041 if (imm == 0) {
3042 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3043 return;
3044 }
Zheng Xuc6667102015-05-15 16:08:45 +08003045
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003046 if (IsPowerOfTwo(AbsOrMin(imm))) {
3047 GenerateIntDivForPower2Denom(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003048 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003049 // Cases imm == -1 or imm == 1 are handled by InstructionSimplifier.
3050 DCHECK(imm < -2 || imm > 2) << imm;
3051 GenerateDivRemWithAnyConstant(instruction);
3052 }
3053}
3054
3055void InstructionCodeGeneratorARM64::GenerateIntDiv(HDiv *instruction) {
3056 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
3057 << instruction->GetResultType();
3058
3059 if (instruction->GetLocations()->InAt(1).IsConstant()) {
3060 GenerateIntDivForConstDenom(instruction);
3061 } else {
3062 Register out = OutputRegister(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003063 Register dividend = InputRegisterAt(instruction, 0);
3064 Register divisor = InputRegisterAt(instruction, 1);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003065 __ Sdiv(out, dividend, divisor);
Zheng Xuc6667102015-05-15 16:08:45 +08003066 }
3067}
3068
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003069void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3070 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003071 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003072 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003073 case DataType::Type::kInt32:
3074 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003075 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003076 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003077 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3078 break;
3079
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003080 case DataType::Type::kFloat32:
3081 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003082 locations->SetInAt(0, Location::RequiresFpuRegister());
3083 locations->SetInAt(1, Location::RequiresFpuRegister());
3084 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3085 break;
3086
3087 default:
3088 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3089 }
3090}
3091
3092void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003093 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003094 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003095 case DataType::Type::kInt32:
3096 case DataType::Type::kInt64:
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003097 GenerateIntDiv(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003098 break;
3099
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003100 case DataType::Type::kFloat32:
3101 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003102 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3103 break;
3104
3105 default:
3106 LOG(FATAL) << "Unexpected div type " << type;
3107 }
3108}
3109
Alexandre Rames67555f72014-11-18 10:55:16 +00003110void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003111 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003112 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003113}
3114
3115void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3116 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003117 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003118 codegen_->AddSlowPath(slow_path);
3119 Location value = instruction->GetLocations()->InAt(0);
3120
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003121 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003122
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003123 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003124 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Elliott Hughesc1896c92018-11-29 11:33:18 -08003125 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003126 }
3127
Alexandre Rames67555f72014-11-18 10:55:16 +00003128 if (value.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003129 int64_t divisor = Int64FromLocation(value);
Alexandre Rames67555f72014-11-18 10:55:16 +00003130 if (divisor == 0) {
3131 __ B(slow_path->GetEntryLabel());
3132 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003133 // A division by a non-null constant is valid. We don't need to perform
3134 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003135 }
3136 } else {
3137 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3138 }
3139}
3140
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003141void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3142 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003143 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003144 locations->SetOut(Location::ConstantLocation(constant));
3145}
3146
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003147void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3148 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003149 // Will be generated at use site.
3150}
3151
Alexandre Rames5319def2014-10-23 10:03:10 +01003152void LocationsBuilderARM64::VisitExit(HExit* exit) {
3153 exit->SetLocations(nullptr);
3154}
3155
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003156void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003157}
3158
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003159void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3160 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003161 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003162 locations->SetOut(Location::ConstantLocation(constant));
3163}
3164
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003165void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003166 // Will be generated at use site.
3167}
3168
David Brazdilfc6a86a2015-06-26 10:33:45 +00003169void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003170 if (successor->IsExitBlock()) {
3171 DCHECK(got->GetPrevious()->AlwaysThrows());
3172 return; // no code needed
3173 }
3174
Serban Constantinescu02164b32014-11-13 14:05:07 +00003175 HBasicBlock* block = got->GetBlock();
3176 HInstruction* previous = got->GetPrevious();
3177 HLoopInformation* info = block->GetLoopInformation();
3178
David Brazdil46e2a392015-03-16 17:31:52 +00003179 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00003180 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
3181 UseScratchRegisterScope temps(GetVIXLAssembler());
3182 Register temp1 = temps.AcquireX();
3183 Register temp2 = temps.AcquireX();
3184 __ Ldr(temp1, MemOperand(sp, 0));
Nicolas Geoffray7ab07772019-08-30 08:26:59 +00003185 __ Ldrh(temp2, MemOperand(temp1, ArtMethod::HotnessCountOffset().Int32Value()));
3186 __ Add(temp2, temp2, 1);
Mathieu Chartier7f8678e2019-08-30 16:22:28 -07003187 // Subtract one if the counter would overflow.
3188 __ Sub(temp2, temp2, Operand(temp2, LSR, 16));
Nicolas Geoffray8d728322018-01-18 22:44:32 +00003189 __ Strh(temp2, MemOperand(temp1, ArtMethod::HotnessCountOffset().Int32Value()));
3190 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003191 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3192 return;
3193 }
3194 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3195 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08003196 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003197 }
3198 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003199 __ B(codegen_->GetLabelOf(successor));
3200 }
3201}
3202
David Brazdilfc6a86a2015-06-26 10:33:45 +00003203void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3204 got->SetLocations(nullptr);
3205}
3206
3207void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3208 HandleGoto(got, got->GetSuccessor());
3209}
3210
3211void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3212 try_boundary->SetLocations(nullptr);
3213}
3214
3215void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3216 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3217 if (!successor->IsExitBlock()) {
3218 HandleGoto(try_boundary, successor);
3219 }
3220}
3221
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003222void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003223 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003224 vixl::aarch64::Label* true_target,
3225 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003226 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003227
David Brazdil0debae72015-11-12 18:37:00 +00003228 if (true_target == nullptr && false_target == nullptr) {
3229 // Nothing to do. The code always falls through.
3230 return;
3231 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003232 // Constant condition, statically compared against "true" (integer value 1).
3233 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003234 if (true_target != nullptr) {
3235 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003236 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003237 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003238 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003239 if (false_target != nullptr) {
3240 __ B(false_target);
3241 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003242 }
David Brazdil0debae72015-11-12 18:37:00 +00003243 return;
3244 }
3245
3246 // The following code generates these patterns:
3247 // (1) true_target == nullptr && false_target != nullptr
3248 // - opposite condition true => branch to false_target
3249 // (2) true_target != nullptr && false_target == nullptr
3250 // - condition true => branch to true_target
3251 // (3) true_target != nullptr && false_target != nullptr
3252 // - condition true => branch to true_target
3253 // - branch to false_target
3254 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003255 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003256 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003257 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003258 if (true_target == nullptr) {
3259 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3260 } else {
3261 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3262 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003263 } else {
3264 // The condition instruction has not been materialized, use its inputs as
3265 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003266 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003267
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003268 DataType::Type type = condition->InputAt(0)->GetType();
3269 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003270 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003271 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003272 IfCondition opposite_condition = condition->GetOppositeCondition();
3273 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003274 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003275 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003276 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003277 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003278 // Integer cases.
3279 Register lhs = InputRegisterAt(condition, 0);
3280 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003281
3282 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003283 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003284 if (true_target == nullptr) {
3285 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3286 non_fallthrough_target = false_target;
3287 } else {
3288 arm64_cond = ARM64Condition(condition->GetCondition());
3289 non_fallthrough_target = true_target;
3290 }
3291
Aart Bik086d27e2016-01-20 17:02:00 -08003292 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003293 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003294 switch (arm64_cond) {
3295 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003296 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003297 break;
3298 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003299 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003300 break;
3301 case lt:
3302 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003303 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003304 break;
3305 case ge:
3306 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003307 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003308 break;
3309 default:
3310 // Without the `static_cast` the compiler throws an error for
3311 // `-Werror=sign-promo`.
3312 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3313 }
3314 } else {
3315 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003316 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003317 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003318 }
3319 }
David Brazdil0debae72015-11-12 18:37:00 +00003320
3321 // If neither branch falls through (case 3), the conditional branch to `true_target`
3322 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3323 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003324 __ B(false_target);
3325 }
3326}
3327
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003328void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003329 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003330 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003331 locations->SetInAt(0, Location::RequiresRegister());
3332 }
3333}
3334
3335void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003336 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3337 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003338 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3339 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3340 true_target = nullptr;
3341 }
3342 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3343 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3344 false_target = nullptr;
3345 }
Andreas Gampe3db70682018-12-26 15:12:03 -08003346 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003347}
3348
3349void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003350 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003351 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003352 InvokeRuntimeCallingConvention calling_convention;
3353 RegisterSet caller_saves = RegisterSet::Empty();
3354 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3355 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003356 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003357 locations->SetInAt(0, Location::RequiresRegister());
3358 }
3359}
3360
3361void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003362 SlowPathCodeARM64* slow_path =
3363 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003364 GenerateTestAndBranch(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08003365 /* condition_input_index= */ 0,
David Brazdil0debae72015-11-12 18:37:00 +00003366 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08003367 /* false_target= */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003368}
3369
Mingyao Yang063fc772016-08-02 11:02:54 -07003370void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003371 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003372 LocationSummary(flag, LocationSummary::kNoCall);
3373 locations->SetOut(Location::RequiresRegister());
3374}
3375
3376void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3377 __ Ldr(OutputRegister(flag),
3378 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3379}
3380
David Brazdilc0b601b2016-02-08 14:20:45 +00003381static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3382 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003383 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003384}
3385
Alexandre Rames880f1192016-06-13 16:04:50 +01003386static inline Condition GetConditionForSelect(HCondition* condition) {
3387 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003388 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3389 : ARM64Condition(cond);
3390}
3391
David Brazdil74eb1b22015-12-14 11:44:01 +00003392void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003393 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003394 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003395 locations->SetInAt(0, Location::RequiresFpuRegister());
3396 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003397 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003398 } else {
3399 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3400 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3401 bool is_true_value_constant = cst_true_value != nullptr;
3402 bool is_false_value_constant = cst_false_value != nullptr;
3403 // Ask VIXL whether we should synthesize constants in registers.
3404 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3405 Operand true_op = is_true_value_constant ?
3406 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3407 Operand false_op = is_false_value_constant ?
3408 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3409 bool true_value_in_register = false;
3410 bool false_value_in_register = false;
3411 MacroAssembler::GetCselSynthesisInformation(
3412 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3413 true_value_in_register |= !is_true_value_constant;
3414 false_value_in_register |= !is_false_value_constant;
3415
3416 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3417 : Location::ConstantLocation(cst_true_value));
3418 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3419 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003420 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003421 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003422
David Brazdil74eb1b22015-12-14 11:44:01 +00003423 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3424 locations->SetInAt(2, Location::RequiresRegister());
3425 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003426}
3427
3428void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003429 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003430 Condition csel_cond;
3431
3432 if (IsBooleanValueOrMaterializedCondition(cond)) {
3433 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003434 // Use the condition flags set by the previous instruction.
3435 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003436 } else {
3437 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003438 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003439 }
3440 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003441 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003442 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003443 } else {
3444 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003445 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003446 }
3447
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003448 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003449 __ Fcsel(OutputFPRegister(select),
3450 InputFPRegisterAt(select, 1),
3451 InputFPRegisterAt(select, 0),
3452 csel_cond);
3453 } else {
3454 __ Csel(OutputRegister(select),
3455 InputOperandAt(select, 1),
3456 InputOperandAt(select, 0),
3457 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003458 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003459}
3460
David Srbecky0cf44932015-12-09 14:09:59 +00003461void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003462 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00003463}
3464
David Srbeckyd28f4a02016-03-14 17:14:24 +00003465void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3466 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003467}
3468
3469void CodeGeneratorARM64::GenerateNop() {
3470 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003471}
3472
Alexandre Rames5319def2014-10-23 10:03:10 +01003473void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003474 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003475}
3476
3477void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003478 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003479}
3480
3481void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003482 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003483}
3484
3485void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003486 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003487}
3488
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003489// Temp is used for read barrier.
3490static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3491 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003492 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003493 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3494 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3495 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3496 return 1;
3497 }
3498 return 0;
3499}
3500
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003501// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003502// interface pointer, one for loading the current interface.
3503// The other checks have one temp for loading the object's class.
3504static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3505 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3506 return 3;
3507 }
3508 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003509}
3510
Alexandre Rames67555f72014-11-18 10:55:16 +00003511void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003512 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003513 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003514 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003515 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003516 case TypeCheckKind::kExactCheck:
3517 case TypeCheckKind::kAbstractClassCheck:
3518 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00003519 case TypeCheckKind::kArrayObjectCheck: {
3520 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
3521 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
3522 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003523 break;
Vladimir Marko87584542017-12-12 17:47:52 +00003524 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003525 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003526 case TypeCheckKind::kUnresolvedCheck:
3527 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003528 call_kind = LocationSummary::kCallOnSlowPath;
3529 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00003530 case TypeCheckKind::kBitstringCheck:
3531 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003532 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003533
Vladimir Markoca6fff82017-10-03 14:49:14 +01003534 LocationSummary* locations =
3535 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003536 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003537 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003538 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003539 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003540 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3541 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3542 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3543 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3544 } else {
3545 locations->SetInAt(1, Location::RequiresRegister());
3546 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003547 // The "out" register is used as a temporary, so it overlaps with the inputs.
3548 // Note that TypeCheckSlowPathARM64 uses this register too.
3549 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003550 // Add temps if necessary for read barriers.
3551 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003552}
3553
3554void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003555 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003556 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003557 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003558 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003559 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3560 ? Register()
3561 : InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003562 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003563 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003564 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3565 DCHECK_LE(num_temps, 1u);
3566 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003567 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3568 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3569 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3570 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003571
Scott Wakeling97c72b72016-06-24 16:19:36 +01003572 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003573 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003574
3575 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003576 // Avoid null check if we know `obj` is not null.
3577 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003578 __ Cbz(obj, &zero);
3579 }
3580
Roland Levillain44015862016-01-22 11:47:17 +00003581 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003582 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003583 ReadBarrierOption read_barrier_option =
3584 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003585 // /* HeapReference<Class> */ out = obj->klass_
3586 GenerateReferenceLoadTwoRegisters(instruction,
3587 out_loc,
3588 obj_loc,
3589 class_offset,
3590 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003591 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003592 __ Cmp(out, cls);
3593 __ Cset(out, eq);
3594 if (zero.IsLinked()) {
3595 __ B(&done);
3596 }
3597 break;
3598 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003599
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003600 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003601 ReadBarrierOption read_barrier_option =
3602 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003603 // /* HeapReference<Class> */ out = obj->klass_
3604 GenerateReferenceLoadTwoRegisters(instruction,
3605 out_loc,
3606 obj_loc,
3607 class_offset,
3608 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003609 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003610 // If the class is abstract, we eagerly fetch the super class of the
3611 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003612 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003613 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003614 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003615 GenerateReferenceLoadOneRegister(instruction,
3616 out_loc,
3617 super_offset,
3618 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003619 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003620 // If `out` is null, we use it for the result, and jump to `done`.
3621 __ Cbz(out, &done);
3622 __ Cmp(out, cls);
3623 __ B(ne, &loop);
3624 __ Mov(out, 1);
3625 if (zero.IsLinked()) {
3626 __ B(&done);
3627 }
3628 break;
3629 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003630
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003631 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003632 ReadBarrierOption read_barrier_option =
3633 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003634 // /* HeapReference<Class> */ out = obj->klass_
3635 GenerateReferenceLoadTwoRegisters(instruction,
3636 out_loc,
3637 obj_loc,
3638 class_offset,
3639 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003640 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003641 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003642 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003643 __ Bind(&loop);
3644 __ Cmp(out, cls);
3645 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003646 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003647 GenerateReferenceLoadOneRegister(instruction,
3648 out_loc,
3649 super_offset,
3650 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003651 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003652 __ Cbnz(out, &loop);
3653 // If `out` is null, we use it for the result, and jump to `done`.
3654 __ B(&done);
3655 __ Bind(&success);
3656 __ Mov(out, 1);
3657 if (zero.IsLinked()) {
3658 __ B(&done);
3659 }
3660 break;
3661 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003662
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003663 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003664 ReadBarrierOption read_barrier_option =
3665 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003666 // /* HeapReference<Class> */ out = obj->klass_
3667 GenerateReferenceLoadTwoRegisters(instruction,
3668 out_loc,
3669 obj_loc,
3670 class_offset,
3671 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003672 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003673 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003674 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003675 __ Cmp(out, cls);
3676 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003677 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003678 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003679 GenerateReferenceLoadOneRegister(instruction,
3680 out_loc,
3681 component_offset,
3682 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003683 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003684 // If `out` is null, we use it for the result, and jump to `done`.
3685 __ Cbz(out, &done);
3686 __ Ldrh(out, HeapOperand(out, primitive_offset));
3687 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3688 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003689 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003690 __ Mov(out, 1);
3691 __ B(&done);
3692 break;
3693 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003694
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003695 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003696 // No read barrier since the slow path will retry upon failure.
3697 // /* HeapReference<Class> */ out = obj->klass_
3698 GenerateReferenceLoadTwoRegisters(instruction,
3699 out_loc,
3700 obj_loc,
3701 class_offset,
3702 maybe_temp_loc,
3703 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003704 __ Cmp(out, cls);
3705 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01003706 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08003707 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003708 codegen_->AddSlowPath(slow_path);
3709 __ B(ne, slow_path->GetEntryLabel());
3710 __ Mov(out, 1);
3711 if (zero.IsLinked()) {
3712 __ B(&done);
3713 }
3714 break;
3715 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003716
Calin Juravle98893e12015-10-02 21:05:03 +01003717 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003718 case TypeCheckKind::kInterfaceCheck: {
3719 // Note that we indeed only call on slow path, but we always go
3720 // into the slow path for the unresolved and interface check
3721 // cases.
3722 //
3723 // We cannot directly call the InstanceofNonTrivial runtime
3724 // entry point without resorting to a type checking slow path
3725 // here (i.e. by calling InvokeRuntime directly), as it would
3726 // require to assign fixed registers for the inputs of this
3727 // HInstanceOf instruction (following the runtime calling
3728 // convention), which might be cluttered by the potential first
3729 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003730 //
3731 // TODO: Introduce a new runtime entry point taking the object
3732 // to test (instead of its class) as argument, and let it deal
3733 // with the read barrier issues. This will let us refactor this
3734 // case of the `switch` code as it was previously (with a direct
3735 // call to the runtime not using a type checking slow path).
3736 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003737 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01003738 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08003739 instruction, /* is_fatal= */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003740 codegen_->AddSlowPath(slow_path);
3741 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003742 if (zero.IsLinked()) {
3743 __ B(&done);
3744 }
3745 break;
3746 }
Vladimir Marko175e7862018-03-27 09:03:13 +00003747
3748 case TypeCheckKind::kBitstringCheck: {
3749 // /* HeapReference<Class> */ temp = obj->klass_
3750 GenerateReferenceLoadTwoRegisters(instruction,
3751 out_loc,
3752 obj_loc,
3753 class_offset,
3754 maybe_temp_loc,
3755 kWithoutReadBarrier);
3756
3757 GenerateBitstringTypeCheckCompare(instruction, out);
3758 __ Cset(out, eq);
3759 if (zero.IsLinked()) {
3760 __ B(&done);
3761 }
3762 break;
3763 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003764 }
3765
3766 if (zero.IsLinked()) {
3767 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003768 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003769 }
3770
3771 if (done.IsLinked()) {
3772 __ Bind(&done);
3773 }
3774
3775 if (slow_path != nullptr) {
3776 __ Bind(slow_path->GetExitLabel());
3777 }
3778}
3779
3780void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003781 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00003782 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003783 LocationSummary* locations =
3784 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003785 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003786 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3787 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3788 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3789 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3790 } else {
3791 locations->SetInAt(1, Location::RequiresRegister());
3792 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003793 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
3794 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003795}
3796
3797void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003798 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003799 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003800 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003801 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003802 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3803 ? Register()
3804 : InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003805 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3806 DCHECK_GE(num_temps, 1u);
3807 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003808 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003809 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
3810 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003811 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003812 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3813 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3814 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3815 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3816 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3817 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3818 const uint32_t object_array_data_offset =
3819 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003820
Vladimir Marko87584542017-12-12 17:47:52 +00003821 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003822 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003823 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
3824 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003825 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003826
Scott Wakeling97c72b72016-06-24 16:19:36 +01003827 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003828 // Avoid null check if we know obj is not null.
3829 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003830 __ Cbz(obj, &done);
3831 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003832
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003833 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003834 case TypeCheckKind::kExactCheck:
3835 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003836 // /* HeapReference<Class> */ temp = obj->klass_
3837 GenerateReferenceLoadTwoRegisters(instruction,
3838 temp_loc,
3839 obj_loc,
3840 class_offset,
3841 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003842 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003843
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003844 __ Cmp(temp, cls);
3845 // Jump to slow path for throwing the exception or doing a
3846 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003847 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003848 break;
3849 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003850
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003851 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003852 // /* HeapReference<Class> */ temp = obj->klass_
3853 GenerateReferenceLoadTwoRegisters(instruction,
3854 temp_loc,
3855 obj_loc,
3856 class_offset,
3857 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003858 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003859
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003860 // If the class is abstract, we eagerly fetch the super class of the
3861 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003862 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003863 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003864 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003865 GenerateReferenceLoadOneRegister(instruction,
3866 temp_loc,
3867 super_offset,
3868 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003869 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003870
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003871 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3872 // exception.
3873 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3874 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003875 __ Cmp(temp, cls);
3876 __ B(ne, &loop);
3877 break;
3878 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003879
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003880 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003881 // /* HeapReference<Class> */ temp = obj->klass_
3882 GenerateReferenceLoadTwoRegisters(instruction,
3883 temp_loc,
3884 obj_loc,
3885 class_offset,
3886 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003887 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003888
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003889 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003890 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003891 __ Bind(&loop);
3892 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003893 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003894
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003895 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003896 GenerateReferenceLoadOneRegister(instruction,
3897 temp_loc,
3898 super_offset,
3899 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003900 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003901
3902 // If the class reference currently in `temp` is not null, jump
3903 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003904 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003905 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003906 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003907 break;
3908 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003909
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003910 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003911 // /* HeapReference<Class> */ temp = obj->klass_
3912 GenerateReferenceLoadTwoRegisters(instruction,
3913 temp_loc,
3914 obj_loc,
3915 class_offset,
3916 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003917 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003918
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003919 // Do an exact check.
3920 __ Cmp(temp, cls);
3921 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003922
3923 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003924 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003925 GenerateReferenceLoadOneRegister(instruction,
3926 temp_loc,
3927 component_offset,
3928 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003929 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003930
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003931 // If the component type is null, jump to the slow path to throw the exception.
3932 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3933 // Otherwise, the object is indeed an array. Further check that this component type is not a
3934 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003935 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3936 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003937 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003938 break;
3939 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003940
Calin Juravle98893e12015-10-02 21:05:03 +01003941 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003942 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003943 //
3944 // We cannot directly call the CheckCast runtime entry point
3945 // without resorting to a type checking slow path here (i.e. by
3946 // calling InvokeRuntime directly), as it would require to
3947 // assign fixed registers for the inputs of this HInstanceOf
3948 // instruction (following the runtime calling convention), which
3949 // might be cluttered by the potential first read barrier
3950 // emission at the beginning of this method.
3951 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003952 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003953 case TypeCheckKind::kInterfaceCheck: {
3954 // /* HeapReference<Class> */ temp = obj->klass_
3955 GenerateReferenceLoadTwoRegisters(instruction,
3956 temp_loc,
3957 obj_loc,
3958 class_offset,
3959 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003960 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003961
3962 // /* HeapReference<Class> */ temp = temp->iftable_
3963 GenerateReferenceLoadTwoRegisters(instruction,
3964 temp_loc,
3965 temp_loc,
3966 iftable_offset,
3967 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003968 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003969 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003970 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08003971 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003972 vixl::aarch64::Label start_loop;
3973 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003974 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003975 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
3976 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003977 // Go to next interface.
3978 __ Add(temp, temp, 2 * kHeapReferenceSize);
3979 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003980 // Compare the classes and continue the loop if they do not match.
3981 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
3982 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003983 break;
3984 }
Vladimir Marko175e7862018-03-27 09:03:13 +00003985
3986 case TypeCheckKind::kBitstringCheck: {
3987 // /* HeapReference<Class> */ temp = obj->klass_
3988 GenerateReferenceLoadTwoRegisters(instruction,
3989 temp_loc,
3990 obj_loc,
3991 class_offset,
3992 maybe_temp2_loc,
3993 kWithoutReadBarrier);
3994
3995 GenerateBitstringTypeCheckCompare(instruction, temp);
3996 __ B(ne, type_check_slow_path->GetEntryLabel());
3997 break;
3998 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003999 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004000 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004001
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004002 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004003}
4004
Alexandre Rames5319def2014-10-23 10:03:10 +01004005void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004006 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004007 locations->SetOut(Location::ConstantLocation(constant));
4008}
4009
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004010void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004011 // Will be generated at use site.
4012}
4013
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004014void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004015 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004016 locations->SetOut(Location::ConstantLocation(constant));
4017}
4018
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004019void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004020 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004021}
4022
Calin Juravle175dc732015-08-25 15:42:32 +01004023void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4024 // The trampoline uses the same calling convention as dex calling conventions,
4025 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4026 // the method_idx.
4027 HandleInvoke(invoke);
4028}
4029
4030void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4031 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004032 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004033}
4034
Alexandre Rames5319def2014-10-23 10:03:10 +01004035void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004036 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004037 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004038}
4039
Alexandre Rames67555f72014-11-18 10:55:16 +00004040void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4041 HandleInvoke(invoke);
4042}
4043
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004044void CodeGeneratorARM64::MaybeGenerateInlineCacheCheck(HInstruction* instruction,
4045 Register klass) {
4046 DCHECK_EQ(klass.GetCode(), 0u);
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004047 // We know the destination of an intrinsic, so no need to record inline
4048 // caches.
4049 if (!instruction->GetLocations()->Intrinsified() &&
4050 GetCompilerOptions().IsBaseline() &&
4051 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004052 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
4053 ScopedObjectAccess soa(Thread::Current());
4054 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
4055 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
4056 uint64_t address = reinterpret_cast64<uint64_t>(cache);
4057 vixl::aarch64::Label done;
4058 __ Mov(x8, address);
4059 __ Ldr(x9, MemOperand(x8, InlineCache::ClassesOffset().Int32Value()));
4060 // Fast path for a monomorphic cache.
4061 __ Cmp(klass, x9);
4062 __ B(eq, &done);
4063 InvokeRuntime(kQuickUpdateInlineCache, instruction, instruction->GetDexPc());
4064 __ Bind(&done);
4065 }
4066}
4067
Alexandre Rames67555f72014-11-18 10:55:16 +00004068void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4069 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004070 LocationSummary* locations = invoke->GetLocations();
4071 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004072 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004073 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004074 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004075
Artem Serov914d7a82017-02-07 14:33:49 +00004076 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004077 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004078 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004079 {
4080 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4081 // /* HeapReference<Class> */ temp = temp->klass_
4082 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4083 codegen_->MaybeRecordImplicitNullCheck(invoke);
4084 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004085 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004086 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004087 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004088 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004089 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004090 }
Artem Serov914d7a82017-02-07 14:33:49 +00004091
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004092 // Instead of simply (possibly) unpoisoning `temp` here, we should
4093 // emit a read barrier for the previous class reference load.
4094 // However this is not required in practice, as this is an
4095 // intermediate/temporary reference and because the current
4096 // concurrent copying collector keeps the from-space memory
4097 // intact/accessible until the end of the marking phase (the
4098 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004099 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004100
4101 // If we're compiling baseline, update the inline cache.
4102 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
4103
4104 // The register ip1 is required to be used for the hidden argument in
4105 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
4106 MacroAssembler* masm = GetVIXLAssembler();
4107 UseScratchRegisterScope scratch_scope(masm);
4108 scratch_scope.Exclude(ip1);
4109 __ Mov(ip1, invoke->GetDexMethodIndex());
4110
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004111 __ Ldr(temp,
4112 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4113 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004114 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004115 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004116 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004117 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004118 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004119
4120 {
4121 // Ensure the pc position is recorded immediately after the `blr` instruction.
4122 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4123
4124 // lr();
4125 __ blr(lr);
4126 DCHECK(!codegen_->IsLeafMethod());
4127 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4128 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004129
Andreas Gampe3db70682018-12-26 15:12:03 -08004130 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004131}
4132
4133void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004134 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004135 if (intrinsic.TryDispatch(invoke)) {
4136 return;
4137 }
4138
Alexandre Rames67555f72014-11-18 10:55:16 +00004139 HandleInvoke(invoke);
4140}
4141
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004142void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004143 // Explicit clinit checks triggered by static invokes must have been pruned by
4144 // art::PrepareForRegisterAllocation.
4145 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004146
Vladimir Markoca6fff82017-10-03 14:49:14 +01004147 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004148 if (intrinsic.TryDispatch(invoke)) {
4149 return;
4150 }
4151
Alexandre Rames67555f72014-11-18 10:55:16 +00004152 HandleInvoke(invoke);
4153}
4154
Andreas Gampe878d58c2015-01-15 23:24:00 -08004155static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4156 if (invoke->GetLocations()->Intrinsified()) {
4157 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4158 intrinsic.Dispatch(invoke);
4159 return true;
4160 }
4161 return false;
4162}
4163
Vladimir Markodc151b22015-10-15 18:02:30 +01004164HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4165 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004166 ArtMethod* method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004167 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004168 return desired_dispatch_info;
4169}
4170
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004171void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4172 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004173 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004174 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4175 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004176 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4177 uint32_t offset =
4178 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004179 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004180 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004181 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004182 }
Vladimir Marko58155012015-08-19 12:49:41 +00004183 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004184 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004185 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004186 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01004187 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01004188 // Add ADRP with its PC-relative method patch.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004189 vixl::aarch64::Label* adrp_label = NewBootImageMethodPatch(invoke->GetTargetMethod());
Vladimir Marko65979462017-05-19 17:25:12 +01004190 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4191 // Add ADD with its PC-relative method patch.
4192 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004193 NewBootImageMethodPatch(invoke->GetTargetMethod(), adrp_label);
Vladimir Marko65979462017-05-19 17:25:12 +01004194 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4195 break;
4196 }
Vladimir Markob066d432018-01-03 13:14:37 +00004197 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4198 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004199 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00004200 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_offset);
4201 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4202 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
4203 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_offset, adrp_label);
4204 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
4205 EmitLdrOffsetPlaceholder(ldr_label, WRegisterFrom(temp), XRegisterFrom(temp));
4206 break;
4207 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004208 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Markob066d432018-01-03 13:14:37 +00004209 // Add ADRP with its PC-relative .bss entry patch.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004210 MethodReference target_method(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4211 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(target_method);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004212 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Markob066d432018-01-03 13:14:37 +00004213 // Add LDR with its PC-relative .bss entry patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004214 vixl::aarch64::Label* ldr_label =
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004215 NewMethodBssEntryPatch(target_method, adrp_label);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01004216 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004217 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004218 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004219 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004220 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
4221 // Load method address from literal pool.
4222 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
4223 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004224 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4225 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4226 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004227 }
4228 }
4229
4230 switch (invoke->GetCodePtrLocation()) {
4231 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004232 {
4233 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4234 ExactAssemblyScope eas(GetVIXLAssembler(),
4235 kInstructionSize,
4236 CodeBufferCheckScope::kExactSize);
4237 __ bl(&frame_entry_label_);
4238 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4239 }
Vladimir Marko58155012015-08-19 12:49:41 +00004240 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004241 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4242 // LR = callee_method->entry_point_from_quick_compiled_code_;
4243 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004244 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004245 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004246 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004247 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004248 ExactAssemblyScope eas(GetVIXLAssembler(),
4249 kInstructionSize,
4250 CodeBufferCheckScope::kExactSize);
4251 // lr()
4252 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004253 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004254 }
Vladimir Marko58155012015-08-19 12:49:41 +00004255 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004256 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004257
Andreas Gampe878d58c2015-01-15 23:24:00 -08004258 DCHECK(!IsLeafMethod());
4259}
4260
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004261void CodeGeneratorARM64::GenerateVirtualCall(
4262 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004263 // Use the calling convention instead of the location of the receiver, as
4264 // intrinsics may have put the receiver in a different register. In the intrinsics
4265 // slow path, the arguments have been moved to the right place, so here we are
4266 // guaranteed that the receiver is the first register of the calling convention.
4267 InvokeDexCallingConvention calling_convention;
4268 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004269 Register temp = XRegisterFrom(temp_in);
4270 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4271 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4272 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004273 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004274
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004275 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004276
4277 {
4278 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4279 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4280 // /* HeapReference<Class> */ temp = receiver->klass_
4281 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4282 MaybeRecordImplicitNullCheck(invoke);
4283 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004284 // Instead of simply (possibly) unpoisoning `temp` here, we should
4285 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004286 // intermediate/temporary reference and because the current
4287 // concurrent copying collector keeps the from-space memory
4288 // intact/accessible until the end of the marking phase (the
4289 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004290 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004291
4292 // If we're compiling baseline, update the inline cache.
4293 MaybeGenerateInlineCacheCheck(invoke, temp);
4294
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004295 // temp = temp->GetMethodAt(method_offset);
4296 __ Ldr(temp, MemOperand(temp, method_offset));
4297 // lr = temp->GetEntryPoint();
4298 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004299 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004300 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004301 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4302 // lr();
4303 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004304 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004305 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004306}
4307
Orion Hodsonac141392017-01-13 11:53:47 +00004308void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4309 HandleInvoke(invoke);
4310}
4311
4312void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4313 codegen_->GenerateInvokePolymorphicCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004314 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004315}
4316
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004317void LocationsBuilderARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4318 HandleInvoke(invoke);
4319}
4320
4321void InstructionCodeGeneratorARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4322 codegen_->GenerateInvokeCustomCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004323 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004324}
4325
Vladimir Marko6fd16062018-06-26 11:02:04 +01004326vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageIntrinsicPatch(
4327 uint32_t intrinsic_data,
4328 vixl::aarch64::Label* adrp_label) {
4329 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004330 /* dex_file= */ nullptr, intrinsic_data, adrp_label, &boot_image_other_patches_);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004331}
4332
Vladimir Markob066d432018-01-03 13:14:37 +00004333vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageRelRoPatch(
4334 uint32_t boot_image_offset,
4335 vixl::aarch64::Label* adrp_label) {
4336 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004337 /* dex_file= */ nullptr, boot_image_offset, adrp_label, &boot_image_other_patches_);
Vladimir Markob066d432018-01-03 13:14:37 +00004338}
4339
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004340vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004341 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004342 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004343 return NewPcRelativePatch(
4344 target_method.dex_file, target_method.index, adrp_label, &boot_image_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004345}
4346
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004347vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4348 MethodReference target_method,
4349 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004350 return NewPcRelativePatch(
4351 target_method.dex_file, target_method.index, adrp_label, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004352}
4353
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004354vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageTypePatch(
Scott Wakeling97c72b72016-06-24 16:19:36 +01004355 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004356 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004357 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004358 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &boot_image_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004359}
4360
Vladimir Marko1998cd02017-01-13 13:02:58 +00004361vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4362 const DexFile& dex_file,
4363 dex::TypeIndex type_index,
4364 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004365 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004366}
4367
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004368vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004369 const DexFile& dex_file,
4370 dex::StringIndex string_index,
4371 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004372 return NewPcRelativePatch(
4373 &dex_file, string_index.index_, adrp_label, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01004374}
4375
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004376vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4377 const DexFile& dex_file,
4378 dex::StringIndex string_index,
4379 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004380 return NewPcRelativePatch(&dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004381}
4382
Vladimir Markof6675082019-05-17 12:05:28 +01004383void CodeGeneratorARM64::EmitEntrypointThunkCall(ThreadOffset64 entrypoint_offset) {
4384 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
4385 DCHECK(!Runtime::Current()->UseJitCompilation());
4386 call_entrypoint_patches_.emplace_back(/*dex_file*/ nullptr, entrypoint_offset.Uint32Value());
4387 vixl::aarch64::Label* bl_label = &call_entrypoint_patches_.back().label;
4388 __ bind(bl_label);
4389 __ bl(static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4390}
4391
Vladimir Marko966b46f2018-08-03 10:20:19 +00004392void CodeGeneratorARM64::EmitBakerReadBarrierCbnz(uint32_t custom_data) {
Vladimir Marko94796f82018-08-08 15:15:33 +01004393 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko966b46f2018-08-03 10:20:19 +00004394 if (Runtime::Current()->UseJitCompilation()) {
4395 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
4396 vixl::aarch64::Label* slow_path_entry = &it->second.label;
4397 __ cbnz(mr, slow_path_entry);
4398 } else {
4399 baker_read_barrier_patches_.emplace_back(custom_data);
4400 vixl::aarch64::Label* cbnz_label = &baker_read_barrier_patches_.back().label;
4401 __ bind(cbnz_label);
4402 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4403 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004404}
4405
Scott Wakeling97c72b72016-06-24 16:19:36 +01004406vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004407 const DexFile* dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004408 uint32_t offset_or_index,
4409 vixl::aarch64::Label* adrp_label,
4410 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004411 // Add a patch entry and return the label.
4412 patches->emplace_back(dex_file, offset_or_index);
4413 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004414 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004415 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4416 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4417 return label;
4418}
4419
Scott Wakeling97c72b72016-06-24 16:19:36 +01004420vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4421 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004422 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004423}
4424
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004425vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004426 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004427 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004428 return jit_string_patches_.GetOrCreate(
4429 StringReference(&dex_file, string_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004430 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004431}
4432
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004433vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004434 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004435 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004436 return jit_class_patches_.GetOrCreate(
4437 TypeReference(&dex_file, type_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004438 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004439}
4440
Vladimir Markoaad75c62016-10-03 08:46:48 +00004441void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4442 vixl::aarch64::Register reg) {
4443 DCHECK(reg.IsX());
4444 SingleEmissionCheckScope guard(GetVIXLAssembler());
4445 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004446 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004447}
4448
4449void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4450 vixl::aarch64::Register out,
4451 vixl::aarch64::Register base) {
4452 DCHECK(out.IsX());
4453 DCHECK(base.IsX());
4454 SingleEmissionCheckScope guard(GetVIXLAssembler());
4455 __ Bind(fixup_label);
4456 __ add(out, base, Operand(/* offset placeholder */ 0));
4457}
4458
4459void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4460 vixl::aarch64::Register out,
4461 vixl::aarch64::Register base) {
4462 DCHECK(base.IsX());
4463 SingleEmissionCheckScope guard(GetVIXLAssembler());
4464 __ Bind(fixup_label);
4465 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4466}
4467
Vladimir Markoeebb8212018-06-05 14:57:24 +01004468void CodeGeneratorARM64::LoadBootImageAddress(vixl::aarch64::Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01004469 uint32_t boot_image_reference) {
4470 if (GetCompilerOptions().IsBootImage()) {
4471 // Add ADRP with its PC-relative type patch.
4472 vixl::aarch64::Label* adrp_label = NewBootImageIntrinsicPatch(boot_image_reference);
4473 EmitAdrpPlaceholder(adrp_label, reg.X());
4474 // Add ADD with its PC-relative type patch.
4475 vixl::aarch64::Label* add_label = NewBootImageIntrinsicPatch(boot_image_reference, adrp_label);
4476 EmitAddPlaceholder(add_label, reg.X(), reg.X());
Vladimir Markoa2da9b92018-10-10 14:21:55 +01004477 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01004478 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004479 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004480 EmitAdrpPlaceholder(adrp_label, reg.X());
4481 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004482 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_reference, adrp_label);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004483 EmitLdrOffsetPlaceholder(ldr_label, reg.W(), reg.X());
4484 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004485 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01004486 gc::Heap* heap = Runtime::Current()->GetHeap();
4487 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004488 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01004489 __ Ldr(reg.W(), DeduplicateBootImageAddressLiteral(reinterpret_cast<uintptr_t>(address)));
4490 }
4491}
4492
Vladimir Marko6fd16062018-06-26 11:02:04 +01004493void CodeGeneratorARM64::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
4494 uint32_t boot_image_offset) {
4495 DCHECK(invoke->IsStatic());
4496 InvokeRuntimeCallingConvention calling_convention;
4497 Register argument = calling_convention.GetRegisterAt(0);
4498 if (GetCompilerOptions().IsBootImage()) {
4499 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
4500 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
4501 MethodReference target_method = invoke->GetTargetMethod();
4502 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
4503 // Add ADRP with its PC-relative type patch.
4504 vixl::aarch64::Label* adrp_label = NewBootImageTypePatch(*target_method.dex_file, type_idx);
4505 EmitAdrpPlaceholder(adrp_label, argument.X());
4506 // Add ADD with its PC-relative type patch.
4507 vixl::aarch64::Label* add_label =
4508 NewBootImageTypePatch(*target_method.dex_file, type_idx, adrp_label);
4509 EmitAddPlaceholder(add_label, argument.X(), argument.X());
4510 } else {
4511 LoadBootImageAddress(argument, boot_image_offset);
4512 }
4513 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
4514 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4515}
4516
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004517template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004518inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4519 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004520 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004521 for (const PcRelativePatchInfo& info : infos) {
4522 linker_patches->push_back(Factory(info.label.GetLocation(),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004523 info.target_dex_file,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004524 info.pc_insn_label->GetLocation(),
4525 info.offset_or_index));
4526 }
4527}
4528
Vladimir Marko6fd16062018-06-26 11:02:04 +01004529template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
4530linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
4531 const DexFile* target_dex_file,
4532 uint32_t pc_insn_offset,
4533 uint32_t boot_image_offset) {
4534 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
4535 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00004536}
4537
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004538void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004539 DCHECK(linker_patches->empty());
4540 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004541 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004542 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004543 boot_image_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004544 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004545 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004546 string_bss_entry_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01004547 boot_image_other_patches_.size() +
Vladimir Markof6675082019-05-17 12:05:28 +01004548 call_entrypoint_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004549 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004550 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01004551 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004552 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004553 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004554 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004555 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004556 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004557 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01004558 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01004559 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004560 DCHECK(boot_image_type_patches_.empty());
4561 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01004562 }
4563 if (GetCompilerOptions().IsBootImage()) {
4564 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
4565 boot_image_other_patches_, linker_patches);
4566 } else {
4567 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
4568 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004569 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004570 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4571 method_bss_entry_patches_, linker_patches);
4572 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4573 type_bss_entry_patches_, linker_patches);
4574 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4575 string_bss_entry_patches_, linker_patches);
Vladimir Markof6675082019-05-17 12:05:28 +01004576 for (const PatchInfo<vixl::aarch64::Label>& info : call_entrypoint_patches_) {
4577 DCHECK(info.target_dex_file == nullptr);
4578 linker_patches->push_back(linker::LinkerPatch::CallEntrypointPatch(
4579 info.label.GetLocation(), info.offset_or_index));
4580 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004581 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004582 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
4583 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004584 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004585 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004586}
4587
Vladimir Markoca1e0382018-04-11 09:58:41 +00004588bool CodeGeneratorARM64::NeedsThunkCode(const linker::LinkerPatch& patch) const {
Vladimir Markof6675082019-05-17 12:05:28 +01004589 return patch.GetType() == linker::LinkerPatch::Type::kCallEntrypoint ||
4590 patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
Vladimir Markoca1e0382018-04-11 09:58:41 +00004591 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
4592}
4593
4594void CodeGeneratorARM64::EmitThunkCode(const linker::LinkerPatch& patch,
4595 /*out*/ ArenaVector<uint8_t>* code,
4596 /*out*/ std::string* debug_name) {
4597 Arm64Assembler assembler(GetGraph()->GetAllocator());
4598 switch (patch.GetType()) {
4599 case linker::LinkerPatch::Type::kCallRelative: {
4600 // The thunk just uses the entry point in the ArtMethod. This works even for calls
4601 // to the generic JNI and interpreter trampolines.
4602 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4603 kArm64PointerSize).Int32Value());
4604 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
4605 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
4606 *debug_name = "MethodCallThunk";
4607 }
4608 break;
4609 }
Vladimir Markof6675082019-05-17 12:05:28 +01004610 case linker::LinkerPatch::Type::kCallEntrypoint: {
4611 Offset offset(patch.EntrypointOffset());
4612 assembler.JumpTo(ManagedRegister(arm64::TR), offset, ManagedRegister(arm64::IP0));
4613 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
4614 *debug_name = "EntrypointCallThunk_" + std::to_string(offset.Uint32Value());
4615 }
4616 break;
4617 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00004618 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
4619 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
4620 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
4621 break;
4622 }
4623 default:
4624 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
4625 UNREACHABLE();
4626 }
4627
4628 // Ensure we emit the literal pool if any.
4629 assembler.FinalizeCode();
4630 code->resize(assembler.CodeSize());
4631 MemoryRegion code_region(code->data(), code->size());
4632 assembler.FinalizeInstructions(code_region);
4633}
4634
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004635vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
4636 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004637 value,
4638 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4639}
4640
Scott Wakeling97c72b72016-06-24 16:19:36 +01004641vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004642 return uint64_literals_.GetOrCreate(
4643 value,
4644 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004645}
4646
Andreas Gampe878d58c2015-01-15 23:24:00 -08004647void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004648 // Explicit clinit checks triggered by static invokes must have been pruned by
4649 // art::PrepareForRegisterAllocation.
4650 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004651
Andreas Gampe878d58c2015-01-15 23:24:00 -08004652 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08004653 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004654 return;
4655 }
4656
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004657 {
4658 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4659 // are no pools emitted.
4660 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4661 LocationSummary* locations = invoke->GetLocations();
4662 codegen_->GenerateStaticOrDirectCall(
4663 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
4664 }
4665
Andreas Gampe3db70682018-12-26 15:12:03 -08004666 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004667}
4668
4669void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004670 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08004671 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004672 return;
4673 }
4674
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004675 {
4676 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4677 // are no pools emitted.
4678 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4679 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
4680 DCHECK(!codegen_->IsLeafMethod());
4681 }
4682
Andreas Gampe3db70682018-12-26 15:12:03 -08004683 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004684}
4685
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004686HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4687 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004688 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004689 case HLoadClass::LoadKind::kInvalid:
4690 LOG(FATAL) << "UNREACHABLE";
4691 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004692 case HLoadClass::LoadKind::kReferrersClass:
4693 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004694 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004695 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004696 case HLoadClass::LoadKind::kBssEntry:
4697 DCHECK(!Runtime::Current()->UseJitCompilation());
4698 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004699 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004700 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004701 DCHECK(Runtime::Current()->UseJitCompilation());
4702 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004703 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004704 break;
4705 }
4706 return desired_class_load_kind;
4707}
4708
Alexandre Rames67555f72014-11-18 10:55:16 +00004709void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004710 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004711 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004712 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004713 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004714 cls,
4715 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004716 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004717 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004718 return;
4719 }
Vladimir Marko41559982017-01-06 14:04:23 +00004720 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004721
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004722 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4723 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004724 ? LocationSummary::kCallOnSlowPath
4725 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004726 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004727 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004728 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004729 }
4730
Vladimir Marko41559982017-01-06 14:04:23 +00004731 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004732 locations->SetInAt(0, Location::RequiresRegister());
4733 }
4734 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004735 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4736 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4737 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01004738 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004739 } else {
4740 // For non-Baker read barrier we have a temp-clobbering call.
4741 }
4742 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004743}
4744
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004745// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4746// move.
4747void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004748 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004749 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00004750 codegen_->GenerateLoadClassRuntimeCall(cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08004751 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01004752 return;
4753 }
Vladimir Marko41559982017-01-06 14:04:23 +00004754 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004755
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004756 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004757 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004758
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004759 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4760 ? kWithoutReadBarrier
4761 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004762 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004763 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004764 case HLoadClass::LoadKind::kReferrersClass: {
4765 DCHECK(!cls->CanCallRuntime());
4766 DCHECK(!cls->MustGenerateClinitCheck());
4767 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4768 Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00004769 codegen_->GenerateGcRootFieldLoad(cls,
4770 out_loc,
4771 current_method,
4772 ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3db70682018-12-26 15:12:03 -08004773 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00004774 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004775 break;
4776 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004777 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01004778 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
4779 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004780 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004781 // Add ADRP with its PC-relative type patch.
4782 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004783 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004784 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004785 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004786 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004787 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004788 codegen_->NewBootImageTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004789 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004790 break;
4791 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004792 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004793 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004794 uint32_t boot_image_offset = codegen_->GetBootImageOffset(cls);
4795 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
4796 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004797 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004798 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004799 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004800 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004801 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004802 break;
4803 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004804 case HLoadClass::LoadKind::kBssEntry: {
4805 // Add ADRP with its PC-relative Class .bss entry patch.
4806 const DexFile& dex_file = cls->GetDexFile();
4807 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00004808 vixl::aarch64::Register temp = XRegisterFrom(out_loc);
4809 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4810 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004811 // Add LDR with its PC-relative Class .bss entry patch.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004812 vixl::aarch64::Label* ldr_label =
Vladimir Markof3c52b42017-11-17 17:32:12 +00004813 codegen_->NewBssEntryTypePatch(dex_file, type_index, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004814 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01004815 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00004816 codegen_->GenerateGcRootFieldLoad(cls,
4817 out_loc,
4818 temp,
4819 /* offset placeholder */ 0u,
4820 ldr_label,
4821 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004822 generate_null_check = true;
4823 break;
4824 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004825 case HLoadClass::LoadKind::kJitBootImageAddress: {
4826 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
4827 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
4828 DCHECK_NE(address, 0u);
4829 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
4830 break;
4831 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004832 case HLoadClass::LoadKind::kJitTableAddress: {
4833 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4834 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004835 cls->GetClass()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00004836 codegen_->GenerateGcRootFieldLoad(cls,
4837 out_loc,
4838 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08004839 /* offset= */ 0,
4840 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00004841 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004842 break;
4843 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004844 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004845 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004846 LOG(FATAL) << "UNREACHABLE";
4847 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004848 }
4849
Vladimir Markoea4c1262017-02-06 19:59:33 +00004850 bool do_clinit = cls->MustGenerateClinitCheck();
4851 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004852 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01004853 SlowPathCodeARM64* slow_path =
4854 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004855 codegen_->AddSlowPath(slow_path);
4856 if (generate_null_check) {
4857 __ Cbz(out, slow_path->GetEntryLabel());
4858 }
4859 if (cls->MustGenerateClinitCheck()) {
4860 GenerateClassInitializationCheck(slow_path, out);
4861 } else {
4862 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004863 }
Andreas Gampe3db70682018-12-26 15:12:03 -08004864 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004865 }
4866}
4867
Orion Hodsondbaa5c72018-05-10 08:22:46 +01004868void LocationsBuilderARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
4869 InvokeRuntimeCallingConvention calling_convention;
4870 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
4871 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
4872}
4873
4874void InstructionCodeGeneratorARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
4875 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
4876}
4877
Orion Hodson18259d72018-04-12 11:18:23 +01004878void LocationsBuilderARM64::VisitLoadMethodType(HLoadMethodType* load) {
4879 InvokeRuntimeCallingConvention calling_convention;
4880 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
4881 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
4882}
4883
4884void InstructionCodeGeneratorARM64::VisitLoadMethodType(HLoadMethodType* load) {
4885 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
4886}
4887
David Brazdilcb1c0552015-08-04 16:22:25 +01004888static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004889 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004890}
4891
Alexandre Rames67555f72014-11-18 10:55:16 +00004892void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4893 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004894 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00004895 locations->SetOut(Location::RequiresRegister());
4896}
4897
4898void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004899 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4900}
4901
4902void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004903 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01004904}
4905
4906void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4907 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004908}
4909
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004910HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4911 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004912 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004913 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004914 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00004915 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004916 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004917 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004918 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004919 case HLoadString::LoadKind::kJitTableAddress:
4920 DCHECK(Runtime::Current()->UseJitCompilation());
4921 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004922 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004923 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004924 }
4925 return desired_string_load_kind;
4926}
4927
Alexandre Rames67555f72014-11-18 10:55:16 +00004928void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004929 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004930 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004931 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004932 InvokeRuntimeCallingConvention calling_convention;
4933 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4934 } else {
4935 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004936 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4937 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00004938 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01004939 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004940 } else {
4941 // For non-Baker read barrier we have a temp-clobbering call.
4942 }
4943 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004944 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004945}
4946
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004947// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4948// move.
4949void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00004950 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004951 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004952
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004953 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004954 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01004955 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
4956 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004957 // Add ADRP with its PC-relative String patch.
4958 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004959 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004960 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004961 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004962 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004963 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004964 codegen_->NewBootImageStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004965 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004966 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004967 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004968 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004969 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004970 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
4971 uint32_t boot_image_offset = codegen_->GetBootImageOffset(load);
4972 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004973 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004974 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004975 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004976 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004977 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
4978 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004979 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004980 case HLoadString::LoadKind::kBssEntry: {
4981 // Add ADRP with its PC-relative String .bss entry patch.
4982 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004983 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00004984 Register temp = XRegisterFrom(out_loc);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004985 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004986 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004987 // Add LDR with its PC-relative String .bss entry patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004988 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004989 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004990 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01004991 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00004992 codegen_->GenerateGcRootFieldLoad(load,
4993 out_loc,
4994 temp,
4995 /* offset placeholder */ 0u,
4996 ldr_label,
4997 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004998 SlowPathCodeARM64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00004999 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005000 codegen_->AddSlowPath(slow_path);
5001 __ Cbz(out.X(), slow_path->GetEntryLabel());
5002 __ Bind(slow_path->GetExitLabel());
Andreas Gampe3db70682018-12-26 15:12:03 -08005003 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005004 return;
5005 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005006 case HLoadString::LoadKind::kJitBootImageAddress: {
5007 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
5008 DCHECK_NE(address, 0u);
5009 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5010 return;
5011 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005012 case HLoadString::LoadKind::kJitTableAddress: {
5013 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005014 load->GetStringIndex(),
5015 load->GetString()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005016 codegen_->GenerateGcRootFieldLoad(load,
5017 out_loc,
5018 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005019 /* offset= */ 0,
5020 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005021 kCompilerReadBarrierOption);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005022 return;
5023 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005024 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005025 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005026 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005027
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005028 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005029 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005030 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005031 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005032 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5033 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005034 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005035}
5036
Alexandre Rames5319def2014-10-23 10:03:10 +01005037void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005038 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01005039 locations->SetOut(Location::ConstantLocation(constant));
5040}
5041
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005042void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005043 // Will be generated at use site.
5044}
5045
Alexandre Rames67555f72014-11-18 10:55:16 +00005046void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005047 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5048 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005049 InvokeRuntimeCallingConvention calling_convention;
5050 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5051}
5052
5053void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005054 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005055 instruction,
5056 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005057 if (instruction->IsEnter()) {
5058 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5059 } else {
5060 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5061 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005062 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005063}
5064
Alexandre Rames42d641b2014-10-27 14:00:51 +00005065void LocationsBuilderARM64::VisitMul(HMul* mul) {
5066 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005067 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005068 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005069 case DataType::Type::kInt32:
5070 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005071 locations->SetInAt(0, Location::RequiresRegister());
5072 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005073 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005074 break;
5075
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005076 case DataType::Type::kFloat32:
5077 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005078 locations->SetInAt(0, Location::RequiresFpuRegister());
5079 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005080 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005081 break;
5082
5083 default:
5084 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5085 }
5086}
5087
5088void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5089 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005090 case DataType::Type::kInt32:
5091 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005092 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5093 break;
5094
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005095 case DataType::Type::kFloat32:
5096 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005097 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005098 break;
5099
5100 default:
5101 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5102 }
5103}
5104
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005105void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5106 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005107 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005108 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005109 case DataType::Type::kInt32:
5110 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005111 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005112 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005113 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005114
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005115 case DataType::Type::kFloat32:
5116 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005117 locations->SetInAt(0, Location::RequiresFpuRegister());
5118 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005119 break;
5120
5121 default:
5122 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5123 }
5124}
5125
5126void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5127 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005128 case DataType::Type::kInt32:
5129 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005130 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5131 break;
5132
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005133 case DataType::Type::kFloat32:
5134 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005135 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005136 break;
5137
5138 default:
5139 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5140 }
5141}
5142
5143void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005144 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5145 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005146 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005147 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005148 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5149 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005150}
5151
5152void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005153 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5154 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005155 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005156 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005157 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005158}
5159
Alexandre Rames5319def2014-10-23 10:03:10 +01005160void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005161 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5162 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005163 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07005164 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005165 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005166}
5167
5168void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005169 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5170 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005171 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005172}
5173
5174void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005175 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005176 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005177 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005178}
5179
5180void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005181 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005182 case DataType::Type::kInt32:
5183 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005184 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005185 break;
5186
5187 default:
5188 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5189 }
5190}
5191
David Brazdil66d126e2015-04-03 16:02:44 +01005192void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005193 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005194 locations->SetInAt(0, Location::RequiresRegister());
5195 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5196}
5197
5198void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005199 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005200}
5201
Alexandre Rames5319def2014-10-23 10:03:10 +01005202void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005203 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5204 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005205}
5206
Calin Juravle2ae48182016-03-16 14:05:09 +00005207void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5208 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005209 return;
5210 }
Artem Serov914d7a82017-02-07 14:33:49 +00005211 {
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01005212 // Ensure that between load and RecordPcInfo there are no pools emitted.
Artem Serov914d7a82017-02-07 14:33:49 +00005213 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5214 Location obj = instruction->GetLocations()->InAt(0);
5215 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5216 RecordPcInfo(instruction, instruction->GetDexPc());
5217 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005218}
5219
Calin Juravle2ae48182016-03-16 14:05:09 +00005220void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005221 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005222 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005223
5224 LocationSummary* locations = instruction->GetLocations();
5225 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005226
5227 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005228}
5229
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005230void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005231 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005232}
5233
Alexandre Rames67555f72014-11-18 10:55:16 +00005234void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5235 HandleBinaryOp(instruction);
5236}
5237
5238void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5239 HandleBinaryOp(instruction);
5240}
5241
Alexandre Rames3e69f162014-12-10 10:36:50 +00005242void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5243 LOG(FATAL) << "Unreachable";
5244}
5245
5246void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005247 if (instruction->GetNext()->IsSuspendCheck() &&
5248 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5249 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5250 // The back edge will generate the suspend check.
5251 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5252 }
5253
Alexandre Rames3e69f162014-12-10 10:36:50 +00005254 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5255}
5256
Alexandre Rames5319def2014-10-23 10:03:10 +01005257void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005258 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005259 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5260 if (location.IsStackSlot()) {
5261 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5262 } else if (location.IsDoubleStackSlot()) {
5263 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5264 }
5265 locations->SetOut(location);
5266}
5267
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005268void InstructionCodeGeneratorARM64::VisitParameterValue(
5269 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005270 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005271}
5272
5273void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5274 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005275 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005276 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005277}
5278
5279void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5280 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5281 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005282}
5283
5284void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005285 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005286 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005287 locations->SetInAt(i, Location::Any());
5288 }
5289 locations->SetOut(Location::Any());
5290}
5291
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005292void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005293 LOG(FATAL) << "Unreachable";
5294}
5295
Serban Constantinescu02164b32014-11-13 14:05:07 +00005296void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005297 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005298 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005299 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005300 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005301 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005302
5303 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005304 case DataType::Type::kInt32:
5305 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005306 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005307 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005308 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5309 break;
5310
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005311 case DataType::Type::kFloat32:
5312 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005313 InvokeRuntimeCallingConvention calling_convention;
5314 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5315 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5316 locations->SetOut(calling_convention.GetReturnLocation(type));
5317
5318 break;
5319 }
5320
Serban Constantinescu02164b32014-11-13 14:05:07 +00005321 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005322 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005323 }
5324}
5325
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005326void InstructionCodeGeneratorARM64::GenerateIntRemForPower2Denom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005327 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005328 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
5329 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
5330
5331 Register out = OutputRegister(instruction);
5332 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005333
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005334 if (abs_imm == 2) {
5335 __ Cmp(dividend, 0);
5336 __ And(out, dividend, 1);
5337 __ Csneg(out, out, out, ge);
5338 } else {
5339 UseScratchRegisterScope temps(GetVIXLAssembler());
5340 Register temp = temps.AcquireSameSizeAs(out);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005341
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005342 __ Negs(temp, dividend);
5343 __ And(out, dividend, abs_imm - 1);
5344 __ And(temp, temp, abs_imm - 1);
5345 __ Csneg(out, out, temp, mi);
5346 }
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005347}
5348
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005349void InstructionCodeGeneratorARM64::GenerateIntRemForConstDenom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005350 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005351
5352 if (imm == 0) {
5353 // Do not generate anything.
5354 // DivZeroCheck would prevent any code to be executed.
5355 return;
5356 }
5357
Evgeny Astigeevichf58dc652018-06-25 17:54:07 +01005358 if (IsPowerOfTwo(AbsOrMin(imm))) {
5359 // Cases imm == -1 or imm == 1 are handled in constant folding by
5360 // InstructionWithAbsorbingInputSimplifier.
5361 // If the cases have survided till code generation they are handled in
5362 // GenerateIntRemForPower2Denom becauses -1 and 1 are the power of 2 (2^0).
5363 // The correct code is generated for them, just more instructions.
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005364 GenerateIntRemForPower2Denom(instruction);
5365 } else {
5366 DCHECK(imm < -2 || imm > 2) << imm;
5367 GenerateDivRemWithAnyConstant(instruction);
5368 }
5369}
5370
5371void InstructionCodeGeneratorARM64::GenerateIntRem(HRem* instruction) {
5372 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
5373 << instruction->GetResultType();
5374
5375 if (instruction->GetLocations()->InAt(1).IsConstant()) {
5376 GenerateIntRemForConstDenom(instruction);
5377 } else {
5378 Register out = OutputRegister(instruction);
5379 Register dividend = InputRegisterAt(instruction, 0);
5380 Register divisor = InputRegisterAt(instruction, 1);
5381 UseScratchRegisterScope temps(GetVIXLAssembler());
5382 Register temp = temps.AcquireSameSizeAs(out);
5383 __ Sdiv(temp, dividend, divisor);
5384 __ Msub(out, temp, divisor, dividend);
5385 }
5386}
5387
Serban Constantinescu02164b32014-11-13 14:05:07 +00005388void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005389 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005390
Serban Constantinescu02164b32014-11-13 14:05:07 +00005391 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005392 case DataType::Type::kInt32:
5393 case DataType::Type::kInt64: {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005394 GenerateIntRem(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005395 break;
5396 }
5397
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005398 case DataType::Type::kFloat32:
5399 case DataType::Type::kFloat64: {
5400 QuickEntrypointEnum entrypoint =
5401 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005402 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005403 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00005404 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5405 } else {
5406 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5407 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005408 break;
5409 }
5410
Serban Constantinescu02164b32014-11-13 14:05:07 +00005411 default:
5412 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005413 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005414 }
5415}
5416
Aart Bik1f8d51b2018-02-15 10:42:37 -08005417void LocationsBuilderARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005418 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005419}
5420
Aart Bik1f8d51b2018-02-15 10:42:37 -08005421void InstructionCodeGeneratorARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005422 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005423}
5424
5425void LocationsBuilderARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005426 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005427}
5428
5429void InstructionCodeGeneratorARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005430 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005431}
5432
Aart Bik3dad3412018-02-28 12:01:46 -08005433void LocationsBuilderARM64::VisitAbs(HAbs* abs) {
5434 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
5435 switch (abs->GetResultType()) {
5436 case DataType::Type::kInt32:
5437 case DataType::Type::kInt64:
5438 locations->SetInAt(0, Location::RequiresRegister());
5439 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5440 break;
5441 case DataType::Type::kFloat32:
5442 case DataType::Type::kFloat64:
5443 locations->SetInAt(0, Location::RequiresFpuRegister());
5444 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5445 break;
5446 default:
5447 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5448 }
5449}
5450
5451void InstructionCodeGeneratorARM64::VisitAbs(HAbs* abs) {
5452 switch (abs->GetResultType()) {
5453 case DataType::Type::kInt32:
5454 case DataType::Type::kInt64: {
5455 Register in_reg = InputRegisterAt(abs, 0);
5456 Register out_reg = OutputRegister(abs);
5457 __ Cmp(in_reg, Operand(0));
5458 __ Cneg(out_reg, in_reg, lt);
5459 break;
5460 }
5461 case DataType::Type::kFloat32:
5462 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01005463 VRegister in_reg = InputFPRegisterAt(abs, 0);
5464 VRegister out_reg = OutputFPRegister(abs);
Aart Bik3dad3412018-02-28 12:01:46 -08005465 __ Fabs(out_reg, in_reg);
5466 break;
5467 }
5468 default:
5469 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5470 }
5471}
5472
Igor Murashkind01745e2017-04-05 16:40:31 -07005473void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5474 constructor_fence->SetLocations(nullptr);
5475}
5476
5477void InstructionCodeGeneratorARM64::VisitConstructorFence(
5478 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5479 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5480}
5481
Calin Juravle27df7582015-04-17 19:12:31 +01005482void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5483 memory_barrier->SetLocations(nullptr);
5484}
5485
5486void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005487 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005488}
5489
Alexandre Rames5319def2014-10-23 10:03:10 +01005490void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005491 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005492 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005493 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005494}
5495
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005496void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005497 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005498}
5499
5500void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5501 instruction->SetLocations(nullptr);
5502}
5503
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005504void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005505 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005506}
5507
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005508void LocationsBuilderARM64::VisitRor(HRor* ror) {
5509 HandleBinaryOp(ror);
5510}
5511
5512void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5513 HandleBinaryOp(ror);
5514}
5515
Serban Constantinescu02164b32014-11-13 14:05:07 +00005516void LocationsBuilderARM64::VisitShl(HShl* shl) {
5517 HandleShift(shl);
5518}
5519
5520void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5521 HandleShift(shl);
5522}
5523
5524void LocationsBuilderARM64::VisitShr(HShr* shr) {
5525 HandleShift(shr);
5526}
5527
5528void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5529 HandleShift(shr);
5530}
5531
Alexandre Rames5319def2014-10-23 10:03:10 +01005532void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005533 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005534}
5535
5536void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005537 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005538}
5539
Alexandre Rames67555f72014-11-18 10:55:16 +00005540void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005541 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005542}
5543
5544void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005545 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005546}
5547
5548void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005549 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005550}
5551
Alexandre Rames67555f72014-11-18 10:55:16 +00005552void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005553 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005554}
5555
Vladimir Marko552a1342017-10-31 10:56:47 +00005556void LocationsBuilderARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5557 codegen_->CreateStringBuilderAppendLocations(instruction, LocationFrom(x0));
5558}
5559
5560void InstructionCodeGeneratorARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5561 __ Mov(w0, instruction->GetFormat()->GetValue());
5562 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
5563}
5564
Calin Juravlee460d1d2015-09-29 04:52:17 +01005565void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5566 HUnresolvedInstanceFieldGet* instruction) {
5567 FieldAccessCallingConventionARM64 calling_convention;
5568 codegen_->CreateUnresolvedFieldLocationSummary(
5569 instruction, instruction->GetFieldType(), calling_convention);
5570}
5571
5572void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5573 HUnresolvedInstanceFieldGet* instruction) {
5574 FieldAccessCallingConventionARM64 calling_convention;
5575 codegen_->GenerateUnresolvedFieldAccess(instruction,
5576 instruction->GetFieldType(),
5577 instruction->GetFieldIndex(),
5578 instruction->GetDexPc(),
5579 calling_convention);
5580}
5581
5582void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5583 HUnresolvedInstanceFieldSet* instruction) {
5584 FieldAccessCallingConventionARM64 calling_convention;
5585 codegen_->CreateUnresolvedFieldLocationSummary(
5586 instruction, instruction->GetFieldType(), calling_convention);
5587}
5588
5589void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5590 HUnresolvedInstanceFieldSet* instruction) {
5591 FieldAccessCallingConventionARM64 calling_convention;
5592 codegen_->GenerateUnresolvedFieldAccess(instruction,
5593 instruction->GetFieldType(),
5594 instruction->GetFieldIndex(),
5595 instruction->GetDexPc(),
5596 calling_convention);
5597}
5598
5599void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5600 HUnresolvedStaticFieldGet* instruction) {
5601 FieldAccessCallingConventionARM64 calling_convention;
5602 codegen_->CreateUnresolvedFieldLocationSummary(
5603 instruction, instruction->GetFieldType(), calling_convention);
5604}
5605
5606void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5607 HUnresolvedStaticFieldGet* instruction) {
5608 FieldAccessCallingConventionARM64 calling_convention;
5609 codegen_->GenerateUnresolvedFieldAccess(instruction,
5610 instruction->GetFieldType(),
5611 instruction->GetFieldIndex(),
5612 instruction->GetDexPc(),
5613 calling_convention);
5614}
5615
5616void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5617 HUnresolvedStaticFieldSet* instruction) {
5618 FieldAccessCallingConventionARM64 calling_convention;
5619 codegen_->CreateUnresolvedFieldLocationSummary(
5620 instruction, instruction->GetFieldType(), calling_convention);
5621}
5622
5623void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5624 HUnresolvedStaticFieldSet* instruction) {
5625 FieldAccessCallingConventionARM64 calling_convention;
5626 codegen_->GenerateUnresolvedFieldAccess(instruction,
5627 instruction->GetFieldType(),
5628 instruction->GetFieldIndex(),
5629 instruction->GetDexPc(),
5630 calling_convention);
5631}
5632
Alexandre Rames5319def2014-10-23 10:03:10 +01005633void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005634 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5635 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005636 // In suspend check slow path, usually there are no caller-save registers at all.
5637 // If SIMD instructions are present, however, we force spilling all live SIMD
5638 // registers in full width (since the runtime only saves/restores lower part).
5639 locations->SetCustomSlowPathCallerSaves(
5640 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005641}
5642
5643void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005644 HBasicBlock* block = instruction->GetBlock();
5645 if (block->GetLoopInformation() != nullptr) {
5646 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5647 // The back edge will generate the suspend check.
5648 return;
5649 }
5650 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5651 // The goto will generate the suspend check.
5652 return;
5653 }
5654 GenerateSuspendCheck(instruction, nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08005655 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005656}
5657
Alexandre Rames67555f72014-11-18 10:55:16 +00005658void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005659 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5660 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005661 InvokeRuntimeCallingConvention calling_convention;
5662 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5663}
5664
5665void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005666 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005667 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005668}
5669
5670void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5671 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005672 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005673 DataType::Type input_type = conversion->GetInputType();
5674 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005675 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5676 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005677 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
5678 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005679 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5680 }
5681
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005682 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005683 locations->SetInAt(0, Location::RequiresFpuRegister());
5684 } else {
5685 locations->SetInAt(0, Location::RequiresRegister());
5686 }
5687
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005688 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005689 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5690 } else {
5691 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5692 }
5693}
5694
5695void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005696 DataType::Type result_type = conversion->GetResultType();
5697 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00005698
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005699 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5700 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005701
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005702 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
5703 int result_size = DataType::Size(result_type);
5704 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005705 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005706 Register output = OutputRegister(conversion);
5707 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005708 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005709 // 'int' values are used directly as W registers, discarding the top
5710 // bits, so we don't need to sign-extend and can just perform a move.
5711 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5712 // top 32 bits of the target register. We theoretically could leave those
5713 // bits unchanged, but we would have to make sure that no code uses a
5714 // 32bit input value as a 64bit value assuming that the top 32 bits are
5715 // zero.
5716 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005717 } else if (DataType::IsUnsignedType(result_type) ||
5718 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
5719 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005720 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005721 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005722 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005723 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005724 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005725 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
5726 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005727 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005728 } else if (DataType::IsFloatingPointType(result_type) &&
5729 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005730 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5731 } else {
5732 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5733 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005734 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005735}
Alexandre Rames67555f72014-11-18 10:55:16 +00005736
Serban Constantinescu02164b32014-11-13 14:05:07 +00005737void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5738 HandleShift(ushr);
5739}
5740
5741void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5742 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005743}
5744
5745void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5746 HandleBinaryOp(instruction);
5747}
5748
5749void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5750 HandleBinaryOp(instruction);
5751}
5752
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005753void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005754 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005755 LOG(FATAL) << "Unreachable";
5756}
5757
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005758void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005759 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005760 LOG(FATAL) << "Unreachable";
5761}
5762
Mark Mendellfe57faa2015-09-18 09:26:15 -04005763// Simple implementation of packed switch - generate cascaded compare/jumps.
5764void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5765 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005766 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005767 locations->SetInAt(0, Location::RequiresRegister());
5768}
5769
5770void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5771 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005772 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005773 Register value_reg = InputRegisterAt(switch_instr, 0);
5774 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5775
Zheng Xu3927c8b2015-11-18 17:46:25 +08005776 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005777 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005778 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5779 // make sure we don't emit it if the target may run out of range.
5780 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5781 // ranges and emit the tables only as required.
5782 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005783
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005784 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005785 // Current instruction id is an upper bound of the number of HIRs in the graph.
5786 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5787 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005788 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5789 Register temp = temps.AcquireW();
5790 __ Subs(temp, value_reg, Operand(lower_bound));
5791
Zheng Xu3927c8b2015-11-18 17:46:25 +08005792 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005793 // Jump to successors[0] if value == lower_bound.
5794 __ B(eq, codegen_->GetLabelOf(successors[0]));
5795 int32_t last_index = 0;
5796 for (; num_entries - last_index > 2; last_index += 2) {
5797 __ Subs(temp, temp, Operand(2));
5798 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5799 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5800 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5801 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5802 }
5803 if (num_entries - last_index == 2) {
5804 // The last missing case_value.
5805 __ Cmp(temp, Operand(1));
5806 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005807 }
5808
5809 // And the default for any other value.
5810 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5811 __ B(codegen_->GetLabelOf(default_block));
5812 }
5813 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005814 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005815
5816 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5817
5818 // Below instructions should use at most one blocked register. Since there are two blocked
5819 // registers, we are free to block one.
5820 Register temp_w = temps.AcquireW();
5821 Register index;
5822 // Remove the bias.
5823 if (lower_bound != 0) {
5824 index = temp_w;
5825 __ Sub(index, value_reg, Operand(lower_bound));
5826 } else {
5827 index = value_reg;
5828 }
5829
5830 // Jump to default block if index is out of the range.
5831 __ Cmp(index, Operand(num_entries));
5832 __ B(hs, codegen_->GetLabelOf(default_block));
5833
5834 // In current VIXL implementation, it won't require any blocked registers to encode the
5835 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5836 // register pressure.
5837 Register table_base = temps.AcquireX();
5838 // Load jump offset from the table.
5839 __ Adr(table_base, jump_table->GetTableStartLabel());
5840 Register jump_offset = temp_w;
5841 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5842
5843 // Jump to target block by branching to table_base(pc related) + offset.
5844 Register target_address = table_base;
5845 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5846 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005847 }
5848}
5849
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005850void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5851 HInstruction* instruction,
5852 Location out,
5853 uint32_t offset,
5854 Location maybe_temp,
5855 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005856 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00005857 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005858 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005859 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005860 if (kUseBakerReadBarrier) {
5861 // Load with fast path based Baker's read barrier.
5862 // /* HeapReference<Object> */ out = *(out + offset)
5863 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5864 out,
5865 out_reg,
5866 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005867 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08005868 /* needs_null_check= */ false,
5869 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005870 } else {
5871 // Load with slow path based read barrier.
5872 // Save the value of `out` into `maybe_temp` before overwriting it
5873 // in the following move operation, as we will need it for the
5874 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005875 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005876 __ Mov(temp_reg, out_reg);
5877 // /* HeapReference<Object> */ out = *(out + offset)
5878 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5879 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5880 }
5881 } else {
5882 // Plain load with no read barrier.
5883 // /* HeapReference<Object> */ out = *(out + offset)
5884 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5885 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5886 }
5887}
5888
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005889void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5890 HInstruction* instruction,
5891 Location out,
5892 Location obj,
5893 uint32_t offset,
5894 Location maybe_temp,
5895 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005896 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00005897 Register out_reg = RegisterFrom(out, type);
5898 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005899 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005900 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005901 if (kUseBakerReadBarrier) {
5902 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00005903 // /* HeapReference<Object> */ out = *(obj + offset)
5904 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5905 out,
5906 obj_reg,
5907 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005908 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08005909 /* needs_null_check= */ false,
5910 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005911 } else {
5912 // Load with slow path based read barrier.
5913 // /* HeapReference<Object> */ out = *(obj + offset)
5914 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5915 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5916 }
5917 } else {
5918 // Plain load with no read barrier.
5919 // /* HeapReference<Object> */ out = *(obj + offset)
5920 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5921 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5922 }
5923}
5924
Vladimir Markoca1e0382018-04-11 09:58:41 +00005925void CodeGeneratorARM64::GenerateGcRootFieldLoad(
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005926 HInstruction* instruction,
5927 Location root,
5928 Register obj,
5929 uint32_t offset,
5930 vixl::aarch64::Label* fixup_label,
5931 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005932 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005933 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005934 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005935 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005936 if (kUseBakerReadBarrier) {
5937 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005938 // Baker's read barrier are used.
Roland Levillain44015862016-01-22 11:47:17 +00005939
Vladimir Marko008e09f32018-08-06 15:42:43 +01005940 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
5941 // the Marking Register) to decide whether we need to enter
5942 // the slow path to mark the GC root.
5943 //
5944 // We use shared thunks for the slow path; shared within the method
5945 // for JIT, across methods for AOT. That thunk checks the reference
5946 // and jumps to the entrypoint if needed.
5947 //
5948 // lr = &return_address;
5949 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5950 // if (mr) { // Thread::Current()->GetIsGcMarking()
5951 // goto gc_root_thunk<root_reg>(lr)
5952 // }
5953 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00005954
Vladimir Marko008e09f32018-08-06 15:42:43 +01005955 UseScratchRegisterScope temps(GetVIXLAssembler());
5956 DCHECK(temps.IsAvailable(ip0));
5957 DCHECK(temps.IsAvailable(ip1));
5958 temps.Exclude(ip0, ip1);
5959 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
Roland Levillain44015862016-01-22 11:47:17 +00005960
Vladimir Marko008e09f32018-08-06 15:42:43 +01005961 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
5962 vixl::aarch64::Label return_address;
5963 __ adr(lr, &return_address);
5964 if (fixup_label != nullptr) {
5965 __ bind(fixup_label);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005966 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01005967 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
Vladimir Marko94796f82018-08-08 15:15:33 +01005968 "GC root LDR must be 2 instructions (8B) before the return address label.");
Vladimir Marko008e09f32018-08-06 15:42:43 +01005969 __ ldr(root_reg, MemOperand(obj.X(), offset));
5970 EmitBakerReadBarrierCbnz(custom_data);
5971 __ bind(&return_address);
Roland Levillain44015862016-01-22 11:47:17 +00005972 } else {
5973 // GC root loaded through a slow path for read barriers other
5974 // than Baker's.
5975 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005976 if (fixup_label == nullptr) {
5977 __ Add(root_reg.X(), obj.X(), offset);
5978 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00005979 EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005980 }
Roland Levillain44015862016-01-22 11:47:17 +00005981 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00005982 GenerateReadBarrierForRootSlow(instruction, root, root);
Roland Levillain44015862016-01-22 11:47:17 +00005983 }
5984 } else {
5985 // Plain GC root load with no read barrier.
5986 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005987 if (fixup_label == nullptr) {
5988 __ Ldr(root_reg, MemOperand(obj, offset));
5989 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00005990 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005991 }
Roland Levillain44015862016-01-22 11:47:17 +00005992 // Note that GC roots are not affected by heap poisoning, thus we
5993 // do not have to unpoison `root_reg` here.
5994 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005995 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00005996}
5997
Vladimir Marko94796f82018-08-08 15:15:33 +01005998void CodeGeneratorARM64::GenerateUnsafeCasOldValueMovWithBakerReadBarrier(
5999 vixl::aarch64::Register marked,
6000 vixl::aarch64::Register old_value) {
6001 DCHECK(kEmitCompilerReadBarrier);
6002 DCHECK(kUseBakerReadBarrier);
6003
6004 // Similar to the Baker RB path in GenerateGcRootFieldLoad(), with a MOV instead of LDR.
6005 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(marked.GetCode());
6006
6007 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6008 vixl::aarch64::Label return_address;
6009 __ adr(lr, &return_address);
6010 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6011 "GC root LDR must be 2 instructions (8B) before the return address label.");
6012 __ mov(marked, old_value);
6013 EmitBakerReadBarrierCbnz(custom_data);
6014 __ bind(&return_address);
6015}
6016
Roland Levillain44015862016-01-22 11:47:17 +00006017void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6018 Location ref,
Vladimir Marko248141f2018-08-10 10:40:07 +01006019 vixl::aarch64::Register obj,
6020 const vixl::aarch64::MemOperand& src,
Roland Levillain44015862016-01-22 11:47:17 +00006021 bool needs_null_check,
6022 bool use_load_acquire) {
6023 DCHECK(kEmitCompilerReadBarrier);
6024 DCHECK(kUseBakerReadBarrier);
6025
Vladimir Marko0ecac682018-08-07 10:40:38 +01006026 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6027 // Marking Register) to decide whether we need to enter the slow
6028 // path to mark the reference. Then, in the slow path, check the
6029 // gray bit in the lock word of the reference's holder (`obj`) to
6030 // decide whether to mark `ref` or not.
6031 //
6032 // We use shared thunks for the slow path; shared within the method
6033 // for JIT, across methods for AOT. That thunk checks the holder
6034 // and jumps to the entrypoint if needed. If the holder is not gray,
6035 // it creates a fake dependency and returns to the LDR instruction.
6036 //
6037 // lr = &gray_return_address;
6038 // if (mr) { // Thread::Current()->GetIsGcMarking()
6039 // goto field_thunk<holder_reg, base_reg, use_load_acquire>(lr)
6040 // }
6041 // not_gray_return_address:
6042 // // Original reference load. If the offset is too large to fit
6043 // // into LDR, we use an adjusted base register here.
6044 // HeapReference<mirror::Object> reference = *(obj+offset);
6045 // gray_return_address:
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006046
Vladimir Marko248141f2018-08-10 10:40:07 +01006047 DCHECK(src.GetAddrMode() == vixl::aarch64::Offset);
6048 DCHECK_ALIGNED(src.GetOffset(), sizeof(mirror::HeapReference<mirror::Object>));
6049
6050 UseScratchRegisterScope temps(GetVIXLAssembler());
6051 DCHECK(temps.IsAvailable(ip0));
6052 DCHECK(temps.IsAvailable(ip1));
6053 temps.Exclude(ip0, ip1);
6054 uint32_t custom_data = use_load_acquire
6055 ? EncodeBakerReadBarrierAcquireData(src.GetBaseRegister().GetCode(), obj.GetCode())
6056 : EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode());
6057
6058 {
6059 ExactAssemblyScope guard(GetVIXLAssembler(),
6060 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6061 vixl::aarch64::Label return_address;
6062 __ adr(lr, &return_address);
6063 EmitBakerReadBarrierCbnz(custom_data);
6064 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6065 "Field LDR must be 1 instruction (4B) before the return address label; "
6066 " 2 instructions (8B) for heap poisoning.");
6067 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
6068 if (use_load_acquire) {
6069 DCHECK_EQ(src.GetOffset(), 0);
6070 __ ldar(ref_reg, src);
6071 } else {
6072 __ ldr(ref_reg, src);
6073 }
6074 if (needs_null_check) {
6075 MaybeRecordImplicitNullCheck(instruction);
6076 }
6077 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6078 // macro instructions disallowed in ExactAssemblyScope.
6079 if (kPoisonHeapReferences) {
6080 __ neg(ref_reg, Operand(ref_reg));
6081 }
6082 __ bind(&return_address);
6083 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006084 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Vladimir Marko248141f2018-08-10 10:40:07 +01006085}
6086
6087void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6088 Location ref,
6089 Register obj,
6090 uint32_t offset,
6091 Location maybe_temp,
6092 bool needs_null_check,
6093 bool use_load_acquire) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006094 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6095 Register base = obj;
6096 if (use_load_acquire) {
6097 DCHECK(maybe_temp.IsRegister());
6098 base = WRegisterFrom(maybe_temp);
6099 __ Add(base, obj, offset);
6100 offset = 0u;
6101 } else if (offset >= kReferenceLoadMinFarOffset) {
6102 DCHECK(maybe_temp.IsRegister());
6103 base = WRegisterFrom(maybe_temp);
6104 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6105 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6106 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006107 }
Vladimir Marko248141f2018-08-10 10:40:07 +01006108 MemOperand src(base.X(), offset);
6109 GenerateFieldLoadWithBakerReadBarrier(
6110 instruction, ref, obj, src, needs_null_check, use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006111}
6112
Artem Serov0806f582018-10-11 20:14:20 +01006113void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HArrayGet* instruction,
6114 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006115 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006116 uint32_t data_offset,
6117 Location index,
Roland Levillain44015862016-01-22 11:47:17 +00006118 bool needs_null_check) {
6119 DCHECK(kEmitCompilerReadBarrier);
6120 DCHECK(kUseBakerReadBarrier);
6121
Vladimir Marko66d691d2017-04-07 17:53:39 +01006122 static_assert(
6123 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6124 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006125 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006126
Vladimir Marko008e09f32018-08-06 15:42:43 +01006127 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6128 // Marking Register) to decide whether we need to enter the slow
6129 // path to mark the reference. Then, in the slow path, check the
6130 // gray bit in the lock word of the reference's holder (`obj`) to
6131 // decide whether to mark `ref` or not.
6132 //
6133 // We use shared thunks for the slow path; shared within the method
6134 // for JIT, across methods for AOT. That thunk checks the holder
6135 // and jumps to the entrypoint if needed. If the holder is not gray,
6136 // it creates a fake dependency and returns to the LDR instruction.
6137 //
6138 // lr = &gray_return_address;
6139 // if (mr) { // Thread::Current()->GetIsGcMarking()
6140 // goto array_thunk<base_reg>(lr)
6141 // }
6142 // not_gray_return_address:
6143 // // Original reference load. If the offset is too large to fit
6144 // // into LDR, we use an adjusted base register here.
6145 // HeapReference<mirror::Object> reference = data[index];
6146 // gray_return_address:
Vladimir Marko66d691d2017-04-07 17:53:39 +01006147
Vladimir Marko008e09f32018-08-06 15:42:43 +01006148 DCHECK(index.IsValid());
6149 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
6150 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006151
Vladimir Marko008e09f32018-08-06 15:42:43 +01006152 UseScratchRegisterScope temps(GetVIXLAssembler());
6153 DCHECK(temps.IsAvailable(ip0));
6154 DCHECK(temps.IsAvailable(ip1));
6155 temps.Exclude(ip0, ip1);
Artem Serov0806f582018-10-11 20:14:20 +01006156
6157 Register temp;
6158 if (instruction->GetArray()->IsIntermediateAddress()) {
6159 // We do not need to compute the intermediate address from the array: the
6160 // input instruction has done it already. See the comment in
6161 // `TryExtractArrayAccessAddress()`.
6162 if (kIsDebugBuild) {
6163 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
6164 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
6165 }
6166 temp = obj;
6167 } else {
6168 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
6169 __ Add(temp.X(), obj.X(), Operand(data_offset));
6170 }
6171
Vladimir Marko008e09f32018-08-06 15:42:43 +01006172 uint32_t custom_data = EncodeBakerReadBarrierArrayData(temp.GetCode());
Vladimir Marko66d691d2017-04-07 17:53:39 +01006173
Vladimir Marko008e09f32018-08-06 15:42:43 +01006174 {
6175 ExactAssemblyScope guard(GetVIXLAssembler(),
6176 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6177 vixl::aarch64::Label return_address;
6178 __ adr(lr, &return_address);
6179 EmitBakerReadBarrierCbnz(custom_data);
6180 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6181 "Array LDR must be 1 instruction (4B) before the return address label; "
6182 " 2 instructions (8B) for heap poisoning.");
6183 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6184 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6185 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6186 // macro instructions disallowed in ExactAssemblyScope.
6187 if (kPoisonHeapReferences) {
6188 __ neg(ref_reg, Operand(ref_reg));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006189 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006190 __ bind(&return_address);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006191 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006192 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Roland Levillain44015862016-01-22 11:47:17 +00006193}
6194
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006195void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6196 // The following condition is a compile-time one, so it does not have a run-time cost.
6197 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
6198 // The following condition is a run-time one; it is executed after the
6199 // previous compile-time test, to avoid penalizing non-debug builds.
6200 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6201 UseScratchRegisterScope temps(GetVIXLAssembler());
6202 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6203 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6204 }
6205 }
6206}
6207
Roland Levillain44015862016-01-22 11:47:17 +00006208void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6209 Location out,
6210 Location ref,
6211 Location obj,
6212 uint32_t offset,
6213 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006214 DCHECK(kEmitCompilerReadBarrier);
6215
Roland Levillain44015862016-01-22 11:47:17 +00006216 // Insert a slow path based read barrier *after* the reference load.
6217 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006218 // If heap poisoning is enabled, the unpoisoning of the loaded
6219 // reference will be carried out by the runtime within the slow
6220 // path.
6221 //
6222 // Note that `ref` currently does not get unpoisoned (when heap
6223 // poisoning is enabled), which is alright as the `ref` argument is
6224 // not used by the artReadBarrierSlow entry point.
6225 //
6226 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006227 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006228 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6229 AddSlowPath(slow_path);
6230
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006231 __ B(slow_path->GetEntryLabel());
6232 __ Bind(slow_path->GetExitLabel());
6233}
6234
Roland Levillain44015862016-01-22 11:47:17 +00006235void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6236 Location out,
6237 Location ref,
6238 Location obj,
6239 uint32_t offset,
6240 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006241 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006242 // Baker's read barriers shall be handled by the fast path
6243 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6244 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006245 // If heap poisoning is enabled, unpoisoning will be taken care of
6246 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006247 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006248 } else if (kPoisonHeapReferences) {
6249 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6250 }
6251}
6252
Roland Levillain44015862016-01-22 11:47:17 +00006253void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6254 Location out,
6255 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006256 DCHECK(kEmitCompilerReadBarrier);
6257
Roland Levillain44015862016-01-22 11:47:17 +00006258 // Insert a slow path based read barrier *after* the GC root load.
6259 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006260 // Note that GC roots are not affected by heap poisoning, so we do
6261 // not need to do anything special for this here.
6262 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006263 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006264 AddSlowPath(slow_path);
6265
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006266 __ B(slow_path->GetEntryLabel());
6267 __ Bind(slow_path->GetExitLabel());
6268}
6269
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006270void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6271 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006272 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006273 locations->SetInAt(0, Location::RequiresRegister());
6274 locations->SetOut(Location::RequiresRegister());
6275}
6276
6277void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6278 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006279 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006280 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006281 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006282 __ Ldr(XRegisterFrom(locations->Out()),
6283 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006284 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006285 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006286 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006287 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6288 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006289 __ Ldr(XRegisterFrom(locations->Out()),
6290 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006291 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006292}
6293
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006294static void PatchJitRootUse(uint8_t* code,
6295 const uint8_t* roots_data,
6296 vixl::aarch64::Literal<uint32_t>* literal,
6297 uint64_t index_in_table) {
6298 uint32_t literal_offset = literal->GetOffset();
6299 uintptr_t address =
6300 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6301 uint8_t* data = code + literal_offset;
6302 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6303}
6304
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006305void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6306 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006307 const StringReference& string_reference = entry.first;
6308 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006309 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006310 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006311 }
6312 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006313 const TypeReference& type_reference = entry.first;
6314 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006315 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006316 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006317 }
6318}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006319
Alexandre Rames67555f72014-11-18 10:55:16 +00006320#undef __
6321#undef QUICK_ENTRY_POINT
6322
Vladimir Markoca1e0382018-04-11 09:58:41 +00006323#define __ assembler.GetVIXLAssembler()->
6324
6325static void EmitGrayCheckAndFastPath(arm64::Arm64Assembler& assembler,
6326 vixl::aarch64::Register base_reg,
6327 vixl::aarch64::MemOperand& lock_word,
Vladimir Marko7a695052018-04-12 10:26:50 +01006328 vixl::aarch64::Label* slow_path,
6329 vixl::aarch64::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006330 // Load the lock word containing the rb_state.
6331 __ Ldr(ip0.W(), lock_word);
6332 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01006333 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Vladimir Markoca1e0382018-04-11 09:58:41 +00006334 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6335 __ Tbnz(ip0.W(), LockWord::kReadBarrierStateShift, slow_path);
6336 static_assert(
6337 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET,
6338 "Field and array LDR offsets must be the same to reuse the same code.");
Vladimir Marko7a695052018-04-12 10:26:50 +01006339 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
6340 if (throw_npe != nullptr) {
6341 __ Bind(throw_npe);
6342 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006343 // Adjust the return address back to the LDR (1 instruction; 2 for heap poisoning).
6344 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6345 "Field LDR must be 1 instruction (4B) before the return address label; "
6346 " 2 instructions (8B) for heap poisoning.");
6347 __ Add(lr, lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6348 // Introduce a dependency on the lock_word including rb_state,
6349 // to prevent load-load reordering, and without using
6350 // a memory barrier (which would be more expensive).
6351 __ Add(base_reg, base_reg, Operand(ip0, LSR, 32));
6352 __ Br(lr); // And return back to the function.
6353 // Note: The fake dependency is unnecessary for the slow path.
6354}
6355
6356// Load the read barrier introspection entrypoint in register `entrypoint`.
6357static void LoadReadBarrierMarkIntrospectionEntrypoint(arm64::Arm64Assembler& assembler,
6358 vixl::aarch64::Register entrypoint) {
6359 // entrypoint = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6360 DCHECK_EQ(ip0.GetCode(), 16u);
6361 const int32_t entry_point_offset =
6362 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6363 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
6364}
6365
6366void CodeGeneratorARM64::CompileBakerReadBarrierThunk(Arm64Assembler& assembler,
6367 uint32_t encoded_data,
6368 /*out*/ std::string* debug_name) {
6369 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
6370 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006371 case BakerReadBarrierKind::kField:
6372 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006373 auto base_reg =
6374 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6375 CheckValidReg(base_reg.GetCode());
6376 auto holder_reg =
6377 Register::GetXRegFromCode(BakerReadBarrierSecondRegField::Decode(encoded_data));
6378 CheckValidReg(holder_reg.GetCode());
6379 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6380 temps.Exclude(ip0, ip1);
Roland Levillain988c3912019-09-25 19:33:35 +01006381 // In the case of a field load (with relaxed semantic), if `base_reg` differs from
6382 // `holder_reg`, the offset was too large and we must have emitted (during the construction
6383 // of the HIR graph, see `art::HInstructionBuilder::BuildInstanceFieldAccess`) and preserved
6384 // (see `art::PrepareForRegisterAllocation::VisitNullCheck`) an explicit null check before
6385 // the load. Otherwise, for implicit null checks, we need to null-check the holder as we do
6386 // not necessarily do that check before going to the thunk.
6387 //
6388 // In the case of a field load with load-acquire semantics (where `base_reg` always differs
6389 // from `holder_reg`), we also need an explicit null check when implicit null checks are
6390 // allowed, as we do not emit one before going to the thunk.
Vladimir Marko7a695052018-04-12 10:26:50 +01006391 vixl::aarch64::Label throw_npe_label;
6392 vixl::aarch64::Label* throw_npe = nullptr;
Roland Levillain988c3912019-09-25 19:33:35 +01006393 if (GetCompilerOptions().GetImplicitNullChecks() &&
6394 (holder_reg.Is(base_reg) || (kind == BakerReadBarrierKind::kAcquire))) {
Vladimir Marko7a695052018-04-12 10:26:50 +01006395 throw_npe = &throw_npe_label;
6396 __ Cbz(holder_reg.W(), throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006397 }
Vladimir Marko7a695052018-04-12 10:26:50 +01006398 // Check if the holder is gray and, if not, add fake dependency to the base register
6399 // and return to the LDR instruction to load the reference. Otherwise, use introspection
6400 // to load the reference and call the entrypoint that performs further checks on the
6401 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006402 vixl::aarch64::Label slow_path;
6403 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
Vladimir Marko7a695052018-04-12 10:26:50 +01006404 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006405 __ Bind(&slow_path);
Vladimir Marko0ecac682018-08-07 10:40:38 +01006406 if (kind == BakerReadBarrierKind::kField) {
6407 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6408 __ Ldr(ip0.W(), ldr_address); // Load the LDR (immediate) unsigned offset.
6409 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6410 __ Ubfx(ip0.W(), ip0.W(), 10, 12); // Extract the offset.
6411 __ Ldr(ip0.W(), MemOperand(base_reg, ip0, LSL, 2)); // Load the reference.
6412 } else {
6413 DCHECK(kind == BakerReadBarrierKind::kAcquire);
6414 DCHECK(!base_reg.Is(holder_reg));
6415 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6416 __ Ldar(ip0.W(), MemOperand(base_reg));
6417 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006418 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
6419 __ Br(ip1); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006420 break;
6421 }
6422 case BakerReadBarrierKind::kArray: {
6423 auto base_reg =
6424 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6425 CheckValidReg(base_reg.GetCode());
6426 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6427 BakerReadBarrierSecondRegField::Decode(encoded_data));
6428 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6429 temps.Exclude(ip0, ip1);
6430 vixl::aarch64::Label slow_path;
6431 int32_t data_offset =
6432 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
6433 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
6434 DCHECK_LT(lock_word.GetOffset(), 0);
6435 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path);
6436 __ Bind(&slow_path);
6437 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
6438 __ Ldr(ip0.W(), ldr_address); // Load the LDR (register) unsigned offset.
6439 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6440 __ Ubfx(ip0, ip0, 16, 6); // Extract the index register, plus 32 (bit 21 is set).
6441 __ Bfi(ip1, ip0, 3, 6); // Insert ip0 to the entrypoint address to create
6442 // a switch case target based on the index register.
6443 __ Mov(ip0, base_reg); // Move the base register to ip0.
6444 __ Br(ip1); // Jump to the entrypoint's array switch case.
6445 break;
6446 }
6447 case BakerReadBarrierKind::kGcRoot: {
6448 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
6449 // and it does not have a forwarding address), call the correct introspection entrypoint;
6450 // otherwise return the reference (or the extracted forwarding address).
6451 // There is no gray bit check for GC roots.
6452 auto root_reg =
6453 Register::GetWRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6454 CheckValidReg(root_reg.GetCode());
6455 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6456 BakerReadBarrierSecondRegField::Decode(encoded_data));
6457 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6458 temps.Exclude(ip0, ip1);
6459 vixl::aarch64::Label return_label, not_marked, forwarding_address;
6460 __ Cbz(root_reg, &return_label);
6461 MemOperand lock_word(root_reg.X(), mirror::Object::MonitorOffset().Int32Value());
6462 __ Ldr(ip0.W(), lock_word);
6463 __ Tbz(ip0.W(), LockWord::kMarkBitStateShift, &not_marked);
6464 __ Bind(&return_label);
6465 __ Br(lr);
6466 __ Bind(&not_marked);
6467 __ Tst(ip0.W(), Operand(ip0.W(), LSL, 1));
6468 __ B(&forwarding_address, mi);
6469 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6470 // Adjust the art_quick_read_barrier_mark_introspection address in IP1 to
6471 // art_quick_read_barrier_mark_introspection_gc_roots.
6472 __ Add(ip1, ip1, Operand(BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRYPOINT_OFFSET));
6473 __ Mov(ip0.W(), root_reg);
6474 __ Br(ip1);
6475 __ Bind(&forwarding_address);
6476 __ Lsl(root_reg, ip0.W(), LockWord::kForwardingAddressShift);
6477 __ Br(lr);
6478 break;
6479 }
6480 default:
6481 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
6482 UNREACHABLE();
6483 }
6484
Vladimir Marko966b46f2018-08-03 10:20:19 +00006485 // For JIT, the slow path is considered part of the compiled method,
6486 // so JIT should pass null as `debug_name`. Tests may not have a runtime.
6487 DCHECK(Runtime::Current() == nullptr ||
6488 !Runtime::Current()->UseJitCompilation() ||
6489 debug_name == nullptr);
6490 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006491 std::ostringstream oss;
6492 oss << "BakerReadBarrierThunk";
6493 switch (kind) {
6494 case BakerReadBarrierKind::kField:
6495 oss << "Field_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
6496 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
6497 break;
Vladimir Marko0ecac682018-08-07 10:40:38 +01006498 case BakerReadBarrierKind::kAcquire:
6499 oss << "Acquire_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
6500 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
6501 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +00006502 case BakerReadBarrierKind::kArray:
6503 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
6504 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6505 BakerReadBarrierSecondRegField::Decode(encoded_data));
6506 break;
6507 case BakerReadBarrierKind::kGcRoot:
6508 oss << "GcRoot_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
6509 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6510 BakerReadBarrierSecondRegField::Decode(encoded_data));
6511 break;
6512 }
6513 *debug_name = oss.str();
6514 }
6515}
6516
6517#undef __
6518
Alexandre Rames5319def2014-10-23 10:03:10 +01006519} // namespace arm64
6520} // namespace art