blob: 651a3f7df62a1a8474187af64a0e12c9432b740f [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"
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method.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
Alexandre Rames5319def2014-10-23 10:03:10 +010053namespace art {
54
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
Andreas Gampe3db70682018-12-26 15:12:03 -0800227 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100228 for (uint32_t i : LowToHighBits(fp_spills)) {
229 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
230 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
231 saved_fpu_stack_offsets_[i] = stack_offset;
232 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800233 }
234
Vladimir Marko804b03f2016-09-14 16:26:36 +0100235 SaveRestoreLiveRegistersHelper(codegen,
236 locations,
Andreas Gampe3db70682018-12-26 15:12:03 -0800237 codegen->GetFirstRegisterSlotInSlowPath(), /* is_save= */ true);
Zheng Xuda403092015-04-24 17:35:39 +0800238}
239
240void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100241 SaveRestoreLiveRegistersHelper(codegen,
242 locations,
Andreas Gampe3db70682018-12-26 15:12:03 -0800243 codegen->GetFirstRegisterSlotInSlowPath(), /* is_save= */ false);
Zheng Xuda403092015-04-24 17:35:39 +0800244}
245
Alexandre Rames5319def2014-10-23 10:03:10 +0100246class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
247 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000248 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100249
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100250 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100251 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000252 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100253
Alexandre Rames5319def2014-10-23 10:03:10 +0100254 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000255 if (instruction_->CanThrowIntoCatchBlock()) {
256 // Live registers will be restored in the catch block if caught.
257 SaveLiveRegisters(codegen, instruction_->GetLocations());
258 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000259 // We're moving two locations to locations that could overlap, so we need a parallel
260 // move resolver.
261 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100262 codegen->EmitParallelMoves(locations->InAt(0),
263 LocationFrom(calling_convention.GetRegisterAt(0)),
264 DataType::Type::kInt32,
265 locations->InAt(1),
266 LocationFrom(calling_convention.GetRegisterAt(1)),
267 DataType::Type::kInt32);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000268 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
269 ? kQuickThrowStringBounds
270 : kQuickThrowArrayBounds;
271 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100272 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800273 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100274 }
275
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100276 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100277
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100278 const char* GetDescription() const override { return "BoundsCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100279
Alexandre Rames5319def2014-10-23 10:03:10 +0100280 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100281 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
282};
283
Alexandre Rames67555f72014-11-18 10:55:16 +0000284class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
285 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000286 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000287
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100288 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000289 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
290 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000291 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800292 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000293 }
294
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100295 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100296
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100297 const char* GetDescription() const override { return "DivZeroCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100298
Alexandre Rames67555f72014-11-18 10:55:16 +0000299 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000300 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
301};
302
303class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
304 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100305 LoadClassSlowPathARM64(HLoadClass* cls, HInstruction* at)
306 : SlowPathCodeARM64(at), cls_(cls) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000307 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100308 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Alexandre Rames67555f72014-11-18 10:55:16 +0000309 }
310
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100311 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000312 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000313 Location out = locations->Out();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100314 const uint32_t dex_pc = instruction_->GetDexPc();
315 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
316 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
Alexandre Rames67555f72014-11-18 10:55:16 +0000317
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100318 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames67555f72014-11-18 10:55:16 +0000319 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000320 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000321
Vladimir Markof3c52b42017-11-17 17:32:12 +0000322 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100323 if (must_resolve_type) {
324 DCHECK(IsSameDexFile(cls_->GetDexFile(), arm64_codegen->GetGraph()->GetDexFile()));
325 dex::TypeIndex type_index = cls_->GetTypeIndex();
326 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Vladimir Marko9d479252018-07-24 11:35:20 +0100327 arm64_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
328 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100329 // If we also must_do_clinit, the resolved type is now in the correct register.
330 } else {
331 DCHECK(must_do_clinit);
332 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
333 arm64_codegen->MoveLocation(LocationFrom(calling_convention.GetRegisterAt(0)),
334 source,
335 cls_->GetType());
336 }
337 if (must_do_clinit) {
338 arm64_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
339 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800340 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000341
342 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000343 if (out.IsValid()) {
344 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100345 DataType::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000346 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000347 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000348 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000349 __ B(GetExitLabel());
350 }
351
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100352 const char* GetDescription() const override { return "LoadClassSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100353
Alexandre Rames67555f72014-11-18 10:55:16 +0000354 private:
355 // The class this slow path will load.
356 HLoadClass* const cls_;
357
Alexandre Rames67555f72014-11-18 10:55:16 +0000358 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
359};
360
Vladimir Markoaad75c62016-10-03 08:46:48 +0000361class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
362 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000363 explicit LoadStringSlowPathARM64(HLoadString* instruction)
364 : SlowPathCodeARM64(instruction) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000365
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100366 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000367 LocationSummary* locations = instruction_->GetLocations();
368 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
369 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
370
371 __ Bind(GetEntryLabel());
372 SaveLiveRegisters(codegen, locations);
373
Vladimir Markof3c52b42017-11-17 17:32:12 +0000374 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000375 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
376 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000377 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
378 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100379 DataType::Type type = instruction_->GetType();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000380 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
381
382 RestoreLiveRegisters(codegen, locations);
383
Vladimir Markoaad75c62016-10-03 08:46:48 +0000384 __ B(GetExitLabel());
385 }
386
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100387 const char* GetDescription() const override { return "LoadStringSlowPathARM64"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000388
389 private:
390 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
391};
392
Alexandre Rames5319def2014-10-23 10:03:10 +0100393class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
394 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000395 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100396
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100397 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000398 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100399 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000400 if (instruction_->CanThrowIntoCatchBlock()) {
401 // Live registers will be restored in the catch block if caught.
402 SaveLiveRegisters(codegen, instruction_->GetLocations());
403 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000404 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
405 instruction_,
406 instruction_->GetDexPc(),
407 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800408 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100409 }
410
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100411 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100412
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100413 const char* GetDescription() const override { return "NullCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100414
Alexandre Rames5319def2014-10-23 10:03:10 +0100415 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100416 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
417};
418
419class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
420 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100421 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000422 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100423
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100424 void EmitNativeCode(CodeGenerator* codegen) override {
Artem Serov7957d952017-04-04 15:44:09 +0100425 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000426 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100427 __ Bind(GetEntryLabel());
Artem Serov7957d952017-04-04 15:44:09 +0100428 SaveLiveRegisters(codegen, locations); // Only saves live 128-bit regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000429 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800430 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov7957d952017-04-04 15:44:09 +0100431 RestoreLiveRegisters(codegen, locations); // Only restores live 128-bit regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000432 if (successor_ == nullptr) {
433 __ B(GetReturnLabel());
434 } else {
435 __ B(arm64_codegen->GetLabelOf(successor_));
436 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100437 }
438
Scott Wakeling97c72b72016-06-24 16:19:36 +0100439 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100440 DCHECK(successor_ == nullptr);
441 return &return_label_;
442 }
443
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100444 HBasicBlock* GetSuccessor() const {
445 return successor_;
446 }
447
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100448 const char* GetDescription() const override { return "SuspendCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100449
Alexandre Rames5319def2014-10-23 10:03:10 +0100450 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100451 // If not null, the block to branch to after the suspend check.
452 HBasicBlock* const successor_;
453
454 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100455 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100456
457 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
458};
459
Alexandre Rames67555f72014-11-18 10:55:16 +0000460class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
461 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000462 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000463 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000464
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100465 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000466 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800467
Alexandre Rames3e69f162014-12-10 10:36:50 +0000468 DCHECK(instruction_->IsCheckCast()
469 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
470 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100471 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000472
Alexandre Rames67555f72014-11-18 10:55:16 +0000473 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000474
Vladimir Marko87584542017-12-12 17:47:52 +0000475 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000476 SaveLiveRegisters(codegen, locations);
477 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000478
479 // We're moving two locations to locations that could overlap, so we need a parallel
480 // move resolver.
481 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800482 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800483 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100484 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800485 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800486 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100487 DataType::Type::kReference);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000488 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000489 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800490 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100491 DataType::Type ret_type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000492 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
493 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
494 } else {
495 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800496 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
497 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000498 }
499
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000500 if (!is_fatal_) {
501 RestoreLiveRegisters(codegen, locations);
502 __ B(GetExitLabel());
503 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000504 }
505
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100506 const char* GetDescription() const override { return "TypeCheckSlowPathARM64"; }
507 bool IsFatal() const override { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100508
Alexandre Rames67555f72014-11-18 10:55:16 +0000509 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000510 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000511
Alexandre Rames67555f72014-11-18 10:55:16 +0000512 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
513};
514
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700515class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
516 public:
Aart Bik42249c32016-01-07 15:33:50 -0800517 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000518 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700519
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100520 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bik42249c32016-01-07 15:33:50 -0800521 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700522 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100523 LocationSummary* locations = instruction_->GetLocations();
524 SaveLiveRegisters(codegen, locations);
525 InvokeRuntimeCallingConvention calling_convention;
526 __ Mov(calling_convention.GetRegisterAt(0),
527 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000528 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100529 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700530 }
531
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100532 const char* GetDescription() const override { return "DeoptimizationSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100533
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700534 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700535 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
536};
537
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100538class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
539 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000540 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100541
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100542 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100543 LocationSummary* locations = instruction_->GetLocations();
544 __ Bind(GetEntryLabel());
545 SaveLiveRegisters(codegen, locations);
546
547 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100548 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100549 parallel_move.AddMove(
550 locations->InAt(0),
551 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100552 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100553 nullptr);
554 parallel_move.AddMove(
555 locations->InAt(1),
556 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100557 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100558 nullptr);
559 parallel_move.AddMove(
560 locations->InAt(2),
561 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100562 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100563 nullptr);
564 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
565
566 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000567 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100568 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
569 RestoreLiveRegisters(codegen, locations);
570 __ B(GetExitLabel());
571 }
572
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100573 const char* GetDescription() const override { return "ArraySetSlowPathARM64"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100574
575 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100576 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
577};
578
Zheng Xu3927c8b2015-11-18 17:46:25 +0800579void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
580 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000581 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800582
583 // We are about to use the assembler to place literals directly. Make sure we have enough
584 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000585 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
586 num_entries * sizeof(int32_t),
587 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800588
589 __ Bind(&table_start_);
590 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
591 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100592 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800593 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100594 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800595 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
596 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
597 Literal<int32_t> literal(jump_offset);
598 __ place(&literal);
599 }
600}
601
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000602// Slow path generating a read barrier for a heap reference.
603class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
604 public:
605 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
606 Location out,
607 Location ref,
608 Location obj,
609 uint32_t offset,
610 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000611 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000612 out_(out),
613 ref_(ref),
614 obj_(obj),
615 offset_(offset),
616 index_(index) {
617 DCHECK(kEmitCompilerReadBarrier);
618 // If `obj` is equal to `out` or `ref`, it means the initial object
619 // has been overwritten by (or after) the heap object reference load
620 // to be instrumented, e.g.:
621 //
622 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000623 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000624 //
625 // In that case, we have lost the information about the original
626 // object, and the emitted read barrier cannot work properly.
627 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
628 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
629 }
630
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100631 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000632 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
633 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100634 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000635 DCHECK(locations->CanCall());
636 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100637 DCHECK(instruction_->IsInstanceFieldGet() ||
638 instruction_->IsStaticFieldGet() ||
639 instruction_->IsArrayGet() ||
640 instruction_->IsInstanceOf() ||
641 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700642 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000643 << "Unexpected instruction in read barrier for heap reference slow path: "
644 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000645 // The read barrier instrumentation of object ArrayGet
646 // instructions does not support the HIntermediateAddress
647 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000648 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100649 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000650
651 __ Bind(GetEntryLabel());
652
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000653 SaveLiveRegisters(codegen, locations);
654
655 // We may have to change the index's value, but as `index_` is a
656 // constant member (like other "inputs" of this slow path),
657 // introduce a copy of it, `index`.
658 Location index = index_;
659 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100660 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000661 if (instruction_->IsArrayGet()) {
662 // Compute the actual memory offset and store it in `index`.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100663 Register index_reg = RegisterFrom(index_, DataType::Type::kInt32);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000664 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
665 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
666 // We are about to change the value of `index_reg` (see the
667 // calls to vixl::MacroAssembler::Lsl and
668 // vixl::MacroAssembler::Mov below), but it has
669 // not been saved by the previous call to
670 // art::SlowPathCode::SaveLiveRegisters, as it is a
671 // callee-save register --
672 // art::SlowPathCode::SaveLiveRegisters does not consider
673 // callee-save registers, as it has been designed with the
674 // assumption that callee-save registers are supposed to be
675 // handled by the called function. So, as a callee-save
676 // register, `index_reg` _would_ eventually be saved onto
677 // the stack, but it would be too late: we would have
678 // changed its value earlier. Therefore, we manually save
679 // it here into another freely available register,
680 // `free_reg`, chosen of course among the caller-save
681 // registers (as a callee-save `free_reg` register would
682 // exhibit the same problem).
683 //
684 // Note we could have requested a temporary register from
685 // the register allocator instead; but we prefer not to, as
686 // this is a slow path, and we know we can find a
687 // caller-save register that is available.
688 Register free_reg = FindAvailableCallerSaveRegister(codegen);
689 __ Mov(free_reg.W(), index_reg);
690 index_reg = free_reg;
691 index = LocationFrom(index_reg);
692 } else {
693 // The initial register stored in `index_` has already been
694 // saved in the call to art::SlowPathCode::SaveLiveRegisters
695 // (as it is not a callee-save register), so we can freely
696 // use it.
697 }
698 // Shifting the index value contained in `index_reg` by the scale
699 // factor (2) cannot overflow in practice, as the runtime is
700 // unable to allocate object arrays with a size larger than
701 // 2^26 - 1 (that is, 2^28 - 4 bytes).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100702 __ Lsl(index_reg, index_reg, DataType::SizeShift(type));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000703 static_assert(
704 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
705 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
706 __ Add(index_reg, index_reg, Operand(offset_));
707 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100708 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
709 // intrinsics, `index_` is not shifted by a scale factor of 2
710 // (as in the case of ArrayGet), as it is actually an offset
711 // to an object field within an object.
712 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000713 DCHECK(instruction_->GetLocations()->Intrinsified());
714 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
715 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
716 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100717 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100718 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000719 }
720 }
721
722 // We're moving two or three locations to locations that could
723 // overlap, so we need a parallel move resolver.
724 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100725 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000726 parallel_move.AddMove(ref_,
727 LocationFrom(calling_convention.GetRegisterAt(0)),
728 type,
729 nullptr);
730 parallel_move.AddMove(obj_,
731 LocationFrom(calling_convention.GetRegisterAt(1)),
732 type,
733 nullptr);
734 if (index.IsValid()) {
735 parallel_move.AddMove(index,
736 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100737 DataType::Type::kInt32,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000738 nullptr);
739 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
740 } else {
741 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
742 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
743 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000744 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000745 instruction_,
746 instruction_->GetDexPc(),
747 this);
748 CheckEntrypointTypes<
749 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
750 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
751
752 RestoreLiveRegisters(codegen, locations);
753
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000754 __ B(GetExitLabel());
755 }
756
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100757 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000758
759 private:
760 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100761 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
762 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000763 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
764 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
765 return Register(VIXLRegCodeFromART(i), kXRegSize);
766 }
767 }
768 // We shall never fail to find a free caller-save register, as
769 // there are more than two core caller-save registers on ARM64
770 // (meaning it is possible to find one which is different from
771 // `ref` and `obj`).
772 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
773 LOG(FATAL) << "Could not find a free register";
774 UNREACHABLE();
775 }
776
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000777 const Location out_;
778 const Location ref_;
779 const Location obj_;
780 const uint32_t offset_;
781 // An additional location containing an index to an array.
782 // Only used for HArrayGet and the UnsafeGetObject &
783 // UnsafeGetObjectVolatile intrinsics.
784 const Location index_;
785
786 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
787};
788
789// Slow path generating a read barrier for a GC root.
790class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
791 public:
792 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000793 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000794 DCHECK(kEmitCompilerReadBarrier);
795 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000796
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100797 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000798 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100799 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000800 DCHECK(locations->CanCall());
801 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000802 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
803 << "Unexpected instruction in read barrier for GC root slow path: "
804 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000805
806 __ Bind(GetEntryLabel());
807 SaveLiveRegisters(codegen, locations);
808
809 InvokeRuntimeCallingConvention calling_convention;
810 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
811 // The argument of the ReadBarrierForRootSlow is not a managed
812 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
813 // thus we need a 64-bit move here, and we cannot use
814 //
815 // arm64_codegen->MoveLocation(
816 // LocationFrom(calling_convention.GetRegisterAt(0)),
817 // root_,
818 // type);
819 //
820 // which would emit a 32-bit move, as `type` is a (32-bit wide)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100821 // reference type (`DataType::Type::kReference`).
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000822 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000823 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000824 instruction_,
825 instruction_->GetDexPc(),
826 this);
827 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
828 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
829
830 RestoreLiveRegisters(codegen, locations);
831 __ B(GetExitLabel());
832 }
833
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100834 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000835
836 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000837 const Location out_;
838 const Location root_;
839
840 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
841};
842
Alexandre Rames5319def2014-10-23 10:03:10 +0100843#undef __
844
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100845Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100846 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100847 if (type == DataType::Type::kVoid) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100848 LOG(FATAL) << "Unreachable type " << type;
849 }
850
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100851 if (DataType::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100852 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
853 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100854 } else if (!DataType::IsFloatingPointType(type) &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000855 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000856 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
857 } else {
858 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100859 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
860 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100861 }
862
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000863 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100864 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100865 return next_location;
866}
867
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100868Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100869 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100870}
871
Serban Constantinescu579885a2015-02-22 20:51:33 +0000872CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100873 const CompilerOptions& compiler_options,
874 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100875 : CodeGenerator(graph,
876 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000877 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000878 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100879 callee_saved_core_registers.GetList(),
880 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100881 compiler_options,
882 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100883 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
884 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100885 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000886 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100887 move_resolver_(graph->GetAllocator(), this),
Artem Serovaa6f4832018-11-21 18:57:54 +0000888 assembler_(graph->GetAllocator(),
889 compiler_options.GetInstructionSetFeatures()->AsArm64InstructionSetFeatures()),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000890 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100891 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000892 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100893 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000894 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100895 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6fd16062018-06-26 11:02:04 +0100896 boot_image_intrinsic_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100897 call_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100898 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100899 uint32_literals_(std::less<uint32_t>(),
900 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
901 uint64_literals_(std::less<uint64_t>(),
902 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000903 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100904 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000905 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +0000906 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
907 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
908 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000909 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000910 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000911}
Alexandre Rames5319def2014-10-23 10:03:10 +0100912
Alexandre Rames67555f72014-11-18 10:55:16 +0000913#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100914
Zheng Xu3927c8b2015-11-18 17:46:25 +0800915void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100916 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800917 jump_table->EmitTable(this);
918 }
919}
920
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000921void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800922 EmitJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +0000923
924 // Emit JIT baker read barrier slow paths.
925 DCHECK(Runtime::Current()->UseJitCompilation() || jit_baker_read_barrier_slow_paths_.empty());
926 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
927 uint32_t encoded_data = entry.first;
928 vixl::aarch64::Label* slow_path_entry = &entry.second.label;
929 __ Bind(slow_path_entry);
Andreas Gampe3db70682018-12-26 15:12:03 -0800930 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name= */ nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +0000931 }
932
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000933 // Ensure we emit the literal pool.
934 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000935
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000936 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +0000937
938 // Verify Baker read barrier linker patches.
939 if (kIsDebugBuild) {
940 ArrayRef<const uint8_t> code = allocator->GetMemory();
941 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
942 DCHECK(info.label.IsBound());
943 uint32_t literal_offset = info.label.GetLocation();
944 DCHECK_ALIGNED(literal_offset, 4u);
945
946 auto GetInsn = [&code](uint32_t offset) {
947 DCHECK_ALIGNED(offset, 4u);
948 return
949 (static_cast<uint32_t>(code[offset + 0]) << 0) +
950 (static_cast<uint32_t>(code[offset + 1]) << 8) +
951 (static_cast<uint32_t>(code[offset + 2]) << 16)+
952 (static_cast<uint32_t>(code[offset + 3]) << 24);
953 };
954
955 const uint32_t encoded_data = info.custom_data;
956 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
957 // Check that the next instruction matches the expected LDR.
958 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +0100959 case BakerReadBarrierKind::kField:
960 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +0000961 DCHECK_GE(code.size() - literal_offset, 8u);
962 uint32_t next_insn = GetInsn(literal_offset + 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +0000963 CheckValidReg(next_insn & 0x1fu); // Check destination register.
964 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko0ecac682018-08-07 10:40:38 +0100965 if (kind == BakerReadBarrierKind::kField) {
966 // LDR (immediate) with correct base_reg.
967 CHECK_EQ(next_insn & 0xffc003e0u, 0xb9400000u | (base_reg << 5));
968 } else {
969 DCHECK(kind == BakerReadBarrierKind::kAcquire);
970 // LDAR with correct base_reg.
971 CHECK_EQ(next_insn & 0xffffffe0u, 0x88dffc00u | (base_reg << 5));
972 }
Vladimir Markoca1e0382018-04-11 09:58:41 +0000973 break;
974 }
975 case BakerReadBarrierKind::kArray: {
976 DCHECK_GE(code.size() - literal_offset, 8u);
977 uint32_t next_insn = GetInsn(literal_offset + 4u);
978 // LDR (register) with the correct base_reg, size=10 (32-bit), option=011 (extend = LSL),
979 // and S=1 (shift amount = 2 for 32-bit version), i.e. LDR Wt, [Xn, Xm, LSL #2].
980 CheckValidReg(next_insn & 0x1fu); // Check destination register.
981 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
982 CHECK_EQ(next_insn & 0xffe0ffe0u, 0xb8607800u | (base_reg << 5));
983 CheckValidReg((next_insn >> 16) & 0x1f); // Check index register
984 break;
985 }
986 case BakerReadBarrierKind::kGcRoot: {
987 DCHECK_GE(literal_offset, 4u);
988 uint32_t prev_insn = GetInsn(literal_offset - 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +0000989 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko94796f82018-08-08 15:15:33 +0100990 // Usually LDR (immediate) with correct root_reg but
991 // we may have a "MOV marked, old_value" for UnsafeCASObject.
992 if ((prev_insn & 0xffe0ffff) != (0x2a0003e0 | root_reg)) { // MOV?
993 CHECK_EQ(prev_insn & 0xffc0001fu, 0xb9400000u | root_reg); // LDR?
994 }
Vladimir Markoca1e0382018-04-11 09:58:41 +0000995 break;
996 }
997 default:
998 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
999 UNREACHABLE();
1000 }
1001 }
1002 }
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001003}
1004
Zheng Xuad4450e2015-04-17 18:48:56 +08001005void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1006 // Note: There are 6 kinds of moves:
1007 // 1. constant -> GPR/FPR (non-cycle)
1008 // 2. constant -> stack (non-cycle)
1009 // 3. GPR/FPR -> GPR/FPR
1010 // 4. GPR/FPR -> stack
1011 // 5. stack -> GPR/FPR
1012 // 6. stack -> stack (non-cycle)
1013 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1014 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1015 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1016 // dependency.
1017 vixl_temps_.Open(GetVIXLAssembler());
1018}
1019
1020void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1021 vixl_temps_.Close();
1022}
1023
1024Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001025 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1026 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1027 || kind == Location::kSIMDStackSlot);
1028 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1029 ? Location::kFpuRegister
1030 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001031 Location scratch = GetScratchLocation(kind);
1032 if (!scratch.Equals(Location::NoLocation())) {
1033 return scratch;
1034 }
1035 // Allocate from VIXL temp registers.
1036 if (kind == Location::kRegister) {
1037 scratch = LocationFrom(vixl_temps_.AcquireX());
1038 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001039 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001040 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1041 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1042 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001043 }
1044 AddScratchLocation(scratch);
1045 return scratch;
1046}
1047
1048void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1049 if (loc.IsRegister()) {
1050 vixl_temps_.Release(XRegisterFrom(loc));
1051 } else {
1052 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001053 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001054 }
1055 RemoveScratchLocation(loc);
1056}
1057
Alexandre Rames3e69f162014-12-10 10:36:50 +00001058void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001059 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001060 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001061}
1062
Alexandre Rames5319def2014-10-23 10:03:10 +01001063void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001064 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001065 __ Bind(&frame_entry_label_);
1066
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001067 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1068 UseScratchRegisterScope temps(masm);
1069 Register temp = temps.AcquireX();
1070 __ Ldrh(temp, MemOperand(kArtMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
1071 __ Add(temp, temp, 1);
1072 __ Strh(temp, MemOperand(kArtMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
1073 }
1074
Vladimir Marko33bff252017-11-01 14:35:42 +00001075 bool do_overflow_check =
1076 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001077 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001078 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001079 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001080 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001081 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001082 {
1083 // Ensure that between load and RecordPcInfo there are no pools emitted.
1084 ExactAssemblyScope eas(GetVIXLAssembler(),
1085 kInstructionSize,
1086 CodeBufferCheckScope::kExactSize);
1087 __ ldr(wzr, MemOperand(temp, 0));
1088 RecordPcInfo(nullptr, 0);
1089 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001090 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001091
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001092 if (!HasEmptyFrame()) {
1093 int frame_size = GetFrameSize();
1094 // Stack layout:
1095 // sp[frame_size - 8] : lr.
1096 // ... : other preserved core registers.
1097 // ... : other preserved fp registers.
1098 // ... : reserved frame space.
1099 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001100
1101 // Save the current method if we need it. Note that we do not
1102 // do this in HCurrentMethod, as the instruction might have been removed
1103 // in the SSA graph.
1104 if (RequiresCurrentMethod()) {
1105 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001106 } else {
1107 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001108 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001109 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001110 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1111 frame_size - GetCoreSpillSize());
1112 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1113 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001114
1115 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1116 // Initialize should_deoptimize flag to 0.
1117 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1118 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1119 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001120 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01001121
Andreas Gampe3db70682018-12-26 15:12:03 -08001122 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001123}
1124
1125void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001126 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001127 if (!HasEmptyFrame()) {
1128 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001129 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1130 frame_size - FrameEntrySpillSize());
1131 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1132 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001133 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001134 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001135 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001136 __ Ret();
1137 GetAssembler()->cfi().RestoreState();
1138 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001139}
1140
Scott Wakeling97c72b72016-06-24 16:19:36 +01001141CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001142 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001143 return CPURegList(CPURegister::kRegister, kXRegSize,
1144 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001145}
1146
Scott Wakeling97c72b72016-06-24 16:19:36 +01001147CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001148 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1149 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001150 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1151 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001152}
1153
Alexandre Rames5319def2014-10-23 10:03:10 +01001154void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1155 __ Bind(GetLabelOf(block));
1156}
1157
Calin Juravle175dc732015-08-25 15:42:32 +01001158void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1159 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001160 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001161}
1162
Calin Juravlee460d1d2015-09-29 04:52:17 +01001163void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1164 if (location.IsRegister()) {
1165 locations->AddTemp(location);
1166 } else {
1167 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1168 }
1169}
1170
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001171void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001172 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001173 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001174 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001175 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001176 if (value_can_be_null) {
1177 __ Cbz(value, &done);
1178 }
Roland Levillainc73f0522018-08-14 15:16:50 +01001179 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07001180 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01001181 // Calculate the offset (in the card table) of the card corresponding to
1182 // `object`.
Alexandre Rames5319def2014-10-23 10:03:10 +01001183 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Roland Levillainc73f0522018-08-14 15:16:50 +01001184 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
1185 // `object`'s card.
1186 //
1187 // Register `card` contains the address of the card table. Note that the card
1188 // table's base is biased during its creation so that it always starts at an
1189 // address whose least-significant byte is equal to `kCardDirty` (see
1190 // art::gc::accounting::CardTable::Create). Therefore the STRB instruction
1191 // below writes the `kCardDirty` (byte) value into the `object`'s card
1192 // (located at `card + object >> kCardShift`).
1193 //
1194 // This dual use of the value in register `card` (1. to calculate the location
1195 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
1196 // (no need to explicitly load `kCardDirty` as an immediate value).
Serban Constantinescu02164b32014-11-13 14:05:07 +00001197 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001198 if (value_can_be_null) {
1199 __ Bind(&done);
1200 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001201}
1202
David Brazdil58282f42016-01-14 12:45:10 +00001203void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001204 // Blocked core registers:
1205 // lr : Runtime reserved.
1206 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001207 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001208 // ip1 : VIXL core temp.
1209 // ip0 : VIXL core temp.
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001210 // x18 : Platform register.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001211 //
1212 // Blocked fp registers:
1213 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001214 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1215 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001216 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001217 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001218 }
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001219 blocked_core_registers_[X18] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001220
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001221 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001222 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001223 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001224 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001225
David Brazdil58282f42016-01-14 12:45:10 +00001226 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001227 // Stubs do not save callee-save floating point registers. If the graph
1228 // is debuggable, we need to deal with these registers differently. For
1229 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001230 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1231 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001232 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001233 }
1234 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001235}
1236
Alexandre Rames3e69f162014-12-10 10:36:50 +00001237size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1238 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1239 __ Str(reg, MemOperand(sp, stack_index));
1240 return kArm64WordSize;
1241}
1242
1243size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1244 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1245 __ Ldr(reg, MemOperand(sp, stack_index));
1246 return kArm64WordSize;
1247}
1248
1249size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1250 FPRegister reg = FPRegister(reg_id, kDRegSize);
1251 __ Str(reg, MemOperand(sp, stack_index));
1252 return kArm64WordSize;
1253}
1254
1255size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1256 FPRegister reg = FPRegister(reg_id, kDRegSize);
1257 __ Ldr(reg, MemOperand(sp, stack_index));
1258 return kArm64WordSize;
1259}
1260
Alexandre Rames5319def2014-10-23 10:03:10 +01001261void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001262 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001263}
1264
1265void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001266 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001267}
1268
Vladimir Markoa0431112018-06-25 09:32:54 +01001269const Arm64InstructionSetFeatures& CodeGeneratorARM64::GetInstructionSetFeatures() const {
1270 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArm64InstructionSetFeatures();
1271}
1272
Alexandre Rames67555f72014-11-18 10:55:16 +00001273void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001274 if (constant->IsIntConstant()) {
1275 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1276 } else if (constant->IsLongConstant()) {
1277 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1278 } else if (constant->IsNullConstant()) {
1279 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001280 } else if (constant->IsFloatConstant()) {
1281 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1282 } else {
1283 DCHECK(constant->IsDoubleConstant());
1284 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1285 }
1286}
1287
Alexandre Rames3e69f162014-12-10 10:36:50 +00001288
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001289static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001290 DCHECK(constant.IsConstant());
1291 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001292 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001293 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001294 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1295 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1296 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1297 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001298}
1299
Roland Levillain952b2352017-05-03 19:49:14 +01001300// Allocate a scratch register from the VIXL pool, querying first
1301// the floating-point register pool, and then the core register
1302// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001303// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1304// using a different allocation strategy.
1305static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1306 vixl::aarch64::UseScratchRegisterScope* temps,
1307 int size_in_bits) {
1308 return masm->GetScratchFPRegisterList()->IsEmpty()
1309 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1310 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1311}
1312
Calin Juravlee460d1d2015-09-29 04:52:17 +01001313void CodeGeneratorARM64::MoveLocation(Location destination,
1314 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001315 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001316 if (source.Equals(destination)) {
1317 return;
1318 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001319
1320 // A valid move can always be inferred from the destination and source
1321 // locations. When moving from and to a register, the argument type can be
1322 // used to generate 32bit instead of 64bit moves. In debug mode we also
1323 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001324 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001325
1326 if (destination.IsRegister() || destination.IsFpuRegister()) {
1327 if (unspecified_type) {
1328 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1329 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001330 (src_cst != nullptr && (src_cst->IsIntConstant()
1331 || src_cst->IsFloatConstant()
1332 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001333 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001334 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001335 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001336 // If the source is a double stack slot or a 64bit constant, a 64bit
1337 // type is appropriate. Else the source is a register, and since the
1338 // type has not been specified, we chose a 64bit type to force a 64bit
1339 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001340 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001341 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001342 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001343 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1344 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001345 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001346 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1347 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1348 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001349 } else if (source.IsSIMDStackSlot()) {
1350 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001351 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001352 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001353 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001354 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001355 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001356 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001357 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001358 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001359 DataType::Type source_type = DataType::Is64BitType(dst_type)
1360 ? DataType::Type::kInt64
1361 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001362 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1363 }
1364 } else {
1365 DCHECK(source.IsFpuRegister());
1366 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001367 DataType::Type source_type = DataType::Is64BitType(dst_type)
1368 ? DataType::Type::kFloat64
1369 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001370 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1371 } else {
1372 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001373 if (GetGraph()->HasSIMD()) {
1374 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1375 } else {
1376 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1377 }
1378 }
1379 }
1380 } else if (destination.IsSIMDStackSlot()) {
1381 if (source.IsFpuRegister()) {
1382 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1383 } else {
1384 DCHECK(source.IsSIMDStackSlot());
1385 UseScratchRegisterScope temps(GetVIXLAssembler());
1386 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1387 Register temp = temps.AcquireX();
1388 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1389 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1390 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1391 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1392 } else {
1393 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1394 __ Ldr(temp, StackOperandFrom(source));
1395 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001396 }
1397 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001398 } else { // The destination is not a register. It must be a stack slot.
1399 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1400 if (source.IsRegister() || source.IsFpuRegister()) {
1401 if (unspecified_type) {
1402 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001403 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001404 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001405 dst_type =
1406 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001407 }
1408 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001409 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1410 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001411 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001412 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001413 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1414 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001415 UseScratchRegisterScope temps(GetVIXLAssembler());
1416 HConstant* src_cst = source.GetConstant();
1417 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001418 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001419 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1420 ? Register(xzr)
1421 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001422 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001423 if (src_cst->IsIntConstant()) {
1424 temp = temps.AcquireW();
1425 } else if (src_cst->IsLongConstant()) {
1426 temp = temps.AcquireX();
1427 } else if (src_cst->IsFloatConstant()) {
1428 temp = temps.AcquireS();
1429 } else {
1430 DCHECK(src_cst->IsDoubleConstant());
1431 temp = temps.AcquireD();
1432 }
1433 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001434 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001435 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001436 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001437 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001438 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001439 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001440 // Use any scratch register (a core or a floating-point one)
1441 // from VIXL scratch register pools as a temporary.
1442 //
1443 // We used to only use the FP scratch register pool, but in some
1444 // rare cases the only register from this pool (D31) would
1445 // already be used (e.g. within a ParallelMove instruction, when
1446 // a move is blocked by a another move requiring a scratch FP
1447 // register, which would reserve D31). To prevent this issue, we
1448 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001449 //
1450 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001451 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001452 // use AcquireFPOrCoreCPURegisterOfSize instead of
1453 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1454 // allocates core scratch registers first.
1455 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1456 GetVIXLAssembler(),
1457 &temps,
1458 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001459 __ Ldr(temp, StackOperandFrom(source));
1460 __ Str(temp, StackOperandFrom(destination));
1461 }
1462 }
1463}
1464
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001465void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001466 CPURegister dst,
1467 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001468 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001469 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001470 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001471 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001472 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001473 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001474 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001475 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001476 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001477 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001478 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001479 case DataType::Type::kInt16:
1480 __ Ldrsh(Register(dst), src);
1481 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001482 case DataType::Type::kInt32:
1483 case DataType::Type::kReference:
1484 case DataType::Type::kInt64:
1485 case DataType::Type::kFloat32:
1486 case DataType::Type::kFloat64:
1487 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001488 __ Ldr(dst, src);
1489 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001490 case DataType::Type::kUint32:
1491 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001492 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001493 LOG(FATAL) << "Unreachable type " << type;
1494 }
1495}
1496
Calin Juravle77520bc2015-01-12 18:45:46 +00001497void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001498 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001499 const MemOperand& src,
1500 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001501 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001502 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001503 Register temp_base = temps.AcquireX();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001504 DataType::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001505
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001506 DCHECK(!src.IsPreIndex());
1507 DCHECK(!src.IsPostIndex());
1508
1509 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001510 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001511 {
1512 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1513 MemOperand base = MemOperand(temp_base);
1514 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001515 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001516 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001517 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001518 {
1519 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1520 __ ldarb(Register(dst), base);
1521 if (needs_null_check) {
1522 MaybeRecordImplicitNullCheck(instruction);
1523 }
1524 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001525 if (type == DataType::Type::kInt8) {
1526 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001527 }
1528 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001529 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001530 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001531 {
1532 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1533 __ ldarh(Register(dst), base);
1534 if (needs_null_check) {
1535 MaybeRecordImplicitNullCheck(instruction);
1536 }
1537 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001538 if (type == DataType::Type::kInt16) {
1539 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
1540 }
Artem Serov914d7a82017-02-07 14:33:49 +00001541 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001542 case DataType::Type::kInt32:
1543 case DataType::Type::kReference:
1544 case DataType::Type::kInt64:
1545 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001546 {
1547 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1548 __ ldar(Register(dst), base);
1549 if (needs_null_check) {
1550 MaybeRecordImplicitNullCheck(instruction);
1551 }
1552 }
1553 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001554 case DataType::Type::kFloat32:
1555 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00001556 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001557 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001558
Artem Serov914d7a82017-02-07 14:33:49 +00001559 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1560 {
1561 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1562 __ ldar(temp, base);
1563 if (needs_null_check) {
1564 MaybeRecordImplicitNullCheck(instruction);
1565 }
1566 }
1567 __ Fmov(FPRegister(dst), temp);
1568 break;
Roland Levillain44015862016-01-22 11:47:17 +00001569 }
Aart Bik66c158e2018-01-31 12:55:04 -08001570 case DataType::Type::kUint32:
1571 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001572 case DataType::Type::kVoid:
Artem Serov914d7a82017-02-07 14:33:49 +00001573 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001574 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001575 }
1576}
1577
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001578void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001579 CPURegister src,
1580 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001581 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001582 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001583 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001584 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001585 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001586 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001587 case DataType::Type::kUint16:
1588 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001589 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001590 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001591 case DataType::Type::kInt32:
1592 case DataType::Type::kReference:
1593 case DataType::Type::kInt64:
1594 case DataType::Type::kFloat32:
1595 case DataType::Type::kFloat64:
1596 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001597 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001598 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001599 case DataType::Type::kUint32:
1600 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001601 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001602 LOG(FATAL) << "Unreachable type " << type;
1603 }
1604}
1605
Artem Serov914d7a82017-02-07 14:33:49 +00001606void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001607 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001608 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00001609 const MemOperand& dst,
1610 bool needs_null_check) {
1611 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001612 UseScratchRegisterScope temps(GetVIXLAssembler());
1613 Register temp_base = temps.AcquireX();
1614
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001615 DCHECK(!dst.IsPreIndex());
1616 DCHECK(!dst.IsPostIndex());
1617
1618 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001619 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001620 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001621 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00001622 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001623 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001624 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001625 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001626 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001627 {
1628 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1629 __ stlrb(Register(src), base);
1630 if (needs_null_check) {
1631 MaybeRecordImplicitNullCheck(instruction);
1632 }
1633 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001634 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001635 case DataType::Type::kUint16:
1636 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001637 {
1638 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1639 __ stlrh(Register(src), base);
1640 if (needs_null_check) {
1641 MaybeRecordImplicitNullCheck(instruction);
1642 }
1643 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001644 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001645 case DataType::Type::kInt32:
1646 case DataType::Type::kReference:
1647 case DataType::Type::kInt64:
1648 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001649 {
1650 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1651 __ stlr(Register(src), base);
1652 if (needs_null_check) {
1653 MaybeRecordImplicitNullCheck(instruction);
1654 }
1655 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001656 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001657 case DataType::Type::kFloat32:
1658 case DataType::Type::kFloat64: {
1659 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001660 Register temp_src;
1661 if (src.IsZero()) {
1662 // The zero register is used to avoid synthesizing zero constants.
1663 temp_src = Register(src);
1664 } else {
1665 DCHECK(src.IsFPRegister());
1666 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1667 __ Fmov(temp_src, FPRegister(src));
1668 }
Artem Serov914d7a82017-02-07 14:33:49 +00001669 {
1670 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1671 __ stlr(temp_src, base);
1672 if (needs_null_check) {
1673 MaybeRecordImplicitNullCheck(instruction);
1674 }
1675 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001676 break;
1677 }
Aart Bik66c158e2018-01-31 12:55:04 -08001678 case DataType::Type::kUint32:
1679 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001680 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001681 LOG(FATAL) << "Unreachable type " << type;
1682 }
1683}
1684
Calin Juravle175dc732015-08-25 15:42:32 +01001685void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1686 HInstruction* instruction,
1687 uint32_t dex_pc,
1688 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001689 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00001690
Vladimir Markof6675082019-05-17 12:05:28 +01001691 ThreadOffset64 entrypoint_offset = GetThreadOffset<kArm64PointerSize>(entrypoint);
1692 // Reduce code size for AOT by using shared trampolines for slow path runtime calls across the
1693 // entire oat file. This adds an extra branch and we do not want to slow down the main path.
1694 // For JIT, thunk sharing is per-method, so the gains would be smaller or even negative.
1695 if (slow_path == nullptr || Runtime::Current()->UseJitCompilation()) {
1696 __ Ldr(lr, MemOperand(tr, entrypoint_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00001697 // Ensure the pc position is recorded immediately after the `blr` instruction.
1698 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1699 __ blr(lr);
1700 if (EntrypointRequiresStackMap(entrypoint)) {
1701 RecordPcInfo(instruction, dex_pc, slow_path);
1702 }
Vladimir Markof6675082019-05-17 12:05:28 +01001703 } else {
1704 // Ensure the pc position is recorded immediately after the `bl` instruction.
1705 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1706 EmitEntrypointThunkCall(entrypoint_offset);
1707 if (EntrypointRequiresStackMap(entrypoint)) {
1708 RecordPcInfo(instruction, dex_pc, slow_path);
1709 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001710 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001711}
1712
Roland Levillaindec8f632016-07-22 17:10:06 +01001713void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1714 HInstruction* instruction,
1715 SlowPathCode* slow_path) {
1716 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01001717 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1718 __ Blr(lr);
1719}
1720
Alexandre Rames67555f72014-11-18 10:55:16 +00001721void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001722 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001723 UseScratchRegisterScope temps(GetVIXLAssembler());
1724 Register temp = temps.AcquireW();
Vladimir Markodc682aa2018-01-04 18:42:57 +00001725 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
1726 const size_t status_byte_offset =
1727 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1728 constexpr uint32_t shifted_initialized_value =
1729 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001730
Serban Constantinescu02164b32014-11-13 14:05:07 +00001731 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001732 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Vladimir Markodc682aa2018-01-04 18:42:57 +00001733 __ Add(temp, class_reg, status_byte_offset);
Igor Murashkin86083f72017-10-27 10:59:04 -07001734 __ Ldarb(temp, HeapOperand(temp));
Vladimir Markodc682aa2018-01-04 18:42:57 +00001735 __ Cmp(temp, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00001736 __ B(lo, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001737 __ Bind(slow_path->GetExitLabel());
1738}
Alexandre Rames5319def2014-10-23 10:03:10 +01001739
Vladimir Marko175e7862018-03-27 09:03:13 +00001740void InstructionCodeGeneratorARM64::GenerateBitstringTypeCheckCompare(
1741 HTypeCheckInstruction* check, vixl::aarch64::Register temp) {
1742 uint32_t path_to_root = check->GetBitstringPathToRoot();
1743 uint32_t mask = check->GetBitstringMask();
1744 DCHECK(IsPowerOfTwo(mask + 1));
1745 size_t mask_bits = WhichPowerOf2(mask + 1);
1746
1747 if (mask_bits == 16u) {
1748 // Load only the bitstring part of the status word.
1749 __ Ldrh(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1750 } else {
1751 // /* uint32_t */ temp = temp->status_
1752 __ Ldr(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1753 // Extract the bitstring bits.
1754 __ Ubfx(temp, temp, 0, mask_bits);
1755 }
1756 // Compare the bitstring bits to `path_to_root`.
1757 __ Cmp(temp, path_to_root);
1758}
1759
Roland Levillain44015862016-01-22 11:47:17 +00001760void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001761 BarrierType type = BarrierAll;
1762
1763 switch (kind) {
1764 case MemBarrierKind::kAnyAny:
1765 case MemBarrierKind::kAnyStore: {
1766 type = BarrierAll;
1767 break;
1768 }
1769 case MemBarrierKind::kLoadAny: {
1770 type = BarrierReads;
1771 break;
1772 }
1773 case MemBarrierKind::kStoreStore: {
1774 type = BarrierWrites;
1775 break;
1776 }
1777 default:
1778 LOG(FATAL) << "Unexpected memory barrier " << kind;
1779 }
1780 __ Dmb(InnerShareable, type);
1781}
1782
Serban Constantinescu02164b32014-11-13 14:05:07 +00001783void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1784 HBasicBlock* successor) {
1785 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001786 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1787 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001788 slow_path =
1789 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001790 instruction->SetSlowPath(slow_path);
1791 codegen_->AddSlowPath(slow_path);
1792 if (successor != nullptr) {
1793 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001794 }
1795 } else {
1796 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1797 }
1798
Serban Constantinescu02164b32014-11-13 14:05:07 +00001799 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1800 Register temp = temps.AcquireW();
1801
Andreas Gampe542451c2016-07-26 09:02:02 -07001802 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001803 if (successor == nullptr) {
1804 __ Cbnz(temp, slow_path->GetEntryLabel());
1805 __ Bind(slow_path->GetReturnLabel());
1806 } else {
1807 __ Cbz(temp, codegen_->GetLabelOf(successor));
1808 __ B(slow_path->GetEntryLabel());
1809 // slow_path will return to GetLabelOf(successor).
1810 }
1811}
1812
Alexandre Rames5319def2014-10-23 10:03:10 +01001813InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1814 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001815 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001816 assembler_(codegen->GetAssembler()),
1817 codegen_(codegen) {}
1818
Alexandre Rames67555f72014-11-18 10:55:16 +00001819void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001820 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001821 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001822 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001823 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001824 case DataType::Type::kInt32:
1825 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01001826 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001827 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001828 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001829 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001830
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001831 case DataType::Type::kFloat32:
1832 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001833 locations->SetInAt(0, Location::RequiresFpuRegister());
1834 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001835 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001836 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001837
Alexandre Rames5319def2014-10-23 10:03:10 +01001838 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001839 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001840 }
1841}
1842
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001843void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
1844 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001845 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1846
1847 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001848 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01001849 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001850 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
1851 object_field_get_with_read_barrier
1852 ? LocationSummary::kCallOnSlowPath
1853 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001854 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001855 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko0ecac682018-08-07 10:40:38 +01001856 // We need a temporary register for the read barrier load in
1857 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
1858 // only if the field is volatile or the offset is too big.
1859 if (field_info.IsVolatile() ||
1860 field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
1861 locations->AddTemp(FixedTempLocation());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001862 }
Vladimir Marko70e97462016-08-09 11:04:26 +01001863 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001864 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001865 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001866 locations->SetOut(Location::RequiresFpuRegister());
1867 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001868 // The output overlaps for an object field get when read barriers
1869 // are enabled: we do not want the load to overwrite the object's
1870 // location, as we need it to emit the read barrier.
1871 locations->SetOut(
1872 Location::RequiresRegister(),
1873 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001874 }
1875}
1876
1877void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1878 const FieldInfo& field_info) {
1879 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001880 LocationSummary* locations = instruction->GetLocations();
1881 Location base_loc = locations->InAt(0);
1882 Location out = locations->Out();
1883 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01001884 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
1885 DataType::Type load_type = instruction->GetType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001886 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001887
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001888 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01001889 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00001890 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00001891 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001892 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001893 Location maybe_temp =
1894 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00001895 // Note that potential implicit null checks are handled in this
1896 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1897 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1898 instruction,
1899 out,
1900 base,
1901 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001902 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08001903 /* needs_null_check= */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001904 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001905 } else {
1906 // General case.
1907 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001908 // Note that a potential implicit null check is handled in this
1909 // CodeGeneratorARM64::LoadAcquire call.
1910 // NB: LoadAcquire will record the pc info if needed.
1911 codegen_->LoadAcquire(
Andreas Gampe3db70682018-12-26 15:12:03 -08001912 instruction, OutputCPURegister(instruction), field, /* needs_null_check= */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001913 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00001914 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1915 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01001916 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001917 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001918 }
Vladimir Marko61b92282017-10-11 13:23:17 +01001919 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00001920 // If read barriers are enabled, emit read barriers other than
1921 // Baker's using a slow path (and also unpoison the loaded
1922 // reference, if heap poisoning is enabled).
1923 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1924 }
Roland Levillain4d027112015-07-01 15:41:14 +01001925 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001926}
1927
1928void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1929 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001930 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01001931 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001932 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1933 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001934 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001935 locations->SetInAt(1, Location::RequiresFpuRegister());
1936 } else {
1937 locations->SetInAt(1, Location::RequiresRegister());
1938 }
1939}
1940
1941void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001942 const FieldInfo& field_info,
1943 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001944 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
1945
1946 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001947 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001948 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001949 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001950 DataType::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001951
Roland Levillain4d027112015-07-01 15:41:14 +01001952 {
1953 // We use a block to end the scratch scope before the write barrier, thus
1954 // freeing the temporary registers so they can be used in `MarkGCCard`.
1955 UseScratchRegisterScope temps(GetVIXLAssembler());
1956
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001957 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01001958 DCHECK(value.IsW());
1959 Register temp = temps.AcquireW();
1960 __ Mov(temp, value.W());
1961 GetAssembler()->PoisonHeapReference(temp.W());
1962 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001963 }
Roland Levillain4d027112015-07-01 15:41:14 +01001964
1965 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00001966 codegen_->StoreRelease(
Andreas Gampe3db70682018-12-26 15:12:03 -08001967 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check= */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01001968 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00001969 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
1970 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01001971 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1972 codegen_->MaybeRecordImplicitNullCheck(instruction);
1973 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001974 }
1975
1976 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001977 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001978 }
1979}
1980
Alexandre Rames67555f72014-11-18 10:55:16 +00001981void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001982 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001983
1984 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001985 case DataType::Type::kInt32:
1986 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001987 Register dst = OutputRegister(instr);
1988 Register lhs = InputRegisterAt(instr, 0);
1989 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001990 if (instr->IsAdd()) {
1991 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001992 } else if (instr->IsAnd()) {
1993 __ And(dst, lhs, rhs);
1994 } else if (instr->IsOr()) {
1995 __ Orr(dst, lhs, rhs);
1996 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001997 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001998 } else if (instr->IsRor()) {
1999 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002000 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002001 __ Ror(dst, lhs, shift);
2002 } else {
2003 // Ensure shift distance is in the same size register as the result. If
2004 // we are rotating a long and the shift comes in a w register originally,
2005 // we don't need to sxtw for use as an x since the shift distances are
2006 // all & reg_bits - 1.
2007 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2008 }
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002009 } else if (instr->IsMin() || instr->IsMax()) {
2010 __ Cmp(lhs, rhs);
2011 __ Csel(dst, lhs, rhs, instr->IsMin() ? lt : gt);
Alexandre Rames67555f72014-11-18 10:55:16 +00002012 } else {
2013 DCHECK(instr->IsXor());
2014 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002015 }
2016 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002017 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002018 case DataType::Type::kFloat32:
2019 case DataType::Type::kFloat64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002020 FPRegister dst = OutputFPRegister(instr);
2021 FPRegister lhs = InputFPRegisterAt(instr, 0);
2022 FPRegister rhs = InputFPRegisterAt(instr, 1);
2023 if (instr->IsAdd()) {
2024 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002025 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002026 __ Fsub(dst, lhs, rhs);
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002027 } else if (instr->IsMin()) {
2028 __ Fmin(dst, lhs, rhs);
2029 } else if (instr->IsMax()) {
2030 __ Fmax(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002031 } else {
2032 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002033 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002034 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002035 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002036 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002037 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002038 }
2039}
2040
Serban Constantinescu02164b32014-11-13 14:05:07 +00002041void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2042 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2043
Vladimir Markoca6fff82017-10-03 14:49:14 +01002044 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002045 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002046 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002047 case DataType::Type::kInt32:
2048 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002049 locations->SetInAt(0, Location::RequiresRegister());
2050 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002051 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002052 break;
2053 }
2054 default:
2055 LOG(FATAL) << "Unexpected shift type " << type;
2056 }
2057}
2058
2059void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2060 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2061
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002062 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002063 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002064 case DataType::Type::kInt32:
2065 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002066 Register dst = OutputRegister(instr);
2067 Register lhs = InputRegisterAt(instr, 0);
2068 Operand rhs = InputOperandAt(instr, 1);
2069 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002070 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002071 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002072 if (instr->IsShl()) {
2073 __ Lsl(dst, lhs, shift_value);
2074 } else if (instr->IsShr()) {
2075 __ Asr(dst, lhs, shift_value);
2076 } else {
2077 __ Lsr(dst, lhs, shift_value);
2078 }
2079 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002080 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002081
2082 if (instr->IsShl()) {
2083 __ Lsl(dst, lhs, rhs_reg);
2084 } else if (instr->IsShr()) {
2085 __ Asr(dst, lhs, rhs_reg);
2086 } else {
2087 __ Lsr(dst, lhs, rhs_reg);
2088 }
2089 }
2090 break;
2091 }
2092 default:
2093 LOG(FATAL) << "Unexpected shift operation type " << type;
2094 }
2095}
2096
Alexandre Rames5319def2014-10-23 10:03:10 +01002097void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002098 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002099}
2100
2101void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002102 HandleBinaryOp(instruction);
2103}
2104
2105void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2106 HandleBinaryOp(instruction);
2107}
2108
2109void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2110 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002111}
2112
Artem Serov7fc63502016-02-09 17:15:29 +00002113void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002114 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002115 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002116 locations->SetInAt(0, Location::RequiresRegister());
2117 // There is no immediate variant of negated bitwise instructions in AArch64.
2118 locations->SetInAt(1, Location::RequiresRegister());
2119 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2120}
2121
Artem Serov7fc63502016-02-09 17:15:29 +00002122void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002123 Register dst = OutputRegister(instr);
2124 Register lhs = InputRegisterAt(instr, 0);
2125 Register rhs = InputRegisterAt(instr, 1);
2126
2127 switch (instr->GetOpKind()) {
2128 case HInstruction::kAnd:
2129 __ Bic(dst, lhs, rhs);
2130 break;
2131 case HInstruction::kOr:
2132 __ Orn(dst, lhs, rhs);
2133 break;
2134 case HInstruction::kXor:
2135 __ Eon(dst, lhs, rhs);
2136 break;
2137 default:
2138 LOG(FATAL) << "Unreachable";
2139 }
2140}
2141
Anton Kirilov74234da2017-01-13 14:42:47 +00002142void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2143 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002144 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2145 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002146 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002147 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002148 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2149 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2150 } else {
2151 locations->SetInAt(0, Location::RequiresRegister());
2152 }
2153 locations->SetInAt(1, Location::RequiresRegister());
2154 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2155}
2156
Anton Kirilov74234da2017-01-13 14:42:47 +00002157void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2158 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002159 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002160 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002161 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002162 Register out = OutputRegister(instruction);
2163 Register left;
2164 if (kind != HInstruction::kNeg) {
2165 left = InputRegisterAt(instruction, 0);
2166 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002167 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002168 // shifter operand operation, the IR generating `right_reg` (input to the type
2169 // conversion) can have a different type from the current instruction's type,
2170 // so we manually indicate the type.
2171 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002172 Operand right_operand(0);
2173
Anton Kirilov74234da2017-01-13 14:42:47 +00002174 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2175 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002176 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2177 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002178 right_operand = Operand(right_reg,
2179 helpers::ShiftFromOpKind(op_kind),
2180 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002181 }
2182
2183 // Logical binary operations do not support extension operations in the
2184 // operand. Note that VIXL would still manage if it was passed by generating
2185 // the extension as a separate instruction.
2186 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2187 DCHECK(!right_operand.IsExtendedRegister() ||
2188 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2189 kind != HInstruction::kNeg));
2190 switch (kind) {
2191 case HInstruction::kAdd:
2192 __ Add(out, left, right_operand);
2193 break;
2194 case HInstruction::kAnd:
2195 __ And(out, left, right_operand);
2196 break;
2197 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002198 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002199 __ Neg(out, right_operand);
2200 break;
2201 case HInstruction::kOr:
2202 __ Orr(out, left, right_operand);
2203 break;
2204 case HInstruction::kSub:
2205 __ Sub(out, left, right_operand);
2206 break;
2207 case HInstruction::kXor:
2208 __ Eor(out, left, right_operand);
2209 break;
2210 default:
2211 LOG(FATAL) << "Unexpected operation kind: " << kind;
2212 UNREACHABLE();
2213 }
2214}
2215
Artem Serov328429f2016-07-06 16:23:04 +01002216void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002217 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002218 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002219 locations->SetInAt(0, Location::RequiresRegister());
2220 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002221 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002222}
2223
Roland Levillain19c54192016-11-04 13:44:09 +00002224void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002225 __ Add(OutputRegister(instruction),
2226 InputRegisterAt(instruction, 0),
2227 Operand(InputOperandAt(instruction, 1)));
2228}
2229
Artem Serove1811ed2017-04-27 16:50:47 +01002230void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2231 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002232 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002233
2234 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2235
2236 locations->SetInAt(0, Location::RequiresRegister());
2237 // For byte case we don't need to shift the index variable so we can encode the data offset into
2238 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2239 // data offset constant generation out of the loop and reduce the critical path length in the
2240 // loop.
2241 locations->SetInAt(1, shift->GetValue() == 0
2242 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2243 : Location::RequiresRegister());
2244 locations->SetInAt(2, Location::ConstantLocation(shift));
2245 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2246}
2247
2248void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2249 HIntermediateAddressIndex* instruction) {
2250 Register index_reg = InputRegisterAt(instruction, 0);
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002251 uint32_t shift = Int64FromLocation(instruction->GetLocations()->InAt(2));
Artem Serove1811ed2017-04-27 16:50:47 +01002252 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2253
2254 if (shift == 0) {
2255 __ Add(OutputRegister(instruction), index_reg, offset);
2256 } else {
2257 Register offset_reg = InputRegisterAt(instruction, 1);
2258 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2259 }
2260}
2261
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002262void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002263 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002264 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002265 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2266 if (instr->GetOpKind() == HInstruction::kSub &&
2267 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002268 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002269 // Don't allocate register for Mneg instruction.
2270 } else {
2271 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2272 Location::RequiresRegister());
2273 }
2274 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2275 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002276 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2277}
2278
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002279void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002280 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002281 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2282 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002283
2284 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2285 // This fixup should be carried out for all multiply-accumulate instructions:
2286 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002287 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002288 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2289 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002290 vixl::aarch64::Instruction* prev =
2291 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002292 if (prev->IsLoadOrStore()) {
2293 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002294 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002295 __ nop();
2296 }
2297 }
2298
2299 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002300 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002301 __ Madd(res, mul_left, mul_right, accumulator);
2302 } else {
2303 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002304 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002305 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002306 __ Mneg(res, mul_left, mul_right);
2307 } else {
2308 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2309 __ Msub(res, mul_left, mul_right, accumulator);
2310 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002311 }
2312}
2313
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002314void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002315 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002316 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002317 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002318 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2319 object_array_get_with_read_barrier
2320 ? LocationSummary::kCallOnSlowPath
2321 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002322 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002323 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002324 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002325 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002326 // We need a temporary register for the read barrier load in
2327 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2328 // only if the offset is too big.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002329 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2330 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002331 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002332 if (offset >= kReferenceLoadMinFarOffset) {
2333 locations->AddTemp(FixedTempLocation());
2334 }
Artem Serov0806f582018-10-11 20:14:20 +01002335 } else if (!instruction->GetArray()->IsIntermediateAddress()) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01002336 // We need a non-scratch temporary for the array data pointer in
Artem Serov0806f582018-10-11 20:14:20 +01002337 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier() for the case with no
2338 // intermediate address.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002339 locations->AddTemp(Location::RequiresRegister());
2340 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002341 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002342 locations->SetInAt(0, Location::RequiresRegister());
2343 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002344 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002345 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2346 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002347 // The output overlaps in the case of an object array get with
2348 // read barriers enabled: we do not want the move to overwrite the
2349 // array's location, as we need it to emit the read barrier.
2350 locations->SetOut(
2351 Location::RequiresRegister(),
2352 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002353 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002354}
2355
2356void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002357 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002358 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002359 LocationSummary* locations = instruction->GetLocations();
2360 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002361 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002362 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002363 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2364 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002365 MacroAssembler* masm = GetVIXLAssembler();
2366 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002367
Artem Serov0806f582018-10-11 20:14:20 +01002368 // The non-Baker read barrier instrumentation of object ArrayGet instructions
Roland Levillain19c54192016-11-04 13:44:09 +00002369 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002370 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002371 instruction->GetArray()->IsIntermediateAddress() &&
Artem Serov0806f582018-10-11 20:14:20 +01002372 kEmitCompilerReadBarrier &&
2373 !kUseBakerReadBarrier));
Roland Levillain19c54192016-11-04 13:44:09 +00002374
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002375 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002376 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002377 // Note that a potential implicit null check is handled in the
2378 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002379 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002380 if (index.IsConstant()) {
Artem Serov0806f582018-10-11 20:14:20 +01002381 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002382 // Array load with a constant index can be treated as a field load.
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002383 offset += Int64FromLocation(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002384 Location maybe_temp =
2385 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2386 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2387 out,
2388 obj.W(),
2389 offset,
2390 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002391 /* needs_null_check= */ false,
2392 /* use_load_acquire= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002393 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002394 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08002395 instruction, out, obj.W(), offset, index, /* needs_null_check= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002396 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002397 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002398 // General case.
2399 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002400 Register length;
2401 if (maybe_compressed_char_at) {
2402 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2403 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002404 {
2405 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2406 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2407
2408 if (instruction->GetArray()->IsIntermediateAddress()) {
2409 DCHECK_LT(count_offset, offset);
2410 int64_t adjusted_offset =
2411 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2412 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2413 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2414 } else {
2415 __ Ldr(length, HeapOperand(obj, count_offset));
2416 }
2417 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002418 }
jessicahandojo05765752016-09-09 19:01:32 -07002419 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002420 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002421 if (maybe_compressed_char_at) {
2422 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002423 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2424 "Expecting 0=compressed, 1=uncompressed");
2425 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002426 __ Ldrb(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002427 HeapOperand(obj, offset + Int64FromLocation(index)));
jessicahandojo05765752016-09-09 19:01:32 -07002428 __ B(&done);
2429 __ Bind(&uncompressed_load);
2430 __ Ldrh(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002431 HeapOperand(obj, offset + (Int64FromLocation(index) << 1)));
jessicahandojo05765752016-09-09 19:01:32 -07002432 __ Bind(&done);
2433 } else {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002434 offset += Int64FromLocation(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002435 source = HeapOperand(obj, offset);
2436 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002437 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002438 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002439 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002440 // We do not need to compute the intermediate address from the array: the
2441 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002442 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002443 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002444 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2445 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
Roland Levillain44015862016-01-22 11:47:17 +00002446 }
2447 temp = obj;
2448 } else {
2449 __ Add(temp, obj, offset);
2450 }
jessicahandojo05765752016-09-09 19:01:32 -07002451 if (maybe_compressed_char_at) {
2452 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002453 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2454 "Expecting 0=compressed, 1=uncompressed");
2455 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002456 __ Ldrb(Register(OutputCPURegister(instruction)),
2457 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2458 __ B(&done);
2459 __ Bind(&uncompressed_load);
2460 __ Ldrh(Register(OutputCPURegister(instruction)),
2461 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2462 __ Bind(&done);
2463 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002464 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002465 }
Roland Levillain44015862016-01-22 11:47:17 +00002466 }
jessicahandojo05765752016-09-09 19:01:32 -07002467 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002468 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2469 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002470 codegen_->Load(type, OutputCPURegister(instruction), source);
2471 codegen_->MaybeRecordImplicitNullCheck(instruction);
2472 }
Roland Levillain44015862016-01-22 11:47:17 +00002473
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002474 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002475 static_assert(
2476 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2477 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2478 Location obj_loc = locations->InAt(0);
2479 if (index.IsConstant()) {
2480 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2481 } else {
2482 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2483 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002484 }
Roland Levillain4d027112015-07-01 15:41:14 +01002485 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002486}
2487
Alexandre Rames5319def2014-10-23 10:03:10 +01002488void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002489 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002490 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002491 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002492}
2493
2494void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002495 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002496 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002497 {
2498 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2499 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2500 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2501 codegen_->MaybeRecordImplicitNullCheck(instruction);
2502 }
jessicahandojo05765752016-09-09 19:01:32 -07002503 // Mask out compression flag from String's array length.
2504 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002505 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002506 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002507}
2508
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002509void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002510 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002511
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002512 bool needs_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002513 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002514 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002515 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002516 locations->SetInAt(0, Location::RequiresRegister());
2517 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002518 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2519 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002520 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002521 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002522 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002523 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002524 }
2525}
2526
2527void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002528 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002529 LocationSummary* locations = instruction->GetLocations();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002530 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002531 bool needs_write_barrier =
2532 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002533
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002534 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002535 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002536 CPURegister source = value;
2537 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002538 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002539 MemOperand destination = HeapOperand(array);
2540 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002541
2542 if (!needs_write_barrier) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002543 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002544 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002545 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002546 destination = HeapOperand(array, offset);
2547 } else {
2548 UseScratchRegisterScope temps(masm);
2549 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002550 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002551 // We do not need to compute the intermediate address from the array: the
2552 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002553 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002554 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002555 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2556 DCHECK(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002557 }
2558 temp = array;
2559 } else {
2560 __ Add(temp, array, offset);
2561 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002562 destination = HeapOperand(temp,
2563 XRegisterFrom(index),
2564 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002565 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002566 }
Artem Serov914d7a82017-02-07 14:33:49 +00002567 {
2568 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2569 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2570 codegen_->Store(value_type, value, destination);
2571 codegen_->MaybeRecordImplicitNullCheck(instruction);
2572 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002573 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002574 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002575
2576 bool can_value_be_null = instruction->GetValueCanBeNull();
2577 vixl::aarch64::Label do_store;
2578 if (can_value_be_null) {
2579 __ Cbz(Register(value), &do_store);
2580 }
2581
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002582 SlowPathCodeARM64* slow_path = nullptr;
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002583 if (needs_type_check) {
2584 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
2585 codegen_->AddSlowPath(slow_path);
2586
2587 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2588 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2589 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2590
Alexandre Rames97833a02015-04-16 15:07:12 +01002591 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002592 Register temp = temps.AcquireSameSizeAs(array);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002593 Register temp2 = temps.AcquireSameSizeAs(array);
2594
2595 // Note that when Baker read barriers are enabled, the type
2596 // checks are performed without read barriers. This is fine,
2597 // even in the case where a class object is in the from-space
2598 // after the flip, as a comparison involving such a type would
2599 // not produce a false positive; it may of course produce a
2600 // false negative, in which case we would take the ArraySet
2601 // slow path.
2602
2603 // /* HeapReference<Class> */ temp = array->klass_
2604 {
2605 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2606 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2607 __ Ldr(temp, HeapOperand(array, class_offset));
2608 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames97833a02015-04-16 15:07:12 +01002609 }
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002610 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Alexandre Rames97833a02015-04-16 15:07:12 +01002611
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002612 // /* HeapReference<Class> */ temp = temp->component_type_
2613 __ Ldr(temp, HeapOperand(temp, component_offset));
2614 // /* HeapReference<Class> */ temp2 = value->klass_
2615 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2616 // If heap poisoning is enabled, no need to unpoison `temp`
2617 // nor `temp2`, as we are comparing two poisoned references.
2618 __ Cmp(temp, temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002619
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002620 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2621 vixl::aarch64::Label do_put;
2622 __ B(eq, &do_put);
2623 // If heap poisoning is enabled, the `temp` reference has
2624 // not been unpoisoned yet; unpoison it now.
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002625 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002626
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002627 // /* HeapReference<Class> */ temp = temp->super_class_
2628 __ Ldr(temp, HeapOperand(temp, super_offset));
2629 // If heap poisoning is enabled, no need to unpoison
2630 // `temp`, as we are comparing against null below.
2631 __ Cbnz(temp, slow_path->GetEntryLabel());
2632 __ Bind(&do_put);
Vladimir Markod1ef8732017-04-18 13:55:13 +01002633 } else {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002634 __ B(ne, slow_path->GetEntryLabel());
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002635 }
2636 }
2637
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002638 codegen_->MarkGCCard(array, value.W(), /* value_can_be_null= */ false);
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002639
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002640 if (can_value_be_null) {
2641 DCHECK(do_store.IsLinked());
2642 __ Bind(&do_store);
2643 }
2644
2645 UseScratchRegisterScope temps(masm);
2646 if (kPoisonHeapReferences) {
2647 Register temp_source = temps.AcquireSameSizeAs(array);
2648 DCHECK(value.IsW());
2649 __ Mov(temp_source, value.W());
2650 GetAssembler()->PoisonHeapReference(temp_source);
2651 source = temp_source;
2652 }
2653
2654 if (index.IsConstant()) {
2655 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
2656 destination = HeapOperand(array, offset);
2657 } else {
2658 Register temp_base = temps.AcquireSameSizeAs(array);
2659 __ Add(temp_base, array, offset);
2660 destination = HeapOperand(temp_base,
2661 XRegisterFrom(index),
2662 LSL,
2663 DataType::SizeShift(value_type));
2664 }
2665
2666 {
2667 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2668 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2669 __ Str(source, destination);
2670
2671 if (can_value_be_null || !needs_type_check) {
2672 codegen_->MaybeRecordImplicitNullCheck(instruction);
2673 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002674 }
2675
2676 if (slow_path != nullptr) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002677 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002678 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002679 }
2680}
2681
Alexandre Rames67555f72014-11-18 10:55:16 +00002682void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002683 RegisterSet caller_saves = RegisterSet::Empty();
2684 InvokeRuntimeCallingConvention calling_convention;
2685 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2686 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2687 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002688 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002689 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002690}
2691
2692void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002693 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002694 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002695 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002696 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2697 __ B(slow_path->GetEntryLabel(), hs);
2698}
2699
Alexandre Rames67555f72014-11-18 10:55:16 +00002700void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2701 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002702 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00002703 locations->SetInAt(0, Location::RequiresRegister());
2704 if (check->HasUses()) {
2705 locations->SetOut(Location::SameAsFirstInput());
2706 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01002707 // Rely on the type initialization to save everything we need.
2708 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Alexandre Rames67555f72014-11-18 10:55:16 +00002709}
2710
2711void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2712 // We assume the class is not null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01002713 SlowPathCodeARM64* slow_path =
2714 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(check->GetLoadClass(), check);
Alexandre Rames67555f72014-11-18 10:55:16 +00002715 codegen_->AddSlowPath(slow_path);
2716 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2717}
2718
Roland Levillain1a653882016-03-18 18:05:57 +00002719static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2720 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2721 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2722}
2723
2724void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2725 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2726 Location rhs_loc = instruction->GetLocations()->InAt(1);
2727 if (rhs_loc.IsConstant()) {
2728 // 0.0 is the only immediate that can be encoded directly in
2729 // an FCMP instruction.
2730 //
2731 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2732 // specify that in a floating-point comparison, positive zero
2733 // and negative zero are considered equal, so we can use the
2734 // literal 0.0 for both cases here.
2735 //
2736 // Note however that some methods (Float.equal, Float.compare,
2737 // Float.compareTo, Double.equal, Double.compare,
2738 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2739 // StrictMath.min) consider 0.0 to be (strictly) greater than
2740 // -0.0. So if we ever translate calls to these methods into a
2741 // HCompare instruction, we must handle the -0.0 case with
2742 // care here.
2743 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2744 __ Fcmp(lhs_reg, 0.0);
2745 } else {
2746 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2747 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002748}
2749
Serban Constantinescu02164b32014-11-13 14:05:07 +00002750void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002751 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002752 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002753 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002754 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002755 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002756 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002757 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002758 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002759 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002760 case DataType::Type::kInt32:
2761 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002762 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002763 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002764 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2765 break;
2766 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002767 case DataType::Type::kFloat32:
2768 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002769 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002770 locations->SetInAt(1,
2771 IsFloatingPointZeroConstant(compare->InputAt(1))
2772 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2773 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002774 locations->SetOut(Location::RequiresRegister());
2775 break;
2776 }
2777 default:
2778 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2779 }
2780}
2781
2782void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002783 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002784
2785 // 0 if: left == right
2786 // 1 if: left > right
2787 // -1 if: left < right
2788 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002789 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002790 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002791 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002792 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002793 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002794 case DataType::Type::kInt32:
2795 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002796 Register result = OutputRegister(compare);
2797 Register left = InputRegisterAt(compare, 0);
2798 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002799 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002800 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2801 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002802 break;
2803 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002804 case DataType::Type::kFloat32:
2805 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002806 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002807 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002808 __ Cset(result, ne);
2809 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002810 break;
2811 }
2812 default:
2813 LOG(FATAL) << "Unimplemented compare type " << in_type;
2814 }
2815}
2816
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002817void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002818 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002819
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002820 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002821 locations->SetInAt(0, Location::RequiresFpuRegister());
2822 locations->SetInAt(1,
2823 IsFloatingPointZeroConstant(instruction->InputAt(1))
2824 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2825 : Location::RequiresFpuRegister());
2826 } else {
2827 // Integer cases.
2828 locations->SetInAt(0, Location::RequiresRegister());
2829 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2830 }
2831
David Brazdilb3e773e2016-01-26 11:28:37 +00002832 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002833 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002834 }
2835}
2836
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002837void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002838 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002839 return;
2840 }
2841
2842 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002843 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002844 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002845
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002846 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002847 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002848 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002849 } else {
2850 // Integer cases.
2851 Register lhs = InputRegisterAt(instruction, 0);
2852 Operand rhs = InputOperandAt(instruction, 1);
2853 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002854 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002855 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002856}
2857
2858#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2859 M(Equal) \
2860 M(NotEqual) \
2861 M(LessThan) \
2862 M(LessThanOrEqual) \
2863 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002864 M(GreaterThanOrEqual) \
2865 M(Below) \
2866 M(BelowOrEqual) \
2867 M(Above) \
2868 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002869#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002870void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2871void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002872FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002873#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002874#undef FOR_EACH_CONDITION_INSTRUCTION
2875
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01002876void InstructionCodeGeneratorARM64::GenerateIntDivForPower2Denom(HDiv* instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002877 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002878 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01002879 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
2880
2881 Register out = OutputRegister(instruction);
2882 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01002883
2884 if (abs_imm == 2) {
2885 int bits = DataType::Size(instruction->GetResultType()) * kBitsPerByte;
2886 __ Add(out, dividend, Operand(dividend, LSR, bits - 1));
2887 } else {
2888 UseScratchRegisterScope temps(GetVIXLAssembler());
2889 Register temp = temps.AcquireSameSizeAs(out);
2890 __ Add(temp, dividend, abs_imm - 1);
2891 __ Cmp(dividend, 0);
2892 __ Csel(out, temp, dividend, lt);
2893 }
2894
Zheng Xuc6667102015-05-15 16:08:45 +08002895 int ctz_imm = CTZ(abs_imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01002896 if (imm > 0) {
2897 __ Asr(out, out, ctz_imm);
Zheng Xuc6667102015-05-15 16:08:45 +08002898 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01002899 __ Neg(out, Operand(out, ASR, ctz_imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002900 }
2901}
2902
2903void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2904 DCHECK(instruction->IsDiv() || instruction->IsRem());
2905
2906 LocationSummary* locations = instruction->GetLocations();
2907 Location second = locations->InAt(1);
2908 DCHECK(second.IsConstant());
2909
2910 Register out = OutputRegister(instruction);
2911 Register dividend = InputRegisterAt(instruction, 0);
2912 int64_t imm = Int64FromConstant(second.GetConstant());
2913
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002914 DataType::Type type = instruction->GetResultType();
2915 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08002916
2917 int64_t magic;
2918 int shift;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002919 CalculateMagicAndShiftForDivRem(
Andreas Gampe3db70682018-12-26 15:12:03 -08002920 imm, /* is_long= */ type == DataType::Type::kInt64, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08002921
2922 UseScratchRegisterScope temps(GetVIXLAssembler());
2923 Register temp = temps.AcquireSameSizeAs(out);
2924
2925 // temp = get_high(dividend * magic)
2926 __ Mov(temp, magic);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002927 if (type == DataType::Type::kInt64) {
Zheng Xuc6667102015-05-15 16:08:45 +08002928 __ Smulh(temp, dividend, temp);
2929 } else {
2930 __ Smull(temp.X(), dividend, temp);
2931 __ Lsr(temp.X(), temp.X(), 32);
2932 }
2933
2934 if (imm > 0 && magic < 0) {
2935 __ Add(temp, temp, dividend);
2936 } else if (imm < 0 && magic > 0) {
2937 __ Sub(temp, temp, dividend);
2938 }
2939
2940 if (shift != 0) {
2941 __ Asr(temp, temp, shift);
2942 }
2943
2944 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002945 __ Sub(out, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08002946 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002947 __ Sub(temp, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08002948 // TODO: Strength reduction for msub.
2949 Register temp_imm = temps.AcquireSameSizeAs(out);
2950 __ Mov(temp_imm, imm);
2951 __ Msub(out, temp, temp_imm, dividend);
2952 }
2953}
2954
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01002955void InstructionCodeGeneratorARM64::GenerateIntDivForConstDenom(HDiv *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002956 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Zheng Xuc6667102015-05-15 16:08:45 +08002957
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01002958 if (imm == 0) {
2959 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2960 return;
2961 }
Zheng Xuc6667102015-05-15 16:08:45 +08002962
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01002963 if (IsPowerOfTwo(AbsOrMin(imm))) {
2964 GenerateIntDivForPower2Denom(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08002965 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01002966 // Cases imm == -1 or imm == 1 are handled by InstructionSimplifier.
2967 DCHECK(imm < -2 || imm > 2) << imm;
2968 GenerateDivRemWithAnyConstant(instruction);
2969 }
2970}
2971
2972void InstructionCodeGeneratorARM64::GenerateIntDiv(HDiv *instruction) {
2973 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
2974 << instruction->GetResultType();
2975
2976 if (instruction->GetLocations()->InAt(1).IsConstant()) {
2977 GenerateIntDivForConstDenom(instruction);
2978 } else {
2979 Register out = OutputRegister(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08002980 Register dividend = InputRegisterAt(instruction, 0);
2981 Register divisor = InputRegisterAt(instruction, 1);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01002982 __ Sdiv(out, dividend, divisor);
Zheng Xuc6667102015-05-15 16:08:45 +08002983 }
2984}
2985
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002986void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2987 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002988 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002989 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002990 case DataType::Type::kInt32:
2991 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002992 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002993 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002994 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2995 break;
2996
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002997 case DataType::Type::kFloat32:
2998 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002999 locations->SetInAt(0, Location::RequiresFpuRegister());
3000 locations->SetInAt(1, Location::RequiresFpuRegister());
3001 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3002 break;
3003
3004 default:
3005 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3006 }
3007}
3008
3009void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003010 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003011 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003012 case DataType::Type::kInt32:
3013 case DataType::Type::kInt64:
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003014 GenerateIntDiv(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003015 break;
3016
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003017 case DataType::Type::kFloat32:
3018 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003019 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3020 break;
3021
3022 default:
3023 LOG(FATAL) << "Unexpected div type " << type;
3024 }
3025}
3026
Alexandre Rames67555f72014-11-18 10:55:16 +00003027void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003028 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003029 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003030}
3031
3032void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3033 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003034 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003035 codegen_->AddSlowPath(slow_path);
3036 Location value = instruction->GetLocations()->InAt(0);
3037
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003038 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003039
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003040 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003041 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Elliott Hughesc1896c92018-11-29 11:33:18 -08003042 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003043 }
3044
Alexandre Rames67555f72014-11-18 10:55:16 +00003045 if (value.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003046 int64_t divisor = Int64FromLocation(value);
Alexandre Rames67555f72014-11-18 10:55:16 +00003047 if (divisor == 0) {
3048 __ B(slow_path->GetEntryLabel());
3049 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003050 // A division by a non-null constant is valid. We don't need to perform
3051 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003052 }
3053 } else {
3054 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3055 }
3056}
3057
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003058void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3059 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003060 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003061 locations->SetOut(Location::ConstantLocation(constant));
3062}
3063
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003064void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3065 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003066 // Will be generated at use site.
3067}
3068
Alexandre Rames5319def2014-10-23 10:03:10 +01003069void LocationsBuilderARM64::VisitExit(HExit* exit) {
3070 exit->SetLocations(nullptr);
3071}
3072
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003073void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003074}
3075
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003076void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3077 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003078 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003079 locations->SetOut(Location::ConstantLocation(constant));
3080}
3081
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003082void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003083 // Will be generated at use site.
3084}
3085
David Brazdilfc6a86a2015-06-26 10:33:45 +00003086void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003087 if (successor->IsExitBlock()) {
3088 DCHECK(got->GetPrevious()->AlwaysThrows());
3089 return; // no code needed
3090 }
3091
Serban Constantinescu02164b32014-11-13 14:05:07 +00003092 HBasicBlock* block = got->GetBlock();
3093 HInstruction* previous = got->GetPrevious();
3094 HLoopInformation* info = block->GetLoopInformation();
3095
David Brazdil46e2a392015-03-16 17:31:52 +00003096 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00003097 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
3098 UseScratchRegisterScope temps(GetVIXLAssembler());
3099 Register temp1 = temps.AcquireX();
3100 Register temp2 = temps.AcquireX();
3101 __ Ldr(temp1, MemOperand(sp, 0));
3102 __ Ldrh(temp2, MemOperand(temp1, ArtMethod::HotnessCountOffset().Int32Value()));
3103 __ Add(temp2, temp2, 1);
3104 __ Strh(temp2, MemOperand(temp1, ArtMethod::HotnessCountOffset().Int32Value()));
3105 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003106 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3107 return;
3108 }
3109 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3110 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08003111 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003112 }
3113 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003114 __ B(codegen_->GetLabelOf(successor));
3115 }
3116}
3117
David Brazdilfc6a86a2015-06-26 10:33:45 +00003118void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3119 got->SetLocations(nullptr);
3120}
3121
3122void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3123 HandleGoto(got, got->GetSuccessor());
3124}
3125
3126void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3127 try_boundary->SetLocations(nullptr);
3128}
3129
3130void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3131 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3132 if (!successor->IsExitBlock()) {
3133 HandleGoto(try_boundary, successor);
3134 }
3135}
3136
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003137void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003138 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003139 vixl::aarch64::Label* true_target,
3140 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003141 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003142
David Brazdil0debae72015-11-12 18:37:00 +00003143 if (true_target == nullptr && false_target == nullptr) {
3144 // Nothing to do. The code always falls through.
3145 return;
3146 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003147 // Constant condition, statically compared against "true" (integer value 1).
3148 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003149 if (true_target != nullptr) {
3150 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003151 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003152 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003153 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003154 if (false_target != nullptr) {
3155 __ B(false_target);
3156 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003157 }
David Brazdil0debae72015-11-12 18:37:00 +00003158 return;
3159 }
3160
3161 // The following code generates these patterns:
3162 // (1) true_target == nullptr && false_target != nullptr
3163 // - opposite condition true => branch to false_target
3164 // (2) true_target != nullptr && false_target == nullptr
3165 // - condition true => branch to true_target
3166 // (3) true_target != nullptr && false_target != nullptr
3167 // - condition true => branch to true_target
3168 // - branch to false_target
3169 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003170 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003171 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003172 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003173 if (true_target == nullptr) {
3174 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3175 } else {
3176 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3177 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003178 } else {
3179 // The condition instruction has not been materialized, use its inputs as
3180 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003181 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003182
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003183 DataType::Type type = condition->InputAt(0)->GetType();
3184 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003185 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003186 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003187 IfCondition opposite_condition = condition->GetOppositeCondition();
3188 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003189 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003190 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003191 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003192 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003193 // Integer cases.
3194 Register lhs = InputRegisterAt(condition, 0);
3195 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003196
3197 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003198 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003199 if (true_target == nullptr) {
3200 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3201 non_fallthrough_target = false_target;
3202 } else {
3203 arm64_cond = ARM64Condition(condition->GetCondition());
3204 non_fallthrough_target = true_target;
3205 }
3206
Aart Bik086d27e2016-01-20 17:02:00 -08003207 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003208 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003209 switch (arm64_cond) {
3210 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003211 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003212 break;
3213 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003214 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003215 break;
3216 case lt:
3217 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003218 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003219 break;
3220 case ge:
3221 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003222 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003223 break;
3224 default:
3225 // Without the `static_cast` the compiler throws an error for
3226 // `-Werror=sign-promo`.
3227 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3228 }
3229 } else {
3230 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003231 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003232 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003233 }
3234 }
David Brazdil0debae72015-11-12 18:37:00 +00003235
3236 // If neither branch falls through (case 3), the conditional branch to `true_target`
3237 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3238 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003239 __ B(false_target);
3240 }
3241}
3242
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003243void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003244 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003245 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003246 locations->SetInAt(0, Location::RequiresRegister());
3247 }
3248}
3249
3250void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003251 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3252 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003253 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3254 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3255 true_target = nullptr;
3256 }
3257 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3258 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3259 false_target = nullptr;
3260 }
Andreas Gampe3db70682018-12-26 15:12:03 -08003261 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003262}
3263
3264void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003265 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003266 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003267 InvokeRuntimeCallingConvention calling_convention;
3268 RegisterSet caller_saves = RegisterSet::Empty();
3269 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3270 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003271 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003272 locations->SetInAt(0, Location::RequiresRegister());
3273 }
3274}
3275
3276void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003277 SlowPathCodeARM64* slow_path =
3278 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003279 GenerateTestAndBranch(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08003280 /* condition_input_index= */ 0,
David Brazdil0debae72015-11-12 18:37:00 +00003281 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08003282 /* false_target= */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003283}
3284
Mingyao Yang063fc772016-08-02 11:02:54 -07003285void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003286 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003287 LocationSummary(flag, LocationSummary::kNoCall);
3288 locations->SetOut(Location::RequiresRegister());
3289}
3290
3291void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3292 __ Ldr(OutputRegister(flag),
3293 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3294}
3295
David Brazdilc0b601b2016-02-08 14:20:45 +00003296static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3297 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003298 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003299}
3300
Alexandre Rames880f1192016-06-13 16:04:50 +01003301static inline Condition GetConditionForSelect(HCondition* condition) {
3302 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003303 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3304 : ARM64Condition(cond);
3305}
3306
David Brazdil74eb1b22015-12-14 11:44:01 +00003307void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003308 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003309 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003310 locations->SetInAt(0, Location::RequiresFpuRegister());
3311 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003312 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003313 } else {
3314 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3315 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3316 bool is_true_value_constant = cst_true_value != nullptr;
3317 bool is_false_value_constant = cst_false_value != nullptr;
3318 // Ask VIXL whether we should synthesize constants in registers.
3319 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3320 Operand true_op = is_true_value_constant ?
3321 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3322 Operand false_op = is_false_value_constant ?
3323 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3324 bool true_value_in_register = false;
3325 bool false_value_in_register = false;
3326 MacroAssembler::GetCselSynthesisInformation(
3327 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3328 true_value_in_register |= !is_true_value_constant;
3329 false_value_in_register |= !is_false_value_constant;
3330
3331 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3332 : Location::ConstantLocation(cst_true_value));
3333 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3334 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003335 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003336 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003337
David Brazdil74eb1b22015-12-14 11:44:01 +00003338 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3339 locations->SetInAt(2, Location::RequiresRegister());
3340 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003341}
3342
3343void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003344 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003345 Condition csel_cond;
3346
3347 if (IsBooleanValueOrMaterializedCondition(cond)) {
3348 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003349 // Use the condition flags set by the previous instruction.
3350 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003351 } else {
3352 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003353 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003354 }
3355 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003356 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003357 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003358 } else {
3359 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003360 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003361 }
3362
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003363 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003364 __ Fcsel(OutputFPRegister(select),
3365 InputFPRegisterAt(select, 1),
3366 InputFPRegisterAt(select, 0),
3367 csel_cond);
3368 } else {
3369 __ Csel(OutputRegister(select),
3370 InputOperandAt(select, 1),
3371 InputOperandAt(select, 0),
3372 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003373 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003374}
3375
David Srbecky0cf44932015-12-09 14:09:59 +00003376void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003377 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00003378}
3379
David Srbeckyd28f4a02016-03-14 17:14:24 +00003380void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3381 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003382}
3383
3384void CodeGeneratorARM64::GenerateNop() {
3385 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003386}
3387
Alexandre Rames5319def2014-10-23 10:03:10 +01003388void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003389 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003390}
3391
3392void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003393 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003394}
3395
3396void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003397 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003398}
3399
3400void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003401 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003402}
3403
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003404// Temp is used for read barrier.
3405static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3406 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003407 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003408 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3409 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3410 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3411 return 1;
3412 }
3413 return 0;
3414}
3415
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003416// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003417// interface pointer, one for loading the current interface.
3418// The other checks have one temp for loading the object's class.
3419static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3420 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3421 return 3;
3422 }
3423 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003424}
3425
Alexandre Rames67555f72014-11-18 10:55:16 +00003426void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003427 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003428 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003429 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003430 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003431 case TypeCheckKind::kExactCheck:
3432 case TypeCheckKind::kAbstractClassCheck:
3433 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00003434 case TypeCheckKind::kArrayObjectCheck: {
3435 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
3436 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
3437 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003438 break;
Vladimir Marko87584542017-12-12 17:47:52 +00003439 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003440 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003441 case TypeCheckKind::kUnresolvedCheck:
3442 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003443 call_kind = LocationSummary::kCallOnSlowPath;
3444 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00003445 case TypeCheckKind::kBitstringCheck:
3446 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003447 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003448
Vladimir Markoca6fff82017-10-03 14:49:14 +01003449 LocationSummary* locations =
3450 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003451 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003452 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003453 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003454 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003455 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3456 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3457 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3458 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3459 } else {
3460 locations->SetInAt(1, Location::RequiresRegister());
3461 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003462 // The "out" register is used as a temporary, so it overlaps with the inputs.
3463 // Note that TypeCheckSlowPathARM64 uses this register too.
3464 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003465 // Add temps if necessary for read barriers.
3466 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003467}
3468
3469void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003470 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003471 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003472 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003473 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003474 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3475 ? Register()
3476 : InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003477 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003478 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003479 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3480 DCHECK_LE(num_temps, 1u);
3481 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003482 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3483 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3484 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3485 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003486
Scott Wakeling97c72b72016-06-24 16:19:36 +01003487 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003488 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003489
3490 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003491 // Avoid null check if we know `obj` is not null.
3492 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003493 __ Cbz(obj, &zero);
3494 }
3495
Roland Levillain44015862016-01-22 11:47:17 +00003496 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003497 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003498 ReadBarrierOption read_barrier_option =
3499 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003500 // /* HeapReference<Class> */ out = obj->klass_
3501 GenerateReferenceLoadTwoRegisters(instruction,
3502 out_loc,
3503 obj_loc,
3504 class_offset,
3505 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003506 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003507 __ Cmp(out, cls);
3508 __ Cset(out, eq);
3509 if (zero.IsLinked()) {
3510 __ B(&done);
3511 }
3512 break;
3513 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003514
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003515 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003516 ReadBarrierOption read_barrier_option =
3517 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003518 // /* HeapReference<Class> */ out = obj->klass_
3519 GenerateReferenceLoadTwoRegisters(instruction,
3520 out_loc,
3521 obj_loc,
3522 class_offset,
3523 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003524 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003525 // If the class is abstract, we eagerly fetch the super class of the
3526 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003527 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003528 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003529 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003530 GenerateReferenceLoadOneRegister(instruction,
3531 out_loc,
3532 super_offset,
3533 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003534 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003535 // If `out` is null, we use it for the result, and jump to `done`.
3536 __ Cbz(out, &done);
3537 __ Cmp(out, cls);
3538 __ B(ne, &loop);
3539 __ Mov(out, 1);
3540 if (zero.IsLinked()) {
3541 __ B(&done);
3542 }
3543 break;
3544 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003545
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003546 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003547 ReadBarrierOption read_barrier_option =
3548 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003549 // /* HeapReference<Class> */ out = obj->klass_
3550 GenerateReferenceLoadTwoRegisters(instruction,
3551 out_loc,
3552 obj_loc,
3553 class_offset,
3554 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003555 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003556 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003557 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003558 __ Bind(&loop);
3559 __ Cmp(out, cls);
3560 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003561 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003562 GenerateReferenceLoadOneRegister(instruction,
3563 out_loc,
3564 super_offset,
3565 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003566 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003567 __ Cbnz(out, &loop);
3568 // If `out` is null, we use it for the result, and jump to `done`.
3569 __ B(&done);
3570 __ Bind(&success);
3571 __ Mov(out, 1);
3572 if (zero.IsLinked()) {
3573 __ B(&done);
3574 }
3575 break;
3576 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003577
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003578 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003579 ReadBarrierOption read_barrier_option =
3580 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003581 // /* HeapReference<Class> */ out = obj->klass_
3582 GenerateReferenceLoadTwoRegisters(instruction,
3583 out_loc,
3584 obj_loc,
3585 class_offset,
3586 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003587 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003588 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003589 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003590 __ Cmp(out, cls);
3591 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003592 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003593 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003594 GenerateReferenceLoadOneRegister(instruction,
3595 out_loc,
3596 component_offset,
3597 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003598 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003599 // If `out` is null, we use it for the result, and jump to `done`.
3600 __ Cbz(out, &done);
3601 __ Ldrh(out, HeapOperand(out, primitive_offset));
3602 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3603 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003604 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003605 __ Mov(out, 1);
3606 __ B(&done);
3607 break;
3608 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003609
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003610 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003611 // No read barrier since the slow path will retry upon failure.
3612 // /* HeapReference<Class> */ out = obj->klass_
3613 GenerateReferenceLoadTwoRegisters(instruction,
3614 out_loc,
3615 obj_loc,
3616 class_offset,
3617 maybe_temp_loc,
3618 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003619 __ Cmp(out, cls);
3620 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01003621 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08003622 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003623 codegen_->AddSlowPath(slow_path);
3624 __ B(ne, slow_path->GetEntryLabel());
3625 __ Mov(out, 1);
3626 if (zero.IsLinked()) {
3627 __ B(&done);
3628 }
3629 break;
3630 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003631
Calin Juravle98893e12015-10-02 21:05:03 +01003632 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003633 case TypeCheckKind::kInterfaceCheck: {
3634 // Note that we indeed only call on slow path, but we always go
3635 // into the slow path for the unresolved and interface check
3636 // cases.
3637 //
3638 // We cannot directly call the InstanceofNonTrivial runtime
3639 // entry point without resorting to a type checking slow path
3640 // here (i.e. by calling InvokeRuntime directly), as it would
3641 // require to assign fixed registers for the inputs of this
3642 // HInstanceOf instruction (following the runtime calling
3643 // convention), which might be cluttered by the potential first
3644 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003645 //
3646 // TODO: Introduce a new runtime entry point taking the object
3647 // to test (instead of its class) as argument, and let it deal
3648 // with the read barrier issues. This will let us refactor this
3649 // case of the `switch` code as it was previously (with a direct
3650 // call to the runtime not using a type checking slow path).
3651 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003652 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01003653 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08003654 instruction, /* is_fatal= */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003655 codegen_->AddSlowPath(slow_path);
3656 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003657 if (zero.IsLinked()) {
3658 __ B(&done);
3659 }
3660 break;
3661 }
Vladimir Marko175e7862018-03-27 09:03:13 +00003662
3663 case TypeCheckKind::kBitstringCheck: {
3664 // /* HeapReference<Class> */ temp = obj->klass_
3665 GenerateReferenceLoadTwoRegisters(instruction,
3666 out_loc,
3667 obj_loc,
3668 class_offset,
3669 maybe_temp_loc,
3670 kWithoutReadBarrier);
3671
3672 GenerateBitstringTypeCheckCompare(instruction, out);
3673 __ Cset(out, eq);
3674 if (zero.IsLinked()) {
3675 __ B(&done);
3676 }
3677 break;
3678 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003679 }
3680
3681 if (zero.IsLinked()) {
3682 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003683 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003684 }
3685
3686 if (done.IsLinked()) {
3687 __ Bind(&done);
3688 }
3689
3690 if (slow_path != nullptr) {
3691 __ Bind(slow_path->GetExitLabel());
3692 }
3693}
3694
3695void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003696 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00003697 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003698 LocationSummary* locations =
3699 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003700 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003701 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3702 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3703 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3704 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3705 } else {
3706 locations->SetInAt(1, Location::RequiresRegister());
3707 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003708 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
3709 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003710}
3711
3712void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003713 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003714 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003715 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003716 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003717 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3718 ? Register()
3719 : InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003720 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3721 DCHECK_GE(num_temps, 1u);
3722 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003723 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003724 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
3725 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003726 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003727 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3728 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3729 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3730 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3731 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3732 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3733 const uint32_t object_array_data_offset =
3734 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003735
Vladimir Marko87584542017-12-12 17:47:52 +00003736 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003737 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003738 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
3739 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003740 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003741
Scott Wakeling97c72b72016-06-24 16:19:36 +01003742 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003743 // Avoid null check if we know obj is not null.
3744 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003745 __ Cbz(obj, &done);
3746 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003747
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003748 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003749 case TypeCheckKind::kExactCheck:
3750 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003751 // /* HeapReference<Class> */ temp = obj->klass_
3752 GenerateReferenceLoadTwoRegisters(instruction,
3753 temp_loc,
3754 obj_loc,
3755 class_offset,
3756 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003757 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003758
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003759 __ Cmp(temp, cls);
3760 // Jump to slow path for throwing the exception or doing a
3761 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003762 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003763 break;
3764 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003765
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003766 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003767 // /* HeapReference<Class> */ temp = obj->klass_
3768 GenerateReferenceLoadTwoRegisters(instruction,
3769 temp_loc,
3770 obj_loc,
3771 class_offset,
3772 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003773 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003774
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003775 // If the class is abstract, we eagerly fetch the super class of the
3776 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003777 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003778 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003779 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003780 GenerateReferenceLoadOneRegister(instruction,
3781 temp_loc,
3782 super_offset,
3783 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003784 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003785
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003786 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3787 // exception.
3788 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3789 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003790 __ Cmp(temp, cls);
3791 __ B(ne, &loop);
3792 break;
3793 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003794
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003795 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003796 // /* HeapReference<Class> */ temp = obj->klass_
3797 GenerateReferenceLoadTwoRegisters(instruction,
3798 temp_loc,
3799 obj_loc,
3800 class_offset,
3801 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003802 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003803
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003804 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003805 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003806 __ Bind(&loop);
3807 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003808 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003809
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003810 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003811 GenerateReferenceLoadOneRegister(instruction,
3812 temp_loc,
3813 super_offset,
3814 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003815 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003816
3817 // If the class reference currently in `temp` is not null, jump
3818 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003819 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003820 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003821 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003822 break;
3823 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003824
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003825 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003826 // /* HeapReference<Class> */ temp = obj->klass_
3827 GenerateReferenceLoadTwoRegisters(instruction,
3828 temp_loc,
3829 obj_loc,
3830 class_offset,
3831 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003832 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003833
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003834 // Do an exact check.
3835 __ Cmp(temp, cls);
3836 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003837
3838 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003839 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003840 GenerateReferenceLoadOneRegister(instruction,
3841 temp_loc,
3842 component_offset,
3843 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003844 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003845
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003846 // If the component type is null, jump to the slow path to throw the exception.
3847 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3848 // Otherwise, the object is indeed an array. Further check that this component type is not a
3849 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003850 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3851 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003852 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003853 break;
3854 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003855
Calin Juravle98893e12015-10-02 21:05:03 +01003856 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003857 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003858 //
3859 // We cannot directly call the CheckCast runtime entry point
3860 // without resorting to a type checking slow path here (i.e. by
3861 // calling InvokeRuntime directly), as it would require to
3862 // assign fixed registers for the inputs of this HInstanceOf
3863 // instruction (following the runtime calling convention), which
3864 // might be cluttered by the potential first read barrier
3865 // emission at the beginning of this method.
3866 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003867 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003868 case TypeCheckKind::kInterfaceCheck: {
3869 // /* HeapReference<Class> */ temp = obj->klass_
3870 GenerateReferenceLoadTwoRegisters(instruction,
3871 temp_loc,
3872 obj_loc,
3873 class_offset,
3874 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003875 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003876
3877 // /* HeapReference<Class> */ temp = temp->iftable_
3878 GenerateReferenceLoadTwoRegisters(instruction,
3879 temp_loc,
3880 temp_loc,
3881 iftable_offset,
3882 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003883 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003884 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003885 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08003886 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003887 vixl::aarch64::Label start_loop;
3888 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003889 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003890 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
3891 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003892 // Go to next interface.
3893 __ Add(temp, temp, 2 * kHeapReferenceSize);
3894 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003895 // Compare the classes and continue the loop if they do not match.
3896 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
3897 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003898 break;
3899 }
Vladimir Marko175e7862018-03-27 09:03:13 +00003900
3901 case TypeCheckKind::kBitstringCheck: {
3902 // /* HeapReference<Class> */ temp = obj->klass_
3903 GenerateReferenceLoadTwoRegisters(instruction,
3904 temp_loc,
3905 obj_loc,
3906 class_offset,
3907 maybe_temp2_loc,
3908 kWithoutReadBarrier);
3909
3910 GenerateBitstringTypeCheckCompare(instruction, temp);
3911 __ B(ne, type_check_slow_path->GetEntryLabel());
3912 break;
3913 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003914 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003915 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003916
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003917 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003918}
3919
Alexandre Rames5319def2014-10-23 10:03:10 +01003920void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003921 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01003922 locations->SetOut(Location::ConstantLocation(constant));
3923}
3924
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003925void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003926 // Will be generated at use site.
3927}
3928
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003929void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003930 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003931 locations->SetOut(Location::ConstantLocation(constant));
3932}
3933
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003934void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003935 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003936}
3937
Calin Juravle175dc732015-08-25 15:42:32 +01003938void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3939 // The trampoline uses the same calling convention as dex calling conventions,
3940 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3941 // the method_idx.
3942 HandleInvoke(invoke);
3943}
3944
3945void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3946 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08003947 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01003948}
3949
Alexandre Rames5319def2014-10-23 10:03:10 +01003950void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003951 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003952 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003953}
3954
Alexandre Rames67555f72014-11-18 10:55:16 +00003955void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3956 HandleInvoke(invoke);
3957}
3958
3959void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3960 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003961 LocationSummary* locations = invoke->GetLocations();
3962 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003963 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003964 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003965 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003966
3967 // The register ip1 is required to be used for the hidden argument in
3968 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003969 MacroAssembler* masm = GetVIXLAssembler();
3970 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003971 scratch_scope.Exclude(ip1);
3972 __ Mov(ip1, invoke->GetDexMethodIndex());
3973
Artem Serov914d7a82017-02-07 14:33:49 +00003974 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00003975 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003976 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00003977 {
3978 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3979 // /* HeapReference<Class> */ temp = temp->klass_
3980 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
3981 codegen_->MaybeRecordImplicitNullCheck(invoke);
3982 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003983 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00003984 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003985 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003986 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00003987 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00003988 }
Artem Serov914d7a82017-02-07 14:33:49 +00003989
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003990 // Instead of simply (possibly) unpoisoning `temp` here, we should
3991 // emit a read barrier for the previous class reference load.
3992 // However this is not required in practice, as this is an
3993 // intermediate/temporary reference and because the current
3994 // concurrent copying collector keeps the from-space memory
3995 // intact/accessible until the end of the marking phase (the
3996 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003997 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003998 __ Ldr(temp,
3999 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4000 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004001 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004002 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004003 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004004 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004005 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004006
4007 {
4008 // Ensure the pc position is recorded immediately after the `blr` instruction.
4009 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4010
4011 // lr();
4012 __ blr(lr);
4013 DCHECK(!codegen_->IsLeafMethod());
4014 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4015 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004016
Andreas Gampe3db70682018-12-26 15:12:03 -08004017 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004018}
4019
4020void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004021 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004022 if (intrinsic.TryDispatch(invoke)) {
4023 return;
4024 }
4025
Alexandre Rames67555f72014-11-18 10:55:16 +00004026 HandleInvoke(invoke);
4027}
4028
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004029void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004030 // Explicit clinit checks triggered by static invokes must have been pruned by
4031 // art::PrepareForRegisterAllocation.
4032 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004033
Vladimir Markoca6fff82017-10-03 14:49:14 +01004034 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004035 if (intrinsic.TryDispatch(invoke)) {
4036 return;
4037 }
4038
Alexandre Rames67555f72014-11-18 10:55:16 +00004039 HandleInvoke(invoke);
4040}
4041
Andreas Gampe878d58c2015-01-15 23:24:00 -08004042static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4043 if (invoke->GetLocations()->Intrinsified()) {
4044 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4045 intrinsic.Dispatch(invoke);
4046 return true;
4047 }
4048 return false;
4049}
4050
Vladimir Markodc151b22015-10-15 18:02:30 +01004051HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4052 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004053 ArtMethod* method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004054 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004055 return desired_dispatch_info;
4056}
4057
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004058void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4059 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004060 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004061 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4062 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004063 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4064 uint32_t offset =
4065 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004066 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004067 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004068 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004069 }
Vladimir Marko58155012015-08-19 12:49:41 +00004070 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004071 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004072 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004073 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4074 DCHECK(GetCompilerOptions().IsBootImage());
4075 // Add ADRP with its PC-relative method patch.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004076 vixl::aarch64::Label* adrp_label = NewBootImageMethodPatch(invoke->GetTargetMethod());
Vladimir Marko65979462017-05-19 17:25:12 +01004077 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4078 // Add ADD with its PC-relative method patch.
4079 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004080 NewBootImageMethodPatch(invoke->GetTargetMethod(), adrp_label);
Vladimir Marko65979462017-05-19 17:25:12 +01004081 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4082 break;
4083 }
Vladimir Markob066d432018-01-03 13:14:37 +00004084 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4085 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004086 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00004087 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_offset);
4088 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4089 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
4090 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_offset, adrp_label);
4091 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
4092 EmitLdrOffsetPlaceholder(ldr_label, WRegisterFrom(temp), XRegisterFrom(temp));
4093 break;
4094 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004095 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Markob066d432018-01-03 13:14:37 +00004096 // Add ADRP with its PC-relative .bss entry patch.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004097 MethodReference target_method(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4098 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(target_method);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004099 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Markob066d432018-01-03 13:14:37 +00004100 // Add LDR with its PC-relative .bss entry patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004101 vixl::aarch64::Label* ldr_label =
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004102 NewMethodBssEntryPatch(target_method, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004103 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004104 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004105 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004106 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
4107 // Load method address from literal pool.
4108 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
4109 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004110 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4111 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4112 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004113 }
4114 }
4115
4116 switch (invoke->GetCodePtrLocation()) {
4117 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004118 {
4119 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4120 ExactAssemblyScope eas(GetVIXLAssembler(),
4121 kInstructionSize,
4122 CodeBufferCheckScope::kExactSize);
4123 __ bl(&frame_entry_label_);
4124 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4125 }
Vladimir Marko58155012015-08-19 12:49:41 +00004126 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004127 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4128 // LR = callee_method->entry_point_from_quick_compiled_code_;
4129 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004130 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004131 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004132 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004133 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004134 ExactAssemblyScope eas(GetVIXLAssembler(),
4135 kInstructionSize,
4136 CodeBufferCheckScope::kExactSize);
4137 // lr()
4138 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004139 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004140 }
Vladimir Marko58155012015-08-19 12:49:41 +00004141 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004142 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004143
Andreas Gampe878d58c2015-01-15 23:24:00 -08004144 DCHECK(!IsLeafMethod());
4145}
4146
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004147void CodeGeneratorARM64::GenerateVirtualCall(
4148 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004149 // Use the calling convention instead of the location of the receiver, as
4150 // intrinsics may have put the receiver in a different register. In the intrinsics
4151 // slow path, the arguments have been moved to the right place, so here we are
4152 // guaranteed that the receiver is the first register of the calling convention.
4153 InvokeDexCallingConvention calling_convention;
4154 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004155 Register temp = XRegisterFrom(temp_in);
4156 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4157 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4158 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004159 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004160
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004161 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004162
4163 {
4164 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4165 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4166 // /* HeapReference<Class> */ temp = receiver->klass_
4167 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4168 MaybeRecordImplicitNullCheck(invoke);
4169 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004170 // Instead of simply (possibly) unpoisoning `temp` here, we should
4171 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004172 // intermediate/temporary reference and because the current
4173 // concurrent copying collector keeps the from-space memory
4174 // intact/accessible until the end of the marking phase (the
4175 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004176 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4177 // temp = temp->GetMethodAt(method_offset);
4178 __ Ldr(temp, MemOperand(temp, method_offset));
4179 // lr = temp->GetEntryPoint();
4180 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004181 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004182 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004183 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4184 // lr();
4185 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004186 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004187 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004188}
4189
Orion Hodsonac141392017-01-13 11:53:47 +00004190void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4191 HandleInvoke(invoke);
4192}
4193
4194void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4195 codegen_->GenerateInvokePolymorphicCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004196 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004197}
4198
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004199void LocationsBuilderARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4200 HandleInvoke(invoke);
4201}
4202
4203void InstructionCodeGeneratorARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4204 codegen_->GenerateInvokeCustomCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004205 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004206}
4207
Vladimir Marko6fd16062018-06-26 11:02:04 +01004208vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageIntrinsicPatch(
4209 uint32_t intrinsic_data,
4210 vixl::aarch64::Label* adrp_label) {
4211 return NewPcRelativePatch(
Andreas Gampe3db70682018-12-26 15:12:03 -08004212 /* dex_file= */ nullptr, intrinsic_data, adrp_label, &boot_image_intrinsic_patches_);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004213}
4214
Vladimir Markob066d432018-01-03 13:14:37 +00004215vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageRelRoPatch(
4216 uint32_t boot_image_offset,
4217 vixl::aarch64::Label* adrp_label) {
4218 return NewPcRelativePatch(
Andreas Gampe3db70682018-12-26 15:12:03 -08004219 /* dex_file= */ nullptr, boot_image_offset, adrp_label, &boot_image_method_patches_);
Vladimir Markob066d432018-01-03 13:14:37 +00004220}
4221
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004222vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004223 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004224 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004225 return NewPcRelativePatch(
4226 target_method.dex_file, target_method.index, adrp_label, &boot_image_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004227}
4228
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004229vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4230 MethodReference target_method,
4231 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004232 return NewPcRelativePatch(
4233 target_method.dex_file, target_method.index, adrp_label, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004234}
4235
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004236vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageTypePatch(
Scott Wakeling97c72b72016-06-24 16:19:36 +01004237 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004238 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004239 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004240 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &boot_image_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004241}
4242
Vladimir Marko1998cd02017-01-13 13:02:58 +00004243vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4244 const DexFile& dex_file,
4245 dex::TypeIndex type_index,
4246 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004247 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004248}
4249
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004250vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004251 const DexFile& dex_file,
4252 dex::StringIndex string_index,
4253 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004254 return NewPcRelativePatch(
4255 &dex_file, string_index.index_, adrp_label, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01004256}
4257
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004258vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4259 const DexFile& dex_file,
4260 dex::StringIndex string_index,
4261 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004262 return NewPcRelativePatch(&dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004263}
4264
Vladimir Markof6675082019-05-17 12:05:28 +01004265void CodeGeneratorARM64::EmitEntrypointThunkCall(ThreadOffset64 entrypoint_offset) {
4266 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
4267 DCHECK(!Runtime::Current()->UseJitCompilation());
4268 call_entrypoint_patches_.emplace_back(/*dex_file*/ nullptr, entrypoint_offset.Uint32Value());
4269 vixl::aarch64::Label* bl_label = &call_entrypoint_patches_.back().label;
4270 __ bind(bl_label);
4271 __ bl(static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4272}
4273
Vladimir Marko966b46f2018-08-03 10:20:19 +00004274void CodeGeneratorARM64::EmitBakerReadBarrierCbnz(uint32_t custom_data) {
Vladimir Marko94796f82018-08-08 15:15:33 +01004275 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko966b46f2018-08-03 10:20:19 +00004276 if (Runtime::Current()->UseJitCompilation()) {
4277 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
4278 vixl::aarch64::Label* slow_path_entry = &it->second.label;
4279 __ cbnz(mr, slow_path_entry);
4280 } else {
4281 baker_read_barrier_patches_.emplace_back(custom_data);
4282 vixl::aarch64::Label* cbnz_label = &baker_read_barrier_patches_.back().label;
4283 __ bind(cbnz_label);
4284 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4285 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004286}
4287
Scott Wakeling97c72b72016-06-24 16:19:36 +01004288vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004289 const DexFile* dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004290 uint32_t offset_or_index,
4291 vixl::aarch64::Label* adrp_label,
4292 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004293 // Add a patch entry and return the label.
4294 patches->emplace_back(dex_file, offset_or_index);
4295 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004296 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004297 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4298 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4299 return label;
4300}
4301
Scott Wakeling97c72b72016-06-24 16:19:36 +01004302vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4303 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004304 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004305}
4306
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004307vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004308 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004309 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004310 return jit_string_patches_.GetOrCreate(
4311 StringReference(&dex_file, string_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004312 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004313}
4314
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004315vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004316 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004317 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004318 return jit_class_patches_.GetOrCreate(
4319 TypeReference(&dex_file, type_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004320 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004321}
4322
Vladimir Markoaad75c62016-10-03 08:46:48 +00004323void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4324 vixl::aarch64::Register reg) {
4325 DCHECK(reg.IsX());
4326 SingleEmissionCheckScope guard(GetVIXLAssembler());
4327 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004328 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004329}
4330
4331void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4332 vixl::aarch64::Register out,
4333 vixl::aarch64::Register base) {
4334 DCHECK(out.IsX());
4335 DCHECK(base.IsX());
4336 SingleEmissionCheckScope guard(GetVIXLAssembler());
4337 __ Bind(fixup_label);
4338 __ add(out, base, Operand(/* offset placeholder */ 0));
4339}
4340
4341void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4342 vixl::aarch64::Register out,
4343 vixl::aarch64::Register base) {
4344 DCHECK(base.IsX());
4345 SingleEmissionCheckScope guard(GetVIXLAssembler());
4346 __ Bind(fixup_label);
4347 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4348}
4349
Vladimir Markoeebb8212018-06-05 14:57:24 +01004350void CodeGeneratorARM64::LoadBootImageAddress(vixl::aarch64::Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01004351 uint32_t boot_image_reference) {
4352 if (GetCompilerOptions().IsBootImage()) {
4353 // Add ADRP with its PC-relative type patch.
4354 vixl::aarch64::Label* adrp_label = NewBootImageIntrinsicPatch(boot_image_reference);
4355 EmitAdrpPlaceholder(adrp_label, reg.X());
4356 // Add ADD with its PC-relative type patch.
4357 vixl::aarch64::Label* add_label = NewBootImageIntrinsicPatch(boot_image_reference, adrp_label);
4358 EmitAddPlaceholder(add_label, reg.X(), reg.X());
Vladimir Markoa2da9b92018-10-10 14:21:55 +01004359 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01004360 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004361 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004362 EmitAdrpPlaceholder(adrp_label, reg.X());
4363 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004364 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_reference, adrp_label);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004365 EmitLdrOffsetPlaceholder(ldr_label, reg.W(), reg.X());
4366 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004367 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01004368 gc::Heap* heap = Runtime::Current()->GetHeap();
4369 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004370 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01004371 __ Ldr(reg.W(), DeduplicateBootImageAddressLiteral(reinterpret_cast<uintptr_t>(address)));
4372 }
4373}
4374
Vladimir Marko6fd16062018-06-26 11:02:04 +01004375void CodeGeneratorARM64::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
4376 uint32_t boot_image_offset) {
4377 DCHECK(invoke->IsStatic());
4378 InvokeRuntimeCallingConvention calling_convention;
4379 Register argument = calling_convention.GetRegisterAt(0);
4380 if (GetCompilerOptions().IsBootImage()) {
4381 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
4382 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
4383 MethodReference target_method = invoke->GetTargetMethod();
4384 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
4385 // Add ADRP with its PC-relative type patch.
4386 vixl::aarch64::Label* adrp_label = NewBootImageTypePatch(*target_method.dex_file, type_idx);
4387 EmitAdrpPlaceholder(adrp_label, argument.X());
4388 // Add ADD with its PC-relative type patch.
4389 vixl::aarch64::Label* add_label =
4390 NewBootImageTypePatch(*target_method.dex_file, type_idx, adrp_label);
4391 EmitAddPlaceholder(add_label, argument.X(), argument.X());
4392 } else {
4393 LoadBootImageAddress(argument, boot_image_offset);
4394 }
4395 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
4396 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4397}
4398
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004399template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004400inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4401 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004402 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004403 for (const PcRelativePatchInfo& info : infos) {
4404 linker_patches->push_back(Factory(info.label.GetLocation(),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004405 info.target_dex_file,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004406 info.pc_insn_label->GetLocation(),
4407 info.offset_or_index));
4408 }
4409}
4410
Vladimir Marko6fd16062018-06-26 11:02:04 +01004411template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
4412linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
4413 const DexFile* target_dex_file,
4414 uint32_t pc_insn_offset,
4415 uint32_t boot_image_offset) {
4416 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
4417 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00004418}
4419
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004420void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004421 DCHECK(linker_patches->empty());
4422 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004423 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004424 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004425 boot_image_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004426 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004427 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004428 string_bss_entry_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01004429 boot_image_intrinsic_patches_.size() +
Vladimir Markof6675082019-05-17 12:05:28 +01004430 call_entrypoint_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004431 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004432 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01004433 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004434 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004435 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004436 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004437 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004438 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004439 boot_image_string_patches_, linker_patches);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004440 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
4441 boot_image_intrinsic_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01004442 } else {
Vladimir Marko6fd16062018-06-26 11:02:04 +01004443 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
Vladimir Markob066d432018-01-03 13:14:37 +00004444 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004445 DCHECK(boot_image_type_patches_.empty());
4446 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004447 DCHECK(boot_image_intrinsic_patches_.empty());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004448 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004449 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4450 method_bss_entry_patches_, linker_patches);
4451 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4452 type_bss_entry_patches_, linker_patches);
4453 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4454 string_bss_entry_patches_, linker_patches);
Vladimir Markof6675082019-05-17 12:05:28 +01004455 for (const PatchInfo<vixl::aarch64::Label>& info : call_entrypoint_patches_) {
4456 DCHECK(info.target_dex_file == nullptr);
4457 linker_patches->push_back(linker::LinkerPatch::CallEntrypointPatch(
4458 info.label.GetLocation(), info.offset_or_index));
4459 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004460 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004461 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
4462 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004463 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004464 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004465}
4466
Vladimir Markoca1e0382018-04-11 09:58:41 +00004467bool CodeGeneratorARM64::NeedsThunkCode(const linker::LinkerPatch& patch) const {
Vladimir Markof6675082019-05-17 12:05:28 +01004468 return patch.GetType() == linker::LinkerPatch::Type::kCallEntrypoint ||
4469 patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
Vladimir Markoca1e0382018-04-11 09:58:41 +00004470 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
4471}
4472
4473void CodeGeneratorARM64::EmitThunkCode(const linker::LinkerPatch& patch,
4474 /*out*/ ArenaVector<uint8_t>* code,
4475 /*out*/ std::string* debug_name) {
4476 Arm64Assembler assembler(GetGraph()->GetAllocator());
4477 switch (patch.GetType()) {
4478 case linker::LinkerPatch::Type::kCallRelative: {
4479 // The thunk just uses the entry point in the ArtMethod. This works even for calls
4480 // to the generic JNI and interpreter trampolines.
4481 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4482 kArm64PointerSize).Int32Value());
4483 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
4484 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
4485 *debug_name = "MethodCallThunk";
4486 }
4487 break;
4488 }
Vladimir Markof6675082019-05-17 12:05:28 +01004489 case linker::LinkerPatch::Type::kCallEntrypoint: {
4490 Offset offset(patch.EntrypointOffset());
4491 assembler.JumpTo(ManagedRegister(arm64::TR), offset, ManagedRegister(arm64::IP0));
4492 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
4493 *debug_name = "EntrypointCallThunk_" + std::to_string(offset.Uint32Value());
4494 }
4495 break;
4496 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00004497 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
4498 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
4499 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
4500 break;
4501 }
4502 default:
4503 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
4504 UNREACHABLE();
4505 }
4506
4507 // Ensure we emit the literal pool if any.
4508 assembler.FinalizeCode();
4509 code->resize(assembler.CodeSize());
4510 MemoryRegion code_region(code->data(), code->size());
4511 assembler.FinalizeInstructions(code_region);
4512}
4513
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004514vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
4515 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004516 value,
4517 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4518}
4519
Scott Wakeling97c72b72016-06-24 16:19:36 +01004520vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004521 return uint64_literals_.GetOrCreate(
4522 value,
4523 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004524}
4525
Andreas Gampe878d58c2015-01-15 23:24:00 -08004526void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004527 // Explicit clinit checks triggered by static invokes must have been pruned by
4528 // art::PrepareForRegisterAllocation.
4529 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004530
Andreas Gampe878d58c2015-01-15 23:24:00 -08004531 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08004532 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004533 return;
4534 }
4535
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004536 {
4537 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4538 // are no pools emitted.
4539 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4540 LocationSummary* locations = invoke->GetLocations();
4541 codegen_->GenerateStaticOrDirectCall(
4542 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
4543 }
4544
Andreas Gampe3db70682018-12-26 15:12:03 -08004545 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004546}
4547
4548void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004549 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08004550 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004551 return;
4552 }
4553
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004554 {
4555 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4556 // are no pools emitted.
4557 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4558 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
4559 DCHECK(!codegen_->IsLeafMethod());
4560 }
4561
Andreas Gampe3db70682018-12-26 15:12:03 -08004562 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004563}
4564
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004565HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4566 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004567 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004568 case HLoadClass::LoadKind::kInvalid:
4569 LOG(FATAL) << "UNREACHABLE";
4570 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004571 case HLoadClass::LoadKind::kReferrersClass:
4572 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004573 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004574 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004575 case HLoadClass::LoadKind::kBssEntry:
4576 DCHECK(!Runtime::Current()->UseJitCompilation());
4577 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004578 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004579 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004580 DCHECK(Runtime::Current()->UseJitCompilation());
4581 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004582 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004583 break;
4584 }
4585 return desired_class_load_kind;
4586}
4587
Alexandre Rames67555f72014-11-18 10:55:16 +00004588void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004589 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004590 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004591 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004592 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004593 cls,
4594 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004595 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004596 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004597 return;
4598 }
Vladimir Marko41559982017-01-06 14:04:23 +00004599 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004600
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004601 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4602 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004603 ? LocationSummary::kCallOnSlowPath
4604 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004605 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004606 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004607 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004608 }
4609
Vladimir Marko41559982017-01-06 14:04:23 +00004610 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004611 locations->SetInAt(0, Location::RequiresRegister());
4612 }
4613 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004614 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4615 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4616 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01004617 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004618 } else {
4619 // For non-Baker read barrier we have a temp-clobbering call.
4620 }
4621 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004622}
4623
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004624// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4625// move.
4626void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004627 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004628 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00004629 codegen_->GenerateLoadClassRuntimeCall(cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08004630 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01004631 return;
4632 }
Vladimir Marko41559982017-01-06 14:04:23 +00004633 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004634
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004635 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004636 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004637
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004638 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4639 ? kWithoutReadBarrier
4640 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004641 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004642 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004643 case HLoadClass::LoadKind::kReferrersClass: {
4644 DCHECK(!cls->CanCallRuntime());
4645 DCHECK(!cls->MustGenerateClinitCheck());
4646 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4647 Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00004648 codegen_->GenerateGcRootFieldLoad(cls,
4649 out_loc,
4650 current_method,
4651 ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3db70682018-12-26 15:12:03 -08004652 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00004653 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004654 break;
4655 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004656 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004657 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004658 // Add ADRP with its PC-relative type patch.
4659 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004660 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004661 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004662 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004663 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004664 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004665 codegen_->NewBootImageTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004666 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004667 break;
4668 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004669 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004670 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004671 uint32_t boot_image_offset = codegen_->GetBootImageOffset(cls);
4672 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
4673 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004674 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004675 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004676 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004677 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004678 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004679 break;
4680 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004681 case HLoadClass::LoadKind::kBssEntry: {
4682 // Add ADRP with its PC-relative Class .bss entry patch.
4683 const DexFile& dex_file = cls->GetDexFile();
4684 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00004685 vixl::aarch64::Register temp = XRegisterFrom(out_loc);
4686 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4687 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004688 // Add LDR with its PC-relative Class .bss entry patch.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004689 vixl::aarch64::Label* ldr_label =
Vladimir Markof3c52b42017-11-17 17:32:12 +00004690 codegen_->NewBssEntryTypePatch(dex_file, type_index, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004691 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoca1e0382018-04-11 09:58:41 +00004692 codegen_->GenerateGcRootFieldLoad(cls,
4693 out_loc,
4694 temp,
4695 /* offset placeholder */ 0u,
4696 ldr_label,
4697 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004698 generate_null_check = true;
4699 break;
4700 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004701 case HLoadClass::LoadKind::kJitBootImageAddress: {
4702 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
4703 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
4704 DCHECK_NE(address, 0u);
4705 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
4706 break;
4707 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004708 case HLoadClass::LoadKind::kJitTableAddress: {
4709 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4710 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004711 cls->GetClass()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00004712 codegen_->GenerateGcRootFieldLoad(cls,
4713 out_loc,
4714 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08004715 /* offset= */ 0,
4716 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00004717 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004718 break;
4719 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004720 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004721 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004722 LOG(FATAL) << "UNREACHABLE";
4723 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004724 }
4725
Vladimir Markoea4c1262017-02-06 19:59:33 +00004726 bool do_clinit = cls->MustGenerateClinitCheck();
4727 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004728 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01004729 SlowPathCodeARM64* slow_path =
4730 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004731 codegen_->AddSlowPath(slow_path);
4732 if (generate_null_check) {
4733 __ Cbz(out, slow_path->GetEntryLabel());
4734 }
4735 if (cls->MustGenerateClinitCheck()) {
4736 GenerateClassInitializationCheck(slow_path, out);
4737 } else {
4738 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004739 }
Andreas Gampe3db70682018-12-26 15:12:03 -08004740 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004741 }
4742}
4743
Orion Hodsondbaa5c72018-05-10 08:22:46 +01004744void LocationsBuilderARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
4745 InvokeRuntimeCallingConvention calling_convention;
4746 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
4747 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
4748}
4749
4750void InstructionCodeGeneratorARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
4751 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
4752}
4753
Orion Hodson18259d72018-04-12 11:18:23 +01004754void LocationsBuilderARM64::VisitLoadMethodType(HLoadMethodType* load) {
4755 InvokeRuntimeCallingConvention calling_convention;
4756 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
4757 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
4758}
4759
4760void InstructionCodeGeneratorARM64::VisitLoadMethodType(HLoadMethodType* load) {
4761 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
4762}
4763
David Brazdilcb1c0552015-08-04 16:22:25 +01004764static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004765 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004766}
4767
Alexandre Rames67555f72014-11-18 10:55:16 +00004768void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4769 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004770 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00004771 locations->SetOut(Location::RequiresRegister());
4772}
4773
4774void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004775 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4776}
4777
4778void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004779 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01004780}
4781
4782void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4783 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004784}
4785
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004786HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4787 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004788 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004789 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004790 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00004791 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004792 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004793 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004794 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004795 case HLoadString::LoadKind::kJitTableAddress:
4796 DCHECK(Runtime::Current()->UseJitCompilation());
4797 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004798 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004799 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004800 }
4801 return desired_string_load_kind;
4802}
4803
Alexandre Rames67555f72014-11-18 10:55:16 +00004804void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004805 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004806 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004807 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004808 InvokeRuntimeCallingConvention calling_convention;
4809 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4810 } else {
4811 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004812 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4813 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00004814 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01004815 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004816 } else {
4817 // For non-Baker read barrier we have a temp-clobbering call.
4818 }
4819 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004820 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004821}
4822
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004823// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4824// move.
4825void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00004826 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004827 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004828
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004829 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004830 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004831 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004832 // Add ADRP with its PC-relative String patch.
4833 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004834 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004835 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004836 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004837 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004838 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004839 codegen_->NewBootImageStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004840 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004841 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004842 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004843 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004844 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004845 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
4846 uint32_t boot_image_offset = codegen_->GetBootImageOffset(load);
4847 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004848 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004849 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004850 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004851 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004852 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
4853 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004854 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004855 case HLoadString::LoadKind::kBssEntry: {
4856 // Add ADRP with its PC-relative String .bss entry patch.
4857 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004858 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00004859 Register temp = XRegisterFrom(out_loc);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004860 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004861 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004862 // Add LDR with its PC-relative String .bss entry patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004863 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004864 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004865 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoca1e0382018-04-11 09:58:41 +00004866 codegen_->GenerateGcRootFieldLoad(load,
4867 out_loc,
4868 temp,
4869 /* offset placeholder */ 0u,
4870 ldr_label,
4871 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004872 SlowPathCodeARM64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00004873 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004874 codegen_->AddSlowPath(slow_path);
4875 __ Cbz(out.X(), slow_path->GetEntryLabel());
4876 __ Bind(slow_path->GetExitLabel());
Andreas Gampe3db70682018-12-26 15:12:03 -08004877 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004878 return;
4879 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004880 case HLoadString::LoadKind::kJitBootImageAddress: {
4881 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
4882 DCHECK_NE(address, 0u);
4883 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
4884 return;
4885 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004886 case HLoadString::LoadKind::kJitTableAddress: {
4887 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004888 load->GetStringIndex(),
4889 load->GetString()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00004890 codegen_->GenerateGcRootFieldLoad(load,
4891 out_loc,
4892 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08004893 /* offset= */ 0,
4894 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00004895 kCompilerReadBarrierOption);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004896 return;
4897 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004898 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004899 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004900 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004901
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004902 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004903 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004904 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004905 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004906 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4907 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08004908 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004909}
4910
Alexandre Rames5319def2014-10-23 10:03:10 +01004911void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004912 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004913 locations->SetOut(Location::ConstantLocation(constant));
4914}
4915
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004916void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004917 // Will be generated at use site.
4918}
4919
Alexandre Rames67555f72014-11-18 10:55:16 +00004920void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004921 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4922 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004923 InvokeRuntimeCallingConvention calling_convention;
4924 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4925}
4926
4927void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004928 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004929 instruction,
4930 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004931 if (instruction->IsEnter()) {
4932 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4933 } else {
4934 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4935 }
Andreas Gampe3db70682018-12-26 15:12:03 -08004936 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004937}
4938
Alexandre Rames42d641b2014-10-27 14:00:51 +00004939void LocationsBuilderARM64::VisitMul(HMul* mul) {
4940 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004941 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004942 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004943 case DataType::Type::kInt32:
4944 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00004945 locations->SetInAt(0, Location::RequiresRegister());
4946 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004947 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004948 break;
4949
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004950 case DataType::Type::kFloat32:
4951 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004952 locations->SetInAt(0, Location::RequiresFpuRegister());
4953 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004954 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004955 break;
4956
4957 default:
4958 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4959 }
4960}
4961
4962void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4963 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004964 case DataType::Type::kInt32:
4965 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00004966 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4967 break;
4968
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004969 case DataType::Type::kFloat32:
4970 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004971 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004972 break;
4973
4974 default:
4975 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4976 }
4977}
4978
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004979void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4980 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004981 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004982 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004983 case DataType::Type::kInt32:
4984 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004985 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004986 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004987 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004988
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004989 case DataType::Type::kFloat32:
4990 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00004991 locations->SetInAt(0, Location::RequiresFpuRegister());
4992 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004993 break;
4994
4995 default:
4996 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4997 }
4998}
4999
5000void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5001 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005002 case DataType::Type::kInt32:
5003 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005004 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5005 break;
5006
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005007 case DataType::Type::kFloat32:
5008 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005009 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005010 break;
5011
5012 default:
5013 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5014 }
5015}
5016
5017void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005018 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5019 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005020 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005021 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005022 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5023 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005024}
5025
5026void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005027 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5028 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005029 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005030 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005031 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005032}
5033
Alexandre Rames5319def2014-10-23 10:03:10 +01005034void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005035 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5036 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005037 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07005038 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005039 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005040}
5041
5042void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005043 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5044 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005045 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005046}
5047
5048void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005049 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005050 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005051 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005052}
5053
5054void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005055 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005056 case DataType::Type::kInt32:
5057 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005058 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005059 break;
5060
5061 default:
5062 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5063 }
5064}
5065
David Brazdil66d126e2015-04-03 16:02:44 +01005066void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005067 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005068 locations->SetInAt(0, Location::RequiresRegister());
5069 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5070}
5071
5072void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005073 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005074}
5075
Alexandre Rames5319def2014-10-23 10:03:10 +01005076void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005077 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5078 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005079}
5080
Calin Juravle2ae48182016-03-16 14:05:09 +00005081void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5082 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005083 return;
5084 }
Artem Serov914d7a82017-02-07 14:33:49 +00005085 {
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01005086 // Ensure that between load and RecordPcInfo there are no pools emitted.
Artem Serov914d7a82017-02-07 14:33:49 +00005087 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5088 Location obj = instruction->GetLocations()->InAt(0);
5089 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5090 RecordPcInfo(instruction, instruction->GetDexPc());
5091 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005092}
5093
Calin Juravle2ae48182016-03-16 14:05:09 +00005094void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005095 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005096 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005097
5098 LocationSummary* locations = instruction->GetLocations();
5099 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005100
5101 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005102}
5103
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005104void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005105 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005106}
5107
Alexandre Rames67555f72014-11-18 10:55:16 +00005108void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5109 HandleBinaryOp(instruction);
5110}
5111
5112void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5113 HandleBinaryOp(instruction);
5114}
5115
Alexandre Rames3e69f162014-12-10 10:36:50 +00005116void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5117 LOG(FATAL) << "Unreachable";
5118}
5119
5120void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005121 if (instruction->GetNext()->IsSuspendCheck() &&
5122 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5123 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5124 // The back edge will generate the suspend check.
5125 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5126 }
5127
Alexandre Rames3e69f162014-12-10 10:36:50 +00005128 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5129}
5130
Alexandre Rames5319def2014-10-23 10:03:10 +01005131void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005132 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005133 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5134 if (location.IsStackSlot()) {
5135 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5136 } else if (location.IsDoubleStackSlot()) {
5137 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5138 }
5139 locations->SetOut(location);
5140}
5141
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005142void InstructionCodeGeneratorARM64::VisitParameterValue(
5143 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005144 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005145}
5146
5147void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5148 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005149 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005150 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005151}
5152
5153void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5154 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5155 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005156}
5157
5158void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005159 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005160 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005161 locations->SetInAt(i, Location::Any());
5162 }
5163 locations->SetOut(Location::Any());
5164}
5165
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005166void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005167 LOG(FATAL) << "Unreachable";
5168}
5169
Serban Constantinescu02164b32014-11-13 14:05:07 +00005170void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005171 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005172 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005173 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005174 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005175 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005176
5177 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005178 case DataType::Type::kInt32:
5179 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005180 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005181 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005182 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5183 break;
5184
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005185 case DataType::Type::kFloat32:
5186 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005187 InvokeRuntimeCallingConvention calling_convention;
5188 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5189 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5190 locations->SetOut(calling_convention.GetReturnLocation(type));
5191
5192 break;
5193 }
5194
Serban Constantinescu02164b32014-11-13 14:05:07 +00005195 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005196 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005197 }
5198}
5199
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005200void InstructionCodeGeneratorARM64::GenerateIntRemForPower2Denom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005201 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005202 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
5203 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
5204
5205 Register out = OutputRegister(instruction);
5206 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005207
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005208 if (abs_imm == 2) {
5209 __ Cmp(dividend, 0);
5210 __ And(out, dividend, 1);
5211 __ Csneg(out, out, out, ge);
5212 } else {
5213 UseScratchRegisterScope temps(GetVIXLAssembler());
5214 Register temp = temps.AcquireSameSizeAs(out);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005215
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005216 __ Negs(temp, dividend);
5217 __ And(out, dividend, abs_imm - 1);
5218 __ And(temp, temp, abs_imm - 1);
5219 __ Csneg(out, out, temp, mi);
5220 }
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005221}
5222
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005223void InstructionCodeGeneratorARM64::GenerateIntRemForConstDenom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005224 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005225
5226 if (imm == 0) {
5227 // Do not generate anything.
5228 // DivZeroCheck would prevent any code to be executed.
5229 return;
5230 }
5231
Evgeny Astigeevichf58dc652018-06-25 17:54:07 +01005232 if (IsPowerOfTwo(AbsOrMin(imm))) {
5233 // Cases imm == -1 or imm == 1 are handled in constant folding by
5234 // InstructionWithAbsorbingInputSimplifier.
5235 // If the cases have survided till code generation they are handled in
5236 // GenerateIntRemForPower2Denom becauses -1 and 1 are the power of 2 (2^0).
5237 // The correct code is generated for them, just more instructions.
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005238 GenerateIntRemForPower2Denom(instruction);
5239 } else {
5240 DCHECK(imm < -2 || imm > 2) << imm;
5241 GenerateDivRemWithAnyConstant(instruction);
5242 }
5243}
5244
5245void InstructionCodeGeneratorARM64::GenerateIntRem(HRem* instruction) {
5246 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
5247 << instruction->GetResultType();
5248
5249 if (instruction->GetLocations()->InAt(1).IsConstant()) {
5250 GenerateIntRemForConstDenom(instruction);
5251 } else {
5252 Register out = OutputRegister(instruction);
5253 Register dividend = InputRegisterAt(instruction, 0);
5254 Register divisor = InputRegisterAt(instruction, 1);
5255 UseScratchRegisterScope temps(GetVIXLAssembler());
5256 Register temp = temps.AcquireSameSizeAs(out);
5257 __ Sdiv(temp, dividend, divisor);
5258 __ Msub(out, temp, divisor, dividend);
5259 }
5260}
5261
Serban Constantinescu02164b32014-11-13 14:05:07 +00005262void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005263 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005264
Serban Constantinescu02164b32014-11-13 14:05:07 +00005265 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005266 case DataType::Type::kInt32:
5267 case DataType::Type::kInt64: {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005268 GenerateIntRem(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005269 break;
5270 }
5271
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005272 case DataType::Type::kFloat32:
5273 case DataType::Type::kFloat64: {
5274 QuickEntrypointEnum entrypoint =
5275 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005276 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005277 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00005278 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5279 } else {
5280 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5281 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005282 break;
5283 }
5284
Serban Constantinescu02164b32014-11-13 14:05:07 +00005285 default:
5286 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005287 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005288 }
5289}
5290
Aart Bik1f8d51b2018-02-15 10:42:37 -08005291void LocationsBuilderARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005292 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005293}
5294
Aart Bik1f8d51b2018-02-15 10:42:37 -08005295void InstructionCodeGeneratorARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005296 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005297}
5298
5299void LocationsBuilderARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005300 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005301}
5302
5303void InstructionCodeGeneratorARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005304 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005305}
5306
Aart Bik3dad3412018-02-28 12:01:46 -08005307void LocationsBuilderARM64::VisitAbs(HAbs* abs) {
5308 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
5309 switch (abs->GetResultType()) {
5310 case DataType::Type::kInt32:
5311 case DataType::Type::kInt64:
5312 locations->SetInAt(0, Location::RequiresRegister());
5313 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5314 break;
5315 case DataType::Type::kFloat32:
5316 case DataType::Type::kFloat64:
5317 locations->SetInAt(0, Location::RequiresFpuRegister());
5318 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5319 break;
5320 default:
5321 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5322 }
5323}
5324
5325void InstructionCodeGeneratorARM64::VisitAbs(HAbs* abs) {
5326 switch (abs->GetResultType()) {
5327 case DataType::Type::kInt32:
5328 case DataType::Type::kInt64: {
5329 Register in_reg = InputRegisterAt(abs, 0);
5330 Register out_reg = OutputRegister(abs);
5331 __ Cmp(in_reg, Operand(0));
5332 __ Cneg(out_reg, in_reg, lt);
5333 break;
5334 }
5335 case DataType::Type::kFloat32:
5336 case DataType::Type::kFloat64: {
5337 FPRegister in_reg = InputFPRegisterAt(abs, 0);
5338 FPRegister out_reg = OutputFPRegister(abs);
5339 __ Fabs(out_reg, in_reg);
5340 break;
5341 }
5342 default:
5343 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5344 }
5345}
5346
Igor Murashkind01745e2017-04-05 16:40:31 -07005347void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5348 constructor_fence->SetLocations(nullptr);
5349}
5350
5351void InstructionCodeGeneratorARM64::VisitConstructorFence(
5352 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5353 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5354}
5355
Calin Juravle27df7582015-04-17 19:12:31 +01005356void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5357 memory_barrier->SetLocations(nullptr);
5358}
5359
5360void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005361 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005362}
5363
Alexandre Rames5319def2014-10-23 10:03:10 +01005364void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005365 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005366 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005367 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005368}
5369
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005370void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005371 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005372}
5373
5374void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5375 instruction->SetLocations(nullptr);
5376}
5377
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005378void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005379 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005380}
5381
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005382void LocationsBuilderARM64::VisitRor(HRor* ror) {
5383 HandleBinaryOp(ror);
5384}
5385
5386void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5387 HandleBinaryOp(ror);
5388}
5389
Serban Constantinescu02164b32014-11-13 14:05:07 +00005390void LocationsBuilderARM64::VisitShl(HShl* shl) {
5391 HandleShift(shl);
5392}
5393
5394void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5395 HandleShift(shl);
5396}
5397
5398void LocationsBuilderARM64::VisitShr(HShr* shr) {
5399 HandleShift(shr);
5400}
5401
5402void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5403 HandleShift(shr);
5404}
5405
Alexandre Rames5319def2014-10-23 10:03:10 +01005406void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005407 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005408}
5409
5410void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005411 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005412}
5413
Alexandre Rames67555f72014-11-18 10:55:16 +00005414void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005415 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005416}
5417
5418void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005419 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005420}
5421
5422void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005423 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005424}
5425
Alexandre Rames67555f72014-11-18 10:55:16 +00005426void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005427 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005428}
5429
Vladimir Marko552a1342017-10-31 10:56:47 +00005430void LocationsBuilderARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5431 codegen_->CreateStringBuilderAppendLocations(instruction, LocationFrom(x0));
5432}
5433
5434void InstructionCodeGeneratorARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5435 __ Mov(w0, instruction->GetFormat()->GetValue());
5436 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
5437}
5438
Calin Juravlee460d1d2015-09-29 04:52:17 +01005439void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5440 HUnresolvedInstanceFieldGet* instruction) {
5441 FieldAccessCallingConventionARM64 calling_convention;
5442 codegen_->CreateUnresolvedFieldLocationSummary(
5443 instruction, instruction->GetFieldType(), calling_convention);
5444}
5445
5446void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5447 HUnresolvedInstanceFieldGet* instruction) {
5448 FieldAccessCallingConventionARM64 calling_convention;
5449 codegen_->GenerateUnresolvedFieldAccess(instruction,
5450 instruction->GetFieldType(),
5451 instruction->GetFieldIndex(),
5452 instruction->GetDexPc(),
5453 calling_convention);
5454}
5455
5456void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5457 HUnresolvedInstanceFieldSet* instruction) {
5458 FieldAccessCallingConventionARM64 calling_convention;
5459 codegen_->CreateUnresolvedFieldLocationSummary(
5460 instruction, instruction->GetFieldType(), calling_convention);
5461}
5462
5463void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5464 HUnresolvedInstanceFieldSet* instruction) {
5465 FieldAccessCallingConventionARM64 calling_convention;
5466 codegen_->GenerateUnresolvedFieldAccess(instruction,
5467 instruction->GetFieldType(),
5468 instruction->GetFieldIndex(),
5469 instruction->GetDexPc(),
5470 calling_convention);
5471}
5472
5473void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5474 HUnresolvedStaticFieldGet* instruction) {
5475 FieldAccessCallingConventionARM64 calling_convention;
5476 codegen_->CreateUnresolvedFieldLocationSummary(
5477 instruction, instruction->GetFieldType(), calling_convention);
5478}
5479
5480void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5481 HUnresolvedStaticFieldGet* instruction) {
5482 FieldAccessCallingConventionARM64 calling_convention;
5483 codegen_->GenerateUnresolvedFieldAccess(instruction,
5484 instruction->GetFieldType(),
5485 instruction->GetFieldIndex(),
5486 instruction->GetDexPc(),
5487 calling_convention);
5488}
5489
5490void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5491 HUnresolvedStaticFieldSet* instruction) {
5492 FieldAccessCallingConventionARM64 calling_convention;
5493 codegen_->CreateUnresolvedFieldLocationSummary(
5494 instruction, instruction->GetFieldType(), calling_convention);
5495}
5496
5497void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5498 HUnresolvedStaticFieldSet* instruction) {
5499 FieldAccessCallingConventionARM64 calling_convention;
5500 codegen_->GenerateUnresolvedFieldAccess(instruction,
5501 instruction->GetFieldType(),
5502 instruction->GetFieldIndex(),
5503 instruction->GetDexPc(),
5504 calling_convention);
5505}
5506
Alexandre Rames5319def2014-10-23 10:03:10 +01005507void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005508 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5509 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005510 // In suspend check slow path, usually there are no caller-save registers at all.
5511 // If SIMD instructions are present, however, we force spilling all live SIMD
5512 // registers in full width (since the runtime only saves/restores lower part).
5513 locations->SetCustomSlowPathCallerSaves(
5514 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005515}
5516
5517void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005518 HBasicBlock* block = instruction->GetBlock();
5519 if (block->GetLoopInformation() != nullptr) {
5520 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5521 // The back edge will generate the suspend check.
5522 return;
5523 }
5524 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5525 // The goto will generate the suspend check.
5526 return;
5527 }
5528 GenerateSuspendCheck(instruction, nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08005529 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005530}
5531
Alexandre Rames67555f72014-11-18 10:55:16 +00005532void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005533 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5534 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005535 InvokeRuntimeCallingConvention calling_convention;
5536 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5537}
5538
5539void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005540 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005541 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005542}
5543
5544void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5545 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005546 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005547 DataType::Type input_type = conversion->GetInputType();
5548 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005549 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5550 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005551 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
5552 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005553 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5554 }
5555
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005556 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005557 locations->SetInAt(0, Location::RequiresFpuRegister());
5558 } else {
5559 locations->SetInAt(0, Location::RequiresRegister());
5560 }
5561
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005562 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005563 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5564 } else {
5565 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5566 }
5567}
5568
5569void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005570 DataType::Type result_type = conversion->GetResultType();
5571 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00005572
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005573 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5574 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005575
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005576 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
5577 int result_size = DataType::Size(result_type);
5578 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005579 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005580 Register output = OutputRegister(conversion);
5581 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005582 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005583 // 'int' values are used directly as W registers, discarding the top
5584 // bits, so we don't need to sign-extend and can just perform a move.
5585 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5586 // top 32 bits of the target register. We theoretically could leave those
5587 // bits unchanged, but we would have to make sure that no code uses a
5588 // 32bit input value as a 64bit value assuming that the top 32 bits are
5589 // zero.
5590 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005591 } else if (DataType::IsUnsignedType(result_type) ||
5592 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
5593 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005594 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005595 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005596 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005597 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005598 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005599 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
5600 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005601 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005602 } else if (DataType::IsFloatingPointType(result_type) &&
5603 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005604 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5605 } else {
5606 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5607 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005608 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005609}
Alexandre Rames67555f72014-11-18 10:55:16 +00005610
Serban Constantinescu02164b32014-11-13 14:05:07 +00005611void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5612 HandleShift(ushr);
5613}
5614
5615void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5616 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005617}
5618
5619void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5620 HandleBinaryOp(instruction);
5621}
5622
5623void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5624 HandleBinaryOp(instruction);
5625}
5626
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005627void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005628 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005629 LOG(FATAL) << "Unreachable";
5630}
5631
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005632void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005633 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005634 LOG(FATAL) << "Unreachable";
5635}
5636
Mark Mendellfe57faa2015-09-18 09:26:15 -04005637// Simple implementation of packed switch - generate cascaded compare/jumps.
5638void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5639 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005640 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005641 locations->SetInAt(0, Location::RequiresRegister());
5642}
5643
5644void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5645 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005646 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005647 Register value_reg = InputRegisterAt(switch_instr, 0);
5648 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5649
Zheng Xu3927c8b2015-11-18 17:46:25 +08005650 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005651 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005652 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5653 // make sure we don't emit it if the target may run out of range.
5654 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5655 // ranges and emit the tables only as required.
5656 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005657
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005658 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005659 // Current instruction id is an upper bound of the number of HIRs in the graph.
5660 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5661 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005662 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5663 Register temp = temps.AcquireW();
5664 __ Subs(temp, value_reg, Operand(lower_bound));
5665
Zheng Xu3927c8b2015-11-18 17:46:25 +08005666 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005667 // Jump to successors[0] if value == lower_bound.
5668 __ B(eq, codegen_->GetLabelOf(successors[0]));
5669 int32_t last_index = 0;
5670 for (; num_entries - last_index > 2; last_index += 2) {
5671 __ Subs(temp, temp, Operand(2));
5672 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5673 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5674 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5675 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5676 }
5677 if (num_entries - last_index == 2) {
5678 // The last missing case_value.
5679 __ Cmp(temp, Operand(1));
5680 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005681 }
5682
5683 // And the default for any other value.
5684 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5685 __ B(codegen_->GetLabelOf(default_block));
5686 }
5687 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005688 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005689
5690 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5691
5692 // Below instructions should use at most one blocked register. Since there are two blocked
5693 // registers, we are free to block one.
5694 Register temp_w = temps.AcquireW();
5695 Register index;
5696 // Remove the bias.
5697 if (lower_bound != 0) {
5698 index = temp_w;
5699 __ Sub(index, value_reg, Operand(lower_bound));
5700 } else {
5701 index = value_reg;
5702 }
5703
5704 // Jump to default block if index is out of the range.
5705 __ Cmp(index, Operand(num_entries));
5706 __ B(hs, codegen_->GetLabelOf(default_block));
5707
5708 // In current VIXL implementation, it won't require any blocked registers to encode the
5709 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5710 // register pressure.
5711 Register table_base = temps.AcquireX();
5712 // Load jump offset from the table.
5713 __ Adr(table_base, jump_table->GetTableStartLabel());
5714 Register jump_offset = temp_w;
5715 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5716
5717 // Jump to target block by branching to table_base(pc related) + offset.
5718 Register target_address = table_base;
5719 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5720 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005721 }
5722}
5723
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005724void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5725 HInstruction* instruction,
5726 Location out,
5727 uint32_t offset,
5728 Location maybe_temp,
5729 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005730 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00005731 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005732 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005733 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005734 if (kUseBakerReadBarrier) {
5735 // Load with fast path based Baker's read barrier.
5736 // /* HeapReference<Object> */ out = *(out + offset)
5737 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5738 out,
5739 out_reg,
5740 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005741 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08005742 /* needs_null_check= */ false,
5743 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005744 } else {
5745 // Load with slow path based read barrier.
5746 // Save the value of `out` into `maybe_temp` before overwriting it
5747 // in the following move operation, as we will need it for the
5748 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005749 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005750 __ Mov(temp_reg, out_reg);
5751 // /* HeapReference<Object> */ out = *(out + offset)
5752 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5753 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5754 }
5755 } else {
5756 // Plain load with no read barrier.
5757 // /* HeapReference<Object> */ out = *(out + offset)
5758 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5759 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5760 }
5761}
5762
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005763void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5764 HInstruction* instruction,
5765 Location out,
5766 Location obj,
5767 uint32_t offset,
5768 Location maybe_temp,
5769 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005770 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00005771 Register out_reg = RegisterFrom(out, type);
5772 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005773 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005774 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005775 if (kUseBakerReadBarrier) {
5776 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00005777 // /* HeapReference<Object> */ out = *(obj + offset)
5778 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5779 out,
5780 obj_reg,
5781 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005782 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08005783 /* needs_null_check= */ false,
5784 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005785 } else {
5786 // Load with slow path based read barrier.
5787 // /* HeapReference<Object> */ out = *(obj + offset)
5788 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5789 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5790 }
5791 } else {
5792 // Plain load with no read barrier.
5793 // /* HeapReference<Object> */ out = *(obj + offset)
5794 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5795 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5796 }
5797}
5798
Vladimir Markoca1e0382018-04-11 09:58:41 +00005799void CodeGeneratorARM64::GenerateGcRootFieldLoad(
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005800 HInstruction* instruction,
5801 Location root,
5802 Register obj,
5803 uint32_t offset,
5804 vixl::aarch64::Label* fixup_label,
5805 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005806 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005807 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005808 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005809 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005810 if (kUseBakerReadBarrier) {
5811 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005812 // Baker's read barrier are used.
Roland Levillain44015862016-01-22 11:47:17 +00005813
Vladimir Marko008e09f32018-08-06 15:42:43 +01005814 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
5815 // the Marking Register) to decide whether we need to enter
5816 // the slow path to mark the GC root.
5817 //
5818 // We use shared thunks for the slow path; shared within the method
5819 // for JIT, across methods for AOT. That thunk checks the reference
5820 // and jumps to the entrypoint if needed.
5821 //
5822 // lr = &return_address;
5823 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5824 // if (mr) { // Thread::Current()->GetIsGcMarking()
5825 // goto gc_root_thunk<root_reg>(lr)
5826 // }
5827 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00005828
Vladimir Marko008e09f32018-08-06 15:42:43 +01005829 UseScratchRegisterScope temps(GetVIXLAssembler());
5830 DCHECK(temps.IsAvailable(ip0));
5831 DCHECK(temps.IsAvailable(ip1));
5832 temps.Exclude(ip0, ip1);
5833 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
Roland Levillain44015862016-01-22 11:47:17 +00005834
Vladimir Marko008e09f32018-08-06 15:42:43 +01005835 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
5836 vixl::aarch64::Label return_address;
5837 __ adr(lr, &return_address);
5838 if (fixup_label != nullptr) {
5839 __ bind(fixup_label);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005840 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01005841 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
Vladimir Marko94796f82018-08-08 15:15:33 +01005842 "GC root LDR must be 2 instructions (8B) before the return address label.");
Vladimir Marko008e09f32018-08-06 15:42:43 +01005843 __ ldr(root_reg, MemOperand(obj.X(), offset));
5844 EmitBakerReadBarrierCbnz(custom_data);
5845 __ bind(&return_address);
Roland Levillain44015862016-01-22 11:47:17 +00005846 } else {
5847 // GC root loaded through a slow path for read barriers other
5848 // than Baker's.
5849 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005850 if (fixup_label == nullptr) {
5851 __ Add(root_reg.X(), obj.X(), offset);
5852 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00005853 EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005854 }
Roland Levillain44015862016-01-22 11:47:17 +00005855 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00005856 GenerateReadBarrierForRootSlow(instruction, root, root);
Roland Levillain44015862016-01-22 11:47:17 +00005857 }
5858 } else {
5859 // Plain GC root load with no read barrier.
5860 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005861 if (fixup_label == nullptr) {
5862 __ Ldr(root_reg, MemOperand(obj, offset));
5863 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00005864 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005865 }
Roland Levillain44015862016-01-22 11:47:17 +00005866 // Note that GC roots are not affected by heap poisoning, thus we
5867 // do not have to unpoison `root_reg` here.
5868 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005869 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00005870}
5871
Vladimir Marko94796f82018-08-08 15:15:33 +01005872void CodeGeneratorARM64::GenerateUnsafeCasOldValueMovWithBakerReadBarrier(
5873 vixl::aarch64::Register marked,
5874 vixl::aarch64::Register old_value) {
5875 DCHECK(kEmitCompilerReadBarrier);
5876 DCHECK(kUseBakerReadBarrier);
5877
5878 // Similar to the Baker RB path in GenerateGcRootFieldLoad(), with a MOV instead of LDR.
5879 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(marked.GetCode());
5880
5881 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
5882 vixl::aarch64::Label return_address;
5883 __ adr(lr, &return_address);
5884 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
5885 "GC root LDR must be 2 instructions (8B) before the return address label.");
5886 __ mov(marked, old_value);
5887 EmitBakerReadBarrierCbnz(custom_data);
5888 __ bind(&return_address);
5889}
5890
Roland Levillain44015862016-01-22 11:47:17 +00005891void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5892 Location ref,
Vladimir Marko248141f2018-08-10 10:40:07 +01005893 vixl::aarch64::Register obj,
5894 const vixl::aarch64::MemOperand& src,
Roland Levillain44015862016-01-22 11:47:17 +00005895 bool needs_null_check,
5896 bool use_load_acquire) {
5897 DCHECK(kEmitCompilerReadBarrier);
5898 DCHECK(kUseBakerReadBarrier);
5899
Vladimir Marko0ecac682018-08-07 10:40:38 +01005900 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
5901 // Marking Register) to decide whether we need to enter the slow
5902 // path to mark the reference. Then, in the slow path, check the
5903 // gray bit in the lock word of the reference's holder (`obj`) to
5904 // decide whether to mark `ref` or not.
5905 //
5906 // We use shared thunks for the slow path; shared within the method
5907 // for JIT, across methods for AOT. That thunk checks the holder
5908 // and jumps to the entrypoint if needed. If the holder is not gray,
5909 // it creates a fake dependency and returns to the LDR instruction.
5910 //
5911 // lr = &gray_return_address;
5912 // if (mr) { // Thread::Current()->GetIsGcMarking()
5913 // goto field_thunk<holder_reg, base_reg, use_load_acquire>(lr)
5914 // }
5915 // not_gray_return_address:
5916 // // Original reference load. If the offset is too large to fit
5917 // // into LDR, we use an adjusted base register here.
5918 // HeapReference<mirror::Object> reference = *(obj+offset);
5919 // gray_return_address:
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005920
Vladimir Marko248141f2018-08-10 10:40:07 +01005921 DCHECK(src.GetAddrMode() == vixl::aarch64::Offset);
5922 DCHECK_ALIGNED(src.GetOffset(), sizeof(mirror::HeapReference<mirror::Object>));
5923
5924 UseScratchRegisterScope temps(GetVIXLAssembler());
5925 DCHECK(temps.IsAvailable(ip0));
5926 DCHECK(temps.IsAvailable(ip1));
5927 temps.Exclude(ip0, ip1);
5928 uint32_t custom_data = use_load_acquire
5929 ? EncodeBakerReadBarrierAcquireData(src.GetBaseRegister().GetCode(), obj.GetCode())
5930 : EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode());
5931
5932 {
5933 ExactAssemblyScope guard(GetVIXLAssembler(),
5934 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
5935 vixl::aarch64::Label return_address;
5936 __ adr(lr, &return_address);
5937 EmitBakerReadBarrierCbnz(custom_data);
5938 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
5939 "Field LDR must be 1 instruction (4B) before the return address label; "
5940 " 2 instructions (8B) for heap poisoning.");
5941 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
5942 if (use_load_acquire) {
5943 DCHECK_EQ(src.GetOffset(), 0);
5944 __ ldar(ref_reg, src);
5945 } else {
5946 __ ldr(ref_reg, src);
5947 }
5948 if (needs_null_check) {
5949 MaybeRecordImplicitNullCheck(instruction);
5950 }
5951 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
5952 // macro instructions disallowed in ExactAssemblyScope.
5953 if (kPoisonHeapReferences) {
5954 __ neg(ref_reg, Operand(ref_reg));
5955 }
5956 __ bind(&return_address);
5957 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005958 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Vladimir Marko248141f2018-08-10 10:40:07 +01005959}
5960
5961void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5962 Location ref,
5963 Register obj,
5964 uint32_t offset,
5965 Location maybe_temp,
5966 bool needs_null_check,
5967 bool use_load_acquire) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01005968 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
5969 Register base = obj;
5970 if (use_load_acquire) {
5971 DCHECK(maybe_temp.IsRegister());
5972 base = WRegisterFrom(maybe_temp);
5973 __ Add(base, obj, offset);
5974 offset = 0u;
5975 } else if (offset >= kReferenceLoadMinFarOffset) {
5976 DCHECK(maybe_temp.IsRegister());
5977 base = WRegisterFrom(maybe_temp);
5978 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
5979 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
5980 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005981 }
Vladimir Marko248141f2018-08-10 10:40:07 +01005982 MemOperand src(base.X(), offset);
5983 GenerateFieldLoadWithBakerReadBarrier(
5984 instruction, ref, obj, src, needs_null_check, use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005985}
5986
Artem Serov0806f582018-10-11 20:14:20 +01005987void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HArrayGet* instruction,
5988 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005989 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005990 uint32_t data_offset,
5991 Location index,
Roland Levillain44015862016-01-22 11:47:17 +00005992 bool needs_null_check) {
5993 DCHECK(kEmitCompilerReadBarrier);
5994 DCHECK(kUseBakerReadBarrier);
5995
Vladimir Marko66d691d2017-04-07 17:53:39 +01005996 static_assert(
5997 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5998 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005999 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006000
Vladimir Marko008e09f32018-08-06 15:42:43 +01006001 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6002 // Marking Register) to decide whether we need to enter the slow
6003 // path to mark the reference. Then, in the slow path, check the
6004 // gray bit in the lock word of the reference's holder (`obj`) to
6005 // decide whether to mark `ref` or not.
6006 //
6007 // We use shared thunks for the slow path; shared within the method
6008 // for JIT, across methods for AOT. That thunk checks the holder
6009 // and jumps to the entrypoint if needed. If the holder is not gray,
6010 // it creates a fake dependency and returns to the LDR instruction.
6011 //
6012 // lr = &gray_return_address;
6013 // if (mr) { // Thread::Current()->GetIsGcMarking()
6014 // goto array_thunk<base_reg>(lr)
6015 // }
6016 // not_gray_return_address:
6017 // // Original reference load. If the offset is too large to fit
6018 // // into LDR, we use an adjusted base register here.
6019 // HeapReference<mirror::Object> reference = data[index];
6020 // gray_return_address:
Vladimir Marko66d691d2017-04-07 17:53:39 +01006021
Vladimir Marko008e09f32018-08-06 15:42:43 +01006022 DCHECK(index.IsValid());
6023 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
6024 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006025
Vladimir Marko008e09f32018-08-06 15:42:43 +01006026 UseScratchRegisterScope temps(GetVIXLAssembler());
6027 DCHECK(temps.IsAvailable(ip0));
6028 DCHECK(temps.IsAvailable(ip1));
6029 temps.Exclude(ip0, ip1);
Artem Serov0806f582018-10-11 20:14:20 +01006030
6031 Register temp;
6032 if (instruction->GetArray()->IsIntermediateAddress()) {
6033 // We do not need to compute the intermediate address from the array: the
6034 // input instruction has done it already. See the comment in
6035 // `TryExtractArrayAccessAddress()`.
6036 if (kIsDebugBuild) {
6037 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
6038 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
6039 }
6040 temp = obj;
6041 } else {
6042 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
6043 __ Add(temp.X(), obj.X(), Operand(data_offset));
6044 }
6045
Vladimir Marko008e09f32018-08-06 15:42:43 +01006046 uint32_t custom_data = EncodeBakerReadBarrierArrayData(temp.GetCode());
Vladimir Marko66d691d2017-04-07 17:53:39 +01006047
Vladimir Marko008e09f32018-08-06 15:42:43 +01006048 {
6049 ExactAssemblyScope guard(GetVIXLAssembler(),
6050 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6051 vixl::aarch64::Label return_address;
6052 __ adr(lr, &return_address);
6053 EmitBakerReadBarrierCbnz(custom_data);
6054 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6055 "Array LDR must be 1 instruction (4B) before the return address label; "
6056 " 2 instructions (8B) for heap poisoning.");
6057 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6058 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6059 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6060 // macro instructions disallowed in ExactAssemblyScope.
6061 if (kPoisonHeapReferences) {
6062 __ neg(ref_reg, Operand(ref_reg));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006063 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006064 __ bind(&return_address);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006065 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006066 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Roland Levillain44015862016-01-22 11:47:17 +00006067}
6068
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006069void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6070 // The following condition is a compile-time one, so it does not have a run-time cost.
6071 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
6072 // The following condition is a run-time one; it is executed after the
6073 // previous compile-time test, to avoid penalizing non-debug builds.
6074 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6075 UseScratchRegisterScope temps(GetVIXLAssembler());
6076 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6077 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6078 }
6079 }
6080}
6081
Roland Levillain44015862016-01-22 11:47:17 +00006082void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6083 Location out,
6084 Location ref,
6085 Location obj,
6086 uint32_t offset,
6087 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006088 DCHECK(kEmitCompilerReadBarrier);
6089
Roland Levillain44015862016-01-22 11:47:17 +00006090 // Insert a slow path based read barrier *after* the reference load.
6091 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006092 // If heap poisoning is enabled, the unpoisoning of the loaded
6093 // reference will be carried out by the runtime within the slow
6094 // path.
6095 //
6096 // Note that `ref` currently does not get unpoisoned (when heap
6097 // poisoning is enabled), which is alright as the `ref` argument is
6098 // not used by the artReadBarrierSlow entry point.
6099 //
6100 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006101 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006102 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6103 AddSlowPath(slow_path);
6104
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006105 __ B(slow_path->GetEntryLabel());
6106 __ Bind(slow_path->GetExitLabel());
6107}
6108
Roland Levillain44015862016-01-22 11:47:17 +00006109void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6110 Location out,
6111 Location ref,
6112 Location obj,
6113 uint32_t offset,
6114 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006115 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006116 // Baker's read barriers shall be handled by the fast path
6117 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6118 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006119 // If heap poisoning is enabled, unpoisoning will be taken care of
6120 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006121 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006122 } else if (kPoisonHeapReferences) {
6123 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6124 }
6125}
6126
Roland Levillain44015862016-01-22 11:47:17 +00006127void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6128 Location out,
6129 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006130 DCHECK(kEmitCompilerReadBarrier);
6131
Roland Levillain44015862016-01-22 11:47:17 +00006132 // Insert a slow path based read barrier *after* the GC root load.
6133 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006134 // Note that GC roots are not affected by heap poisoning, so we do
6135 // not need to do anything special for this here.
6136 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006137 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006138 AddSlowPath(slow_path);
6139
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006140 __ B(slow_path->GetEntryLabel());
6141 __ Bind(slow_path->GetExitLabel());
6142}
6143
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006144void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6145 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006146 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006147 locations->SetInAt(0, Location::RequiresRegister());
6148 locations->SetOut(Location::RequiresRegister());
6149}
6150
6151void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6152 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006153 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006154 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006155 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006156 __ Ldr(XRegisterFrom(locations->Out()),
6157 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006158 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006159 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006160 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006161 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6162 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006163 __ Ldr(XRegisterFrom(locations->Out()),
6164 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006165 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006166}
6167
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006168static void PatchJitRootUse(uint8_t* code,
6169 const uint8_t* roots_data,
6170 vixl::aarch64::Literal<uint32_t>* literal,
6171 uint64_t index_in_table) {
6172 uint32_t literal_offset = literal->GetOffset();
6173 uintptr_t address =
6174 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6175 uint8_t* data = code + literal_offset;
6176 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6177}
6178
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006179void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6180 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006181 const StringReference& string_reference = entry.first;
6182 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006183 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006184 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006185 }
6186 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006187 const TypeReference& type_reference = entry.first;
6188 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006189 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006190 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006191 }
6192}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006193
Alexandre Rames67555f72014-11-18 10:55:16 +00006194#undef __
6195#undef QUICK_ENTRY_POINT
6196
Vladimir Markoca1e0382018-04-11 09:58:41 +00006197#define __ assembler.GetVIXLAssembler()->
6198
6199static void EmitGrayCheckAndFastPath(arm64::Arm64Assembler& assembler,
6200 vixl::aarch64::Register base_reg,
6201 vixl::aarch64::MemOperand& lock_word,
Vladimir Marko7a695052018-04-12 10:26:50 +01006202 vixl::aarch64::Label* slow_path,
6203 vixl::aarch64::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006204 // Load the lock word containing the rb_state.
6205 __ Ldr(ip0.W(), lock_word);
6206 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01006207 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Vladimir Markoca1e0382018-04-11 09:58:41 +00006208 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6209 __ Tbnz(ip0.W(), LockWord::kReadBarrierStateShift, slow_path);
6210 static_assert(
6211 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET,
6212 "Field and array LDR offsets must be the same to reuse the same code.");
Vladimir Marko7a695052018-04-12 10:26:50 +01006213 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
6214 if (throw_npe != nullptr) {
6215 __ Bind(throw_npe);
6216 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006217 // Adjust the return address back to the LDR (1 instruction; 2 for heap poisoning).
6218 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6219 "Field LDR must be 1 instruction (4B) before the return address label; "
6220 " 2 instructions (8B) for heap poisoning.");
6221 __ Add(lr, lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6222 // Introduce a dependency on the lock_word including rb_state,
6223 // to prevent load-load reordering, and without using
6224 // a memory barrier (which would be more expensive).
6225 __ Add(base_reg, base_reg, Operand(ip0, LSR, 32));
6226 __ Br(lr); // And return back to the function.
6227 // Note: The fake dependency is unnecessary for the slow path.
6228}
6229
6230// Load the read barrier introspection entrypoint in register `entrypoint`.
6231static void LoadReadBarrierMarkIntrospectionEntrypoint(arm64::Arm64Assembler& assembler,
6232 vixl::aarch64::Register entrypoint) {
6233 // entrypoint = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6234 DCHECK_EQ(ip0.GetCode(), 16u);
6235 const int32_t entry_point_offset =
6236 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6237 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
6238}
6239
6240void CodeGeneratorARM64::CompileBakerReadBarrierThunk(Arm64Assembler& assembler,
6241 uint32_t encoded_data,
6242 /*out*/ std::string* debug_name) {
6243 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
6244 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006245 case BakerReadBarrierKind::kField:
6246 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006247 auto base_reg =
6248 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6249 CheckValidReg(base_reg.GetCode());
6250 auto holder_reg =
6251 Register::GetXRegFromCode(BakerReadBarrierSecondRegField::Decode(encoded_data));
6252 CheckValidReg(holder_reg.GetCode());
6253 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6254 temps.Exclude(ip0, ip1);
Vladimir Marko7a695052018-04-12 10:26:50 +01006255 // If base_reg differs from holder_reg, the offset was too large and we must have emitted
6256 // an explicit null check before the load. Otherwise, for implicit null checks, we need to
6257 // null-check the holder as we do not necessarily do that check before going to the thunk.
6258 vixl::aarch64::Label throw_npe_label;
6259 vixl::aarch64::Label* throw_npe = nullptr;
6260 if (GetCompilerOptions().GetImplicitNullChecks() && holder_reg.Is(base_reg)) {
6261 throw_npe = &throw_npe_label;
6262 __ Cbz(holder_reg.W(), throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006263 }
Vladimir Marko7a695052018-04-12 10:26:50 +01006264 // Check if the holder is gray and, if not, add fake dependency to the base register
6265 // and return to the LDR instruction to load the reference. Otherwise, use introspection
6266 // to load the reference and call the entrypoint that performs further checks on the
6267 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006268 vixl::aarch64::Label slow_path;
6269 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
Vladimir Marko7a695052018-04-12 10:26:50 +01006270 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006271 __ Bind(&slow_path);
Vladimir Marko0ecac682018-08-07 10:40:38 +01006272 if (kind == BakerReadBarrierKind::kField) {
6273 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6274 __ Ldr(ip0.W(), ldr_address); // Load the LDR (immediate) unsigned offset.
6275 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6276 __ Ubfx(ip0.W(), ip0.W(), 10, 12); // Extract the offset.
6277 __ Ldr(ip0.W(), MemOperand(base_reg, ip0, LSL, 2)); // Load the reference.
6278 } else {
6279 DCHECK(kind == BakerReadBarrierKind::kAcquire);
6280 DCHECK(!base_reg.Is(holder_reg));
6281 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6282 __ Ldar(ip0.W(), MemOperand(base_reg));
6283 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006284 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
6285 __ Br(ip1); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006286 break;
6287 }
6288 case BakerReadBarrierKind::kArray: {
6289 auto base_reg =
6290 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6291 CheckValidReg(base_reg.GetCode());
6292 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6293 BakerReadBarrierSecondRegField::Decode(encoded_data));
6294 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6295 temps.Exclude(ip0, ip1);
6296 vixl::aarch64::Label slow_path;
6297 int32_t data_offset =
6298 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
6299 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
6300 DCHECK_LT(lock_word.GetOffset(), 0);
6301 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path);
6302 __ Bind(&slow_path);
6303 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
6304 __ Ldr(ip0.W(), ldr_address); // Load the LDR (register) unsigned offset.
6305 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6306 __ Ubfx(ip0, ip0, 16, 6); // Extract the index register, plus 32 (bit 21 is set).
6307 __ Bfi(ip1, ip0, 3, 6); // Insert ip0 to the entrypoint address to create
6308 // a switch case target based on the index register.
6309 __ Mov(ip0, base_reg); // Move the base register to ip0.
6310 __ Br(ip1); // Jump to the entrypoint's array switch case.
6311 break;
6312 }
6313 case BakerReadBarrierKind::kGcRoot: {
6314 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
6315 // and it does not have a forwarding address), call the correct introspection entrypoint;
6316 // otherwise return the reference (or the extracted forwarding address).
6317 // There is no gray bit check for GC roots.
6318 auto root_reg =
6319 Register::GetWRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6320 CheckValidReg(root_reg.GetCode());
6321 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6322 BakerReadBarrierSecondRegField::Decode(encoded_data));
6323 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6324 temps.Exclude(ip0, ip1);
6325 vixl::aarch64::Label return_label, not_marked, forwarding_address;
6326 __ Cbz(root_reg, &return_label);
6327 MemOperand lock_word(root_reg.X(), mirror::Object::MonitorOffset().Int32Value());
6328 __ Ldr(ip0.W(), lock_word);
6329 __ Tbz(ip0.W(), LockWord::kMarkBitStateShift, &not_marked);
6330 __ Bind(&return_label);
6331 __ Br(lr);
6332 __ Bind(&not_marked);
6333 __ Tst(ip0.W(), Operand(ip0.W(), LSL, 1));
6334 __ B(&forwarding_address, mi);
6335 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6336 // Adjust the art_quick_read_barrier_mark_introspection address in IP1 to
6337 // art_quick_read_barrier_mark_introspection_gc_roots.
6338 __ Add(ip1, ip1, Operand(BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRYPOINT_OFFSET));
6339 __ Mov(ip0.W(), root_reg);
6340 __ Br(ip1);
6341 __ Bind(&forwarding_address);
6342 __ Lsl(root_reg, ip0.W(), LockWord::kForwardingAddressShift);
6343 __ Br(lr);
6344 break;
6345 }
6346 default:
6347 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
6348 UNREACHABLE();
6349 }
6350
Vladimir Marko966b46f2018-08-03 10:20:19 +00006351 // For JIT, the slow path is considered part of the compiled method,
6352 // so JIT should pass null as `debug_name`. Tests may not have a runtime.
6353 DCHECK(Runtime::Current() == nullptr ||
6354 !Runtime::Current()->UseJitCompilation() ||
6355 debug_name == nullptr);
6356 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006357 std::ostringstream oss;
6358 oss << "BakerReadBarrierThunk";
6359 switch (kind) {
6360 case BakerReadBarrierKind::kField:
6361 oss << "Field_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
6362 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
6363 break;
Vladimir Marko0ecac682018-08-07 10:40:38 +01006364 case BakerReadBarrierKind::kAcquire:
6365 oss << "Acquire_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
6366 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
6367 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +00006368 case BakerReadBarrierKind::kArray:
6369 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
6370 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6371 BakerReadBarrierSecondRegField::Decode(encoded_data));
6372 break;
6373 case BakerReadBarrierKind::kGcRoot:
6374 oss << "GcRoot_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
6375 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6376 BakerReadBarrierSecondRegField::Decode(encoded_data));
6377 break;
6378 }
6379 *debug_name = oss.str();
6380 }
6381}
6382
6383#undef __
6384
Alexandre Rames5319def2014-10-23 10:03:10 +01006385} // namespace arm64
6386} // namespace art