blob: 177d98248fb3a49aeae767bc6dbe20feaefb2d72 [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 Markocac5a7e2016-02-22 10:39:50 +0000890 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100891 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100892 uint64_literals_(std::less<uint64_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100893 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000894 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100895 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000896 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100897 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000898 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100899 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6fd16062018-06-26 11:02:04 +0100900 boot_image_intrinsic_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100901 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000902 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100903 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000904 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +0000905 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
906 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
907 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000908 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000909 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000910}
Alexandre Rames5319def2014-10-23 10:03:10 +0100911
Alexandre Rames67555f72014-11-18 10:55:16 +0000912#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100913
Zheng Xu3927c8b2015-11-18 17:46:25 +0800914void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100915 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800916 jump_table->EmitTable(this);
917 }
918}
919
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000920void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800921 EmitJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +0000922
923 // Emit JIT baker read barrier slow paths.
924 DCHECK(Runtime::Current()->UseJitCompilation() || jit_baker_read_barrier_slow_paths_.empty());
925 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
926 uint32_t encoded_data = entry.first;
927 vixl::aarch64::Label* slow_path_entry = &entry.second.label;
928 __ Bind(slow_path_entry);
Andreas Gampe3db70682018-12-26 15:12:03 -0800929 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name= */ nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +0000930 }
931
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000932 // Ensure we emit the literal pool.
933 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000934
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000935 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +0000936
937 // Verify Baker read barrier linker patches.
938 if (kIsDebugBuild) {
939 ArrayRef<const uint8_t> code = allocator->GetMemory();
940 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
941 DCHECK(info.label.IsBound());
942 uint32_t literal_offset = info.label.GetLocation();
943 DCHECK_ALIGNED(literal_offset, 4u);
944
945 auto GetInsn = [&code](uint32_t offset) {
946 DCHECK_ALIGNED(offset, 4u);
947 return
948 (static_cast<uint32_t>(code[offset + 0]) << 0) +
949 (static_cast<uint32_t>(code[offset + 1]) << 8) +
950 (static_cast<uint32_t>(code[offset + 2]) << 16)+
951 (static_cast<uint32_t>(code[offset + 3]) << 24);
952 };
953
954 const uint32_t encoded_data = info.custom_data;
955 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
956 // Check that the next instruction matches the expected LDR.
957 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +0100958 case BakerReadBarrierKind::kField:
959 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +0000960 DCHECK_GE(code.size() - literal_offset, 8u);
961 uint32_t next_insn = GetInsn(literal_offset + 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +0000962 CheckValidReg(next_insn & 0x1fu); // Check destination register.
963 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko0ecac682018-08-07 10:40:38 +0100964 if (kind == BakerReadBarrierKind::kField) {
965 // LDR (immediate) with correct base_reg.
966 CHECK_EQ(next_insn & 0xffc003e0u, 0xb9400000u | (base_reg << 5));
967 } else {
968 DCHECK(kind == BakerReadBarrierKind::kAcquire);
969 // LDAR with correct base_reg.
970 CHECK_EQ(next_insn & 0xffffffe0u, 0x88dffc00u | (base_reg << 5));
971 }
Vladimir Markoca1e0382018-04-11 09:58:41 +0000972 break;
973 }
974 case BakerReadBarrierKind::kArray: {
975 DCHECK_GE(code.size() - literal_offset, 8u);
976 uint32_t next_insn = GetInsn(literal_offset + 4u);
977 // LDR (register) with the correct base_reg, size=10 (32-bit), option=011 (extend = LSL),
978 // and S=1 (shift amount = 2 for 32-bit version), i.e. LDR Wt, [Xn, Xm, LSL #2].
979 CheckValidReg(next_insn & 0x1fu); // Check destination register.
980 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
981 CHECK_EQ(next_insn & 0xffe0ffe0u, 0xb8607800u | (base_reg << 5));
982 CheckValidReg((next_insn >> 16) & 0x1f); // Check index register
983 break;
984 }
985 case BakerReadBarrierKind::kGcRoot: {
986 DCHECK_GE(literal_offset, 4u);
987 uint32_t prev_insn = GetInsn(literal_offset - 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +0000988 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko94796f82018-08-08 15:15:33 +0100989 // Usually LDR (immediate) with correct root_reg but
990 // we may have a "MOV marked, old_value" for UnsafeCASObject.
991 if ((prev_insn & 0xffe0ffff) != (0x2a0003e0 | root_reg)) { // MOV?
992 CHECK_EQ(prev_insn & 0xffc0001fu, 0xb9400000u | root_reg); // LDR?
993 }
Vladimir Markoca1e0382018-04-11 09:58:41 +0000994 break;
995 }
996 default:
997 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
998 UNREACHABLE();
999 }
1000 }
1001 }
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001002}
1003
Zheng Xuad4450e2015-04-17 18:48:56 +08001004void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1005 // Note: There are 6 kinds of moves:
1006 // 1. constant -> GPR/FPR (non-cycle)
1007 // 2. constant -> stack (non-cycle)
1008 // 3. GPR/FPR -> GPR/FPR
1009 // 4. GPR/FPR -> stack
1010 // 5. stack -> GPR/FPR
1011 // 6. stack -> stack (non-cycle)
1012 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1013 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1014 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1015 // dependency.
1016 vixl_temps_.Open(GetVIXLAssembler());
1017}
1018
1019void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1020 vixl_temps_.Close();
1021}
1022
1023Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001024 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1025 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1026 || kind == Location::kSIMDStackSlot);
1027 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1028 ? Location::kFpuRegister
1029 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001030 Location scratch = GetScratchLocation(kind);
1031 if (!scratch.Equals(Location::NoLocation())) {
1032 return scratch;
1033 }
1034 // Allocate from VIXL temp registers.
1035 if (kind == Location::kRegister) {
1036 scratch = LocationFrom(vixl_temps_.AcquireX());
1037 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001038 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001039 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1040 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1041 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001042 }
1043 AddScratchLocation(scratch);
1044 return scratch;
1045}
1046
1047void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1048 if (loc.IsRegister()) {
1049 vixl_temps_.Release(XRegisterFrom(loc));
1050 } else {
1051 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001052 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001053 }
1054 RemoveScratchLocation(loc);
1055}
1056
Alexandre Rames3e69f162014-12-10 10:36:50 +00001057void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001058 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001059 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001060}
1061
Alexandre Rames5319def2014-10-23 10:03:10 +01001062void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001063 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001064 __ Bind(&frame_entry_label_);
1065
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001066 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1067 UseScratchRegisterScope temps(masm);
1068 Register temp = temps.AcquireX();
1069 __ Ldrh(temp, MemOperand(kArtMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
1070 __ Add(temp, temp, 1);
1071 __ Strh(temp, MemOperand(kArtMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
1072 }
1073
Vladimir Marko33bff252017-11-01 14:35:42 +00001074 bool do_overflow_check =
1075 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001076 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001077 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001078 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001079 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001080 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001081 {
1082 // Ensure that between load and RecordPcInfo there are no pools emitted.
1083 ExactAssemblyScope eas(GetVIXLAssembler(),
1084 kInstructionSize,
1085 CodeBufferCheckScope::kExactSize);
1086 __ ldr(wzr, MemOperand(temp, 0));
1087 RecordPcInfo(nullptr, 0);
1088 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001089 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001090
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001091 if (!HasEmptyFrame()) {
1092 int frame_size = GetFrameSize();
1093 // Stack layout:
1094 // sp[frame_size - 8] : lr.
1095 // ... : other preserved core registers.
1096 // ... : other preserved fp registers.
1097 // ... : reserved frame space.
1098 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001099
1100 // Save the current method if we need it. Note that we do not
1101 // do this in HCurrentMethod, as the instruction might have been removed
1102 // in the SSA graph.
1103 if (RequiresCurrentMethod()) {
1104 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001105 } else {
1106 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001107 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001108 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001109 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1110 frame_size - GetCoreSpillSize());
1111 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1112 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001113
1114 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1115 // Initialize should_deoptimize flag to 0.
1116 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1117 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1118 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001119 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01001120
Andreas Gampe3db70682018-12-26 15:12:03 -08001121 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001122}
1123
1124void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001125 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001126 if (!HasEmptyFrame()) {
1127 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001128 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1129 frame_size - FrameEntrySpillSize());
1130 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1131 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001132 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001133 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001134 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001135 __ Ret();
1136 GetAssembler()->cfi().RestoreState();
1137 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001138}
1139
Scott Wakeling97c72b72016-06-24 16:19:36 +01001140CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001141 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001142 return CPURegList(CPURegister::kRegister, kXRegSize,
1143 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001144}
1145
Scott Wakeling97c72b72016-06-24 16:19:36 +01001146CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001147 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1148 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001149 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1150 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001151}
1152
Alexandre Rames5319def2014-10-23 10:03:10 +01001153void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1154 __ Bind(GetLabelOf(block));
1155}
1156
Calin Juravle175dc732015-08-25 15:42:32 +01001157void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1158 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001159 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001160}
1161
Calin Juravlee460d1d2015-09-29 04:52:17 +01001162void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1163 if (location.IsRegister()) {
1164 locations->AddTemp(location);
1165 } else {
1166 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1167 }
1168}
1169
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001170void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001171 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001172 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001173 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001174 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001175 if (value_can_be_null) {
1176 __ Cbz(value, &done);
1177 }
Roland Levillainc73f0522018-08-14 15:16:50 +01001178 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07001179 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01001180 // Calculate the offset (in the card table) of the card corresponding to
1181 // `object`.
Alexandre Rames5319def2014-10-23 10:03:10 +01001182 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Roland Levillainc73f0522018-08-14 15:16:50 +01001183 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
1184 // `object`'s card.
1185 //
1186 // Register `card` contains the address of the card table. Note that the card
1187 // table's base is biased during its creation so that it always starts at an
1188 // address whose least-significant byte is equal to `kCardDirty` (see
1189 // art::gc::accounting::CardTable::Create). Therefore the STRB instruction
1190 // below writes the `kCardDirty` (byte) value into the `object`'s card
1191 // (located at `card + object >> kCardShift`).
1192 //
1193 // This dual use of the value in register `card` (1. to calculate the location
1194 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
1195 // (no need to explicitly load `kCardDirty` as an immediate value).
Serban Constantinescu02164b32014-11-13 14:05:07 +00001196 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001197 if (value_can_be_null) {
1198 __ Bind(&done);
1199 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001200}
1201
David Brazdil58282f42016-01-14 12:45:10 +00001202void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001203 // Blocked core registers:
1204 // lr : Runtime reserved.
1205 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001206 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001207 // ip1 : VIXL core temp.
1208 // ip0 : VIXL core temp.
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001209 // x18 : Platform register.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001210 //
1211 // Blocked fp registers:
1212 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001213 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1214 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001215 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001216 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001217 }
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001218 blocked_core_registers_[X18] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001219
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001220 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001221 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001222 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001223 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001224
David Brazdil58282f42016-01-14 12:45:10 +00001225 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001226 // Stubs do not save callee-save floating point registers. If the graph
1227 // is debuggable, we need to deal with these registers differently. For
1228 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001229 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1230 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001231 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001232 }
1233 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001234}
1235
Alexandre Rames3e69f162014-12-10 10:36:50 +00001236size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1237 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1238 __ Str(reg, MemOperand(sp, stack_index));
1239 return kArm64WordSize;
1240}
1241
1242size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1243 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1244 __ Ldr(reg, MemOperand(sp, stack_index));
1245 return kArm64WordSize;
1246}
1247
1248size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1249 FPRegister reg = FPRegister(reg_id, kDRegSize);
1250 __ Str(reg, MemOperand(sp, stack_index));
1251 return kArm64WordSize;
1252}
1253
1254size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1255 FPRegister reg = FPRegister(reg_id, kDRegSize);
1256 __ Ldr(reg, MemOperand(sp, stack_index));
1257 return kArm64WordSize;
1258}
1259
Alexandre Rames5319def2014-10-23 10:03:10 +01001260void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001261 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001262}
1263
1264void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001265 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001266}
1267
Vladimir Markoa0431112018-06-25 09:32:54 +01001268const Arm64InstructionSetFeatures& CodeGeneratorARM64::GetInstructionSetFeatures() const {
1269 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArm64InstructionSetFeatures();
1270}
1271
Alexandre Rames67555f72014-11-18 10:55:16 +00001272void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001273 if (constant->IsIntConstant()) {
1274 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1275 } else if (constant->IsLongConstant()) {
1276 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1277 } else if (constant->IsNullConstant()) {
1278 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001279 } else if (constant->IsFloatConstant()) {
1280 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1281 } else {
1282 DCHECK(constant->IsDoubleConstant());
1283 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1284 }
1285}
1286
Alexandre Rames3e69f162014-12-10 10:36:50 +00001287
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001288static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001289 DCHECK(constant.IsConstant());
1290 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001291 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001292 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001293 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1294 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1295 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1296 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001297}
1298
Roland Levillain952b2352017-05-03 19:49:14 +01001299// Allocate a scratch register from the VIXL pool, querying first
1300// the floating-point register pool, and then the core register
1301// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001302// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1303// using a different allocation strategy.
1304static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1305 vixl::aarch64::UseScratchRegisterScope* temps,
1306 int size_in_bits) {
1307 return masm->GetScratchFPRegisterList()->IsEmpty()
1308 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1309 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1310}
1311
Calin Juravlee460d1d2015-09-29 04:52:17 +01001312void CodeGeneratorARM64::MoveLocation(Location destination,
1313 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001314 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001315 if (source.Equals(destination)) {
1316 return;
1317 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001318
1319 // A valid move can always be inferred from the destination and source
1320 // locations. When moving from and to a register, the argument type can be
1321 // used to generate 32bit instead of 64bit moves. In debug mode we also
1322 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001323 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001324
1325 if (destination.IsRegister() || destination.IsFpuRegister()) {
1326 if (unspecified_type) {
1327 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1328 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001329 (src_cst != nullptr && (src_cst->IsIntConstant()
1330 || src_cst->IsFloatConstant()
1331 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001332 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001333 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001334 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001335 // If the source is a double stack slot or a 64bit constant, a 64bit
1336 // type is appropriate. Else the source is a register, and since the
1337 // type has not been specified, we chose a 64bit type to force a 64bit
1338 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001339 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001340 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001341 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001342 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1343 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001344 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001345 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1346 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1347 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001348 } else if (source.IsSIMDStackSlot()) {
1349 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001350 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001351 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001352 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001353 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001354 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001355 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001356 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001357 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001358 DataType::Type source_type = DataType::Is64BitType(dst_type)
1359 ? DataType::Type::kInt64
1360 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001361 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1362 }
1363 } else {
1364 DCHECK(source.IsFpuRegister());
1365 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001366 DataType::Type source_type = DataType::Is64BitType(dst_type)
1367 ? DataType::Type::kFloat64
1368 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001369 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1370 } else {
1371 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001372 if (GetGraph()->HasSIMD()) {
1373 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1374 } else {
1375 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1376 }
1377 }
1378 }
1379 } else if (destination.IsSIMDStackSlot()) {
1380 if (source.IsFpuRegister()) {
1381 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1382 } else {
1383 DCHECK(source.IsSIMDStackSlot());
1384 UseScratchRegisterScope temps(GetVIXLAssembler());
1385 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1386 Register temp = temps.AcquireX();
1387 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1388 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1389 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1390 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1391 } else {
1392 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1393 __ Ldr(temp, StackOperandFrom(source));
1394 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001395 }
1396 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001397 } else { // The destination is not a register. It must be a stack slot.
1398 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1399 if (source.IsRegister() || source.IsFpuRegister()) {
1400 if (unspecified_type) {
1401 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001402 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001403 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001404 dst_type =
1405 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001406 }
1407 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001408 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1409 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001410 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001411 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001412 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1413 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001414 UseScratchRegisterScope temps(GetVIXLAssembler());
1415 HConstant* src_cst = source.GetConstant();
1416 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001417 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001418 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1419 ? Register(xzr)
1420 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001421 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001422 if (src_cst->IsIntConstant()) {
1423 temp = temps.AcquireW();
1424 } else if (src_cst->IsLongConstant()) {
1425 temp = temps.AcquireX();
1426 } else if (src_cst->IsFloatConstant()) {
1427 temp = temps.AcquireS();
1428 } else {
1429 DCHECK(src_cst->IsDoubleConstant());
1430 temp = temps.AcquireD();
1431 }
1432 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001433 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001434 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001435 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001436 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001437 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001438 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001439 // Use any scratch register (a core or a floating-point one)
1440 // from VIXL scratch register pools as a temporary.
1441 //
1442 // We used to only use the FP scratch register pool, but in some
1443 // rare cases the only register from this pool (D31) would
1444 // already be used (e.g. within a ParallelMove instruction, when
1445 // a move is blocked by a another move requiring a scratch FP
1446 // register, which would reserve D31). To prevent this issue, we
1447 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001448 //
1449 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001450 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001451 // use AcquireFPOrCoreCPURegisterOfSize instead of
1452 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1453 // allocates core scratch registers first.
1454 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1455 GetVIXLAssembler(),
1456 &temps,
1457 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001458 __ Ldr(temp, StackOperandFrom(source));
1459 __ Str(temp, StackOperandFrom(destination));
1460 }
1461 }
1462}
1463
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001464void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001465 CPURegister dst,
1466 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001467 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001468 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001469 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001470 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001471 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001472 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001473 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001474 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001475 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001476 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001477 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001478 case DataType::Type::kInt16:
1479 __ Ldrsh(Register(dst), src);
1480 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001481 case DataType::Type::kInt32:
1482 case DataType::Type::kReference:
1483 case DataType::Type::kInt64:
1484 case DataType::Type::kFloat32:
1485 case DataType::Type::kFloat64:
1486 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001487 __ Ldr(dst, src);
1488 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001489 case DataType::Type::kUint32:
1490 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001491 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001492 LOG(FATAL) << "Unreachable type " << type;
1493 }
1494}
1495
Calin Juravle77520bc2015-01-12 18:45:46 +00001496void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001497 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001498 const MemOperand& src,
1499 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001500 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001501 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001502 Register temp_base = temps.AcquireX();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001503 DataType::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001504
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001505 DCHECK(!src.IsPreIndex());
1506 DCHECK(!src.IsPostIndex());
1507
1508 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001509 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001510 {
1511 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1512 MemOperand base = MemOperand(temp_base);
1513 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001514 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001515 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001516 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001517 {
1518 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1519 __ ldarb(Register(dst), base);
1520 if (needs_null_check) {
1521 MaybeRecordImplicitNullCheck(instruction);
1522 }
1523 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001524 if (type == DataType::Type::kInt8) {
1525 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001526 }
1527 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001528 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001529 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001530 {
1531 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1532 __ ldarh(Register(dst), base);
1533 if (needs_null_check) {
1534 MaybeRecordImplicitNullCheck(instruction);
1535 }
1536 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001537 if (type == DataType::Type::kInt16) {
1538 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
1539 }
Artem Serov914d7a82017-02-07 14:33:49 +00001540 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001541 case DataType::Type::kInt32:
1542 case DataType::Type::kReference:
1543 case DataType::Type::kInt64:
1544 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001545 {
1546 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1547 __ ldar(Register(dst), base);
1548 if (needs_null_check) {
1549 MaybeRecordImplicitNullCheck(instruction);
1550 }
1551 }
1552 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001553 case DataType::Type::kFloat32:
1554 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00001555 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001556 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001557
Artem Serov914d7a82017-02-07 14:33:49 +00001558 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1559 {
1560 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1561 __ ldar(temp, base);
1562 if (needs_null_check) {
1563 MaybeRecordImplicitNullCheck(instruction);
1564 }
1565 }
1566 __ Fmov(FPRegister(dst), temp);
1567 break;
Roland Levillain44015862016-01-22 11:47:17 +00001568 }
Aart Bik66c158e2018-01-31 12:55:04 -08001569 case DataType::Type::kUint32:
1570 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001571 case DataType::Type::kVoid:
Artem Serov914d7a82017-02-07 14:33:49 +00001572 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001573 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001574 }
1575}
1576
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001577void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001578 CPURegister src,
1579 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001580 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001581 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001582 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001583 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001584 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001585 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001586 case DataType::Type::kUint16:
1587 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001588 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001589 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001590 case DataType::Type::kInt32:
1591 case DataType::Type::kReference:
1592 case DataType::Type::kInt64:
1593 case DataType::Type::kFloat32:
1594 case DataType::Type::kFloat64:
1595 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001596 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001597 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001598 case DataType::Type::kUint32:
1599 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001600 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001601 LOG(FATAL) << "Unreachable type " << type;
1602 }
1603}
1604
Artem Serov914d7a82017-02-07 14:33:49 +00001605void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001606 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001607 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00001608 const MemOperand& dst,
1609 bool needs_null_check) {
1610 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001611 UseScratchRegisterScope temps(GetVIXLAssembler());
1612 Register temp_base = temps.AcquireX();
1613
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001614 DCHECK(!dst.IsPreIndex());
1615 DCHECK(!dst.IsPostIndex());
1616
1617 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001618 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001619 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001620 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00001621 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001622 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001623 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001624 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001625 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001626 {
1627 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1628 __ stlrb(Register(src), base);
1629 if (needs_null_check) {
1630 MaybeRecordImplicitNullCheck(instruction);
1631 }
1632 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001633 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001634 case DataType::Type::kUint16:
1635 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001636 {
1637 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1638 __ stlrh(Register(src), base);
1639 if (needs_null_check) {
1640 MaybeRecordImplicitNullCheck(instruction);
1641 }
1642 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001643 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001644 case DataType::Type::kInt32:
1645 case DataType::Type::kReference:
1646 case DataType::Type::kInt64:
1647 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001648 {
1649 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1650 __ stlr(Register(src), base);
1651 if (needs_null_check) {
1652 MaybeRecordImplicitNullCheck(instruction);
1653 }
1654 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001655 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001656 case DataType::Type::kFloat32:
1657 case DataType::Type::kFloat64: {
1658 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001659 Register temp_src;
1660 if (src.IsZero()) {
1661 // The zero register is used to avoid synthesizing zero constants.
1662 temp_src = Register(src);
1663 } else {
1664 DCHECK(src.IsFPRegister());
1665 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1666 __ Fmov(temp_src, FPRegister(src));
1667 }
Artem Serov914d7a82017-02-07 14:33:49 +00001668 {
1669 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1670 __ stlr(temp_src, base);
1671 if (needs_null_check) {
1672 MaybeRecordImplicitNullCheck(instruction);
1673 }
1674 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001675 break;
1676 }
Aart Bik66c158e2018-01-31 12:55:04 -08001677 case DataType::Type::kUint32:
1678 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001679 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001680 LOG(FATAL) << "Unreachable type " << type;
1681 }
1682}
1683
Calin Juravle175dc732015-08-25 15:42:32 +01001684void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1685 HInstruction* instruction,
1686 uint32_t dex_pc,
1687 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001688 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00001689
1690 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
1691 {
1692 // Ensure the pc position is recorded immediately after the `blr` instruction.
1693 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1694 __ blr(lr);
1695 if (EntrypointRequiresStackMap(entrypoint)) {
1696 RecordPcInfo(instruction, dex_pc, slow_path);
1697 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001698 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001699}
1700
Roland Levillaindec8f632016-07-22 17:10:06 +01001701void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1702 HInstruction* instruction,
1703 SlowPathCode* slow_path) {
1704 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01001705 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1706 __ Blr(lr);
1707}
1708
Alexandre Rames67555f72014-11-18 10:55:16 +00001709void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001710 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001711 UseScratchRegisterScope temps(GetVIXLAssembler());
1712 Register temp = temps.AcquireW();
Vladimir Markodc682aa2018-01-04 18:42:57 +00001713 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
1714 const size_t status_byte_offset =
1715 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1716 constexpr uint32_t shifted_initialized_value =
1717 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001718
Serban Constantinescu02164b32014-11-13 14:05:07 +00001719 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001720 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Vladimir Markodc682aa2018-01-04 18:42:57 +00001721 __ Add(temp, class_reg, status_byte_offset);
Igor Murashkin86083f72017-10-27 10:59:04 -07001722 __ Ldarb(temp, HeapOperand(temp));
Vladimir Markodc682aa2018-01-04 18:42:57 +00001723 __ Cmp(temp, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00001724 __ B(lo, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001725 __ Bind(slow_path->GetExitLabel());
1726}
Alexandre Rames5319def2014-10-23 10:03:10 +01001727
Vladimir Marko175e7862018-03-27 09:03:13 +00001728void InstructionCodeGeneratorARM64::GenerateBitstringTypeCheckCompare(
1729 HTypeCheckInstruction* check, vixl::aarch64::Register temp) {
1730 uint32_t path_to_root = check->GetBitstringPathToRoot();
1731 uint32_t mask = check->GetBitstringMask();
1732 DCHECK(IsPowerOfTwo(mask + 1));
1733 size_t mask_bits = WhichPowerOf2(mask + 1);
1734
1735 if (mask_bits == 16u) {
1736 // Load only the bitstring part of the status word.
1737 __ Ldrh(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1738 } else {
1739 // /* uint32_t */ temp = temp->status_
1740 __ Ldr(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1741 // Extract the bitstring bits.
1742 __ Ubfx(temp, temp, 0, mask_bits);
1743 }
1744 // Compare the bitstring bits to `path_to_root`.
1745 __ Cmp(temp, path_to_root);
1746}
1747
Roland Levillain44015862016-01-22 11:47:17 +00001748void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001749 BarrierType type = BarrierAll;
1750
1751 switch (kind) {
1752 case MemBarrierKind::kAnyAny:
1753 case MemBarrierKind::kAnyStore: {
1754 type = BarrierAll;
1755 break;
1756 }
1757 case MemBarrierKind::kLoadAny: {
1758 type = BarrierReads;
1759 break;
1760 }
1761 case MemBarrierKind::kStoreStore: {
1762 type = BarrierWrites;
1763 break;
1764 }
1765 default:
1766 LOG(FATAL) << "Unexpected memory barrier " << kind;
1767 }
1768 __ Dmb(InnerShareable, type);
1769}
1770
Serban Constantinescu02164b32014-11-13 14:05:07 +00001771void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1772 HBasicBlock* successor) {
1773 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001774 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1775 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001776 slow_path =
1777 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001778 instruction->SetSlowPath(slow_path);
1779 codegen_->AddSlowPath(slow_path);
1780 if (successor != nullptr) {
1781 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001782 }
1783 } else {
1784 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1785 }
1786
Serban Constantinescu02164b32014-11-13 14:05:07 +00001787 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1788 Register temp = temps.AcquireW();
1789
Andreas Gampe542451c2016-07-26 09:02:02 -07001790 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001791 if (successor == nullptr) {
1792 __ Cbnz(temp, slow_path->GetEntryLabel());
1793 __ Bind(slow_path->GetReturnLabel());
1794 } else {
1795 __ Cbz(temp, codegen_->GetLabelOf(successor));
1796 __ B(slow_path->GetEntryLabel());
1797 // slow_path will return to GetLabelOf(successor).
1798 }
1799}
1800
Alexandre Rames5319def2014-10-23 10:03:10 +01001801InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1802 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001803 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001804 assembler_(codegen->GetAssembler()),
1805 codegen_(codegen) {}
1806
Alexandre Rames67555f72014-11-18 10:55:16 +00001807void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001808 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001809 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001810 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001811 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001812 case DataType::Type::kInt32:
1813 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01001814 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001815 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001816 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001817 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001818
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001819 case DataType::Type::kFloat32:
1820 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001821 locations->SetInAt(0, Location::RequiresFpuRegister());
1822 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001823 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001824 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001825
Alexandre Rames5319def2014-10-23 10:03:10 +01001826 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001827 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001828 }
1829}
1830
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001831void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
1832 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001833 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1834
1835 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001836 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01001837 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001838 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
1839 object_field_get_with_read_barrier
1840 ? LocationSummary::kCallOnSlowPath
1841 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001842 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001843 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko0ecac682018-08-07 10:40:38 +01001844 // We need a temporary register for the read barrier load in
1845 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
1846 // only if the field is volatile or the offset is too big.
1847 if (field_info.IsVolatile() ||
1848 field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
1849 locations->AddTemp(FixedTempLocation());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001850 }
Vladimir Marko70e97462016-08-09 11:04:26 +01001851 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001852 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001853 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001854 locations->SetOut(Location::RequiresFpuRegister());
1855 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001856 // The output overlaps for an object field get when read barriers
1857 // are enabled: we do not want the load to overwrite the object's
1858 // location, as we need it to emit the read barrier.
1859 locations->SetOut(
1860 Location::RequiresRegister(),
1861 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001862 }
1863}
1864
1865void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1866 const FieldInfo& field_info) {
1867 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001868 LocationSummary* locations = instruction->GetLocations();
1869 Location base_loc = locations->InAt(0);
1870 Location out = locations->Out();
1871 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01001872 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
1873 DataType::Type load_type = instruction->GetType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001874 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001875
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001876 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01001877 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00001878 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00001879 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001880 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001881 Location maybe_temp =
1882 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00001883 // Note that potential implicit null checks are handled in this
1884 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1885 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1886 instruction,
1887 out,
1888 base,
1889 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001890 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08001891 /* needs_null_check= */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001892 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001893 } else {
1894 // General case.
1895 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001896 // Note that a potential implicit null check is handled in this
1897 // CodeGeneratorARM64::LoadAcquire call.
1898 // NB: LoadAcquire will record the pc info if needed.
1899 codegen_->LoadAcquire(
Andreas Gampe3db70682018-12-26 15:12:03 -08001900 instruction, OutputCPURegister(instruction), field, /* needs_null_check= */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001901 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00001902 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1903 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01001904 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001905 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001906 }
Vladimir Marko61b92282017-10-11 13:23:17 +01001907 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00001908 // If read barriers are enabled, emit read barriers other than
1909 // Baker's using a slow path (and also unpoison the loaded
1910 // reference, if heap poisoning is enabled).
1911 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1912 }
Roland Levillain4d027112015-07-01 15:41:14 +01001913 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001914}
1915
1916void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1917 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001918 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01001919 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001920 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1921 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001922 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001923 locations->SetInAt(1, Location::RequiresFpuRegister());
1924 } else {
1925 locations->SetInAt(1, Location::RequiresRegister());
1926 }
1927}
1928
1929void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001930 const FieldInfo& field_info,
1931 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001932 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
1933
1934 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001935 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001936 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001937 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001938 DataType::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001939
Roland Levillain4d027112015-07-01 15:41:14 +01001940 {
1941 // We use a block to end the scratch scope before the write barrier, thus
1942 // freeing the temporary registers so they can be used in `MarkGCCard`.
1943 UseScratchRegisterScope temps(GetVIXLAssembler());
1944
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001945 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01001946 DCHECK(value.IsW());
1947 Register temp = temps.AcquireW();
1948 __ Mov(temp, value.W());
1949 GetAssembler()->PoisonHeapReference(temp.W());
1950 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001951 }
Roland Levillain4d027112015-07-01 15:41:14 +01001952
1953 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00001954 codegen_->StoreRelease(
Andreas Gampe3db70682018-12-26 15:12:03 -08001955 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check= */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01001956 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00001957 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
1958 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01001959 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1960 codegen_->MaybeRecordImplicitNullCheck(instruction);
1961 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001962 }
1963
1964 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001965 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001966 }
1967}
1968
Alexandre Rames67555f72014-11-18 10:55:16 +00001969void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001970 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001971
1972 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001973 case DataType::Type::kInt32:
1974 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001975 Register dst = OutputRegister(instr);
1976 Register lhs = InputRegisterAt(instr, 0);
1977 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001978 if (instr->IsAdd()) {
1979 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001980 } else if (instr->IsAnd()) {
1981 __ And(dst, lhs, rhs);
1982 } else if (instr->IsOr()) {
1983 __ Orr(dst, lhs, rhs);
1984 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001985 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001986 } else if (instr->IsRor()) {
1987 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001988 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001989 __ Ror(dst, lhs, shift);
1990 } else {
1991 // Ensure shift distance is in the same size register as the result. If
1992 // we are rotating a long and the shift comes in a w register originally,
1993 // we don't need to sxtw for use as an x since the shift distances are
1994 // all & reg_bits - 1.
1995 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1996 }
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01001997 } else if (instr->IsMin() || instr->IsMax()) {
1998 __ Cmp(lhs, rhs);
1999 __ Csel(dst, lhs, rhs, instr->IsMin() ? lt : gt);
Alexandre Rames67555f72014-11-18 10:55:16 +00002000 } else {
2001 DCHECK(instr->IsXor());
2002 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002003 }
2004 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002005 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002006 case DataType::Type::kFloat32:
2007 case DataType::Type::kFloat64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002008 FPRegister dst = OutputFPRegister(instr);
2009 FPRegister lhs = InputFPRegisterAt(instr, 0);
2010 FPRegister rhs = InputFPRegisterAt(instr, 1);
2011 if (instr->IsAdd()) {
2012 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002013 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002014 __ Fsub(dst, lhs, rhs);
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002015 } else if (instr->IsMin()) {
2016 __ Fmin(dst, lhs, rhs);
2017 } else if (instr->IsMax()) {
2018 __ Fmax(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002019 } else {
2020 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002021 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002022 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002023 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002024 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002025 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002026 }
2027}
2028
Serban Constantinescu02164b32014-11-13 14:05:07 +00002029void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2030 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2031
Vladimir Markoca6fff82017-10-03 14:49:14 +01002032 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002033 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002034 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002035 case DataType::Type::kInt32:
2036 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002037 locations->SetInAt(0, Location::RequiresRegister());
2038 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002039 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002040 break;
2041 }
2042 default:
2043 LOG(FATAL) << "Unexpected shift type " << type;
2044 }
2045}
2046
2047void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2048 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2049
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002050 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002051 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002052 case DataType::Type::kInt32:
2053 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002054 Register dst = OutputRegister(instr);
2055 Register lhs = InputRegisterAt(instr, 0);
2056 Operand rhs = InputOperandAt(instr, 1);
2057 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002058 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002059 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002060 if (instr->IsShl()) {
2061 __ Lsl(dst, lhs, shift_value);
2062 } else if (instr->IsShr()) {
2063 __ Asr(dst, lhs, shift_value);
2064 } else {
2065 __ Lsr(dst, lhs, shift_value);
2066 }
2067 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002068 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002069
2070 if (instr->IsShl()) {
2071 __ Lsl(dst, lhs, rhs_reg);
2072 } else if (instr->IsShr()) {
2073 __ Asr(dst, lhs, rhs_reg);
2074 } else {
2075 __ Lsr(dst, lhs, rhs_reg);
2076 }
2077 }
2078 break;
2079 }
2080 default:
2081 LOG(FATAL) << "Unexpected shift operation type " << type;
2082 }
2083}
2084
Alexandre Rames5319def2014-10-23 10:03:10 +01002085void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002086 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002087}
2088
2089void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002090 HandleBinaryOp(instruction);
2091}
2092
2093void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2094 HandleBinaryOp(instruction);
2095}
2096
2097void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2098 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002099}
2100
Artem Serov7fc63502016-02-09 17:15:29 +00002101void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002102 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002103 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002104 locations->SetInAt(0, Location::RequiresRegister());
2105 // There is no immediate variant of negated bitwise instructions in AArch64.
2106 locations->SetInAt(1, Location::RequiresRegister());
2107 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2108}
2109
Artem Serov7fc63502016-02-09 17:15:29 +00002110void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002111 Register dst = OutputRegister(instr);
2112 Register lhs = InputRegisterAt(instr, 0);
2113 Register rhs = InputRegisterAt(instr, 1);
2114
2115 switch (instr->GetOpKind()) {
2116 case HInstruction::kAnd:
2117 __ Bic(dst, lhs, rhs);
2118 break;
2119 case HInstruction::kOr:
2120 __ Orn(dst, lhs, rhs);
2121 break;
2122 case HInstruction::kXor:
2123 __ Eon(dst, lhs, rhs);
2124 break;
2125 default:
2126 LOG(FATAL) << "Unreachable";
2127 }
2128}
2129
Anton Kirilov74234da2017-01-13 14:42:47 +00002130void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2131 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002132 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2133 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002134 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002135 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002136 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2137 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2138 } else {
2139 locations->SetInAt(0, Location::RequiresRegister());
2140 }
2141 locations->SetInAt(1, Location::RequiresRegister());
2142 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2143}
2144
Anton Kirilov74234da2017-01-13 14:42:47 +00002145void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2146 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002147 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002148 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002149 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002150 Register out = OutputRegister(instruction);
2151 Register left;
2152 if (kind != HInstruction::kNeg) {
2153 left = InputRegisterAt(instruction, 0);
2154 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002155 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002156 // shifter operand operation, the IR generating `right_reg` (input to the type
2157 // conversion) can have a different type from the current instruction's type,
2158 // so we manually indicate the type.
2159 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002160 Operand right_operand(0);
2161
Anton Kirilov74234da2017-01-13 14:42:47 +00002162 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2163 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002164 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2165 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002166 right_operand = Operand(right_reg,
2167 helpers::ShiftFromOpKind(op_kind),
2168 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002169 }
2170
2171 // Logical binary operations do not support extension operations in the
2172 // operand. Note that VIXL would still manage if it was passed by generating
2173 // the extension as a separate instruction.
2174 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2175 DCHECK(!right_operand.IsExtendedRegister() ||
2176 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2177 kind != HInstruction::kNeg));
2178 switch (kind) {
2179 case HInstruction::kAdd:
2180 __ Add(out, left, right_operand);
2181 break;
2182 case HInstruction::kAnd:
2183 __ And(out, left, right_operand);
2184 break;
2185 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002186 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002187 __ Neg(out, right_operand);
2188 break;
2189 case HInstruction::kOr:
2190 __ Orr(out, left, right_operand);
2191 break;
2192 case HInstruction::kSub:
2193 __ Sub(out, left, right_operand);
2194 break;
2195 case HInstruction::kXor:
2196 __ Eor(out, left, right_operand);
2197 break;
2198 default:
2199 LOG(FATAL) << "Unexpected operation kind: " << kind;
2200 UNREACHABLE();
2201 }
2202}
2203
Artem Serov328429f2016-07-06 16:23:04 +01002204void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002205 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002206 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002207 locations->SetInAt(0, Location::RequiresRegister());
2208 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002209 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002210}
2211
Roland Levillain19c54192016-11-04 13:44:09 +00002212void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002213 __ Add(OutputRegister(instruction),
2214 InputRegisterAt(instruction, 0),
2215 Operand(InputOperandAt(instruction, 1)));
2216}
2217
Artem Serove1811ed2017-04-27 16:50:47 +01002218void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2219 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002220 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002221
2222 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2223
2224 locations->SetInAt(0, Location::RequiresRegister());
2225 // For byte case we don't need to shift the index variable so we can encode the data offset into
2226 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2227 // data offset constant generation out of the loop and reduce the critical path length in the
2228 // loop.
2229 locations->SetInAt(1, shift->GetValue() == 0
2230 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2231 : Location::RequiresRegister());
2232 locations->SetInAt(2, Location::ConstantLocation(shift));
2233 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2234}
2235
2236void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2237 HIntermediateAddressIndex* instruction) {
2238 Register index_reg = InputRegisterAt(instruction, 0);
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002239 uint32_t shift = Int64FromLocation(instruction->GetLocations()->InAt(2));
Artem Serove1811ed2017-04-27 16:50:47 +01002240 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2241
2242 if (shift == 0) {
2243 __ Add(OutputRegister(instruction), index_reg, offset);
2244 } else {
2245 Register offset_reg = InputRegisterAt(instruction, 1);
2246 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2247 }
2248}
2249
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002250void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002251 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002252 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002253 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2254 if (instr->GetOpKind() == HInstruction::kSub &&
2255 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002256 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002257 // Don't allocate register for Mneg instruction.
2258 } else {
2259 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2260 Location::RequiresRegister());
2261 }
2262 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2263 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002264 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2265}
2266
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002267void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002268 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002269 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2270 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002271
2272 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2273 // This fixup should be carried out for all multiply-accumulate instructions:
2274 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002275 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002276 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2277 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002278 vixl::aarch64::Instruction* prev =
2279 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002280 if (prev->IsLoadOrStore()) {
2281 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002282 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002283 __ nop();
2284 }
2285 }
2286
2287 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002288 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002289 __ Madd(res, mul_left, mul_right, accumulator);
2290 } else {
2291 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002292 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002293 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002294 __ Mneg(res, mul_left, mul_right);
2295 } else {
2296 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2297 __ Msub(res, mul_left, mul_right, accumulator);
2298 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002299 }
2300}
2301
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002302void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002303 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002304 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002305 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002306 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2307 object_array_get_with_read_barrier
2308 ? LocationSummary::kCallOnSlowPath
2309 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002310 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002311 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002312 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002313 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002314 // We need a temporary register for the read barrier load in
2315 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2316 // only if the offset is too big.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002317 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2318 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002319 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002320 if (offset >= kReferenceLoadMinFarOffset) {
2321 locations->AddTemp(FixedTempLocation());
2322 }
Artem Serov0806f582018-10-11 20:14:20 +01002323 } else if (!instruction->GetArray()->IsIntermediateAddress()) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01002324 // We need a non-scratch temporary for the array data pointer in
Artem Serov0806f582018-10-11 20:14:20 +01002325 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier() for the case with no
2326 // intermediate address.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002327 locations->AddTemp(Location::RequiresRegister());
2328 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002329 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002330 locations->SetInAt(0, Location::RequiresRegister());
2331 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002332 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002333 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2334 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002335 // The output overlaps in the case of an object array get with
2336 // read barriers enabled: we do not want the move to overwrite the
2337 // array's location, as we need it to emit the read barrier.
2338 locations->SetOut(
2339 Location::RequiresRegister(),
2340 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002341 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002342}
2343
2344void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002345 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002346 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002347 LocationSummary* locations = instruction->GetLocations();
2348 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002349 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002350 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002351 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2352 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002353 MacroAssembler* masm = GetVIXLAssembler();
2354 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002355
Artem Serov0806f582018-10-11 20:14:20 +01002356 // The non-Baker read barrier instrumentation of object ArrayGet instructions
Roland Levillain19c54192016-11-04 13:44:09 +00002357 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002358 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002359 instruction->GetArray()->IsIntermediateAddress() &&
Artem Serov0806f582018-10-11 20:14:20 +01002360 kEmitCompilerReadBarrier &&
2361 !kUseBakerReadBarrier));
Roland Levillain19c54192016-11-04 13:44:09 +00002362
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002363 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002364 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002365 // Note that a potential implicit null check is handled in the
2366 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002367 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002368 if (index.IsConstant()) {
Artem Serov0806f582018-10-11 20:14:20 +01002369 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002370 // Array load with a constant index can be treated as a field load.
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002371 offset += Int64FromLocation(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002372 Location maybe_temp =
2373 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2374 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2375 out,
2376 obj.W(),
2377 offset,
2378 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002379 /* needs_null_check= */ false,
2380 /* use_load_acquire= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002381 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002382 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08002383 instruction, out, obj.W(), offset, index, /* needs_null_check= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002384 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002385 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002386 // General case.
2387 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002388 Register length;
2389 if (maybe_compressed_char_at) {
2390 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2391 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002392 {
2393 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2394 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2395
2396 if (instruction->GetArray()->IsIntermediateAddress()) {
2397 DCHECK_LT(count_offset, offset);
2398 int64_t adjusted_offset =
2399 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2400 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2401 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2402 } else {
2403 __ Ldr(length, HeapOperand(obj, count_offset));
2404 }
2405 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002406 }
jessicahandojo05765752016-09-09 19:01:32 -07002407 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002408 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002409 if (maybe_compressed_char_at) {
2410 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002411 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2412 "Expecting 0=compressed, 1=uncompressed");
2413 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002414 __ Ldrb(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002415 HeapOperand(obj, offset + Int64FromLocation(index)));
jessicahandojo05765752016-09-09 19:01:32 -07002416 __ B(&done);
2417 __ Bind(&uncompressed_load);
2418 __ Ldrh(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002419 HeapOperand(obj, offset + (Int64FromLocation(index) << 1)));
jessicahandojo05765752016-09-09 19:01:32 -07002420 __ Bind(&done);
2421 } else {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002422 offset += Int64FromLocation(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002423 source = HeapOperand(obj, offset);
2424 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002425 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002426 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002427 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002428 // We do not need to compute the intermediate address from the array: the
2429 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002430 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002431 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002432 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2433 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
Roland Levillain44015862016-01-22 11:47:17 +00002434 }
2435 temp = obj;
2436 } else {
2437 __ Add(temp, obj, offset);
2438 }
jessicahandojo05765752016-09-09 19:01:32 -07002439 if (maybe_compressed_char_at) {
2440 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002441 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2442 "Expecting 0=compressed, 1=uncompressed");
2443 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002444 __ Ldrb(Register(OutputCPURegister(instruction)),
2445 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2446 __ B(&done);
2447 __ Bind(&uncompressed_load);
2448 __ Ldrh(Register(OutputCPURegister(instruction)),
2449 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2450 __ Bind(&done);
2451 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002452 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002453 }
Roland Levillain44015862016-01-22 11:47:17 +00002454 }
jessicahandojo05765752016-09-09 19:01:32 -07002455 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002456 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2457 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002458 codegen_->Load(type, OutputCPURegister(instruction), source);
2459 codegen_->MaybeRecordImplicitNullCheck(instruction);
2460 }
Roland Levillain44015862016-01-22 11:47:17 +00002461
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002462 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002463 static_assert(
2464 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2465 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2466 Location obj_loc = locations->InAt(0);
2467 if (index.IsConstant()) {
2468 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2469 } else {
2470 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2471 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002472 }
Roland Levillain4d027112015-07-01 15:41:14 +01002473 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002474}
2475
Alexandre Rames5319def2014-10-23 10:03:10 +01002476void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002477 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002478 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002479 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002480}
2481
2482void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002483 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002484 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002485 {
2486 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2487 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2488 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2489 codegen_->MaybeRecordImplicitNullCheck(instruction);
2490 }
jessicahandojo05765752016-09-09 19:01:32 -07002491 // Mask out compression flag from String's array length.
2492 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002493 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002494 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002495}
2496
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002497void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002498 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002499
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002500 bool needs_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002501 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002502 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002503 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002504 locations->SetInAt(0, Location::RequiresRegister());
2505 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002506 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2507 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002508 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002509 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002510 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002511 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002512 }
2513}
2514
2515void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002516 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002517 LocationSummary* locations = instruction->GetLocations();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002518 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002519 bool needs_write_barrier =
2520 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002521
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002522 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002523 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002524 CPURegister source = value;
2525 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002526 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002527 MemOperand destination = HeapOperand(array);
2528 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002529
2530 if (!needs_write_barrier) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002531 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002532 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002533 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002534 destination = HeapOperand(array, offset);
2535 } else {
2536 UseScratchRegisterScope temps(masm);
2537 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002538 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002539 // We do not need to compute the intermediate address from the array: the
2540 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002541 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002542 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002543 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2544 DCHECK(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002545 }
2546 temp = array;
2547 } else {
2548 __ Add(temp, array, offset);
2549 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002550 destination = HeapOperand(temp,
2551 XRegisterFrom(index),
2552 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002553 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002554 }
Artem Serov914d7a82017-02-07 14:33:49 +00002555 {
2556 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2557 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2558 codegen_->Store(value_type, value, destination);
2559 codegen_->MaybeRecordImplicitNullCheck(instruction);
2560 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002561 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002562 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002563
2564 bool can_value_be_null = instruction->GetValueCanBeNull();
2565 vixl::aarch64::Label do_store;
2566 if (can_value_be_null) {
2567 __ Cbz(Register(value), &do_store);
2568 }
2569
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002570 SlowPathCodeARM64* slow_path = nullptr;
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002571 if (needs_type_check) {
2572 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
2573 codegen_->AddSlowPath(slow_path);
2574
2575 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2576 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2577 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2578
Alexandre Rames97833a02015-04-16 15:07:12 +01002579 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002580 Register temp = temps.AcquireSameSizeAs(array);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002581 Register temp2 = temps.AcquireSameSizeAs(array);
2582
2583 // Note that when Baker read barriers are enabled, the type
2584 // checks are performed without read barriers. This is fine,
2585 // even in the case where a class object is in the from-space
2586 // after the flip, as a comparison involving such a type would
2587 // not produce a false positive; it may of course produce a
2588 // false negative, in which case we would take the ArraySet
2589 // slow path.
2590
2591 // /* HeapReference<Class> */ temp = array->klass_
2592 {
2593 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2594 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2595 __ Ldr(temp, HeapOperand(array, class_offset));
2596 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames97833a02015-04-16 15:07:12 +01002597 }
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002598 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Alexandre Rames97833a02015-04-16 15:07:12 +01002599
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002600 // /* HeapReference<Class> */ temp = temp->component_type_
2601 __ Ldr(temp, HeapOperand(temp, component_offset));
2602 // /* HeapReference<Class> */ temp2 = value->klass_
2603 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2604 // If heap poisoning is enabled, no need to unpoison `temp`
2605 // nor `temp2`, as we are comparing two poisoned references.
2606 __ Cmp(temp, temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002607
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002608 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2609 vixl::aarch64::Label do_put;
2610 __ B(eq, &do_put);
2611 // If heap poisoning is enabled, the `temp` reference has
2612 // not been unpoisoned yet; unpoison it now.
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002613 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002614
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002615 // /* HeapReference<Class> */ temp = temp->super_class_
2616 __ Ldr(temp, HeapOperand(temp, super_offset));
2617 // If heap poisoning is enabled, no need to unpoison
2618 // `temp`, as we are comparing against null below.
2619 __ Cbnz(temp, slow_path->GetEntryLabel());
2620 __ Bind(&do_put);
Vladimir Markod1ef8732017-04-18 13:55:13 +01002621 } else {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002622 __ B(ne, slow_path->GetEntryLabel());
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002623 }
2624 }
2625
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002626 codegen_->MarkGCCard(array, value.W(), /* value_can_be_null= */ false);
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002627
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002628 if (can_value_be_null) {
2629 DCHECK(do_store.IsLinked());
2630 __ Bind(&do_store);
2631 }
2632
2633 UseScratchRegisterScope temps(masm);
2634 if (kPoisonHeapReferences) {
2635 Register temp_source = temps.AcquireSameSizeAs(array);
2636 DCHECK(value.IsW());
2637 __ Mov(temp_source, value.W());
2638 GetAssembler()->PoisonHeapReference(temp_source);
2639 source = temp_source;
2640 }
2641
2642 if (index.IsConstant()) {
2643 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
2644 destination = HeapOperand(array, offset);
2645 } else {
2646 Register temp_base = temps.AcquireSameSizeAs(array);
2647 __ Add(temp_base, array, offset);
2648 destination = HeapOperand(temp_base,
2649 XRegisterFrom(index),
2650 LSL,
2651 DataType::SizeShift(value_type));
2652 }
2653
2654 {
2655 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2656 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2657 __ Str(source, destination);
2658
2659 if (can_value_be_null || !needs_type_check) {
2660 codegen_->MaybeRecordImplicitNullCheck(instruction);
2661 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002662 }
2663
2664 if (slow_path != nullptr) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002665 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002666 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002667 }
2668}
2669
Alexandre Rames67555f72014-11-18 10:55:16 +00002670void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002671 RegisterSet caller_saves = RegisterSet::Empty();
2672 InvokeRuntimeCallingConvention calling_convention;
2673 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2674 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2675 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002676 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002677 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002678}
2679
2680void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002681 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002682 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002683 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002684 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2685 __ B(slow_path->GetEntryLabel(), hs);
2686}
2687
Alexandre Rames67555f72014-11-18 10:55:16 +00002688void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2689 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002690 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00002691 locations->SetInAt(0, Location::RequiresRegister());
2692 if (check->HasUses()) {
2693 locations->SetOut(Location::SameAsFirstInput());
2694 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01002695 // Rely on the type initialization to save everything we need.
2696 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Alexandre Rames67555f72014-11-18 10:55:16 +00002697}
2698
2699void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2700 // We assume the class is not null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01002701 SlowPathCodeARM64* slow_path =
2702 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(check->GetLoadClass(), check);
Alexandre Rames67555f72014-11-18 10:55:16 +00002703 codegen_->AddSlowPath(slow_path);
2704 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2705}
2706
Roland Levillain1a653882016-03-18 18:05:57 +00002707static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2708 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2709 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2710}
2711
2712void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2713 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2714 Location rhs_loc = instruction->GetLocations()->InAt(1);
2715 if (rhs_loc.IsConstant()) {
2716 // 0.0 is the only immediate that can be encoded directly in
2717 // an FCMP instruction.
2718 //
2719 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2720 // specify that in a floating-point comparison, positive zero
2721 // and negative zero are considered equal, so we can use the
2722 // literal 0.0 for both cases here.
2723 //
2724 // Note however that some methods (Float.equal, Float.compare,
2725 // Float.compareTo, Double.equal, Double.compare,
2726 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2727 // StrictMath.min) consider 0.0 to be (strictly) greater than
2728 // -0.0. So if we ever translate calls to these methods into a
2729 // HCompare instruction, we must handle the -0.0 case with
2730 // care here.
2731 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2732 __ Fcmp(lhs_reg, 0.0);
2733 } else {
2734 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2735 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002736}
2737
Serban Constantinescu02164b32014-11-13 14:05:07 +00002738void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002739 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002740 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002741 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002742 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002743 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002744 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002745 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002746 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002747 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002748 case DataType::Type::kInt32:
2749 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002750 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002751 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002752 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2753 break;
2754 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002755 case DataType::Type::kFloat32:
2756 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002757 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002758 locations->SetInAt(1,
2759 IsFloatingPointZeroConstant(compare->InputAt(1))
2760 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2761 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002762 locations->SetOut(Location::RequiresRegister());
2763 break;
2764 }
2765 default:
2766 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2767 }
2768}
2769
2770void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002771 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002772
2773 // 0 if: left == right
2774 // 1 if: left > right
2775 // -1 if: left < right
2776 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002777 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002778 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002779 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002780 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002781 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002782 case DataType::Type::kInt32:
2783 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002784 Register result = OutputRegister(compare);
2785 Register left = InputRegisterAt(compare, 0);
2786 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002787 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002788 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2789 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002790 break;
2791 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002792 case DataType::Type::kFloat32:
2793 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002794 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002795 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002796 __ Cset(result, ne);
2797 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002798 break;
2799 }
2800 default:
2801 LOG(FATAL) << "Unimplemented compare type " << in_type;
2802 }
2803}
2804
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002805void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002806 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002807
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002808 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002809 locations->SetInAt(0, Location::RequiresFpuRegister());
2810 locations->SetInAt(1,
2811 IsFloatingPointZeroConstant(instruction->InputAt(1))
2812 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2813 : Location::RequiresFpuRegister());
2814 } else {
2815 // Integer cases.
2816 locations->SetInAt(0, Location::RequiresRegister());
2817 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2818 }
2819
David Brazdilb3e773e2016-01-26 11:28:37 +00002820 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002821 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002822 }
2823}
2824
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002825void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002826 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002827 return;
2828 }
2829
2830 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002831 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002832 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002833
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002834 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002835 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002836 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002837 } else {
2838 // Integer cases.
2839 Register lhs = InputRegisterAt(instruction, 0);
2840 Operand rhs = InputOperandAt(instruction, 1);
2841 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002842 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002843 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002844}
2845
2846#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2847 M(Equal) \
2848 M(NotEqual) \
2849 M(LessThan) \
2850 M(LessThanOrEqual) \
2851 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002852 M(GreaterThanOrEqual) \
2853 M(Below) \
2854 M(BelowOrEqual) \
2855 M(Above) \
2856 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002857#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002858void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2859void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002860FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002861#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002862#undef FOR_EACH_CONDITION_INSTRUCTION
2863
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01002864void InstructionCodeGeneratorARM64::GenerateIntDivForPower2Denom(HDiv* instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002865 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002866 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01002867 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
2868
2869 Register out = OutputRegister(instruction);
2870 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01002871
2872 if (abs_imm == 2) {
2873 int bits = DataType::Size(instruction->GetResultType()) * kBitsPerByte;
2874 __ Add(out, dividend, Operand(dividend, LSR, bits - 1));
2875 } else {
2876 UseScratchRegisterScope temps(GetVIXLAssembler());
2877 Register temp = temps.AcquireSameSizeAs(out);
2878 __ Add(temp, dividend, abs_imm - 1);
2879 __ Cmp(dividend, 0);
2880 __ Csel(out, temp, dividend, lt);
2881 }
2882
Zheng Xuc6667102015-05-15 16:08:45 +08002883 int ctz_imm = CTZ(abs_imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01002884 if (imm > 0) {
2885 __ Asr(out, out, ctz_imm);
Zheng Xuc6667102015-05-15 16:08:45 +08002886 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01002887 __ Neg(out, Operand(out, ASR, ctz_imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002888 }
2889}
2890
2891void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2892 DCHECK(instruction->IsDiv() || instruction->IsRem());
2893
2894 LocationSummary* locations = instruction->GetLocations();
2895 Location second = locations->InAt(1);
2896 DCHECK(second.IsConstant());
2897
2898 Register out = OutputRegister(instruction);
2899 Register dividend = InputRegisterAt(instruction, 0);
2900 int64_t imm = Int64FromConstant(second.GetConstant());
2901
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002902 DataType::Type type = instruction->GetResultType();
2903 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08002904
2905 int64_t magic;
2906 int shift;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002907 CalculateMagicAndShiftForDivRem(
Andreas Gampe3db70682018-12-26 15:12:03 -08002908 imm, /* is_long= */ type == DataType::Type::kInt64, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08002909
2910 UseScratchRegisterScope temps(GetVIXLAssembler());
2911 Register temp = temps.AcquireSameSizeAs(out);
2912
2913 // temp = get_high(dividend * magic)
2914 __ Mov(temp, magic);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002915 if (type == DataType::Type::kInt64) {
Zheng Xuc6667102015-05-15 16:08:45 +08002916 __ Smulh(temp, dividend, temp);
2917 } else {
2918 __ Smull(temp.X(), dividend, temp);
2919 __ Lsr(temp.X(), temp.X(), 32);
2920 }
2921
2922 if (imm > 0 && magic < 0) {
2923 __ Add(temp, temp, dividend);
2924 } else if (imm < 0 && magic > 0) {
2925 __ Sub(temp, temp, dividend);
2926 }
2927
2928 if (shift != 0) {
2929 __ Asr(temp, temp, shift);
2930 }
2931
2932 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002933 __ Sub(out, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08002934 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002935 __ Sub(temp, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08002936 // TODO: Strength reduction for msub.
2937 Register temp_imm = temps.AcquireSameSizeAs(out);
2938 __ Mov(temp_imm, imm);
2939 __ Msub(out, temp, temp_imm, dividend);
2940 }
2941}
2942
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01002943void InstructionCodeGeneratorARM64::GenerateIntDivForConstDenom(HDiv *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002944 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Zheng Xuc6667102015-05-15 16:08:45 +08002945
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01002946 if (imm == 0) {
2947 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2948 return;
2949 }
Zheng Xuc6667102015-05-15 16:08:45 +08002950
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01002951 if (IsPowerOfTwo(AbsOrMin(imm))) {
2952 GenerateIntDivForPower2Denom(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08002953 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01002954 // Cases imm == -1 or imm == 1 are handled by InstructionSimplifier.
2955 DCHECK(imm < -2 || imm > 2) << imm;
2956 GenerateDivRemWithAnyConstant(instruction);
2957 }
2958}
2959
2960void InstructionCodeGeneratorARM64::GenerateIntDiv(HDiv *instruction) {
2961 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
2962 << instruction->GetResultType();
2963
2964 if (instruction->GetLocations()->InAt(1).IsConstant()) {
2965 GenerateIntDivForConstDenom(instruction);
2966 } else {
2967 Register out = OutputRegister(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08002968 Register dividend = InputRegisterAt(instruction, 0);
2969 Register divisor = InputRegisterAt(instruction, 1);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01002970 __ Sdiv(out, dividend, divisor);
Zheng Xuc6667102015-05-15 16:08:45 +08002971 }
2972}
2973
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002974void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2975 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002976 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002977 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002978 case DataType::Type::kInt32:
2979 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002980 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002981 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002982 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2983 break;
2984
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002985 case DataType::Type::kFloat32:
2986 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002987 locations->SetInAt(0, Location::RequiresFpuRegister());
2988 locations->SetInAt(1, Location::RequiresFpuRegister());
2989 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2990 break;
2991
2992 default:
2993 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2994 }
2995}
2996
2997void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002998 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002999 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003000 case DataType::Type::kInt32:
3001 case DataType::Type::kInt64:
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003002 GenerateIntDiv(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003003 break;
3004
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003005 case DataType::Type::kFloat32:
3006 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003007 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3008 break;
3009
3010 default:
3011 LOG(FATAL) << "Unexpected div type " << type;
3012 }
3013}
3014
Alexandre Rames67555f72014-11-18 10:55:16 +00003015void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003016 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003017 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003018}
3019
3020void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3021 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003022 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003023 codegen_->AddSlowPath(slow_path);
3024 Location value = instruction->GetLocations()->InAt(0);
3025
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003026 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003027
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003028 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003029 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Elliott Hughesc1896c92018-11-29 11:33:18 -08003030 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003031 }
3032
Alexandre Rames67555f72014-11-18 10:55:16 +00003033 if (value.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003034 int64_t divisor = Int64FromLocation(value);
Alexandre Rames67555f72014-11-18 10:55:16 +00003035 if (divisor == 0) {
3036 __ B(slow_path->GetEntryLabel());
3037 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003038 // A division by a non-null constant is valid. We don't need to perform
3039 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003040 }
3041 } else {
3042 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3043 }
3044}
3045
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003046void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3047 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003048 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003049 locations->SetOut(Location::ConstantLocation(constant));
3050}
3051
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003052void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3053 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003054 // Will be generated at use site.
3055}
3056
Alexandre Rames5319def2014-10-23 10:03:10 +01003057void LocationsBuilderARM64::VisitExit(HExit* exit) {
3058 exit->SetLocations(nullptr);
3059}
3060
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003061void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003062}
3063
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003064void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3065 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003066 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003067 locations->SetOut(Location::ConstantLocation(constant));
3068}
3069
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003070void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003071 // Will be generated at use site.
3072}
3073
David Brazdilfc6a86a2015-06-26 10:33:45 +00003074void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003075 if (successor->IsExitBlock()) {
3076 DCHECK(got->GetPrevious()->AlwaysThrows());
3077 return; // no code needed
3078 }
3079
Serban Constantinescu02164b32014-11-13 14:05:07 +00003080 HBasicBlock* block = got->GetBlock();
3081 HInstruction* previous = got->GetPrevious();
3082 HLoopInformation* info = block->GetLoopInformation();
3083
David Brazdil46e2a392015-03-16 17:31:52 +00003084 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00003085 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
3086 UseScratchRegisterScope temps(GetVIXLAssembler());
3087 Register temp1 = temps.AcquireX();
3088 Register temp2 = temps.AcquireX();
3089 __ Ldr(temp1, MemOperand(sp, 0));
3090 __ Ldrh(temp2, MemOperand(temp1, ArtMethod::HotnessCountOffset().Int32Value()));
3091 __ Add(temp2, temp2, 1);
3092 __ Strh(temp2, MemOperand(temp1, ArtMethod::HotnessCountOffset().Int32Value()));
3093 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003094 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3095 return;
3096 }
3097 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3098 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08003099 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003100 }
3101 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003102 __ B(codegen_->GetLabelOf(successor));
3103 }
3104}
3105
David Brazdilfc6a86a2015-06-26 10:33:45 +00003106void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3107 got->SetLocations(nullptr);
3108}
3109
3110void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3111 HandleGoto(got, got->GetSuccessor());
3112}
3113
3114void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3115 try_boundary->SetLocations(nullptr);
3116}
3117
3118void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3119 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3120 if (!successor->IsExitBlock()) {
3121 HandleGoto(try_boundary, successor);
3122 }
3123}
3124
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003125void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003126 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003127 vixl::aarch64::Label* true_target,
3128 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003129 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003130
David Brazdil0debae72015-11-12 18:37:00 +00003131 if (true_target == nullptr && false_target == nullptr) {
3132 // Nothing to do. The code always falls through.
3133 return;
3134 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003135 // Constant condition, statically compared against "true" (integer value 1).
3136 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003137 if (true_target != nullptr) {
3138 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003139 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003140 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003141 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003142 if (false_target != nullptr) {
3143 __ B(false_target);
3144 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003145 }
David Brazdil0debae72015-11-12 18:37:00 +00003146 return;
3147 }
3148
3149 // The following code generates these patterns:
3150 // (1) true_target == nullptr && false_target != nullptr
3151 // - opposite condition true => branch to false_target
3152 // (2) true_target != nullptr && false_target == nullptr
3153 // - condition true => branch to true_target
3154 // (3) true_target != nullptr && false_target != nullptr
3155 // - condition true => branch to true_target
3156 // - branch to false_target
3157 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003158 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003159 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003160 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003161 if (true_target == nullptr) {
3162 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3163 } else {
3164 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3165 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003166 } else {
3167 // The condition instruction has not been materialized, use its inputs as
3168 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003169 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003170
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003171 DataType::Type type = condition->InputAt(0)->GetType();
3172 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003173 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003174 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003175 IfCondition opposite_condition = condition->GetOppositeCondition();
3176 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003177 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003178 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003179 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003180 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003181 // Integer cases.
3182 Register lhs = InputRegisterAt(condition, 0);
3183 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003184
3185 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003186 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003187 if (true_target == nullptr) {
3188 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3189 non_fallthrough_target = false_target;
3190 } else {
3191 arm64_cond = ARM64Condition(condition->GetCondition());
3192 non_fallthrough_target = true_target;
3193 }
3194
Aart Bik086d27e2016-01-20 17:02:00 -08003195 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003196 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003197 switch (arm64_cond) {
3198 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003199 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003200 break;
3201 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003202 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003203 break;
3204 case lt:
3205 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003206 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003207 break;
3208 case ge:
3209 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003210 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003211 break;
3212 default:
3213 // Without the `static_cast` the compiler throws an error for
3214 // `-Werror=sign-promo`.
3215 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3216 }
3217 } else {
3218 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003219 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003220 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003221 }
3222 }
David Brazdil0debae72015-11-12 18:37:00 +00003223
3224 // If neither branch falls through (case 3), the conditional branch to `true_target`
3225 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3226 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003227 __ B(false_target);
3228 }
3229}
3230
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003231void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003232 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003233 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003234 locations->SetInAt(0, Location::RequiresRegister());
3235 }
3236}
3237
3238void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003239 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3240 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003241 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3242 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3243 true_target = nullptr;
3244 }
3245 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3246 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3247 false_target = nullptr;
3248 }
Andreas Gampe3db70682018-12-26 15:12:03 -08003249 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003250}
3251
3252void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003253 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003254 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003255 InvokeRuntimeCallingConvention calling_convention;
3256 RegisterSet caller_saves = RegisterSet::Empty();
3257 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3258 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003259 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003260 locations->SetInAt(0, Location::RequiresRegister());
3261 }
3262}
3263
3264void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003265 SlowPathCodeARM64* slow_path =
3266 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003267 GenerateTestAndBranch(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08003268 /* condition_input_index= */ 0,
David Brazdil0debae72015-11-12 18:37:00 +00003269 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08003270 /* false_target= */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003271}
3272
Mingyao Yang063fc772016-08-02 11:02:54 -07003273void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003274 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003275 LocationSummary(flag, LocationSummary::kNoCall);
3276 locations->SetOut(Location::RequiresRegister());
3277}
3278
3279void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3280 __ Ldr(OutputRegister(flag),
3281 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3282}
3283
David Brazdilc0b601b2016-02-08 14:20:45 +00003284static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3285 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003286 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003287}
3288
Alexandre Rames880f1192016-06-13 16:04:50 +01003289static inline Condition GetConditionForSelect(HCondition* condition) {
3290 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003291 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3292 : ARM64Condition(cond);
3293}
3294
David Brazdil74eb1b22015-12-14 11:44:01 +00003295void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003296 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003297 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003298 locations->SetInAt(0, Location::RequiresFpuRegister());
3299 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003300 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003301 } else {
3302 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3303 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3304 bool is_true_value_constant = cst_true_value != nullptr;
3305 bool is_false_value_constant = cst_false_value != nullptr;
3306 // Ask VIXL whether we should synthesize constants in registers.
3307 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3308 Operand true_op = is_true_value_constant ?
3309 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3310 Operand false_op = is_false_value_constant ?
3311 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3312 bool true_value_in_register = false;
3313 bool false_value_in_register = false;
3314 MacroAssembler::GetCselSynthesisInformation(
3315 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3316 true_value_in_register |= !is_true_value_constant;
3317 false_value_in_register |= !is_false_value_constant;
3318
3319 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3320 : Location::ConstantLocation(cst_true_value));
3321 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3322 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003323 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003324 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003325
David Brazdil74eb1b22015-12-14 11:44:01 +00003326 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3327 locations->SetInAt(2, Location::RequiresRegister());
3328 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003329}
3330
3331void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003332 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003333 Condition csel_cond;
3334
3335 if (IsBooleanValueOrMaterializedCondition(cond)) {
3336 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003337 // Use the condition flags set by the previous instruction.
3338 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003339 } else {
3340 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003341 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003342 }
3343 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003344 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003345 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003346 } else {
3347 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003348 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003349 }
3350
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003351 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003352 __ Fcsel(OutputFPRegister(select),
3353 InputFPRegisterAt(select, 1),
3354 InputFPRegisterAt(select, 0),
3355 csel_cond);
3356 } else {
3357 __ Csel(OutputRegister(select),
3358 InputOperandAt(select, 1),
3359 InputOperandAt(select, 0),
3360 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003361 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003362}
3363
David Srbecky0cf44932015-12-09 14:09:59 +00003364void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003365 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00003366}
3367
David Srbeckyd28f4a02016-03-14 17:14:24 +00003368void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3369 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003370}
3371
3372void CodeGeneratorARM64::GenerateNop() {
3373 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003374}
3375
Alexandre Rames5319def2014-10-23 10:03:10 +01003376void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003377 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003378}
3379
3380void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003381 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003382}
3383
3384void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003385 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003386}
3387
3388void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003389 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003390}
3391
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003392// Temp is used for read barrier.
3393static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3394 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003395 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003396 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3397 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3398 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3399 return 1;
3400 }
3401 return 0;
3402}
3403
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003404// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003405// interface pointer, one for loading the current interface.
3406// The other checks have one temp for loading the object's class.
3407static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3408 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3409 return 3;
3410 }
3411 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003412}
3413
Alexandre Rames67555f72014-11-18 10:55:16 +00003414void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003415 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003416 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003417 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003418 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003419 case TypeCheckKind::kExactCheck:
3420 case TypeCheckKind::kAbstractClassCheck:
3421 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00003422 case TypeCheckKind::kArrayObjectCheck: {
3423 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
3424 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
3425 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003426 break;
Vladimir Marko87584542017-12-12 17:47:52 +00003427 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003428 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003429 case TypeCheckKind::kUnresolvedCheck:
3430 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003431 call_kind = LocationSummary::kCallOnSlowPath;
3432 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00003433 case TypeCheckKind::kBitstringCheck:
3434 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003435 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003436
Vladimir Markoca6fff82017-10-03 14:49:14 +01003437 LocationSummary* locations =
3438 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003439 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003440 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003441 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003442 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003443 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3444 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3445 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3446 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3447 } else {
3448 locations->SetInAt(1, Location::RequiresRegister());
3449 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003450 // The "out" register is used as a temporary, so it overlaps with the inputs.
3451 // Note that TypeCheckSlowPathARM64 uses this register too.
3452 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003453 // Add temps if necessary for read barriers.
3454 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003455}
3456
3457void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003458 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003459 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003460 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003461 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003462 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3463 ? Register()
3464 : InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003465 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003466 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003467 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3468 DCHECK_LE(num_temps, 1u);
3469 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003470 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3471 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3472 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3473 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003474
Scott Wakeling97c72b72016-06-24 16:19:36 +01003475 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003476 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003477
3478 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003479 // Avoid null check if we know `obj` is not null.
3480 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003481 __ Cbz(obj, &zero);
3482 }
3483
Roland Levillain44015862016-01-22 11:47:17 +00003484 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003485 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003486 ReadBarrierOption read_barrier_option =
3487 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003488 // /* HeapReference<Class> */ out = obj->klass_
3489 GenerateReferenceLoadTwoRegisters(instruction,
3490 out_loc,
3491 obj_loc,
3492 class_offset,
3493 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003494 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003495 __ Cmp(out, cls);
3496 __ Cset(out, eq);
3497 if (zero.IsLinked()) {
3498 __ B(&done);
3499 }
3500 break;
3501 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003502
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003503 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003504 ReadBarrierOption read_barrier_option =
3505 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003506 // /* HeapReference<Class> */ out = obj->klass_
3507 GenerateReferenceLoadTwoRegisters(instruction,
3508 out_loc,
3509 obj_loc,
3510 class_offset,
3511 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003512 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003513 // If the class is abstract, we eagerly fetch the super class of the
3514 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003515 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003516 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003517 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003518 GenerateReferenceLoadOneRegister(instruction,
3519 out_loc,
3520 super_offset,
3521 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003522 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003523 // If `out` is null, we use it for the result, and jump to `done`.
3524 __ Cbz(out, &done);
3525 __ Cmp(out, cls);
3526 __ B(ne, &loop);
3527 __ Mov(out, 1);
3528 if (zero.IsLinked()) {
3529 __ B(&done);
3530 }
3531 break;
3532 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003533
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003534 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003535 ReadBarrierOption read_barrier_option =
3536 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003537 // /* HeapReference<Class> */ out = obj->klass_
3538 GenerateReferenceLoadTwoRegisters(instruction,
3539 out_loc,
3540 obj_loc,
3541 class_offset,
3542 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003543 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003544 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003545 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003546 __ Bind(&loop);
3547 __ Cmp(out, cls);
3548 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003549 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003550 GenerateReferenceLoadOneRegister(instruction,
3551 out_loc,
3552 super_offset,
3553 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003554 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003555 __ Cbnz(out, &loop);
3556 // If `out` is null, we use it for the result, and jump to `done`.
3557 __ B(&done);
3558 __ Bind(&success);
3559 __ Mov(out, 1);
3560 if (zero.IsLinked()) {
3561 __ B(&done);
3562 }
3563 break;
3564 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003565
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003566 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003567 ReadBarrierOption read_barrier_option =
3568 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003569 // /* HeapReference<Class> */ out = obj->klass_
3570 GenerateReferenceLoadTwoRegisters(instruction,
3571 out_loc,
3572 obj_loc,
3573 class_offset,
3574 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003575 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003576 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003577 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003578 __ Cmp(out, cls);
3579 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003580 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003581 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003582 GenerateReferenceLoadOneRegister(instruction,
3583 out_loc,
3584 component_offset,
3585 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003586 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003587 // If `out` is null, we use it for the result, and jump to `done`.
3588 __ Cbz(out, &done);
3589 __ Ldrh(out, HeapOperand(out, primitive_offset));
3590 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3591 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003592 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003593 __ Mov(out, 1);
3594 __ B(&done);
3595 break;
3596 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003597
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003598 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003599 // No read barrier since the slow path will retry upon failure.
3600 // /* HeapReference<Class> */ out = obj->klass_
3601 GenerateReferenceLoadTwoRegisters(instruction,
3602 out_loc,
3603 obj_loc,
3604 class_offset,
3605 maybe_temp_loc,
3606 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003607 __ Cmp(out, cls);
3608 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01003609 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08003610 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003611 codegen_->AddSlowPath(slow_path);
3612 __ B(ne, slow_path->GetEntryLabel());
3613 __ Mov(out, 1);
3614 if (zero.IsLinked()) {
3615 __ B(&done);
3616 }
3617 break;
3618 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003619
Calin Juravle98893e12015-10-02 21:05:03 +01003620 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003621 case TypeCheckKind::kInterfaceCheck: {
3622 // Note that we indeed only call on slow path, but we always go
3623 // into the slow path for the unresolved and interface check
3624 // cases.
3625 //
3626 // We cannot directly call the InstanceofNonTrivial runtime
3627 // entry point without resorting to a type checking slow path
3628 // here (i.e. by calling InvokeRuntime directly), as it would
3629 // require to assign fixed registers for the inputs of this
3630 // HInstanceOf instruction (following the runtime calling
3631 // convention), which might be cluttered by the potential first
3632 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003633 //
3634 // TODO: Introduce a new runtime entry point taking the object
3635 // to test (instead of its class) as argument, and let it deal
3636 // with the read barrier issues. This will let us refactor this
3637 // case of the `switch` code as it was previously (with a direct
3638 // call to the runtime not using a type checking slow path).
3639 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003640 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01003641 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08003642 instruction, /* is_fatal= */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003643 codegen_->AddSlowPath(slow_path);
3644 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003645 if (zero.IsLinked()) {
3646 __ B(&done);
3647 }
3648 break;
3649 }
Vladimir Marko175e7862018-03-27 09:03:13 +00003650
3651 case TypeCheckKind::kBitstringCheck: {
3652 // /* HeapReference<Class> */ temp = obj->klass_
3653 GenerateReferenceLoadTwoRegisters(instruction,
3654 out_loc,
3655 obj_loc,
3656 class_offset,
3657 maybe_temp_loc,
3658 kWithoutReadBarrier);
3659
3660 GenerateBitstringTypeCheckCompare(instruction, out);
3661 __ Cset(out, eq);
3662 if (zero.IsLinked()) {
3663 __ B(&done);
3664 }
3665 break;
3666 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003667 }
3668
3669 if (zero.IsLinked()) {
3670 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003671 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003672 }
3673
3674 if (done.IsLinked()) {
3675 __ Bind(&done);
3676 }
3677
3678 if (slow_path != nullptr) {
3679 __ Bind(slow_path->GetExitLabel());
3680 }
3681}
3682
3683void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003684 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00003685 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003686 LocationSummary* locations =
3687 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003688 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003689 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3690 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3691 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3692 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3693 } else {
3694 locations->SetInAt(1, Location::RequiresRegister());
3695 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003696 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
3697 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003698}
3699
3700void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003701 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003702 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003703 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003704 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003705 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3706 ? Register()
3707 : InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003708 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3709 DCHECK_GE(num_temps, 1u);
3710 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003711 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003712 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
3713 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003714 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003715 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3716 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3717 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3718 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3719 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3720 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3721 const uint32_t object_array_data_offset =
3722 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003723
Vladimir Marko87584542017-12-12 17:47:52 +00003724 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003725 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003726 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
3727 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003728 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003729
Scott Wakeling97c72b72016-06-24 16:19:36 +01003730 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003731 // Avoid null check if we know obj is not null.
3732 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003733 __ Cbz(obj, &done);
3734 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003735
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003736 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003737 case TypeCheckKind::kExactCheck:
3738 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003739 // /* HeapReference<Class> */ temp = obj->klass_
3740 GenerateReferenceLoadTwoRegisters(instruction,
3741 temp_loc,
3742 obj_loc,
3743 class_offset,
3744 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003745 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003746
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003747 __ Cmp(temp, cls);
3748 // Jump to slow path for throwing the exception or doing a
3749 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003750 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003751 break;
3752 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003753
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003754 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003755 // /* HeapReference<Class> */ temp = obj->klass_
3756 GenerateReferenceLoadTwoRegisters(instruction,
3757 temp_loc,
3758 obj_loc,
3759 class_offset,
3760 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003761 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003762
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003763 // If the class is abstract, we eagerly fetch the super class of the
3764 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003765 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003766 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003767 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003768 GenerateReferenceLoadOneRegister(instruction,
3769 temp_loc,
3770 super_offset,
3771 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003772 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003773
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003774 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3775 // exception.
3776 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3777 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003778 __ Cmp(temp, cls);
3779 __ B(ne, &loop);
3780 break;
3781 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003782
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003783 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003784 // /* HeapReference<Class> */ temp = obj->klass_
3785 GenerateReferenceLoadTwoRegisters(instruction,
3786 temp_loc,
3787 obj_loc,
3788 class_offset,
3789 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003790 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003791
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003792 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003793 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003794 __ Bind(&loop);
3795 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003796 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003797
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003798 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003799 GenerateReferenceLoadOneRegister(instruction,
3800 temp_loc,
3801 super_offset,
3802 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003803 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003804
3805 // If the class reference currently in `temp` is not null, jump
3806 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003807 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003808 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003809 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003810 break;
3811 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003812
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003813 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003814 // /* HeapReference<Class> */ temp = obj->klass_
3815 GenerateReferenceLoadTwoRegisters(instruction,
3816 temp_loc,
3817 obj_loc,
3818 class_offset,
3819 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003820 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003821
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003822 // Do an exact check.
3823 __ Cmp(temp, cls);
3824 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003825
3826 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003827 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003828 GenerateReferenceLoadOneRegister(instruction,
3829 temp_loc,
3830 component_offset,
3831 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003832 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003833
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003834 // If the component type is null, jump to the slow path to throw the exception.
3835 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3836 // Otherwise, the object is indeed an array. Further check that this component type is not a
3837 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003838 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3839 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003840 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003841 break;
3842 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003843
Calin Juravle98893e12015-10-02 21:05:03 +01003844 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003845 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003846 //
3847 // We cannot directly call the CheckCast runtime entry point
3848 // without resorting to a type checking slow path here (i.e. by
3849 // calling InvokeRuntime directly), as it would require to
3850 // assign fixed registers for the inputs of this HInstanceOf
3851 // instruction (following the runtime calling convention), which
3852 // might be cluttered by the potential first read barrier
3853 // emission at the beginning of this method.
3854 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003855 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003856 case TypeCheckKind::kInterfaceCheck: {
3857 // /* HeapReference<Class> */ temp = obj->klass_
3858 GenerateReferenceLoadTwoRegisters(instruction,
3859 temp_loc,
3860 obj_loc,
3861 class_offset,
3862 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003863 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003864
3865 // /* HeapReference<Class> */ temp = temp->iftable_
3866 GenerateReferenceLoadTwoRegisters(instruction,
3867 temp_loc,
3868 temp_loc,
3869 iftable_offset,
3870 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003871 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003872 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003873 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08003874 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003875 vixl::aarch64::Label start_loop;
3876 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003877 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003878 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
3879 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003880 // Go to next interface.
3881 __ Add(temp, temp, 2 * kHeapReferenceSize);
3882 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003883 // Compare the classes and continue the loop if they do not match.
3884 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
3885 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003886 break;
3887 }
Vladimir Marko175e7862018-03-27 09:03:13 +00003888
3889 case TypeCheckKind::kBitstringCheck: {
3890 // /* HeapReference<Class> */ temp = obj->klass_
3891 GenerateReferenceLoadTwoRegisters(instruction,
3892 temp_loc,
3893 obj_loc,
3894 class_offset,
3895 maybe_temp2_loc,
3896 kWithoutReadBarrier);
3897
3898 GenerateBitstringTypeCheckCompare(instruction, temp);
3899 __ B(ne, type_check_slow_path->GetEntryLabel());
3900 break;
3901 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003902 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003903 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003904
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003905 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003906}
3907
Alexandre Rames5319def2014-10-23 10:03:10 +01003908void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003909 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01003910 locations->SetOut(Location::ConstantLocation(constant));
3911}
3912
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003913void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003914 // Will be generated at use site.
3915}
3916
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003917void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003918 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003919 locations->SetOut(Location::ConstantLocation(constant));
3920}
3921
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003922void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003923 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003924}
3925
Calin Juravle175dc732015-08-25 15:42:32 +01003926void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3927 // The trampoline uses the same calling convention as dex calling conventions,
3928 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3929 // the method_idx.
3930 HandleInvoke(invoke);
3931}
3932
3933void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3934 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08003935 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01003936}
3937
Alexandre Rames5319def2014-10-23 10:03:10 +01003938void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003939 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003940 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003941}
3942
Alexandre Rames67555f72014-11-18 10:55:16 +00003943void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3944 HandleInvoke(invoke);
3945}
3946
3947void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3948 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003949 LocationSummary* locations = invoke->GetLocations();
3950 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003951 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003952 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003953 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003954
3955 // The register ip1 is required to be used for the hidden argument in
3956 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003957 MacroAssembler* masm = GetVIXLAssembler();
3958 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003959 scratch_scope.Exclude(ip1);
3960 __ Mov(ip1, invoke->GetDexMethodIndex());
3961
Artem Serov914d7a82017-02-07 14:33:49 +00003962 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00003963 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003964 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00003965 {
3966 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3967 // /* HeapReference<Class> */ temp = temp->klass_
3968 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
3969 codegen_->MaybeRecordImplicitNullCheck(invoke);
3970 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003971 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00003972 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003973 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003974 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00003975 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00003976 }
Artem Serov914d7a82017-02-07 14:33:49 +00003977
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003978 // Instead of simply (possibly) unpoisoning `temp` here, we should
3979 // emit a read barrier for the previous class reference load.
3980 // However this is not required in practice, as this is an
3981 // intermediate/temporary reference and because the current
3982 // concurrent copying collector keeps the from-space memory
3983 // intact/accessible until the end of the marking phase (the
3984 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003985 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003986 __ Ldr(temp,
3987 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3988 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003989 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003990 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003991 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003992 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003993 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00003994
3995 {
3996 // Ensure the pc position is recorded immediately after the `blr` instruction.
3997 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
3998
3999 // lr();
4000 __ blr(lr);
4001 DCHECK(!codegen_->IsLeafMethod());
4002 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4003 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004004
Andreas Gampe3db70682018-12-26 15:12:03 -08004005 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004006}
4007
4008void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004009 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004010 if (intrinsic.TryDispatch(invoke)) {
4011 return;
4012 }
4013
Alexandre Rames67555f72014-11-18 10:55:16 +00004014 HandleInvoke(invoke);
4015}
4016
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004017void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004018 // Explicit clinit checks triggered by static invokes must have been pruned by
4019 // art::PrepareForRegisterAllocation.
4020 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004021
Vladimir Markoca6fff82017-10-03 14:49:14 +01004022 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004023 if (intrinsic.TryDispatch(invoke)) {
4024 return;
4025 }
4026
Alexandre Rames67555f72014-11-18 10:55:16 +00004027 HandleInvoke(invoke);
4028}
4029
Andreas Gampe878d58c2015-01-15 23:24:00 -08004030static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4031 if (invoke->GetLocations()->Intrinsified()) {
4032 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4033 intrinsic.Dispatch(invoke);
4034 return true;
4035 }
4036 return false;
4037}
4038
Vladimir Markodc151b22015-10-15 18:02:30 +01004039HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4040 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004041 ArtMethod* method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004042 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004043 return desired_dispatch_info;
4044}
4045
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004046void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4047 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004048 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004049 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4050 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004051 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4052 uint32_t offset =
4053 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004054 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004055 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004056 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004057 }
Vladimir Marko58155012015-08-19 12:49:41 +00004058 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004059 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004060 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004061 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4062 DCHECK(GetCompilerOptions().IsBootImage());
4063 // Add ADRP with its PC-relative method patch.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004064 vixl::aarch64::Label* adrp_label = NewBootImageMethodPatch(invoke->GetTargetMethod());
Vladimir Marko65979462017-05-19 17:25:12 +01004065 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4066 // Add ADD with its PC-relative method patch.
4067 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004068 NewBootImageMethodPatch(invoke->GetTargetMethod(), adrp_label);
Vladimir Marko65979462017-05-19 17:25:12 +01004069 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4070 break;
4071 }
Vladimir Markob066d432018-01-03 13:14:37 +00004072 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4073 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004074 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00004075 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_offset);
4076 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4077 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
4078 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_offset, adrp_label);
4079 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
4080 EmitLdrOffsetPlaceholder(ldr_label, WRegisterFrom(temp), XRegisterFrom(temp));
4081 break;
4082 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004083 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Markob066d432018-01-03 13:14:37 +00004084 // Add ADRP with its PC-relative .bss entry patch.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004085 MethodReference target_method(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4086 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(target_method);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004087 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Markob066d432018-01-03 13:14:37 +00004088 // Add LDR with its PC-relative .bss entry patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004089 vixl::aarch64::Label* ldr_label =
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004090 NewMethodBssEntryPatch(target_method, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004091 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004092 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004093 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004094 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
4095 // Load method address from literal pool.
4096 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
4097 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004098 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4099 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4100 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004101 }
4102 }
4103
4104 switch (invoke->GetCodePtrLocation()) {
4105 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004106 {
4107 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4108 ExactAssemblyScope eas(GetVIXLAssembler(),
4109 kInstructionSize,
4110 CodeBufferCheckScope::kExactSize);
4111 __ bl(&frame_entry_label_);
4112 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4113 }
Vladimir Marko58155012015-08-19 12:49:41 +00004114 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004115 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4116 // LR = callee_method->entry_point_from_quick_compiled_code_;
4117 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004118 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004119 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004120 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004121 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004122 ExactAssemblyScope eas(GetVIXLAssembler(),
4123 kInstructionSize,
4124 CodeBufferCheckScope::kExactSize);
4125 // lr()
4126 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004127 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004128 }
Vladimir Marko58155012015-08-19 12:49:41 +00004129 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004130 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004131
Andreas Gampe878d58c2015-01-15 23:24:00 -08004132 DCHECK(!IsLeafMethod());
4133}
4134
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004135void CodeGeneratorARM64::GenerateVirtualCall(
4136 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004137 // Use the calling convention instead of the location of the receiver, as
4138 // intrinsics may have put the receiver in a different register. In the intrinsics
4139 // slow path, the arguments have been moved to the right place, so here we are
4140 // guaranteed that the receiver is the first register of the calling convention.
4141 InvokeDexCallingConvention calling_convention;
4142 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004143 Register temp = XRegisterFrom(temp_in);
4144 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4145 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4146 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004147 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004148
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004149 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004150
4151 {
4152 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4153 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4154 // /* HeapReference<Class> */ temp = receiver->klass_
4155 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4156 MaybeRecordImplicitNullCheck(invoke);
4157 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004158 // Instead of simply (possibly) unpoisoning `temp` here, we should
4159 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004160 // intermediate/temporary reference and because the current
4161 // concurrent copying collector keeps the from-space memory
4162 // intact/accessible until the end of the marking phase (the
4163 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004164 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4165 // temp = temp->GetMethodAt(method_offset);
4166 __ Ldr(temp, MemOperand(temp, method_offset));
4167 // lr = temp->GetEntryPoint();
4168 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004169 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004170 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004171 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4172 // lr();
4173 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004174 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004175 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004176}
4177
Orion Hodsonac141392017-01-13 11:53:47 +00004178void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4179 HandleInvoke(invoke);
4180}
4181
4182void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4183 codegen_->GenerateInvokePolymorphicCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004184 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004185}
4186
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004187void LocationsBuilderARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4188 HandleInvoke(invoke);
4189}
4190
4191void InstructionCodeGeneratorARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4192 codegen_->GenerateInvokeCustomCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004193 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004194}
4195
Vladimir Marko6fd16062018-06-26 11:02:04 +01004196vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageIntrinsicPatch(
4197 uint32_t intrinsic_data,
4198 vixl::aarch64::Label* adrp_label) {
4199 return NewPcRelativePatch(
Andreas Gampe3db70682018-12-26 15:12:03 -08004200 /* dex_file= */ nullptr, intrinsic_data, adrp_label, &boot_image_intrinsic_patches_);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004201}
4202
Vladimir Markob066d432018-01-03 13:14:37 +00004203vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageRelRoPatch(
4204 uint32_t boot_image_offset,
4205 vixl::aarch64::Label* adrp_label) {
4206 return NewPcRelativePatch(
Andreas Gampe3db70682018-12-26 15:12:03 -08004207 /* dex_file= */ nullptr, boot_image_offset, adrp_label, &boot_image_method_patches_);
Vladimir Markob066d432018-01-03 13:14:37 +00004208}
4209
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004210vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004211 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004212 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004213 return NewPcRelativePatch(
4214 target_method.dex_file, target_method.index, adrp_label, &boot_image_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004215}
4216
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004217vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4218 MethodReference target_method,
4219 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004220 return NewPcRelativePatch(
4221 target_method.dex_file, target_method.index, adrp_label, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004222}
4223
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004224vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageTypePatch(
Scott Wakeling97c72b72016-06-24 16:19:36 +01004225 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004226 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004227 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004228 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &boot_image_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004229}
4230
Vladimir Marko1998cd02017-01-13 13:02:58 +00004231vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4232 const DexFile& dex_file,
4233 dex::TypeIndex type_index,
4234 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004235 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004236}
4237
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004238vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004239 const DexFile& dex_file,
4240 dex::StringIndex string_index,
4241 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004242 return NewPcRelativePatch(
4243 &dex_file, string_index.index_, adrp_label, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01004244}
4245
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004246vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4247 const DexFile& dex_file,
4248 dex::StringIndex string_index,
4249 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004250 return NewPcRelativePatch(&dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004251}
4252
Vladimir Marko966b46f2018-08-03 10:20:19 +00004253void CodeGeneratorARM64::EmitBakerReadBarrierCbnz(uint32_t custom_data) {
Vladimir Marko94796f82018-08-08 15:15:33 +01004254 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko966b46f2018-08-03 10:20:19 +00004255 if (Runtime::Current()->UseJitCompilation()) {
4256 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
4257 vixl::aarch64::Label* slow_path_entry = &it->second.label;
4258 __ cbnz(mr, slow_path_entry);
4259 } else {
4260 baker_read_barrier_patches_.emplace_back(custom_data);
4261 vixl::aarch64::Label* cbnz_label = &baker_read_barrier_patches_.back().label;
4262 __ bind(cbnz_label);
4263 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4264 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004265}
4266
Scott Wakeling97c72b72016-06-24 16:19:36 +01004267vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004268 const DexFile* dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004269 uint32_t offset_or_index,
4270 vixl::aarch64::Label* adrp_label,
4271 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004272 // Add a patch entry and return the label.
4273 patches->emplace_back(dex_file, offset_or_index);
4274 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004275 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004276 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4277 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4278 return label;
4279}
4280
Scott Wakeling97c72b72016-06-24 16:19:36 +01004281vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4282 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004283 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004284}
4285
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004286vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004287 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004288 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004289 return jit_string_patches_.GetOrCreate(
4290 StringReference(&dex_file, string_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004291 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004292}
4293
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004294vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004295 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004296 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004297 return jit_class_patches_.GetOrCreate(
4298 TypeReference(&dex_file, type_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004299 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004300}
4301
Vladimir Markoaad75c62016-10-03 08:46:48 +00004302void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4303 vixl::aarch64::Register reg) {
4304 DCHECK(reg.IsX());
4305 SingleEmissionCheckScope guard(GetVIXLAssembler());
4306 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004307 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004308}
4309
4310void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4311 vixl::aarch64::Register out,
4312 vixl::aarch64::Register base) {
4313 DCHECK(out.IsX());
4314 DCHECK(base.IsX());
4315 SingleEmissionCheckScope guard(GetVIXLAssembler());
4316 __ Bind(fixup_label);
4317 __ add(out, base, Operand(/* offset placeholder */ 0));
4318}
4319
4320void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4321 vixl::aarch64::Register out,
4322 vixl::aarch64::Register base) {
4323 DCHECK(base.IsX());
4324 SingleEmissionCheckScope guard(GetVIXLAssembler());
4325 __ Bind(fixup_label);
4326 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4327}
4328
Vladimir Markoeebb8212018-06-05 14:57:24 +01004329void CodeGeneratorARM64::LoadBootImageAddress(vixl::aarch64::Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01004330 uint32_t boot_image_reference) {
4331 if (GetCompilerOptions().IsBootImage()) {
4332 // Add ADRP with its PC-relative type patch.
4333 vixl::aarch64::Label* adrp_label = NewBootImageIntrinsicPatch(boot_image_reference);
4334 EmitAdrpPlaceholder(adrp_label, reg.X());
4335 // Add ADD with its PC-relative type patch.
4336 vixl::aarch64::Label* add_label = NewBootImageIntrinsicPatch(boot_image_reference, adrp_label);
4337 EmitAddPlaceholder(add_label, reg.X(), reg.X());
Vladimir Markoa2da9b92018-10-10 14:21:55 +01004338 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01004339 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004340 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004341 EmitAdrpPlaceholder(adrp_label, reg.X());
4342 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004343 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_reference, adrp_label);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004344 EmitLdrOffsetPlaceholder(ldr_label, reg.W(), reg.X());
4345 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004346 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01004347 gc::Heap* heap = Runtime::Current()->GetHeap();
4348 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004349 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01004350 __ Ldr(reg.W(), DeduplicateBootImageAddressLiteral(reinterpret_cast<uintptr_t>(address)));
4351 }
4352}
4353
Vladimir Marko6fd16062018-06-26 11:02:04 +01004354void CodeGeneratorARM64::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
4355 uint32_t boot_image_offset) {
4356 DCHECK(invoke->IsStatic());
4357 InvokeRuntimeCallingConvention calling_convention;
4358 Register argument = calling_convention.GetRegisterAt(0);
4359 if (GetCompilerOptions().IsBootImage()) {
4360 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
4361 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
4362 MethodReference target_method = invoke->GetTargetMethod();
4363 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
4364 // Add ADRP with its PC-relative type patch.
4365 vixl::aarch64::Label* adrp_label = NewBootImageTypePatch(*target_method.dex_file, type_idx);
4366 EmitAdrpPlaceholder(adrp_label, argument.X());
4367 // Add ADD with its PC-relative type patch.
4368 vixl::aarch64::Label* add_label =
4369 NewBootImageTypePatch(*target_method.dex_file, type_idx, adrp_label);
4370 EmitAddPlaceholder(add_label, argument.X(), argument.X());
4371 } else {
4372 LoadBootImageAddress(argument, boot_image_offset);
4373 }
4374 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
4375 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4376}
4377
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004378template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004379inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4380 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004381 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004382 for (const PcRelativePatchInfo& info : infos) {
4383 linker_patches->push_back(Factory(info.label.GetLocation(),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004384 info.target_dex_file,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004385 info.pc_insn_label->GetLocation(),
4386 info.offset_or_index));
4387 }
4388}
4389
Vladimir Marko6fd16062018-06-26 11:02:04 +01004390template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
4391linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
4392 const DexFile* target_dex_file,
4393 uint32_t pc_insn_offset,
4394 uint32_t boot_image_offset) {
4395 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
4396 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00004397}
4398
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004399void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004400 DCHECK(linker_patches->empty());
4401 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004402 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004403 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004404 boot_image_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004405 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004406 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004407 string_bss_entry_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01004408 boot_image_intrinsic_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004409 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004410 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01004411 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004412 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004413 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004414 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004415 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004416 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004417 boot_image_string_patches_, linker_patches);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004418 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
4419 boot_image_intrinsic_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01004420 } else {
Vladimir Marko6fd16062018-06-26 11:02:04 +01004421 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
Vladimir Markob066d432018-01-03 13:14:37 +00004422 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004423 DCHECK(boot_image_type_patches_.empty());
4424 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004425 DCHECK(boot_image_intrinsic_patches_.empty());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004426 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004427 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4428 method_bss_entry_patches_, linker_patches);
4429 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4430 type_bss_entry_patches_, linker_patches);
4431 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4432 string_bss_entry_patches_, linker_patches);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004433 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004434 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
4435 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004436 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004437 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004438}
4439
Vladimir Markoca1e0382018-04-11 09:58:41 +00004440bool CodeGeneratorARM64::NeedsThunkCode(const linker::LinkerPatch& patch) const {
4441 return patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
4442 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
4443}
4444
4445void CodeGeneratorARM64::EmitThunkCode(const linker::LinkerPatch& patch,
4446 /*out*/ ArenaVector<uint8_t>* code,
4447 /*out*/ std::string* debug_name) {
4448 Arm64Assembler assembler(GetGraph()->GetAllocator());
4449 switch (patch.GetType()) {
4450 case linker::LinkerPatch::Type::kCallRelative: {
4451 // The thunk just uses the entry point in the ArtMethod. This works even for calls
4452 // to the generic JNI and interpreter trampolines.
4453 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4454 kArm64PointerSize).Int32Value());
4455 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
4456 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
4457 *debug_name = "MethodCallThunk";
4458 }
4459 break;
4460 }
4461 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
4462 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
4463 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
4464 break;
4465 }
4466 default:
4467 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
4468 UNREACHABLE();
4469 }
4470
4471 // Ensure we emit the literal pool if any.
4472 assembler.FinalizeCode();
4473 code->resize(assembler.CodeSize());
4474 MemoryRegion code_region(code->data(), code->size());
4475 assembler.FinalizeInstructions(code_region);
4476}
4477
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004478vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
4479 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004480 value,
4481 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4482}
4483
Scott Wakeling97c72b72016-06-24 16:19:36 +01004484vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004485 return uint64_literals_.GetOrCreate(
4486 value,
4487 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004488}
4489
Andreas Gampe878d58c2015-01-15 23:24:00 -08004490void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004491 // Explicit clinit checks triggered by static invokes must have been pruned by
4492 // art::PrepareForRegisterAllocation.
4493 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004494
Andreas Gampe878d58c2015-01-15 23:24:00 -08004495 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08004496 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004497 return;
4498 }
4499
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004500 {
4501 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4502 // are no pools emitted.
4503 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4504 LocationSummary* locations = invoke->GetLocations();
4505 codegen_->GenerateStaticOrDirectCall(
4506 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
4507 }
4508
Andreas Gampe3db70682018-12-26 15:12:03 -08004509 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004510}
4511
4512void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004513 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08004514 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004515 return;
4516 }
4517
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004518 {
4519 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4520 // are no pools emitted.
4521 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4522 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
4523 DCHECK(!codegen_->IsLeafMethod());
4524 }
4525
Andreas Gampe3db70682018-12-26 15:12:03 -08004526 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004527}
4528
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004529HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4530 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004531 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004532 case HLoadClass::LoadKind::kInvalid:
4533 LOG(FATAL) << "UNREACHABLE";
4534 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004535 case HLoadClass::LoadKind::kReferrersClass:
4536 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004537 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004538 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004539 case HLoadClass::LoadKind::kBssEntry:
4540 DCHECK(!Runtime::Current()->UseJitCompilation());
4541 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004542 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004543 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004544 DCHECK(Runtime::Current()->UseJitCompilation());
4545 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004546 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004547 break;
4548 }
4549 return desired_class_load_kind;
4550}
4551
Alexandre Rames67555f72014-11-18 10:55:16 +00004552void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004553 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004554 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004555 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004556 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004557 cls,
4558 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004559 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004560 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004561 return;
4562 }
Vladimir Marko41559982017-01-06 14:04:23 +00004563 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004564
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004565 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4566 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004567 ? LocationSummary::kCallOnSlowPath
4568 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004569 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004570 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004571 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004572 }
4573
Vladimir Marko41559982017-01-06 14:04:23 +00004574 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004575 locations->SetInAt(0, Location::RequiresRegister());
4576 }
4577 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004578 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4579 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4580 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01004581 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004582 } else {
4583 // For non-Baker read barrier we have a temp-clobbering call.
4584 }
4585 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004586}
4587
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004588// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4589// move.
4590void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004591 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004592 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00004593 codegen_->GenerateLoadClassRuntimeCall(cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08004594 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01004595 return;
4596 }
Vladimir Marko41559982017-01-06 14:04:23 +00004597 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004598
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004599 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004600 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004601
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004602 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4603 ? kWithoutReadBarrier
4604 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004605 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004606 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004607 case HLoadClass::LoadKind::kReferrersClass: {
4608 DCHECK(!cls->CanCallRuntime());
4609 DCHECK(!cls->MustGenerateClinitCheck());
4610 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4611 Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00004612 codegen_->GenerateGcRootFieldLoad(cls,
4613 out_loc,
4614 current_method,
4615 ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3db70682018-12-26 15:12:03 -08004616 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00004617 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004618 break;
4619 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004620 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004621 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004622 // Add ADRP with its PC-relative type patch.
4623 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004624 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004625 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004626 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004627 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004628 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004629 codegen_->NewBootImageTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004630 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004631 break;
4632 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004633 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004634 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004635 uint32_t boot_image_offset = codegen_->GetBootImageOffset(cls);
4636 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
4637 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004638 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004639 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004640 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004641 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004642 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004643 break;
4644 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004645 case HLoadClass::LoadKind::kBssEntry: {
4646 // Add ADRP with its PC-relative Class .bss entry patch.
4647 const DexFile& dex_file = cls->GetDexFile();
4648 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00004649 vixl::aarch64::Register temp = XRegisterFrom(out_loc);
4650 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4651 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004652 // Add LDR with its PC-relative Class .bss entry patch.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004653 vixl::aarch64::Label* ldr_label =
Vladimir Markof3c52b42017-11-17 17:32:12 +00004654 codegen_->NewBssEntryTypePatch(dex_file, type_index, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004655 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoca1e0382018-04-11 09:58:41 +00004656 codegen_->GenerateGcRootFieldLoad(cls,
4657 out_loc,
4658 temp,
4659 /* offset placeholder */ 0u,
4660 ldr_label,
4661 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004662 generate_null_check = true;
4663 break;
4664 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004665 case HLoadClass::LoadKind::kJitBootImageAddress: {
4666 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
4667 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
4668 DCHECK_NE(address, 0u);
4669 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
4670 break;
4671 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004672 case HLoadClass::LoadKind::kJitTableAddress: {
4673 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4674 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004675 cls->GetClass()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00004676 codegen_->GenerateGcRootFieldLoad(cls,
4677 out_loc,
4678 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08004679 /* offset= */ 0,
4680 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00004681 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004682 break;
4683 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004684 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004685 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004686 LOG(FATAL) << "UNREACHABLE";
4687 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004688 }
4689
Vladimir Markoea4c1262017-02-06 19:59:33 +00004690 bool do_clinit = cls->MustGenerateClinitCheck();
4691 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004692 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01004693 SlowPathCodeARM64* slow_path =
4694 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004695 codegen_->AddSlowPath(slow_path);
4696 if (generate_null_check) {
4697 __ Cbz(out, slow_path->GetEntryLabel());
4698 }
4699 if (cls->MustGenerateClinitCheck()) {
4700 GenerateClassInitializationCheck(slow_path, out);
4701 } else {
4702 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004703 }
Andreas Gampe3db70682018-12-26 15:12:03 -08004704 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004705 }
4706}
4707
Orion Hodsondbaa5c72018-05-10 08:22:46 +01004708void LocationsBuilderARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
4709 InvokeRuntimeCallingConvention calling_convention;
4710 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
4711 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
4712}
4713
4714void InstructionCodeGeneratorARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
4715 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
4716}
4717
Orion Hodson18259d72018-04-12 11:18:23 +01004718void LocationsBuilderARM64::VisitLoadMethodType(HLoadMethodType* load) {
4719 InvokeRuntimeCallingConvention calling_convention;
4720 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
4721 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
4722}
4723
4724void InstructionCodeGeneratorARM64::VisitLoadMethodType(HLoadMethodType* load) {
4725 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
4726}
4727
David Brazdilcb1c0552015-08-04 16:22:25 +01004728static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004729 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004730}
4731
Alexandre Rames67555f72014-11-18 10:55:16 +00004732void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4733 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004734 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00004735 locations->SetOut(Location::RequiresRegister());
4736}
4737
4738void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004739 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4740}
4741
4742void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004743 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01004744}
4745
4746void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4747 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004748}
4749
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004750HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4751 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004752 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004753 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004754 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00004755 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004756 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004757 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004758 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004759 case HLoadString::LoadKind::kJitTableAddress:
4760 DCHECK(Runtime::Current()->UseJitCompilation());
4761 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004762 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004763 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004764 }
4765 return desired_string_load_kind;
4766}
4767
Alexandre Rames67555f72014-11-18 10:55:16 +00004768void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004769 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004770 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004771 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004772 InvokeRuntimeCallingConvention calling_convention;
4773 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4774 } else {
4775 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004776 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4777 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00004778 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01004779 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004780 } else {
4781 // For non-Baker read barrier we have a temp-clobbering call.
4782 }
4783 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004784 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004785}
4786
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004787// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4788// move.
4789void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00004790 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004791 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004792
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004793 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004794 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004795 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004796 // Add ADRP with its PC-relative String patch.
4797 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004798 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004799 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004800 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004801 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004802 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004803 codegen_->NewBootImageStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004804 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004805 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004806 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004807 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004808 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004809 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
4810 uint32_t boot_image_offset = codegen_->GetBootImageOffset(load);
4811 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004812 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004813 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004814 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004815 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004816 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
4817 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004818 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004819 case HLoadString::LoadKind::kBssEntry: {
4820 // Add ADRP with its PC-relative String .bss entry patch.
4821 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004822 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00004823 Register temp = XRegisterFrom(out_loc);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004824 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004825 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004826 // Add LDR with its PC-relative String .bss entry patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004827 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004828 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004829 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoca1e0382018-04-11 09:58:41 +00004830 codegen_->GenerateGcRootFieldLoad(load,
4831 out_loc,
4832 temp,
4833 /* offset placeholder */ 0u,
4834 ldr_label,
4835 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004836 SlowPathCodeARM64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00004837 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004838 codegen_->AddSlowPath(slow_path);
4839 __ Cbz(out.X(), slow_path->GetEntryLabel());
4840 __ Bind(slow_path->GetExitLabel());
Andreas Gampe3db70682018-12-26 15:12:03 -08004841 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004842 return;
4843 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004844 case HLoadString::LoadKind::kJitBootImageAddress: {
4845 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
4846 DCHECK_NE(address, 0u);
4847 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
4848 return;
4849 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004850 case HLoadString::LoadKind::kJitTableAddress: {
4851 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004852 load->GetStringIndex(),
4853 load->GetString()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00004854 codegen_->GenerateGcRootFieldLoad(load,
4855 out_loc,
4856 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08004857 /* offset= */ 0,
4858 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00004859 kCompilerReadBarrierOption);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004860 return;
4861 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004862 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004863 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004864 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004865
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004866 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004867 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004868 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004869 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004870 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4871 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08004872 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004873}
4874
Alexandre Rames5319def2014-10-23 10:03:10 +01004875void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004876 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004877 locations->SetOut(Location::ConstantLocation(constant));
4878}
4879
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004880void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004881 // Will be generated at use site.
4882}
4883
Alexandre Rames67555f72014-11-18 10:55:16 +00004884void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004885 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4886 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004887 InvokeRuntimeCallingConvention calling_convention;
4888 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4889}
4890
4891void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004892 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004893 instruction,
4894 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004895 if (instruction->IsEnter()) {
4896 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4897 } else {
4898 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4899 }
Andreas Gampe3db70682018-12-26 15:12:03 -08004900 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004901}
4902
Alexandre Rames42d641b2014-10-27 14:00:51 +00004903void LocationsBuilderARM64::VisitMul(HMul* mul) {
4904 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004905 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004906 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004907 case DataType::Type::kInt32:
4908 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00004909 locations->SetInAt(0, Location::RequiresRegister());
4910 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004911 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004912 break;
4913
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004914 case DataType::Type::kFloat32:
4915 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004916 locations->SetInAt(0, Location::RequiresFpuRegister());
4917 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004918 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004919 break;
4920
4921 default:
4922 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4923 }
4924}
4925
4926void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4927 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004928 case DataType::Type::kInt32:
4929 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00004930 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4931 break;
4932
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004933 case DataType::Type::kFloat32:
4934 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004935 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004936 break;
4937
4938 default:
4939 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4940 }
4941}
4942
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004943void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4944 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004945 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004946 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004947 case DataType::Type::kInt32:
4948 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004949 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004950 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004951 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004952
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004953 case DataType::Type::kFloat32:
4954 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00004955 locations->SetInAt(0, Location::RequiresFpuRegister());
4956 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004957 break;
4958
4959 default:
4960 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4961 }
4962}
4963
4964void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4965 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004966 case DataType::Type::kInt32:
4967 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004968 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4969 break;
4970
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004971 case DataType::Type::kFloat32:
4972 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00004973 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004974 break;
4975
4976 default:
4977 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4978 }
4979}
4980
4981void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004982 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4983 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004984 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004985 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004986 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4987 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004988}
4989
4990void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01004991 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
4992 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00004993 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004994 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08004995 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004996}
4997
Alexandre Rames5319def2014-10-23 10:03:10 +01004998void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004999 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5000 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005001 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07005002 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005003 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005004}
5005
5006void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005007 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5008 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005009 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005010}
5011
5012void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005013 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005014 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005015 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005016}
5017
5018void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005019 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005020 case DataType::Type::kInt32:
5021 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005022 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005023 break;
5024
5025 default:
5026 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5027 }
5028}
5029
David Brazdil66d126e2015-04-03 16:02:44 +01005030void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005031 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005032 locations->SetInAt(0, Location::RequiresRegister());
5033 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5034}
5035
5036void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005037 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005038}
5039
Alexandre Rames5319def2014-10-23 10:03:10 +01005040void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005041 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5042 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005043}
5044
Calin Juravle2ae48182016-03-16 14:05:09 +00005045void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5046 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005047 return;
5048 }
Artem Serov914d7a82017-02-07 14:33:49 +00005049 {
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01005050 // Ensure that between load and RecordPcInfo there are no pools emitted.
Artem Serov914d7a82017-02-07 14:33:49 +00005051 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5052 Location obj = instruction->GetLocations()->InAt(0);
5053 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5054 RecordPcInfo(instruction, instruction->GetDexPc());
5055 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005056}
5057
Calin Juravle2ae48182016-03-16 14:05:09 +00005058void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005059 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005060 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005061
5062 LocationSummary* locations = instruction->GetLocations();
5063 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005064
5065 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005066}
5067
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005068void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005069 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005070}
5071
Alexandre Rames67555f72014-11-18 10:55:16 +00005072void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5073 HandleBinaryOp(instruction);
5074}
5075
5076void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5077 HandleBinaryOp(instruction);
5078}
5079
Alexandre Rames3e69f162014-12-10 10:36:50 +00005080void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5081 LOG(FATAL) << "Unreachable";
5082}
5083
5084void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005085 if (instruction->GetNext()->IsSuspendCheck() &&
5086 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5087 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5088 // The back edge will generate the suspend check.
5089 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5090 }
5091
Alexandre Rames3e69f162014-12-10 10:36:50 +00005092 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5093}
5094
Alexandre Rames5319def2014-10-23 10:03:10 +01005095void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005096 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005097 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5098 if (location.IsStackSlot()) {
5099 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5100 } else if (location.IsDoubleStackSlot()) {
5101 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5102 }
5103 locations->SetOut(location);
5104}
5105
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005106void InstructionCodeGeneratorARM64::VisitParameterValue(
5107 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005108 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005109}
5110
5111void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5112 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005113 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005114 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005115}
5116
5117void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5118 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5119 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005120}
5121
5122void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005123 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005124 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005125 locations->SetInAt(i, Location::Any());
5126 }
5127 locations->SetOut(Location::Any());
5128}
5129
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005130void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005131 LOG(FATAL) << "Unreachable";
5132}
5133
Serban Constantinescu02164b32014-11-13 14:05:07 +00005134void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005135 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005136 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005137 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005138 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005139 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005140
5141 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005142 case DataType::Type::kInt32:
5143 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005144 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005145 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005146 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5147 break;
5148
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005149 case DataType::Type::kFloat32:
5150 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005151 InvokeRuntimeCallingConvention calling_convention;
5152 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5153 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5154 locations->SetOut(calling_convention.GetReturnLocation(type));
5155
5156 break;
5157 }
5158
Serban Constantinescu02164b32014-11-13 14:05:07 +00005159 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005160 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005161 }
5162}
5163
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005164void InstructionCodeGeneratorARM64::GenerateIntRemForPower2Denom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005165 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005166 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
5167 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
5168
5169 Register out = OutputRegister(instruction);
5170 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005171
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005172 if (abs_imm == 2) {
5173 __ Cmp(dividend, 0);
5174 __ And(out, dividend, 1);
5175 __ Csneg(out, out, out, ge);
5176 } else {
5177 UseScratchRegisterScope temps(GetVIXLAssembler());
5178 Register temp = temps.AcquireSameSizeAs(out);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005179
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005180 __ Negs(temp, dividend);
5181 __ And(out, dividend, abs_imm - 1);
5182 __ And(temp, temp, abs_imm - 1);
5183 __ Csneg(out, out, temp, mi);
5184 }
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005185}
5186
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005187void InstructionCodeGeneratorARM64::GenerateIntRemForConstDenom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005188 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005189
5190 if (imm == 0) {
5191 // Do not generate anything.
5192 // DivZeroCheck would prevent any code to be executed.
5193 return;
5194 }
5195
Evgeny Astigeevichf58dc652018-06-25 17:54:07 +01005196 if (IsPowerOfTwo(AbsOrMin(imm))) {
5197 // Cases imm == -1 or imm == 1 are handled in constant folding by
5198 // InstructionWithAbsorbingInputSimplifier.
5199 // If the cases have survided till code generation they are handled in
5200 // GenerateIntRemForPower2Denom becauses -1 and 1 are the power of 2 (2^0).
5201 // The correct code is generated for them, just more instructions.
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005202 GenerateIntRemForPower2Denom(instruction);
5203 } else {
5204 DCHECK(imm < -2 || imm > 2) << imm;
5205 GenerateDivRemWithAnyConstant(instruction);
5206 }
5207}
5208
5209void InstructionCodeGeneratorARM64::GenerateIntRem(HRem* instruction) {
5210 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
5211 << instruction->GetResultType();
5212
5213 if (instruction->GetLocations()->InAt(1).IsConstant()) {
5214 GenerateIntRemForConstDenom(instruction);
5215 } else {
5216 Register out = OutputRegister(instruction);
5217 Register dividend = InputRegisterAt(instruction, 0);
5218 Register divisor = InputRegisterAt(instruction, 1);
5219 UseScratchRegisterScope temps(GetVIXLAssembler());
5220 Register temp = temps.AcquireSameSizeAs(out);
5221 __ Sdiv(temp, dividend, divisor);
5222 __ Msub(out, temp, divisor, dividend);
5223 }
5224}
5225
Serban Constantinescu02164b32014-11-13 14:05:07 +00005226void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005227 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005228
Serban Constantinescu02164b32014-11-13 14:05:07 +00005229 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005230 case DataType::Type::kInt32:
5231 case DataType::Type::kInt64: {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005232 GenerateIntRem(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005233 break;
5234 }
5235
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005236 case DataType::Type::kFloat32:
5237 case DataType::Type::kFloat64: {
5238 QuickEntrypointEnum entrypoint =
5239 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005240 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005241 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00005242 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5243 } else {
5244 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5245 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005246 break;
5247 }
5248
Serban Constantinescu02164b32014-11-13 14:05:07 +00005249 default:
5250 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005251 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005252 }
5253}
5254
Aart Bik1f8d51b2018-02-15 10:42:37 -08005255void LocationsBuilderARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005256 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005257}
5258
Aart Bik1f8d51b2018-02-15 10:42:37 -08005259void InstructionCodeGeneratorARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005260 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005261}
5262
5263void LocationsBuilderARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005264 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005265}
5266
5267void InstructionCodeGeneratorARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005268 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005269}
5270
Aart Bik3dad3412018-02-28 12:01:46 -08005271void LocationsBuilderARM64::VisitAbs(HAbs* abs) {
5272 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
5273 switch (abs->GetResultType()) {
5274 case DataType::Type::kInt32:
5275 case DataType::Type::kInt64:
5276 locations->SetInAt(0, Location::RequiresRegister());
5277 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5278 break;
5279 case DataType::Type::kFloat32:
5280 case DataType::Type::kFloat64:
5281 locations->SetInAt(0, Location::RequiresFpuRegister());
5282 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5283 break;
5284 default:
5285 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5286 }
5287}
5288
5289void InstructionCodeGeneratorARM64::VisitAbs(HAbs* abs) {
5290 switch (abs->GetResultType()) {
5291 case DataType::Type::kInt32:
5292 case DataType::Type::kInt64: {
5293 Register in_reg = InputRegisterAt(abs, 0);
5294 Register out_reg = OutputRegister(abs);
5295 __ Cmp(in_reg, Operand(0));
5296 __ Cneg(out_reg, in_reg, lt);
5297 break;
5298 }
5299 case DataType::Type::kFloat32:
5300 case DataType::Type::kFloat64: {
5301 FPRegister in_reg = InputFPRegisterAt(abs, 0);
5302 FPRegister out_reg = OutputFPRegister(abs);
5303 __ Fabs(out_reg, in_reg);
5304 break;
5305 }
5306 default:
5307 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5308 }
5309}
5310
Igor Murashkind01745e2017-04-05 16:40:31 -07005311void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5312 constructor_fence->SetLocations(nullptr);
5313}
5314
5315void InstructionCodeGeneratorARM64::VisitConstructorFence(
5316 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5317 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5318}
5319
Calin Juravle27df7582015-04-17 19:12:31 +01005320void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5321 memory_barrier->SetLocations(nullptr);
5322}
5323
5324void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005325 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005326}
5327
Alexandre Rames5319def2014-10-23 10:03:10 +01005328void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005329 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005330 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005331 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005332}
5333
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005334void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005335 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005336}
5337
5338void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5339 instruction->SetLocations(nullptr);
5340}
5341
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005342void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005343 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005344}
5345
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005346void LocationsBuilderARM64::VisitRor(HRor* ror) {
5347 HandleBinaryOp(ror);
5348}
5349
5350void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5351 HandleBinaryOp(ror);
5352}
5353
Serban Constantinescu02164b32014-11-13 14:05:07 +00005354void LocationsBuilderARM64::VisitShl(HShl* shl) {
5355 HandleShift(shl);
5356}
5357
5358void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5359 HandleShift(shl);
5360}
5361
5362void LocationsBuilderARM64::VisitShr(HShr* shr) {
5363 HandleShift(shr);
5364}
5365
5366void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5367 HandleShift(shr);
5368}
5369
Alexandre Rames5319def2014-10-23 10:03:10 +01005370void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005371 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005372}
5373
5374void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005375 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005376}
5377
Alexandre Rames67555f72014-11-18 10:55:16 +00005378void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005379 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005380}
5381
5382void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005383 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005384}
5385
5386void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005387 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005388}
5389
Alexandre Rames67555f72014-11-18 10:55:16 +00005390void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005391 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005392}
5393
Vladimir Marko552a1342017-10-31 10:56:47 +00005394void LocationsBuilderARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5395 codegen_->CreateStringBuilderAppendLocations(instruction, LocationFrom(x0));
5396}
5397
5398void InstructionCodeGeneratorARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5399 __ Mov(w0, instruction->GetFormat()->GetValue());
5400 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
5401}
5402
Calin Juravlee460d1d2015-09-29 04:52:17 +01005403void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5404 HUnresolvedInstanceFieldGet* instruction) {
5405 FieldAccessCallingConventionARM64 calling_convention;
5406 codegen_->CreateUnresolvedFieldLocationSummary(
5407 instruction, instruction->GetFieldType(), calling_convention);
5408}
5409
5410void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5411 HUnresolvedInstanceFieldGet* instruction) {
5412 FieldAccessCallingConventionARM64 calling_convention;
5413 codegen_->GenerateUnresolvedFieldAccess(instruction,
5414 instruction->GetFieldType(),
5415 instruction->GetFieldIndex(),
5416 instruction->GetDexPc(),
5417 calling_convention);
5418}
5419
5420void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5421 HUnresolvedInstanceFieldSet* instruction) {
5422 FieldAccessCallingConventionARM64 calling_convention;
5423 codegen_->CreateUnresolvedFieldLocationSummary(
5424 instruction, instruction->GetFieldType(), calling_convention);
5425}
5426
5427void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5428 HUnresolvedInstanceFieldSet* instruction) {
5429 FieldAccessCallingConventionARM64 calling_convention;
5430 codegen_->GenerateUnresolvedFieldAccess(instruction,
5431 instruction->GetFieldType(),
5432 instruction->GetFieldIndex(),
5433 instruction->GetDexPc(),
5434 calling_convention);
5435}
5436
5437void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5438 HUnresolvedStaticFieldGet* instruction) {
5439 FieldAccessCallingConventionARM64 calling_convention;
5440 codegen_->CreateUnresolvedFieldLocationSummary(
5441 instruction, instruction->GetFieldType(), calling_convention);
5442}
5443
5444void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5445 HUnresolvedStaticFieldGet* instruction) {
5446 FieldAccessCallingConventionARM64 calling_convention;
5447 codegen_->GenerateUnresolvedFieldAccess(instruction,
5448 instruction->GetFieldType(),
5449 instruction->GetFieldIndex(),
5450 instruction->GetDexPc(),
5451 calling_convention);
5452}
5453
5454void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5455 HUnresolvedStaticFieldSet* instruction) {
5456 FieldAccessCallingConventionARM64 calling_convention;
5457 codegen_->CreateUnresolvedFieldLocationSummary(
5458 instruction, instruction->GetFieldType(), calling_convention);
5459}
5460
5461void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5462 HUnresolvedStaticFieldSet* instruction) {
5463 FieldAccessCallingConventionARM64 calling_convention;
5464 codegen_->GenerateUnresolvedFieldAccess(instruction,
5465 instruction->GetFieldType(),
5466 instruction->GetFieldIndex(),
5467 instruction->GetDexPc(),
5468 calling_convention);
5469}
5470
Alexandre Rames5319def2014-10-23 10:03:10 +01005471void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005472 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5473 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005474 // In suspend check slow path, usually there are no caller-save registers at all.
5475 // If SIMD instructions are present, however, we force spilling all live SIMD
5476 // registers in full width (since the runtime only saves/restores lower part).
5477 locations->SetCustomSlowPathCallerSaves(
5478 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005479}
5480
5481void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005482 HBasicBlock* block = instruction->GetBlock();
5483 if (block->GetLoopInformation() != nullptr) {
5484 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5485 // The back edge will generate the suspend check.
5486 return;
5487 }
5488 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5489 // The goto will generate the suspend check.
5490 return;
5491 }
5492 GenerateSuspendCheck(instruction, nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08005493 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005494}
5495
Alexandre Rames67555f72014-11-18 10:55:16 +00005496void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005497 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5498 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005499 InvokeRuntimeCallingConvention calling_convention;
5500 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5501}
5502
5503void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005504 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005505 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005506}
5507
5508void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5509 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005510 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005511 DataType::Type input_type = conversion->GetInputType();
5512 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005513 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5514 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005515 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
5516 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005517 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5518 }
5519
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005520 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005521 locations->SetInAt(0, Location::RequiresFpuRegister());
5522 } else {
5523 locations->SetInAt(0, Location::RequiresRegister());
5524 }
5525
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005526 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005527 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5528 } else {
5529 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5530 }
5531}
5532
5533void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005534 DataType::Type result_type = conversion->GetResultType();
5535 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00005536
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005537 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5538 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005539
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005540 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
5541 int result_size = DataType::Size(result_type);
5542 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005543 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005544 Register output = OutputRegister(conversion);
5545 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005546 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005547 // 'int' values are used directly as W registers, discarding the top
5548 // bits, so we don't need to sign-extend and can just perform a move.
5549 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5550 // top 32 bits of the target register. We theoretically could leave those
5551 // bits unchanged, but we would have to make sure that no code uses a
5552 // 32bit input value as a 64bit value assuming that the top 32 bits are
5553 // zero.
5554 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005555 } else if (DataType::IsUnsignedType(result_type) ||
5556 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
5557 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005558 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005559 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005560 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005561 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005562 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005563 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
5564 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005565 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005566 } else if (DataType::IsFloatingPointType(result_type) &&
5567 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005568 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5569 } else {
5570 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5571 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005572 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005573}
Alexandre Rames67555f72014-11-18 10:55:16 +00005574
Serban Constantinescu02164b32014-11-13 14:05:07 +00005575void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5576 HandleShift(ushr);
5577}
5578
5579void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5580 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005581}
5582
5583void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5584 HandleBinaryOp(instruction);
5585}
5586
5587void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5588 HandleBinaryOp(instruction);
5589}
5590
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005591void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005592 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005593 LOG(FATAL) << "Unreachable";
5594}
5595
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005596void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005597 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005598 LOG(FATAL) << "Unreachable";
5599}
5600
Mark Mendellfe57faa2015-09-18 09:26:15 -04005601// Simple implementation of packed switch - generate cascaded compare/jumps.
5602void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5603 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005604 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005605 locations->SetInAt(0, Location::RequiresRegister());
5606}
5607
5608void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5609 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005610 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005611 Register value_reg = InputRegisterAt(switch_instr, 0);
5612 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5613
Zheng Xu3927c8b2015-11-18 17:46:25 +08005614 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005615 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005616 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5617 // make sure we don't emit it if the target may run out of range.
5618 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5619 // ranges and emit the tables only as required.
5620 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005621
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005622 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005623 // Current instruction id is an upper bound of the number of HIRs in the graph.
5624 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5625 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005626 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5627 Register temp = temps.AcquireW();
5628 __ Subs(temp, value_reg, Operand(lower_bound));
5629
Zheng Xu3927c8b2015-11-18 17:46:25 +08005630 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005631 // Jump to successors[0] if value == lower_bound.
5632 __ B(eq, codegen_->GetLabelOf(successors[0]));
5633 int32_t last_index = 0;
5634 for (; num_entries - last_index > 2; last_index += 2) {
5635 __ Subs(temp, temp, Operand(2));
5636 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5637 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5638 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5639 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5640 }
5641 if (num_entries - last_index == 2) {
5642 // The last missing case_value.
5643 __ Cmp(temp, Operand(1));
5644 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005645 }
5646
5647 // And the default for any other value.
5648 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5649 __ B(codegen_->GetLabelOf(default_block));
5650 }
5651 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005652 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005653
5654 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5655
5656 // Below instructions should use at most one blocked register. Since there are two blocked
5657 // registers, we are free to block one.
5658 Register temp_w = temps.AcquireW();
5659 Register index;
5660 // Remove the bias.
5661 if (lower_bound != 0) {
5662 index = temp_w;
5663 __ Sub(index, value_reg, Operand(lower_bound));
5664 } else {
5665 index = value_reg;
5666 }
5667
5668 // Jump to default block if index is out of the range.
5669 __ Cmp(index, Operand(num_entries));
5670 __ B(hs, codegen_->GetLabelOf(default_block));
5671
5672 // In current VIXL implementation, it won't require any blocked registers to encode the
5673 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5674 // register pressure.
5675 Register table_base = temps.AcquireX();
5676 // Load jump offset from the table.
5677 __ Adr(table_base, jump_table->GetTableStartLabel());
5678 Register jump_offset = temp_w;
5679 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5680
5681 // Jump to target block by branching to table_base(pc related) + offset.
5682 Register target_address = table_base;
5683 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5684 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005685 }
5686}
5687
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005688void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5689 HInstruction* instruction,
5690 Location out,
5691 uint32_t offset,
5692 Location maybe_temp,
5693 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005694 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00005695 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005696 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005697 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005698 if (kUseBakerReadBarrier) {
5699 // Load with fast path based Baker's read barrier.
5700 // /* HeapReference<Object> */ out = *(out + offset)
5701 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5702 out,
5703 out_reg,
5704 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005705 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08005706 /* needs_null_check= */ false,
5707 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005708 } else {
5709 // Load with slow path based read barrier.
5710 // Save the value of `out` into `maybe_temp` before overwriting it
5711 // in the following move operation, as we will need it for the
5712 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005713 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005714 __ Mov(temp_reg, out_reg);
5715 // /* HeapReference<Object> */ out = *(out + offset)
5716 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5717 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5718 }
5719 } else {
5720 // Plain load with no read barrier.
5721 // /* HeapReference<Object> */ out = *(out + offset)
5722 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5723 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5724 }
5725}
5726
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005727void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5728 HInstruction* instruction,
5729 Location out,
5730 Location obj,
5731 uint32_t offset,
5732 Location maybe_temp,
5733 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005734 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00005735 Register out_reg = RegisterFrom(out, type);
5736 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005737 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005738 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005739 if (kUseBakerReadBarrier) {
5740 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00005741 // /* HeapReference<Object> */ out = *(obj + offset)
5742 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5743 out,
5744 obj_reg,
5745 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005746 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08005747 /* needs_null_check= */ false,
5748 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005749 } else {
5750 // Load with slow path based read barrier.
5751 // /* HeapReference<Object> */ out = *(obj + offset)
5752 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5753 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5754 }
5755 } else {
5756 // Plain load with no read barrier.
5757 // /* HeapReference<Object> */ out = *(obj + offset)
5758 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5759 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5760 }
5761}
5762
Vladimir Markoca1e0382018-04-11 09:58:41 +00005763void CodeGeneratorARM64::GenerateGcRootFieldLoad(
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005764 HInstruction* instruction,
5765 Location root,
5766 Register obj,
5767 uint32_t offset,
5768 vixl::aarch64::Label* fixup_label,
5769 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005770 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005771 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005772 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005773 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005774 if (kUseBakerReadBarrier) {
5775 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005776 // Baker's read barrier are used.
Roland Levillain44015862016-01-22 11:47:17 +00005777
Vladimir Marko008e09f32018-08-06 15:42:43 +01005778 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
5779 // the Marking Register) to decide whether we need to enter
5780 // the slow path to mark the GC root.
5781 //
5782 // We use shared thunks for the slow path; shared within the method
5783 // for JIT, across methods for AOT. That thunk checks the reference
5784 // and jumps to the entrypoint if needed.
5785 //
5786 // lr = &return_address;
5787 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5788 // if (mr) { // Thread::Current()->GetIsGcMarking()
5789 // goto gc_root_thunk<root_reg>(lr)
5790 // }
5791 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00005792
Vladimir Marko008e09f32018-08-06 15:42:43 +01005793 UseScratchRegisterScope temps(GetVIXLAssembler());
5794 DCHECK(temps.IsAvailable(ip0));
5795 DCHECK(temps.IsAvailable(ip1));
5796 temps.Exclude(ip0, ip1);
5797 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
Roland Levillain44015862016-01-22 11:47:17 +00005798
Vladimir Marko008e09f32018-08-06 15:42:43 +01005799 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
5800 vixl::aarch64::Label return_address;
5801 __ adr(lr, &return_address);
5802 if (fixup_label != nullptr) {
5803 __ bind(fixup_label);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005804 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01005805 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
Vladimir Marko94796f82018-08-08 15:15:33 +01005806 "GC root LDR must be 2 instructions (8B) before the return address label.");
Vladimir Marko008e09f32018-08-06 15:42:43 +01005807 __ ldr(root_reg, MemOperand(obj.X(), offset));
5808 EmitBakerReadBarrierCbnz(custom_data);
5809 __ bind(&return_address);
Roland Levillain44015862016-01-22 11:47:17 +00005810 } else {
5811 // GC root loaded through a slow path for read barriers other
5812 // than Baker's.
5813 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005814 if (fixup_label == nullptr) {
5815 __ Add(root_reg.X(), obj.X(), offset);
5816 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00005817 EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005818 }
Roland Levillain44015862016-01-22 11:47:17 +00005819 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00005820 GenerateReadBarrierForRootSlow(instruction, root, root);
Roland Levillain44015862016-01-22 11:47:17 +00005821 }
5822 } else {
5823 // Plain GC root load with no read barrier.
5824 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005825 if (fixup_label == nullptr) {
5826 __ Ldr(root_reg, MemOperand(obj, offset));
5827 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00005828 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005829 }
Roland Levillain44015862016-01-22 11:47:17 +00005830 // Note that GC roots are not affected by heap poisoning, thus we
5831 // do not have to unpoison `root_reg` here.
5832 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005833 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00005834}
5835
Vladimir Marko94796f82018-08-08 15:15:33 +01005836void CodeGeneratorARM64::GenerateUnsafeCasOldValueMovWithBakerReadBarrier(
5837 vixl::aarch64::Register marked,
5838 vixl::aarch64::Register old_value) {
5839 DCHECK(kEmitCompilerReadBarrier);
5840 DCHECK(kUseBakerReadBarrier);
5841
5842 // Similar to the Baker RB path in GenerateGcRootFieldLoad(), with a MOV instead of LDR.
5843 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(marked.GetCode());
5844
5845 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
5846 vixl::aarch64::Label return_address;
5847 __ adr(lr, &return_address);
5848 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
5849 "GC root LDR must be 2 instructions (8B) before the return address label.");
5850 __ mov(marked, old_value);
5851 EmitBakerReadBarrierCbnz(custom_data);
5852 __ bind(&return_address);
5853}
5854
Roland Levillain44015862016-01-22 11:47:17 +00005855void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5856 Location ref,
Vladimir Marko248141f2018-08-10 10:40:07 +01005857 vixl::aarch64::Register obj,
5858 const vixl::aarch64::MemOperand& src,
Roland Levillain44015862016-01-22 11:47:17 +00005859 bool needs_null_check,
5860 bool use_load_acquire) {
5861 DCHECK(kEmitCompilerReadBarrier);
5862 DCHECK(kUseBakerReadBarrier);
5863
Vladimir Marko0ecac682018-08-07 10:40:38 +01005864 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
5865 // Marking Register) to decide whether we need to enter the slow
5866 // path to mark the reference. Then, in the slow path, check the
5867 // gray bit in the lock word of the reference's holder (`obj`) to
5868 // decide whether to mark `ref` or not.
5869 //
5870 // We use shared thunks for the slow path; shared within the method
5871 // for JIT, across methods for AOT. That thunk checks the holder
5872 // and jumps to the entrypoint if needed. If the holder is not gray,
5873 // it creates a fake dependency and returns to the LDR instruction.
5874 //
5875 // lr = &gray_return_address;
5876 // if (mr) { // Thread::Current()->GetIsGcMarking()
5877 // goto field_thunk<holder_reg, base_reg, use_load_acquire>(lr)
5878 // }
5879 // not_gray_return_address:
5880 // // Original reference load. If the offset is too large to fit
5881 // // into LDR, we use an adjusted base register here.
5882 // HeapReference<mirror::Object> reference = *(obj+offset);
5883 // gray_return_address:
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005884
Vladimir Marko248141f2018-08-10 10:40:07 +01005885 DCHECK(src.GetAddrMode() == vixl::aarch64::Offset);
5886 DCHECK_ALIGNED(src.GetOffset(), sizeof(mirror::HeapReference<mirror::Object>));
5887
5888 UseScratchRegisterScope temps(GetVIXLAssembler());
5889 DCHECK(temps.IsAvailable(ip0));
5890 DCHECK(temps.IsAvailable(ip1));
5891 temps.Exclude(ip0, ip1);
5892 uint32_t custom_data = use_load_acquire
5893 ? EncodeBakerReadBarrierAcquireData(src.GetBaseRegister().GetCode(), obj.GetCode())
5894 : EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode());
5895
5896 {
5897 ExactAssemblyScope guard(GetVIXLAssembler(),
5898 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
5899 vixl::aarch64::Label return_address;
5900 __ adr(lr, &return_address);
5901 EmitBakerReadBarrierCbnz(custom_data);
5902 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
5903 "Field LDR must be 1 instruction (4B) before the return address label; "
5904 " 2 instructions (8B) for heap poisoning.");
5905 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
5906 if (use_load_acquire) {
5907 DCHECK_EQ(src.GetOffset(), 0);
5908 __ ldar(ref_reg, src);
5909 } else {
5910 __ ldr(ref_reg, src);
5911 }
5912 if (needs_null_check) {
5913 MaybeRecordImplicitNullCheck(instruction);
5914 }
5915 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
5916 // macro instructions disallowed in ExactAssemblyScope.
5917 if (kPoisonHeapReferences) {
5918 __ neg(ref_reg, Operand(ref_reg));
5919 }
5920 __ bind(&return_address);
5921 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005922 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Vladimir Marko248141f2018-08-10 10:40:07 +01005923}
5924
5925void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5926 Location ref,
5927 Register obj,
5928 uint32_t offset,
5929 Location maybe_temp,
5930 bool needs_null_check,
5931 bool use_load_acquire) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01005932 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
5933 Register base = obj;
5934 if (use_load_acquire) {
5935 DCHECK(maybe_temp.IsRegister());
5936 base = WRegisterFrom(maybe_temp);
5937 __ Add(base, obj, offset);
5938 offset = 0u;
5939 } else if (offset >= kReferenceLoadMinFarOffset) {
5940 DCHECK(maybe_temp.IsRegister());
5941 base = WRegisterFrom(maybe_temp);
5942 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
5943 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
5944 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005945 }
Vladimir Marko248141f2018-08-10 10:40:07 +01005946 MemOperand src(base.X(), offset);
5947 GenerateFieldLoadWithBakerReadBarrier(
5948 instruction, ref, obj, src, needs_null_check, use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005949}
5950
Artem Serov0806f582018-10-11 20:14:20 +01005951void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HArrayGet* instruction,
5952 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005953 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005954 uint32_t data_offset,
5955 Location index,
Roland Levillain44015862016-01-22 11:47:17 +00005956 bool needs_null_check) {
5957 DCHECK(kEmitCompilerReadBarrier);
5958 DCHECK(kUseBakerReadBarrier);
5959
Vladimir Marko66d691d2017-04-07 17:53:39 +01005960 static_assert(
5961 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5962 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005963 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01005964
Vladimir Marko008e09f32018-08-06 15:42:43 +01005965 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
5966 // Marking Register) to decide whether we need to enter the slow
5967 // path to mark the reference. Then, in the slow path, check the
5968 // gray bit in the lock word of the reference's holder (`obj`) to
5969 // decide whether to mark `ref` or not.
5970 //
5971 // We use shared thunks for the slow path; shared within the method
5972 // for JIT, across methods for AOT. That thunk checks the holder
5973 // and jumps to the entrypoint if needed. If the holder is not gray,
5974 // it creates a fake dependency and returns to the LDR instruction.
5975 //
5976 // lr = &gray_return_address;
5977 // if (mr) { // Thread::Current()->GetIsGcMarking()
5978 // goto array_thunk<base_reg>(lr)
5979 // }
5980 // not_gray_return_address:
5981 // // Original reference load. If the offset is too large to fit
5982 // // into LDR, we use an adjusted base register here.
5983 // HeapReference<mirror::Object> reference = data[index];
5984 // gray_return_address:
Vladimir Marko66d691d2017-04-07 17:53:39 +01005985
Vladimir Marko008e09f32018-08-06 15:42:43 +01005986 DCHECK(index.IsValid());
5987 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
5988 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01005989
Vladimir Marko008e09f32018-08-06 15:42:43 +01005990 UseScratchRegisterScope temps(GetVIXLAssembler());
5991 DCHECK(temps.IsAvailable(ip0));
5992 DCHECK(temps.IsAvailable(ip1));
5993 temps.Exclude(ip0, ip1);
Artem Serov0806f582018-10-11 20:14:20 +01005994
5995 Register temp;
5996 if (instruction->GetArray()->IsIntermediateAddress()) {
5997 // We do not need to compute the intermediate address from the array: the
5998 // input instruction has done it already. See the comment in
5999 // `TryExtractArrayAccessAddress()`.
6000 if (kIsDebugBuild) {
6001 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
6002 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
6003 }
6004 temp = obj;
6005 } else {
6006 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
6007 __ Add(temp.X(), obj.X(), Operand(data_offset));
6008 }
6009
Vladimir Marko008e09f32018-08-06 15:42:43 +01006010 uint32_t custom_data = EncodeBakerReadBarrierArrayData(temp.GetCode());
Vladimir Marko66d691d2017-04-07 17:53:39 +01006011
Vladimir Marko008e09f32018-08-06 15:42:43 +01006012 {
6013 ExactAssemblyScope guard(GetVIXLAssembler(),
6014 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6015 vixl::aarch64::Label return_address;
6016 __ adr(lr, &return_address);
6017 EmitBakerReadBarrierCbnz(custom_data);
6018 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6019 "Array LDR must be 1 instruction (4B) before the return address label; "
6020 " 2 instructions (8B) for heap poisoning.");
6021 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6022 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6023 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6024 // macro instructions disallowed in ExactAssemblyScope.
6025 if (kPoisonHeapReferences) {
6026 __ neg(ref_reg, Operand(ref_reg));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006027 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006028 __ bind(&return_address);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006029 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006030 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Roland Levillain44015862016-01-22 11:47:17 +00006031}
6032
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006033void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6034 // The following condition is a compile-time one, so it does not have a run-time cost.
6035 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
6036 // The following condition is a run-time one; it is executed after the
6037 // previous compile-time test, to avoid penalizing non-debug builds.
6038 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6039 UseScratchRegisterScope temps(GetVIXLAssembler());
6040 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6041 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6042 }
6043 }
6044}
6045
Roland Levillain44015862016-01-22 11:47:17 +00006046void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6047 Location out,
6048 Location ref,
6049 Location obj,
6050 uint32_t offset,
6051 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006052 DCHECK(kEmitCompilerReadBarrier);
6053
Roland Levillain44015862016-01-22 11:47:17 +00006054 // Insert a slow path based read barrier *after* the reference load.
6055 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006056 // If heap poisoning is enabled, the unpoisoning of the loaded
6057 // reference will be carried out by the runtime within the slow
6058 // path.
6059 //
6060 // Note that `ref` currently does not get unpoisoned (when heap
6061 // poisoning is enabled), which is alright as the `ref` argument is
6062 // not used by the artReadBarrierSlow entry point.
6063 //
6064 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006065 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006066 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6067 AddSlowPath(slow_path);
6068
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006069 __ B(slow_path->GetEntryLabel());
6070 __ Bind(slow_path->GetExitLabel());
6071}
6072
Roland Levillain44015862016-01-22 11:47:17 +00006073void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6074 Location out,
6075 Location ref,
6076 Location obj,
6077 uint32_t offset,
6078 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006079 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006080 // Baker's read barriers shall be handled by the fast path
6081 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6082 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006083 // If heap poisoning is enabled, unpoisoning will be taken care of
6084 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006085 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006086 } else if (kPoisonHeapReferences) {
6087 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6088 }
6089}
6090
Roland Levillain44015862016-01-22 11:47:17 +00006091void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6092 Location out,
6093 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006094 DCHECK(kEmitCompilerReadBarrier);
6095
Roland Levillain44015862016-01-22 11:47:17 +00006096 // Insert a slow path based read barrier *after* the GC root load.
6097 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006098 // Note that GC roots are not affected by heap poisoning, so we do
6099 // not need to do anything special for this here.
6100 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006101 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006102 AddSlowPath(slow_path);
6103
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006104 __ B(slow_path->GetEntryLabel());
6105 __ Bind(slow_path->GetExitLabel());
6106}
6107
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006108void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6109 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006110 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006111 locations->SetInAt(0, Location::RequiresRegister());
6112 locations->SetOut(Location::RequiresRegister());
6113}
6114
6115void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6116 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006117 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006118 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006119 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006120 __ Ldr(XRegisterFrom(locations->Out()),
6121 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006122 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006123 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006124 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006125 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6126 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006127 __ Ldr(XRegisterFrom(locations->Out()),
6128 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006129 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006130}
6131
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006132static void PatchJitRootUse(uint8_t* code,
6133 const uint8_t* roots_data,
6134 vixl::aarch64::Literal<uint32_t>* literal,
6135 uint64_t index_in_table) {
6136 uint32_t literal_offset = literal->GetOffset();
6137 uintptr_t address =
6138 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6139 uint8_t* data = code + literal_offset;
6140 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6141}
6142
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006143void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6144 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006145 const StringReference& string_reference = entry.first;
6146 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006147 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006148 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006149 }
6150 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006151 const TypeReference& type_reference = entry.first;
6152 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006153 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006154 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006155 }
6156}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006157
Alexandre Rames67555f72014-11-18 10:55:16 +00006158#undef __
6159#undef QUICK_ENTRY_POINT
6160
Vladimir Markoca1e0382018-04-11 09:58:41 +00006161#define __ assembler.GetVIXLAssembler()->
6162
6163static void EmitGrayCheckAndFastPath(arm64::Arm64Assembler& assembler,
6164 vixl::aarch64::Register base_reg,
6165 vixl::aarch64::MemOperand& lock_word,
Vladimir Marko7a695052018-04-12 10:26:50 +01006166 vixl::aarch64::Label* slow_path,
6167 vixl::aarch64::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006168 // Load the lock word containing the rb_state.
6169 __ Ldr(ip0.W(), lock_word);
6170 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01006171 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Vladimir Markoca1e0382018-04-11 09:58:41 +00006172 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6173 __ Tbnz(ip0.W(), LockWord::kReadBarrierStateShift, slow_path);
6174 static_assert(
6175 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET,
6176 "Field and array LDR offsets must be the same to reuse the same code.");
Vladimir Marko7a695052018-04-12 10:26:50 +01006177 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
6178 if (throw_npe != nullptr) {
6179 __ Bind(throw_npe);
6180 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006181 // Adjust the return address back to the LDR (1 instruction; 2 for heap poisoning).
6182 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6183 "Field LDR must be 1 instruction (4B) before the return address label; "
6184 " 2 instructions (8B) for heap poisoning.");
6185 __ Add(lr, lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6186 // Introduce a dependency on the lock_word including rb_state,
6187 // to prevent load-load reordering, and without using
6188 // a memory barrier (which would be more expensive).
6189 __ Add(base_reg, base_reg, Operand(ip0, LSR, 32));
6190 __ Br(lr); // And return back to the function.
6191 // Note: The fake dependency is unnecessary for the slow path.
6192}
6193
6194// Load the read barrier introspection entrypoint in register `entrypoint`.
6195static void LoadReadBarrierMarkIntrospectionEntrypoint(arm64::Arm64Assembler& assembler,
6196 vixl::aarch64::Register entrypoint) {
6197 // entrypoint = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6198 DCHECK_EQ(ip0.GetCode(), 16u);
6199 const int32_t entry_point_offset =
6200 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6201 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
6202}
6203
6204void CodeGeneratorARM64::CompileBakerReadBarrierThunk(Arm64Assembler& assembler,
6205 uint32_t encoded_data,
6206 /*out*/ std::string* debug_name) {
6207 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
6208 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006209 case BakerReadBarrierKind::kField:
6210 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006211 auto base_reg =
6212 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6213 CheckValidReg(base_reg.GetCode());
6214 auto holder_reg =
6215 Register::GetXRegFromCode(BakerReadBarrierSecondRegField::Decode(encoded_data));
6216 CheckValidReg(holder_reg.GetCode());
6217 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6218 temps.Exclude(ip0, ip1);
Vladimir Marko7a695052018-04-12 10:26:50 +01006219 // If base_reg differs from holder_reg, the offset was too large and we must have emitted
6220 // an explicit null check before the load. Otherwise, for implicit null checks, we need to
6221 // null-check the holder as we do not necessarily do that check before going to the thunk.
6222 vixl::aarch64::Label throw_npe_label;
6223 vixl::aarch64::Label* throw_npe = nullptr;
6224 if (GetCompilerOptions().GetImplicitNullChecks() && holder_reg.Is(base_reg)) {
6225 throw_npe = &throw_npe_label;
6226 __ Cbz(holder_reg.W(), throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006227 }
Vladimir Marko7a695052018-04-12 10:26:50 +01006228 // Check if the holder is gray and, if not, add fake dependency to the base register
6229 // and return to the LDR instruction to load the reference. Otherwise, use introspection
6230 // to load the reference and call the entrypoint that performs further checks on the
6231 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006232 vixl::aarch64::Label slow_path;
6233 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
Vladimir Marko7a695052018-04-12 10:26:50 +01006234 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006235 __ Bind(&slow_path);
Vladimir Marko0ecac682018-08-07 10:40:38 +01006236 if (kind == BakerReadBarrierKind::kField) {
6237 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6238 __ Ldr(ip0.W(), ldr_address); // Load the LDR (immediate) unsigned offset.
6239 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6240 __ Ubfx(ip0.W(), ip0.W(), 10, 12); // Extract the offset.
6241 __ Ldr(ip0.W(), MemOperand(base_reg, ip0, LSL, 2)); // Load the reference.
6242 } else {
6243 DCHECK(kind == BakerReadBarrierKind::kAcquire);
6244 DCHECK(!base_reg.Is(holder_reg));
6245 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6246 __ Ldar(ip0.W(), MemOperand(base_reg));
6247 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006248 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
6249 __ Br(ip1); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006250 break;
6251 }
6252 case BakerReadBarrierKind::kArray: {
6253 auto base_reg =
6254 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6255 CheckValidReg(base_reg.GetCode());
6256 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6257 BakerReadBarrierSecondRegField::Decode(encoded_data));
6258 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6259 temps.Exclude(ip0, ip1);
6260 vixl::aarch64::Label slow_path;
6261 int32_t data_offset =
6262 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
6263 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
6264 DCHECK_LT(lock_word.GetOffset(), 0);
6265 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path);
6266 __ Bind(&slow_path);
6267 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
6268 __ Ldr(ip0.W(), ldr_address); // Load the LDR (register) unsigned offset.
6269 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6270 __ Ubfx(ip0, ip0, 16, 6); // Extract the index register, plus 32 (bit 21 is set).
6271 __ Bfi(ip1, ip0, 3, 6); // Insert ip0 to the entrypoint address to create
6272 // a switch case target based on the index register.
6273 __ Mov(ip0, base_reg); // Move the base register to ip0.
6274 __ Br(ip1); // Jump to the entrypoint's array switch case.
6275 break;
6276 }
6277 case BakerReadBarrierKind::kGcRoot: {
6278 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
6279 // and it does not have a forwarding address), call the correct introspection entrypoint;
6280 // otherwise return the reference (or the extracted forwarding address).
6281 // There is no gray bit check for GC roots.
6282 auto root_reg =
6283 Register::GetWRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6284 CheckValidReg(root_reg.GetCode());
6285 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6286 BakerReadBarrierSecondRegField::Decode(encoded_data));
6287 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6288 temps.Exclude(ip0, ip1);
6289 vixl::aarch64::Label return_label, not_marked, forwarding_address;
6290 __ Cbz(root_reg, &return_label);
6291 MemOperand lock_word(root_reg.X(), mirror::Object::MonitorOffset().Int32Value());
6292 __ Ldr(ip0.W(), lock_word);
6293 __ Tbz(ip0.W(), LockWord::kMarkBitStateShift, &not_marked);
6294 __ Bind(&return_label);
6295 __ Br(lr);
6296 __ Bind(&not_marked);
6297 __ Tst(ip0.W(), Operand(ip0.W(), LSL, 1));
6298 __ B(&forwarding_address, mi);
6299 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6300 // Adjust the art_quick_read_barrier_mark_introspection address in IP1 to
6301 // art_quick_read_barrier_mark_introspection_gc_roots.
6302 __ Add(ip1, ip1, Operand(BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRYPOINT_OFFSET));
6303 __ Mov(ip0.W(), root_reg);
6304 __ Br(ip1);
6305 __ Bind(&forwarding_address);
6306 __ Lsl(root_reg, ip0.W(), LockWord::kForwardingAddressShift);
6307 __ Br(lr);
6308 break;
6309 }
6310 default:
6311 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
6312 UNREACHABLE();
6313 }
6314
Vladimir Marko966b46f2018-08-03 10:20:19 +00006315 // For JIT, the slow path is considered part of the compiled method,
6316 // so JIT should pass null as `debug_name`. Tests may not have a runtime.
6317 DCHECK(Runtime::Current() == nullptr ||
6318 !Runtime::Current()->UseJitCompilation() ||
6319 debug_name == nullptr);
6320 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006321 std::ostringstream oss;
6322 oss << "BakerReadBarrierThunk";
6323 switch (kind) {
6324 case BakerReadBarrierKind::kField:
6325 oss << "Field_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
6326 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
6327 break;
Vladimir Marko0ecac682018-08-07 10:40:38 +01006328 case BakerReadBarrierKind::kAcquire:
6329 oss << "Acquire_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
6330 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
6331 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +00006332 case BakerReadBarrierKind::kArray:
6333 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
6334 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6335 BakerReadBarrierSecondRegField::Decode(encoded_data));
6336 break;
6337 case BakerReadBarrierKind::kGcRoot:
6338 oss << "GcRoot_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
6339 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6340 BakerReadBarrierSecondRegField::Decode(encoded_data));
6341 break;
6342 }
6343 *debug_name = oss.str();
6344 }
6345}
6346
6347#undef __
6348
Alexandre Rames5319def2014-10-23 10:03:10 +01006349} // namespace arm64
6350} // namespace art