blob: 415c6bf71e309f57d5d4b400863c6a0a8c2ffa43 [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) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100171 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
172 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
173 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();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100215 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
216 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
Vladimir Marko804b03f2016-09-14 16:26:36 +0100227 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
228 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,
Zheng Xuda403092015-04-24 17:35:39 +0800237 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
238}
239
240void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100241 SaveRestoreLiveRegistersHelper(codegen,
242 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800243 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
244}
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
Alexandre Rames67555f72014-11-18 10:55:16 +0000250 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
Alexandre Rames8158f282015-08-07 10:26:17 +0100276 bool IsFatal() const OVERRIDE { return true; }
277
Alexandre Rames9931f312015-06-19 14:47:01 +0100278 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
279
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
288 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
289 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
Alexandre Rames8158f282015-08-07 10:26:17 +0100295 bool IsFatal() const OVERRIDE { return true; }
296
Alexandre Rames9931f312015-06-19 14:47:01 +0100297 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
298
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
311 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
Alexandre Rames9931f312015-06-19 14:47:01 +0100352 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
353
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
366 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
367 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
387 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
388
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
Alexandre Rames67555f72014-11-18 10:55:16 +0000397 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
398 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
Alexandre Rames8158f282015-08-07 10:26:17 +0100411 bool IsFatal() const OVERRIDE { return true; }
412
Alexandre Rames9931f312015-06-19 14:47:01 +0100413 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
414
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
Alexandre Rames67555f72014-11-18 10:55:16 +0000424 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
Alexandre Rames9931f312015-06-19 14:47:01 +0100448 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
449
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
465 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
Alexandre Rames9931f312015-06-19 14:47:01 +0100506 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100507 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
520 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
Alexandre Rames9931f312015-06-19 14:47:01 +0100532 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
533
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
542 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
543 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
573 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
574
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 Levillain54f869e2017-03-06 13:54:11 +0000602// Abstract base class for read barrier slow paths marking a reference
603// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000604//
Roland Levillain54f869e2017-03-06 13:54:11 +0000605// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100606// barrier marking runtime entry point to be invoked or an empty
607// location; in the latter case, the read barrier marking runtime
608// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000609class ReadBarrierMarkSlowPathBaseARM64 : public SlowPathCodeARM64 {
610 protected:
611 ReadBarrierMarkSlowPathBaseARM64(HInstruction* instruction, Location ref, Location entrypoint)
612 : SlowPathCodeARM64(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000613 DCHECK(kEmitCompilerReadBarrier);
614 }
615
Roland Levillain54f869e2017-03-06 13:54:11 +0000616 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM64"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000617
Roland Levillain54f869e2017-03-06 13:54:11 +0000618 // Generate assembly code calling the read barrier marking runtime
619 // entry point (ReadBarrierMarkRegX).
620 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000621 // No need to save live registers; it's taken care of by the
622 // entrypoint. Also, there is no need to update the stack mask,
623 // as this runtime call will not trigger a garbage collection.
624 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
625 DCHECK_NE(ref_.reg(), LR);
626 DCHECK_NE(ref_.reg(), WSP);
627 DCHECK_NE(ref_.reg(), WZR);
628 // IP0 is used internally by the ReadBarrierMarkRegX entry point
629 // as a temporary, it cannot be the entry point's input/output.
630 DCHECK_NE(ref_.reg(), IP0);
631 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
632 // "Compact" slow path, saving two moves.
633 //
634 // Instead of using the standard runtime calling convention (input
635 // and output in W0):
636 //
637 // W0 <- ref
638 // W0 <- ReadBarrierMark(W0)
639 // ref <- W0
640 //
641 // we just use rX (the register containing `ref`) as input and output
642 // of a dedicated entrypoint:
643 //
644 // rX <- ReadBarrierMarkRegX(rX)
645 //
646 if (entrypoint_.IsValid()) {
647 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
648 __ Blr(XRegisterFrom(entrypoint_));
649 } else {
650 // Entrypoint is not already loaded, load from the thread.
651 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100652 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000653 // This runtime call does not require a stack map.
654 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
655 }
656 }
657
658 // The location (register) of the marked object reference.
659 const Location ref_;
660
661 // The location of the entrypoint if it is already loaded.
662 const Location entrypoint_;
663
Roland Levillain54f869e2017-03-06 13:54:11 +0000664 private:
665 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM64);
666};
667
Roland Levillain54f869e2017-03-06 13:54:11 +0000668// Slow path loading `obj`'s lock word, loading a reference from
669// object `*(obj + offset + (index << scale_factor))` into `ref`, and
670// marking `ref` if `obj` is gray according to the lock word (Baker
Roland Levillain54f869e2017-03-06 13:54:11 +0000671// read barrier). If needed, this slow path also atomically updates
672// the field `obj.field` in the object `obj` holding this reference
Vladimir Marko248141f2018-08-10 10:40:07 +0100673// after marking.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100674//
675// This means that after the execution of this slow path, both `ref`
676// and `obj.field` will be up-to-date; i.e., after the flip, both will
677// hold the same to-space reference (unless another thread installed
678// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000679//
Roland Levillain54f869e2017-03-06 13:54:11 +0000680// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100681// barrier marking runtime entry point to be invoked or an empty
682// location; in the latter case, the read barrier marking runtime
683// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000684class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
685 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100686 public:
Roland Levillain97c46462017-05-11 14:04:03 +0100687 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
688 HInstruction* instruction,
689 Location ref,
690 Register obj,
691 uint32_t offset,
692 Location index,
693 size_t scale_factor,
694 bool needs_null_check,
695 bool use_load_acquire,
696 Register temp,
697 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000698 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100699 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000700 offset_(offset),
701 index_(index),
702 scale_factor_(scale_factor),
703 needs_null_check_(needs_null_check),
704 use_load_acquire_(use_load_acquire),
Roland Levillain35345a52017-02-27 14:32:08 +0000705 temp_(temp) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100706 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000707 DCHECK(kUseBakerReadBarrier);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100708 }
709
710 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000711 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64";
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100712 }
713
714 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
715 LocationSummary* locations = instruction_->GetLocations();
716 Register ref_reg = WRegisterFrom(ref_);
717 DCHECK(locations->CanCall());
718 DCHECK(ref_.IsRegister()) << ref_;
719 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000720 DCHECK(obj_.IsW());
721 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
722
723 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100724 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
725 << "Unexpected instruction in read barrier marking and field updating slow path: "
726 << instruction_->DebugName();
727 DCHECK(instruction_->GetLocations()->Intrinsified());
728 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000729 DCHECK_EQ(offset_, 0u);
730 DCHECK_EQ(scale_factor_, 0u);
731 DCHECK_EQ(use_load_acquire_, false);
732 // The location of the offset of the marked reference field within `obj_`.
733 Location field_offset = index_;
734 DCHECK(field_offset.IsRegister()) << field_offset;
735
736 // Temporary register `temp_`, used to store the lock word, must
737 // not be IP0 nor IP1, as we may use them to emit the reference
738 // load (in the call to GenerateRawReferenceLoad below), and we
739 // need the lock word to still be in `temp_` after the reference
740 // load.
741 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
742 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100743
744 __ Bind(GetEntryLabel());
745
Vladimir Marko248141f2018-08-10 10:40:07 +0100746 // The implementation is:
Roland Levillainff487002017-03-07 16:50:01 +0000747 //
748 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
749 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
750 // HeapReference<mirror::Object> ref = *src; // Original reference load.
751 // bool is_gray = (rb_state == ReadBarrier::GrayState());
752 // if (is_gray) {
753 // old_ref = ref;
754 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
755 // compareAndSwapObject(obj, field_offset, old_ref, ref);
756 // }
757
Roland Levillain54f869e2017-03-06 13:54:11 +0000758 // /* int32_t */ monitor = obj->monitor_
759 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
760 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
761 if (needs_null_check_) {
762 codegen->MaybeRecordImplicitNullCheck(instruction_);
763 }
764 // /* LockWord */ lock_word = LockWord(monitor)
765 static_assert(sizeof(LockWord) == sizeof(int32_t),
766 "art::LockWord and int32_t have different sizes.");
767
768 // Introduce a dependency on the lock_word including rb_state,
769 // to prevent load-load reordering, and without using
770 // a memory barrier (which would be more expensive).
771 // `obj` is unchanged by this operation, but its value now depends
772 // on `temp`.
773 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
774
775 // The actual reference load.
776 // A possible implicit null check has already been handled above.
777 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
778 arm64_codegen->GenerateRawReferenceLoad(instruction_,
779 ref_,
780 obj_,
781 offset_,
782 index_,
783 scale_factor_,
784 /* needs_null_check */ false,
785 use_load_acquire_);
786
787 // Mark the object `ref` when `obj` is gray.
788 //
789 // if (rb_state == ReadBarrier::GrayState())
790 // ref = ReadBarrier::Mark(ref);
791 //
792 // Given the numeric representation, it's enough to check the low bit of the rb_state.
793 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
794 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
795 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
796
797 // Save the old value of the reference before marking it.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100798 // Note that we cannot use IP to save the old reference, as IP is
799 // used internally by the ReadBarrierMarkRegX entry point, and we
800 // need the old reference after the call to that entry point.
801 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
802 __ Mov(temp_.W(), ref_reg);
803
Roland Levillain54f869e2017-03-06 13:54:11 +0000804 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100805
806 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +0000807 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100808 //
809 // Note that this field could also hold a different object, if
810 // another thread had concurrently changed it. In that case, the
811 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
812 // (CAS) operation below would abort the CAS, leaving the field
813 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100814 __ Cmp(temp_.W(), ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +0000815 __ B(eq, GetExitLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100816
817 // Update the the holder's field atomically. This may fail if
818 // mutator updates before us, but it's OK. This is achieved
819 // using a strong compare-and-set (CAS) operation with relaxed
820 // memory synchronization ordering, where the expected value is
821 // the old reference and the desired value is the new reference.
822
823 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
824 UseScratchRegisterScope temps(masm);
825
826 // Convenience aliases.
827 Register base = obj_.W();
Roland Levillain54f869e2017-03-06 13:54:11 +0000828 Register offset = XRegisterFrom(field_offset);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100829 Register expected = temp_.W();
830 Register value = ref_reg;
831 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
832 Register tmp_value = temps.AcquireW(); // Value in memory.
833
834 __ Add(tmp_ptr, base.X(), Operand(offset));
835
836 if (kPoisonHeapReferences) {
837 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
838 if (value.Is(expected)) {
839 // Do not poison `value`, as it is the same register as
840 // `expected`, which has just been poisoned.
841 } else {
842 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
843 }
844 }
845
846 // do {
847 // tmp_value = [tmp_ptr] - expected;
848 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
849
Roland Levillain24a4d112016-10-26 13:10:46 +0100850 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100851 __ Bind(&loop_head);
852 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
853 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +0100854 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100855 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
856 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +0100857 __ B(&exit_loop);
858 __ Bind(&comparison_failed);
859 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100860 __ Bind(&exit_loop);
861
862 if (kPoisonHeapReferences) {
863 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
864 if (value.Is(expected)) {
865 // Do not unpoison `value`, as it is the same register as
866 // `expected`, which has just been unpoisoned.
867 } else {
868 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
869 }
870 }
871
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100872 __ B(GetExitLabel());
873 }
874
875 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100876 // The register containing the object holding the marked object reference field.
877 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +0000878 // The offset, index and scale factor to access the reference in `obj_`.
879 uint32_t offset_;
880 Location index_;
881 size_t scale_factor_;
882 // Is a null check required?
883 bool needs_null_check_;
884 // Should this reference load use Load-Acquire semantics?
885 bool use_load_acquire_;
886 // A temporary register used to hold the lock word of `obj_`; and
887 // also to hold the original reference value, when the reference is
888 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100889 const Register temp_;
890
Roland Levillain54f869e2017-03-06 13:54:11 +0000891 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100892};
893
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000894// Slow path generating a read barrier for a heap reference.
895class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
896 public:
897 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
898 Location out,
899 Location ref,
900 Location obj,
901 uint32_t offset,
902 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000903 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000904 out_(out),
905 ref_(ref),
906 obj_(obj),
907 offset_(offset),
908 index_(index) {
909 DCHECK(kEmitCompilerReadBarrier);
910 // If `obj` is equal to `out` or `ref`, it means the initial object
911 // has been overwritten by (or after) the heap object reference load
912 // to be instrumented, e.g.:
913 //
914 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000915 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000916 //
917 // In that case, we have lost the information about the original
918 // object, and the emitted read barrier cannot work properly.
919 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
920 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
921 }
922
923 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
924 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
925 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100926 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000927 DCHECK(locations->CanCall());
928 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100929 DCHECK(instruction_->IsInstanceFieldGet() ||
930 instruction_->IsStaticFieldGet() ||
931 instruction_->IsArrayGet() ||
932 instruction_->IsInstanceOf() ||
933 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700934 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000935 << "Unexpected instruction in read barrier for heap reference slow path: "
936 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000937 // The read barrier instrumentation of object ArrayGet
938 // instructions does not support the HIntermediateAddress
939 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000940 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100941 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000942
943 __ Bind(GetEntryLabel());
944
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000945 SaveLiveRegisters(codegen, locations);
946
947 // We may have to change the index's value, but as `index_` is a
948 // constant member (like other "inputs" of this slow path),
949 // introduce a copy of it, `index`.
950 Location index = index_;
951 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100952 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000953 if (instruction_->IsArrayGet()) {
954 // Compute the actual memory offset and store it in `index`.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100955 Register index_reg = RegisterFrom(index_, DataType::Type::kInt32);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000956 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
957 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
958 // We are about to change the value of `index_reg` (see the
959 // calls to vixl::MacroAssembler::Lsl and
960 // vixl::MacroAssembler::Mov below), but it has
961 // not been saved by the previous call to
962 // art::SlowPathCode::SaveLiveRegisters, as it is a
963 // callee-save register --
964 // art::SlowPathCode::SaveLiveRegisters does not consider
965 // callee-save registers, as it has been designed with the
966 // assumption that callee-save registers are supposed to be
967 // handled by the called function. So, as a callee-save
968 // register, `index_reg` _would_ eventually be saved onto
969 // the stack, but it would be too late: we would have
970 // changed its value earlier. Therefore, we manually save
971 // it here into another freely available register,
972 // `free_reg`, chosen of course among the caller-save
973 // registers (as a callee-save `free_reg` register would
974 // exhibit the same problem).
975 //
976 // Note we could have requested a temporary register from
977 // the register allocator instead; but we prefer not to, as
978 // this is a slow path, and we know we can find a
979 // caller-save register that is available.
980 Register free_reg = FindAvailableCallerSaveRegister(codegen);
981 __ Mov(free_reg.W(), index_reg);
982 index_reg = free_reg;
983 index = LocationFrom(index_reg);
984 } else {
985 // The initial register stored in `index_` has already been
986 // saved in the call to art::SlowPathCode::SaveLiveRegisters
987 // (as it is not a callee-save register), so we can freely
988 // use it.
989 }
990 // Shifting the index value contained in `index_reg` by the scale
991 // factor (2) cannot overflow in practice, as the runtime is
992 // unable to allocate object arrays with a size larger than
993 // 2^26 - 1 (that is, 2^28 - 4 bytes).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100994 __ Lsl(index_reg, index_reg, DataType::SizeShift(type));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000995 static_assert(
996 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
997 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
998 __ Add(index_reg, index_reg, Operand(offset_));
999 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001000 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1001 // intrinsics, `index_` is not shifted by a scale factor of 2
1002 // (as in the case of ArrayGet), as it is actually an offset
1003 // to an object field within an object.
1004 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001005 DCHECK(instruction_->GetLocations()->Intrinsified());
1006 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1007 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1008 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001009 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001010 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001011 }
1012 }
1013
1014 // We're moving two or three locations to locations that could
1015 // overlap, so we need a parallel move resolver.
1016 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001017 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001018 parallel_move.AddMove(ref_,
1019 LocationFrom(calling_convention.GetRegisterAt(0)),
1020 type,
1021 nullptr);
1022 parallel_move.AddMove(obj_,
1023 LocationFrom(calling_convention.GetRegisterAt(1)),
1024 type,
1025 nullptr);
1026 if (index.IsValid()) {
1027 parallel_move.AddMove(index,
1028 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001029 DataType::Type::kInt32,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001030 nullptr);
1031 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1032 } else {
1033 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1034 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1035 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001036 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001037 instruction_,
1038 instruction_->GetDexPc(),
1039 this);
1040 CheckEntrypointTypes<
1041 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1042 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1043
1044 RestoreLiveRegisters(codegen, locations);
1045
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001046 __ B(GetExitLabel());
1047 }
1048
1049 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1050
1051 private:
1052 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001053 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1054 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001055 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1056 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1057 return Register(VIXLRegCodeFromART(i), kXRegSize);
1058 }
1059 }
1060 // We shall never fail to find a free caller-save register, as
1061 // there are more than two core caller-save registers on ARM64
1062 // (meaning it is possible to find one which is different from
1063 // `ref` and `obj`).
1064 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1065 LOG(FATAL) << "Could not find a free register";
1066 UNREACHABLE();
1067 }
1068
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001069 const Location out_;
1070 const Location ref_;
1071 const Location obj_;
1072 const uint32_t offset_;
1073 // An additional location containing an index to an array.
1074 // Only used for HArrayGet and the UnsafeGetObject &
1075 // UnsafeGetObjectVolatile intrinsics.
1076 const Location index_;
1077
1078 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1079};
1080
1081// Slow path generating a read barrier for a GC root.
1082class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1083 public:
1084 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001085 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001086 DCHECK(kEmitCompilerReadBarrier);
1087 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001088
1089 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1090 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001091 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001092 DCHECK(locations->CanCall());
1093 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001094 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1095 << "Unexpected instruction in read barrier for GC root slow path: "
1096 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001097
1098 __ Bind(GetEntryLabel());
1099 SaveLiveRegisters(codegen, locations);
1100
1101 InvokeRuntimeCallingConvention calling_convention;
1102 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1103 // The argument of the ReadBarrierForRootSlow is not a managed
1104 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1105 // thus we need a 64-bit move here, and we cannot use
1106 //
1107 // arm64_codegen->MoveLocation(
1108 // LocationFrom(calling_convention.GetRegisterAt(0)),
1109 // root_,
1110 // type);
1111 //
1112 // which would emit a 32-bit move, as `type` is a (32-bit wide)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001113 // reference type (`DataType::Type::kReference`).
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001114 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001115 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001116 instruction_,
1117 instruction_->GetDexPc(),
1118 this);
1119 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1120 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1121
1122 RestoreLiveRegisters(codegen, locations);
1123 __ B(GetExitLabel());
1124 }
1125
1126 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1127
1128 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001129 const Location out_;
1130 const Location root_;
1131
1132 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1133};
1134
Alexandre Rames5319def2014-10-23 10:03:10 +01001135#undef __
1136
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001137Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001138 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001139 if (type == DataType::Type::kVoid) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001140 LOG(FATAL) << "Unreachable type " << type;
1141 }
1142
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001143 if (DataType::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001144 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
1145 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001146 } else if (!DataType::IsFloatingPointType(type) &&
Alexandre Rames542361f2015-01-29 16:57:31 +00001147 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001148 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1149 } else {
1150 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001151 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1152 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001153 }
1154
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001155 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001156 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001157 return next_location;
1158}
1159
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001160Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001161 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001162}
1163
Serban Constantinescu579885a2015-02-22 20:51:33 +00001164CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001165 const CompilerOptions& compiler_options,
1166 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001167 : CodeGenerator(graph,
1168 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001169 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001170 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001171 callee_saved_core_registers.GetList(),
1172 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001173 compiler_options,
1174 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001175 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1176 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001177 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001178 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001179 move_resolver_(graph->GetAllocator(), this),
1180 assembler_(graph->GetAllocator()),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001181 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001182 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001183 uint64_literals_(std::less<uint64_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001184 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001185 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001186 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001187 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001188 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001189 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001190 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6fd16062018-06-26 11:02:04 +01001191 boot_image_intrinsic_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001192 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001193 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001194 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001195 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +00001196 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1197 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
1198 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001199 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001200 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001201}
Alexandre Rames5319def2014-10-23 10:03:10 +01001202
Alexandre Rames67555f72014-11-18 10:55:16 +00001203#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001204
Zheng Xu3927c8b2015-11-18 17:46:25 +08001205void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001206 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001207 jump_table->EmitTable(this);
1208 }
1209}
1210
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001211void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001212 EmitJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +00001213
1214 // Emit JIT baker read barrier slow paths.
1215 DCHECK(Runtime::Current()->UseJitCompilation() || jit_baker_read_barrier_slow_paths_.empty());
1216 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
1217 uint32_t encoded_data = entry.first;
1218 vixl::aarch64::Label* slow_path_entry = &entry.second.label;
1219 __ Bind(slow_path_entry);
1220 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name */ nullptr);
1221 }
1222
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001223 // Ensure we emit the literal pool.
1224 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001225
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001226 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001227
1228 // Verify Baker read barrier linker patches.
1229 if (kIsDebugBuild) {
1230 ArrayRef<const uint8_t> code = allocator->GetMemory();
1231 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
1232 DCHECK(info.label.IsBound());
1233 uint32_t literal_offset = info.label.GetLocation();
1234 DCHECK_ALIGNED(literal_offset, 4u);
1235
1236 auto GetInsn = [&code](uint32_t offset) {
1237 DCHECK_ALIGNED(offset, 4u);
1238 return
1239 (static_cast<uint32_t>(code[offset + 0]) << 0) +
1240 (static_cast<uint32_t>(code[offset + 1]) << 8) +
1241 (static_cast<uint32_t>(code[offset + 2]) << 16)+
1242 (static_cast<uint32_t>(code[offset + 3]) << 24);
1243 };
1244
1245 const uint32_t encoded_data = info.custom_data;
1246 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
1247 // Check that the next instruction matches the expected LDR.
1248 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01001249 case BakerReadBarrierKind::kField:
1250 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00001251 DCHECK_GE(code.size() - literal_offset, 8u);
1252 uint32_t next_insn = GetInsn(literal_offset + 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001253 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1254 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko0ecac682018-08-07 10:40:38 +01001255 if (kind == BakerReadBarrierKind::kField) {
1256 // LDR (immediate) with correct base_reg.
1257 CHECK_EQ(next_insn & 0xffc003e0u, 0xb9400000u | (base_reg << 5));
1258 } else {
1259 DCHECK(kind == BakerReadBarrierKind::kAcquire);
1260 // LDAR with correct base_reg.
1261 CHECK_EQ(next_insn & 0xffffffe0u, 0x88dffc00u | (base_reg << 5));
1262 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001263 break;
1264 }
1265 case BakerReadBarrierKind::kArray: {
1266 DCHECK_GE(code.size() - literal_offset, 8u);
1267 uint32_t next_insn = GetInsn(literal_offset + 4u);
1268 // LDR (register) with the correct base_reg, size=10 (32-bit), option=011 (extend = LSL),
1269 // and S=1 (shift amount = 2 for 32-bit version), i.e. LDR Wt, [Xn, Xm, LSL #2].
1270 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1271 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1272 CHECK_EQ(next_insn & 0xffe0ffe0u, 0xb8607800u | (base_reg << 5));
1273 CheckValidReg((next_insn >> 16) & 0x1f); // Check index register
1274 break;
1275 }
1276 case BakerReadBarrierKind::kGcRoot: {
1277 DCHECK_GE(literal_offset, 4u);
1278 uint32_t prev_insn = GetInsn(literal_offset - 4u);
1279 // LDR (immediate) with correct root_reg.
1280 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1281 CHECK_EQ(prev_insn & 0xffc0001fu, 0xb9400000u | root_reg);
1282 break;
1283 }
1284 default:
1285 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
1286 UNREACHABLE();
1287 }
1288 }
1289 }
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001290}
1291
Zheng Xuad4450e2015-04-17 18:48:56 +08001292void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1293 // Note: There are 6 kinds of moves:
1294 // 1. constant -> GPR/FPR (non-cycle)
1295 // 2. constant -> stack (non-cycle)
1296 // 3. GPR/FPR -> GPR/FPR
1297 // 4. GPR/FPR -> stack
1298 // 5. stack -> GPR/FPR
1299 // 6. stack -> stack (non-cycle)
1300 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1301 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1302 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1303 // dependency.
1304 vixl_temps_.Open(GetVIXLAssembler());
1305}
1306
1307void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1308 vixl_temps_.Close();
1309}
1310
1311Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001312 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1313 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1314 || kind == Location::kSIMDStackSlot);
1315 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1316 ? Location::kFpuRegister
1317 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001318 Location scratch = GetScratchLocation(kind);
1319 if (!scratch.Equals(Location::NoLocation())) {
1320 return scratch;
1321 }
1322 // Allocate from VIXL temp registers.
1323 if (kind == Location::kRegister) {
1324 scratch = LocationFrom(vixl_temps_.AcquireX());
1325 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001326 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001327 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1328 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1329 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001330 }
1331 AddScratchLocation(scratch);
1332 return scratch;
1333}
1334
1335void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1336 if (loc.IsRegister()) {
1337 vixl_temps_.Release(XRegisterFrom(loc));
1338 } else {
1339 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001340 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001341 }
1342 RemoveScratchLocation(loc);
1343}
1344
Alexandre Rames3e69f162014-12-10 10:36:50 +00001345void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001346 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001347 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001348}
1349
Alexandre Rames5319def2014-10-23 10:03:10 +01001350void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001351 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001352 __ Bind(&frame_entry_label_);
1353
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001354 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1355 UseScratchRegisterScope temps(masm);
1356 Register temp = temps.AcquireX();
1357 __ Ldrh(temp, MemOperand(kArtMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
1358 __ Add(temp, temp, 1);
1359 __ Strh(temp, MemOperand(kArtMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
1360 }
1361
Vladimir Marko33bff252017-11-01 14:35:42 +00001362 bool do_overflow_check =
1363 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001364 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001365 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001366 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001367 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001368 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001369 {
1370 // Ensure that between load and RecordPcInfo there are no pools emitted.
1371 ExactAssemblyScope eas(GetVIXLAssembler(),
1372 kInstructionSize,
1373 CodeBufferCheckScope::kExactSize);
1374 __ ldr(wzr, MemOperand(temp, 0));
1375 RecordPcInfo(nullptr, 0);
1376 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001377 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001378
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001379 if (!HasEmptyFrame()) {
1380 int frame_size = GetFrameSize();
1381 // Stack layout:
1382 // sp[frame_size - 8] : lr.
1383 // ... : other preserved core registers.
1384 // ... : other preserved fp registers.
1385 // ... : reserved frame space.
1386 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001387
1388 // Save the current method if we need it. Note that we do not
1389 // do this in HCurrentMethod, as the instruction might have been removed
1390 // in the SSA graph.
1391 if (RequiresCurrentMethod()) {
1392 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001393 } else {
1394 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001395 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001396 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001397 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1398 frame_size - GetCoreSpillSize());
1399 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1400 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001401
1402 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1403 // Initialize should_deoptimize flag to 0.
1404 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1405 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1406 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001407 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01001408
1409 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001410}
1411
1412void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001413 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001414 if (!HasEmptyFrame()) {
1415 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001416 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1417 frame_size - FrameEntrySpillSize());
1418 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1419 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001420 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001421 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001422 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001423 __ Ret();
1424 GetAssembler()->cfi().RestoreState();
1425 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001426}
1427
Scott Wakeling97c72b72016-06-24 16:19:36 +01001428CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001429 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001430 return CPURegList(CPURegister::kRegister, kXRegSize,
1431 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001432}
1433
Scott Wakeling97c72b72016-06-24 16:19:36 +01001434CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001435 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1436 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001437 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1438 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001439}
1440
Alexandre Rames5319def2014-10-23 10:03:10 +01001441void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1442 __ Bind(GetLabelOf(block));
1443}
1444
Calin Juravle175dc732015-08-25 15:42:32 +01001445void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1446 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001447 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001448}
1449
Calin Juravlee460d1d2015-09-29 04:52:17 +01001450void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1451 if (location.IsRegister()) {
1452 locations->AddTemp(location);
1453 } else {
1454 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1455 }
1456}
1457
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001458void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001459 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001460 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001461 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001462 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001463 if (value_can_be_null) {
1464 __ Cbz(value, &done);
1465 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001466 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001467 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001468 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001469 if (value_can_be_null) {
1470 __ Bind(&done);
1471 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001472}
1473
David Brazdil58282f42016-01-14 12:45:10 +00001474void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001475 // Blocked core registers:
1476 // lr : Runtime reserved.
1477 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001478 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001479 // ip1 : VIXL core temp.
1480 // ip0 : VIXL core temp.
1481 //
1482 // Blocked fp registers:
1483 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001484 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1485 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001486 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001487 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001488 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001489
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001490 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001491 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001492 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001493 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001494
David Brazdil58282f42016-01-14 12:45:10 +00001495 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001496 // Stubs do not save callee-save floating point registers. If the graph
1497 // is debuggable, we need to deal with these registers differently. For
1498 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001499 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1500 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001501 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001502 }
1503 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001504}
1505
Alexandre Rames3e69f162014-12-10 10:36:50 +00001506size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1507 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1508 __ Str(reg, MemOperand(sp, stack_index));
1509 return kArm64WordSize;
1510}
1511
1512size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1513 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1514 __ Ldr(reg, MemOperand(sp, stack_index));
1515 return kArm64WordSize;
1516}
1517
1518size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1519 FPRegister reg = FPRegister(reg_id, kDRegSize);
1520 __ Str(reg, MemOperand(sp, stack_index));
1521 return kArm64WordSize;
1522}
1523
1524size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1525 FPRegister reg = FPRegister(reg_id, kDRegSize);
1526 __ Ldr(reg, MemOperand(sp, stack_index));
1527 return kArm64WordSize;
1528}
1529
Alexandre Rames5319def2014-10-23 10:03:10 +01001530void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001531 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001532}
1533
1534void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001535 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001536}
1537
Vladimir Markoa0431112018-06-25 09:32:54 +01001538const Arm64InstructionSetFeatures& CodeGeneratorARM64::GetInstructionSetFeatures() const {
1539 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArm64InstructionSetFeatures();
1540}
1541
Alexandre Rames67555f72014-11-18 10:55:16 +00001542void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001543 if (constant->IsIntConstant()) {
1544 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1545 } else if (constant->IsLongConstant()) {
1546 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1547 } else if (constant->IsNullConstant()) {
1548 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001549 } else if (constant->IsFloatConstant()) {
1550 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1551 } else {
1552 DCHECK(constant->IsDoubleConstant());
1553 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1554 }
1555}
1556
Alexandre Rames3e69f162014-12-10 10:36:50 +00001557
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001558static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001559 DCHECK(constant.IsConstant());
1560 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001561 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001562 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001563 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1564 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1565 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1566 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001567}
1568
Roland Levillain952b2352017-05-03 19:49:14 +01001569// Allocate a scratch register from the VIXL pool, querying first
1570// the floating-point register pool, and then the core register
1571// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001572// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1573// using a different allocation strategy.
1574static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1575 vixl::aarch64::UseScratchRegisterScope* temps,
1576 int size_in_bits) {
1577 return masm->GetScratchFPRegisterList()->IsEmpty()
1578 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1579 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1580}
1581
Calin Juravlee460d1d2015-09-29 04:52:17 +01001582void CodeGeneratorARM64::MoveLocation(Location destination,
1583 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001584 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001585 if (source.Equals(destination)) {
1586 return;
1587 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001588
1589 // A valid move can always be inferred from the destination and source
1590 // locations. When moving from and to a register, the argument type can be
1591 // used to generate 32bit instead of 64bit moves. In debug mode we also
1592 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001593 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001594
1595 if (destination.IsRegister() || destination.IsFpuRegister()) {
1596 if (unspecified_type) {
1597 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1598 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001599 (src_cst != nullptr && (src_cst->IsIntConstant()
1600 || src_cst->IsFloatConstant()
1601 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001602 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001603 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001604 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001605 // If the source is a double stack slot or a 64bit constant, a 64bit
1606 // type is appropriate. Else the source is a register, and since the
1607 // type has not been specified, we chose a 64bit type to force a 64bit
1608 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001609 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001610 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001611 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001612 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1613 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001614 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001615 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1616 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1617 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001618 } else if (source.IsSIMDStackSlot()) {
1619 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001620 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001621 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001622 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001623 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001624 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001625 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001626 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001627 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001628 DataType::Type source_type = DataType::Is64BitType(dst_type)
1629 ? DataType::Type::kInt64
1630 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001631 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1632 }
1633 } else {
1634 DCHECK(source.IsFpuRegister());
1635 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001636 DataType::Type source_type = DataType::Is64BitType(dst_type)
1637 ? DataType::Type::kFloat64
1638 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001639 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1640 } else {
1641 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001642 if (GetGraph()->HasSIMD()) {
1643 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1644 } else {
1645 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1646 }
1647 }
1648 }
1649 } else if (destination.IsSIMDStackSlot()) {
1650 if (source.IsFpuRegister()) {
1651 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1652 } else {
1653 DCHECK(source.IsSIMDStackSlot());
1654 UseScratchRegisterScope temps(GetVIXLAssembler());
1655 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1656 Register temp = temps.AcquireX();
1657 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1658 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1659 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1660 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1661 } else {
1662 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1663 __ Ldr(temp, StackOperandFrom(source));
1664 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001665 }
1666 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001667 } else { // The destination is not a register. It must be a stack slot.
1668 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1669 if (source.IsRegister() || source.IsFpuRegister()) {
1670 if (unspecified_type) {
1671 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001672 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001673 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001674 dst_type =
1675 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001676 }
1677 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001678 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1679 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001680 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001681 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001682 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1683 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001684 UseScratchRegisterScope temps(GetVIXLAssembler());
1685 HConstant* src_cst = source.GetConstant();
1686 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001687 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001688 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1689 ? Register(xzr)
1690 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001691 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001692 if (src_cst->IsIntConstant()) {
1693 temp = temps.AcquireW();
1694 } else if (src_cst->IsLongConstant()) {
1695 temp = temps.AcquireX();
1696 } else if (src_cst->IsFloatConstant()) {
1697 temp = temps.AcquireS();
1698 } else {
1699 DCHECK(src_cst->IsDoubleConstant());
1700 temp = temps.AcquireD();
1701 }
1702 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001703 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001704 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001705 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001706 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001707 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001708 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001709 // Use any scratch register (a core or a floating-point one)
1710 // from VIXL scratch register pools as a temporary.
1711 //
1712 // We used to only use the FP scratch register pool, but in some
1713 // rare cases the only register from this pool (D31) would
1714 // already be used (e.g. within a ParallelMove instruction, when
1715 // a move is blocked by a another move requiring a scratch FP
1716 // register, which would reserve D31). To prevent this issue, we
1717 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001718 //
1719 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001720 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001721 // use AcquireFPOrCoreCPURegisterOfSize instead of
1722 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1723 // allocates core scratch registers first.
1724 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1725 GetVIXLAssembler(),
1726 &temps,
1727 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001728 __ Ldr(temp, StackOperandFrom(source));
1729 __ Str(temp, StackOperandFrom(destination));
1730 }
1731 }
1732}
1733
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001734void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001735 CPURegister dst,
1736 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001737 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001738 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001739 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001740 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001741 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001742 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001743 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001744 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001745 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001746 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001747 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001748 case DataType::Type::kInt16:
1749 __ Ldrsh(Register(dst), src);
1750 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001751 case DataType::Type::kInt32:
1752 case DataType::Type::kReference:
1753 case DataType::Type::kInt64:
1754 case DataType::Type::kFloat32:
1755 case DataType::Type::kFloat64:
1756 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001757 __ Ldr(dst, src);
1758 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001759 case DataType::Type::kUint32:
1760 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001761 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001762 LOG(FATAL) << "Unreachable type " << type;
1763 }
1764}
1765
Calin Juravle77520bc2015-01-12 18:45:46 +00001766void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001767 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001768 const MemOperand& src,
1769 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001770 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001771 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001772 Register temp_base = temps.AcquireX();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001773 DataType::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001774
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001775 DCHECK(!src.IsPreIndex());
1776 DCHECK(!src.IsPostIndex());
1777
1778 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001779 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001780 {
1781 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1782 MemOperand base = MemOperand(temp_base);
1783 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001784 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001785 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001786 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001787 {
1788 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1789 __ ldarb(Register(dst), base);
1790 if (needs_null_check) {
1791 MaybeRecordImplicitNullCheck(instruction);
1792 }
1793 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001794 if (type == DataType::Type::kInt8) {
1795 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001796 }
1797 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001798 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001799 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001800 {
1801 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1802 __ ldarh(Register(dst), base);
1803 if (needs_null_check) {
1804 MaybeRecordImplicitNullCheck(instruction);
1805 }
1806 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001807 if (type == DataType::Type::kInt16) {
1808 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
1809 }
Artem Serov914d7a82017-02-07 14:33:49 +00001810 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001811 case DataType::Type::kInt32:
1812 case DataType::Type::kReference:
1813 case DataType::Type::kInt64:
1814 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001815 {
1816 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1817 __ ldar(Register(dst), base);
1818 if (needs_null_check) {
1819 MaybeRecordImplicitNullCheck(instruction);
1820 }
1821 }
1822 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001823 case DataType::Type::kFloat32:
1824 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00001825 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001826 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001827
Artem Serov914d7a82017-02-07 14:33:49 +00001828 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1829 {
1830 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1831 __ ldar(temp, base);
1832 if (needs_null_check) {
1833 MaybeRecordImplicitNullCheck(instruction);
1834 }
1835 }
1836 __ Fmov(FPRegister(dst), temp);
1837 break;
Roland Levillain44015862016-01-22 11:47:17 +00001838 }
Aart Bik66c158e2018-01-31 12:55:04 -08001839 case DataType::Type::kUint32:
1840 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001841 case DataType::Type::kVoid:
Artem Serov914d7a82017-02-07 14:33:49 +00001842 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001843 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001844 }
1845}
1846
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001847void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001848 CPURegister src,
1849 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001850 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001851 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001852 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001853 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001854 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001855 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001856 case DataType::Type::kUint16:
1857 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001858 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001859 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001860 case DataType::Type::kInt32:
1861 case DataType::Type::kReference:
1862 case DataType::Type::kInt64:
1863 case DataType::Type::kFloat32:
1864 case DataType::Type::kFloat64:
1865 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001866 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001867 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001868 case DataType::Type::kUint32:
1869 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001870 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001871 LOG(FATAL) << "Unreachable type " << type;
1872 }
1873}
1874
Artem Serov914d7a82017-02-07 14:33:49 +00001875void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001876 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001877 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00001878 const MemOperand& dst,
1879 bool needs_null_check) {
1880 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001881 UseScratchRegisterScope temps(GetVIXLAssembler());
1882 Register temp_base = temps.AcquireX();
1883
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001884 DCHECK(!dst.IsPreIndex());
1885 DCHECK(!dst.IsPostIndex());
1886
1887 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001888 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001889 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001890 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00001891 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001892 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001893 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001894 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001895 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001896 {
1897 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1898 __ stlrb(Register(src), base);
1899 if (needs_null_check) {
1900 MaybeRecordImplicitNullCheck(instruction);
1901 }
1902 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001903 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001904 case DataType::Type::kUint16:
1905 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001906 {
1907 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1908 __ stlrh(Register(src), base);
1909 if (needs_null_check) {
1910 MaybeRecordImplicitNullCheck(instruction);
1911 }
1912 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001913 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001914 case DataType::Type::kInt32:
1915 case DataType::Type::kReference:
1916 case DataType::Type::kInt64:
1917 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001918 {
1919 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1920 __ stlr(Register(src), base);
1921 if (needs_null_check) {
1922 MaybeRecordImplicitNullCheck(instruction);
1923 }
1924 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001925 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001926 case DataType::Type::kFloat32:
1927 case DataType::Type::kFloat64: {
1928 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001929 Register temp_src;
1930 if (src.IsZero()) {
1931 // The zero register is used to avoid synthesizing zero constants.
1932 temp_src = Register(src);
1933 } else {
1934 DCHECK(src.IsFPRegister());
1935 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1936 __ Fmov(temp_src, FPRegister(src));
1937 }
Artem Serov914d7a82017-02-07 14:33:49 +00001938 {
1939 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1940 __ stlr(temp_src, base);
1941 if (needs_null_check) {
1942 MaybeRecordImplicitNullCheck(instruction);
1943 }
1944 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001945 break;
1946 }
Aart Bik66c158e2018-01-31 12:55:04 -08001947 case DataType::Type::kUint32:
1948 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001949 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001950 LOG(FATAL) << "Unreachable type " << type;
1951 }
1952}
1953
Calin Juravle175dc732015-08-25 15:42:32 +01001954void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1955 HInstruction* instruction,
1956 uint32_t dex_pc,
1957 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001958 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00001959
1960 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
1961 {
1962 // Ensure the pc position is recorded immediately after the `blr` instruction.
1963 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1964 __ blr(lr);
1965 if (EntrypointRequiresStackMap(entrypoint)) {
1966 RecordPcInfo(instruction, dex_pc, slow_path);
1967 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001968 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001969}
1970
Roland Levillaindec8f632016-07-22 17:10:06 +01001971void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1972 HInstruction* instruction,
1973 SlowPathCode* slow_path) {
1974 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01001975 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1976 __ Blr(lr);
1977}
1978
Alexandre Rames67555f72014-11-18 10:55:16 +00001979void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001980 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001981 UseScratchRegisterScope temps(GetVIXLAssembler());
1982 Register temp = temps.AcquireW();
Vladimir Markodc682aa2018-01-04 18:42:57 +00001983 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
1984 const size_t status_byte_offset =
1985 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1986 constexpr uint32_t shifted_initialized_value =
1987 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001988
Serban Constantinescu02164b32014-11-13 14:05:07 +00001989 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001990 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Vladimir Markodc682aa2018-01-04 18:42:57 +00001991 __ Add(temp, class_reg, status_byte_offset);
Igor Murashkin86083f72017-10-27 10:59:04 -07001992 __ Ldarb(temp, HeapOperand(temp));
Vladimir Markodc682aa2018-01-04 18:42:57 +00001993 __ Cmp(temp, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00001994 __ B(lo, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001995 __ Bind(slow_path->GetExitLabel());
1996}
Alexandre Rames5319def2014-10-23 10:03:10 +01001997
Vladimir Marko175e7862018-03-27 09:03:13 +00001998void InstructionCodeGeneratorARM64::GenerateBitstringTypeCheckCompare(
1999 HTypeCheckInstruction* check, vixl::aarch64::Register temp) {
2000 uint32_t path_to_root = check->GetBitstringPathToRoot();
2001 uint32_t mask = check->GetBitstringMask();
2002 DCHECK(IsPowerOfTwo(mask + 1));
2003 size_t mask_bits = WhichPowerOf2(mask + 1);
2004
2005 if (mask_bits == 16u) {
2006 // Load only the bitstring part of the status word.
2007 __ Ldrh(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
2008 } else {
2009 // /* uint32_t */ temp = temp->status_
2010 __ Ldr(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
2011 // Extract the bitstring bits.
2012 __ Ubfx(temp, temp, 0, mask_bits);
2013 }
2014 // Compare the bitstring bits to `path_to_root`.
2015 __ Cmp(temp, path_to_root);
2016}
2017
Roland Levillain44015862016-01-22 11:47:17 +00002018void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002019 BarrierType type = BarrierAll;
2020
2021 switch (kind) {
2022 case MemBarrierKind::kAnyAny:
2023 case MemBarrierKind::kAnyStore: {
2024 type = BarrierAll;
2025 break;
2026 }
2027 case MemBarrierKind::kLoadAny: {
2028 type = BarrierReads;
2029 break;
2030 }
2031 case MemBarrierKind::kStoreStore: {
2032 type = BarrierWrites;
2033 break;
2034 }
2035 default:
2036 LOG(FATAL) << "Unexpected memory barrier " << kind;
2037 }
2038 __ Dmb(InnerShareable, type);
2039}
2040
Serban Constantinescu02164b32014-11-13 14:05:07 +00002041void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2042 HBasicBlock* successor) {
2043 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002044 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2045 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002046 slow_path =
2047 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002048 instruction->SetSlowPath(slow_path);
2049 codegen_->AddSlowPath(slow_path);
2050 if (successor != nullptr) {
2051 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002052 }
2053 } else {
2054 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2055 }
2056
Serban Constantinescu02164b32014-11-13 14:05:07 +00002057 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2058 Register temp = temps.AcquireW();
2059
Andreas Gampe542451c2016-07-26 09:02:02 -07002060 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002061 if (successor == nullptr) {
2062 __ Cbnz(temp, slow_path->GetEntryLabel());
2063 __ Bind(slow_path->GetReturnLabel());
2064 } else {
2065 __ Cbz(temp, codegen_->GetLabelOf(successor));
2066 __ B(slow_path->GetEntryLabel());
2067 // slow_path will return to GetLabelOf(successor).
2068 }
2069}
2070
Alexandre Rames5319def2014-10-23 10:03:10 +01002071InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2072 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002073 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002074 assembler_(codegen->GetAssembler()),
2075 codegen_(codegen) {}
2076
Alexandre Rames67555f72014-11-18 10:55:16 +00002077void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002078 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002079 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002080 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002081 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002082 case DataType::Type::kInt32:
2083 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01002084 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002085 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002086 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002087 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002088
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002089 case DataType::Type::kFloat32:
2090 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002091 locations->SetInAt(0, Location::RequiresFpuRegister());
2092 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002093 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002094 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002095
Alexandre Rames5319def2014-10-23 10:03:10 +01002096 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002097 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002098 }
2099}
2100
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002101void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2102 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002103 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2104
2105 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002106 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01002107 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002108 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2109 object_field_get_with_read_barrier
2110 ? LocationSummary::kCallOnSlowPath
2111 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002112 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002113 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko0ecac682018-08-07 10:40:38 +01002114 // We need a temporary register for the read barrier load in
2115 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2116 // only if the field is volatile or the offset is too big.
2117 if (field_info.IsVolatile() ||
2118 field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2119 locations->AddTemp(FixedTempLocation());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002120 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002121 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002122 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002123 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002124 locations->SetOut(Location::RequiresFpuRegister());
2125 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002126 // The output overlaps for an object field get when read barriers
2127 // are enabled: we do not want the load to overwrite the object's
2128 // location, as we need it to emit the read barrier.
2129 locations->SetOut(
2130 Location::RequiresRegister(),
2131 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002132 }
2133}
2134
2135void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2136 const FieldInfo& field_info) {
2137 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002138 LocationSummary* locations = instruction->GetLocations();
2139 Location base_loc = locations->InAt(0);
2140 Location out = locations->Out();
2141 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01002142 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
2143 DataType::Type load_type = instruction->GetType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002144 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002145
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002146 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01002147 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002148 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002149 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002150 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002151 Location maybe_temp =
2152 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002153 // Note that potential implicit null checks are handled in this
2154 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2155 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2156 instruction,
2157 out,
2158 base,
2159 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002160 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00002161 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002162 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002163 } else {
2164 // General case.
2165 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002166 // Note that a potential implicit null check is handled in this
2167 // CodeGeneratorARM64::LoadAcquire call.
2168 // NB: LoadAcquire will record the pc info if needed.
2169 codegen_->LoadAcquire(
2170 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002171 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002172 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2173 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01002174 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002175 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002176 }
Vladimir Marko61b92282017-10-11 13:23:17 +01002177 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002178 // If read barriers are enabled, emit read barriers other than
2179 // Baker's using a slow path (and also unpoison the loaded
2180 // reference, if heap poisoning is enabled).
2181 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2182 }
Roland Levillain4d027112015-07-01 15:41:14 +01002183 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002184}
2185
2186void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2187 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002188 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01002189 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002190 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2191 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002192 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002193 locations->SetInAt(1, Location::RequiresFpuRegister());
2194 } else {
2195 locations->SetInAt(1, Location::RequiresRegister());
2196 }
2197}
2198
2199void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002200 const FieldInfo& field_info,
2201 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002202 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2203
2204 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002205 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002206 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002207 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002208 DataType::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002209
Roland Levillain4d027112015-07-01 15:41:14 +01002210 {
2211 // We use a block to end the scratch scope before the write barrier, thus
2212 // freeing the temporary registers so they can be used in `MarkGCCard`.
2213 UseScratchRegisterScope temps(GetVIXLAssembler());
2214
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002215 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002216 DCHECK(value.IsW());
2217 Register temp = temps.AcquireW();
2218 __ Mov(temp, value.W());
2219 GetAssembler()->PoisonHeapReference(temp.W());
2220 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002221 }
Roland Levillain4d027112015-07-01 15:41:14 +01002222
2223 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002224 codegen_->StoreRelease(
2225 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002226 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002227 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2228 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002229 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2230 codegen_->MaybeRecordImplicitNullCheck(instruction);
2231 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002232 }
2233
2234 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002235 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002236 }
2237}
2238
Alexandre Rames67555f72014-11-18 10:55:16 +00002239void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002240 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002241
2242 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002243 case DataType::Type::kInt32:
2244 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002245 Register dst = OutputRegister(instr);
2246 Register lhs = InputRegisterAt(instr, 0);
2247 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002248 if (instr->IsAdd()) {
2249 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002250 } else if (instr->IsAnd()) {
2251 __ And(dst, lhs, rhs);
2252 } else if (instr->IsOr()) {
2253 __ Orr(dst, lhs, rhs);
2254 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002255 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002256 } else if (instr->IsRor()) {
2257 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002258 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002259 __ Ror(dst, lhs, shift);
2260 } else {
2261 // Ensure shift distance is in the same size register as the result. If
2262 // we are rotating a long and the shift comes in a w register originally,
2263 // we don't need to sxtw for use as an x since the shift distances are
2264 // all & reg_bits - 1.
2265 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2266 }
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002267 } else if (instr->IsMin() || instr->IsMax()) {
2268 __ Cmp(lhs, rhs);
2269 __ Csel(dst, lhs, rhs, instr->IsMin() ? lt : gt);
Alexandre Rames67555f72014-11-18 10:55:16 +00002270 } else {
2271 DCHECK(instr->IsXor());
2272 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002273 }
2274 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002275 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002276 case DataType::Type::kFloat32:
2277 case DataType::Type::kFloat64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002278 FPRegister dst = OutputFPRegister(instr);
2279 FPRegister lhs = InputFPRegisterAt(instr, 0);
2280 FPRegister rhs = InputFPRegisterAt(instr, 1);
2281 if (instr->IsAdd()) {
2282 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002283 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002284 __ Fsub(dst, lhs, rhs);
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002285 } else if (instr->IsMin()) {
2286 __ Fmin(dst, lhs, rhs);
2287 } else if (instr->IsMax()) {
2288 __ Fmax(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002289 } else {
2290 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002291 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002292 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002293 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002294 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002295 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002296 }
2297}
2298
Serban Constantinescu02164b32014-11-13 14:05:07 +00002299void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2300 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2301
Vladimir Markoca6fff82017-10-03 14:49:14 +01002302 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002303 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002304 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002305 case DataType::Type::kInt32:
2306 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002307 locations->SetInAt(0, Location::RequiresRegister());
2308 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002309 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002310 break;
2311 }
2312 default:
2313 LOG(FATAL) << "Unexpected shift type " << type;
2314 }
2315}
2316
2317void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2318 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2319
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002320 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002321 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002322 case DataType::Type::kInt32:
2323 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002324 Register dst = OutputRegister(instr);
2325 Register lhs = InputRegisterAt(instr, 0);
2326 Operand rhs = InputOperandAt(instr, 1);
2327 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002328 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002329 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002330 if (instr->IsShl()) {
2331 __ Lsl(dst, lhs, shift_value);
2332 } else if (instr->IsShr()) {
2333 __ Asr(dst, lhs, shift_value);
2334 } else {
2335 __ Lsr(dst, lhs, shift_value);
2336 }
2337 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002338 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002339
2340 if (instr->IsShl()) {
2341 __ Lsl(dst, lhs, rhs_reg);
2342 } else if (instr->IsShr()) {
2343 __ Asr(dst, lhs, rhs_reg);
2344 } else {
2345 __ Lsr(dst, lhs, rhs_reg);
2346 }
2347 }
2348 break;
2349 }
2350 default:
2351 LOG(FATAL) << "Unexpected shift operation type " << type;
2352 }
2353}
2354
Alexandre Rames5319def2014-10-23 10:03:10 +01002355void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002356 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002357}
2358
2359void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002360 HandleBinaryOp(instruction);
2361}
2362
2363void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2364 HandleBinaryOp(instruction);
2365}
2366
2367void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2368 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002369}
2370
Artem Serov7fc63502016-02-09 17:15:29 +00002371void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002372 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002373 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002374 locations->SetInAt(0, Location::RequiresRegister());
2375 // There is no immediate variant of negated bitwise instructions in AArch64.
2376 locations->SetInAt(1, Location::RequiresRegister());
2377 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2378}
2379
Artem Serov7fc63502016-02-09 17:15:29 +00002380void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002381 Register dst = OutputRegister(instr);
2382 Register lhs = InputRegisterAt(instr, 0);
2383 Register rhs = InputRegisterAt(instr, 1);
2384
2385 switch (instr->GetOpKind()) {
2386 case HInstruction::kAnd:
2387 __ Bic(dst, lhs, rhs);
2388 break;
2389 case HInstruction::kOr:
2390 __ Orn(dst, lhs, rhs);
2391 break;
2392 case HInstruction::kXor:
2393 __ Eon(dst, lhs, rhs);
2394 break;
2395 default:
2396 LOG(FATAL) << "Unreachable";
2397 }
2398}
2399
Anton Kirilov74234da2017-01-13 14:42:47 +00002400void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2401 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002402 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2403 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002404 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002405 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002406 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2407 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2408 } else {
2409 locations->SetInAt(0, Location::RequiresRegister());
2410 }
2411 locations->SetInAt(1, Location::RequiresRegister());
2412 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2413}
2414
Anton Kirilov74234da2017-01-13 14:42:47 +00002415void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2416 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002417 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002418 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002419 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002420 Register out = OutputRegister(instruction);
2421 Register left;
2422 if (kind != HInstruction::kNeg) {
2423 left = InputRegisterAt(instruction, 0);
2424 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002425 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002426 // shifter operand operation, the IR generating `right_reg` (input to the type
2427 // conversion) can have a different type from the current instruction's type,
2428 // so we manually indicate the type.
2429 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002430 Operand right_operand(0);
2431
Anton Kirilov74234da2017-01-13 14:42:47 +00002432 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2433 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002434 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2435 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002436 right_operand = Operand(right_reg,
2437 helpers::ShiftFromOpKind(op_kind),
2438 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002439 }
2440
2441 // Logical binary operations do not support extension operations in the
2442 // operand. Note that VIXL would still manage if it was passed by generating
2443 // the extension as a separate instruction.
2444 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2445 DCHECK(!right_operand.IsExtendedRegister() ||
2446 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2447 kind != HInstruction::kNeg));
2448 switch (kind) {
2449 case HInstruction::kAdd:
2450 __ Add(out, left, right_operand);
2451 break;
2452 case HInstruction::kAnd:
2453 __ And(out, left, right_operand);
2454 break;
2455 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002456 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002457 __ Neg(out, right_operand);
2458 break;
2459 case HInstruction::kOr:
2460 __ Orr(out, left, right_operand);
2461 break;
2462 case HInstruction::kSub:
2463 __ Sub(out, left, right_operand);
2464 break;
2465 case HInstruction::kXor:
2466 __ Eor(out, left, right_operand);
2467 break;
2468 default:
2469 LOG(FATAL) << "Unexpected operation kind: " << kind;
2470 UNREACHABLE();
2471 }
2472}
2473
Artem Serov328429f2016-07-06 16:23:04 +01002474void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002475 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002476 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002477 locations->SetInAt(0, Location::RequiresRegister());
2478 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002479 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002480}
2481
Roland Levillain19c54192016-11-04 13:44:09 +00002482void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002483 __ Add(OutputRegister(instruction),
2484 InputRegisterAt(instruction, 0),
2485 Operand(InputOperandAt(instruction, 1)));
2486}
2487
Artem Serove1811ed2017-04-27 16:50:47 +01002488void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2489 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002490 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002491
2492 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2493
2494 locations->SetInAt(0, Location::RequiresRegister());
2495 // For byte case we don't need to shift the index variable so we can encode the data offset into
2496 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2497 // data offset constant generation out of the loop and reduce the critical path length in the
2498 // loop.
2499 locations->SetInAt(1, shift->GetValue() == 0
2500 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2501 : Location::RequiresRegister());
2502 locations->SetInAt(2, Location::ConstantLocation(shift));
2503 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2504}
2505
2506void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2507 HIntermediateAddressIndex* instruction) {
2508 Register index_reg = InputRegisterAt(instruction, 0);
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002509 uint32_t shift = Int64FromLocation(instruction->GetLocations()->InAt(2));
Artem Serove1811ed2017-04-27 16:50:47 +01002510 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2511
2512 if (shift == 0) {
2513 __ Add(OutputRegister(instruction), index_reg, offset);
2514 } else {
2515 Register offset_reg = InputRegisterAt(instruction, 1);
2516 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2517 }
2518}
2519
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002520void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002521 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002522 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002523 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2524 if (instr->GetOpKind() == HInstruction::kSub &&
2525 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002526 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002527 // Don't allocate register for Mneg instruction.
2528 } else {
2529 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2530 Location::RequiresRegister());
2531 }
2532 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2533 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002534 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2535}
2536
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002537void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002538 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002539 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2540 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002541
2542 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2543 // This fixup should be carried out for all multiply-accumulate instructions:
2544 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002545 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002546 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2547 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002548 vixl::aarch64::Instruction* prev =
2549 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002550 if (prev->IsLoadOrStore()) {
2551 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002552 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002553 __ nop();
2554 }
2555 }
2556
2557 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002558 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002559 __ Madd(res, mul_left, mul_right, accumulator);
2560 } else {
2561 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002562 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002563 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002564 __ Mneg(res, mul_left, mul_right);
2565 } else {
2566 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2567 __ Msub(res, mul_left, mul_right, accumulator);
2568 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002569 }
2570}
2571
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002572void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002573 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002574 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002575 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002576 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2577 object_array_get_with_read_barrier
2578 ? LocationSummary::kCallOnSlowPath
2579 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002580 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002581 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002582 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002583 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002584 // We need a temporary register for the read barrier load in
2585 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2586 // only if the offset is too big.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002587 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2588 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002589 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002590 if (offset >= kReferenceLoadMinFarOffset) {
2591 locations->AddTemp(FixedTempLocation());
2592 }
2593 } else {
Vladimir Marko008e09f32018-08-06 15:42:43 +01002594 // We need a non-scratch temporary for the array data pointer in
2595 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier().
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002596 locations->AddTemp(Location::RequiresRegister());
2597 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002598 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002599 locations->SetInAt(0, Location::RequiresRegister());
2600 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002601 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002602 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2603 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002604 // The output overlaps in the case of an object array get with
2605 // read barriers enabled: we do not want the move to overwrite the
2606 // array's location, as we need it to emit the read barrier.
2607 locations->SetOut(
2608 Location::RequiresRegister(),
2609 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002610 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002611}
2612
2613void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002614 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002615 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002616 LocationSummary* locations = instruction->GetLocations();
2617 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002618 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002619 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002620 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2621 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002622 MacroAssembler* masm = GetVIXLAssembler();
2623 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002624
Roland Levillain19c54192016-11-04 13:44:09 +00002625 // The read barrier instrumentation of object ArrayGet instructions
2626 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002627 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002628 instruction->GetArray()->IsIntermediateAddress() &&
2629 kEmitCompilerReadBarrier));
2630
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002631 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002632 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002633 // Note that a potential implicit null check is handled in the
2634 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002635 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002636 if (index.IsConstant()) {
2637 // Array load with a constant index can be treated as a field load.
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002638 offset += Int64FromLocation(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002639 Location maybe_temp =
2640 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2641 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2642 out,
2643 obj.W(),
2644 offset,
2645 maybe_temp,
Vladimir Marko66d691d2017-04-07 17:53:39 +01002646 /* needs_null_check */ false,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002647 /* use_load_acquire */ false);
2648 } else {
2649 Register temp = WRegisterFrom(locations->GetTemp(0));
2650 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko008e09f32018-08-06 15:42:43 +01002651 out, obj.W(), offset, index, temp, /* needs_null_check */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002652 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002653 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002654 // General case.
2655 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002656 Register length;
2657 if (maybe_compressed_char_at) {
2658 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2659 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002660 {
2661 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2662 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2663
2664 if (instruction->GetArray()->IsIntermediateAddress()) {
2665 DCHECK_LT(count_offset, offset);
2666 int64_t adjusted_offset =
2667 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2668 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2669 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2670 } else {
2671 __ Ldr(length, HeapOperand(obj, count_offset));
2672 }
2673 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002674 }
jessicahandojo05765752016-09-09 19:01:32 -07002675 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002676 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002677 if (maybe_compressed_char_at) {
2678 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002679 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2680 "Expecting 0=compressed, 1=uncompressed");
2681 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002682 __ Ldrb(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002683 HeapOperand(obj, offset + Int64FromLocation(index)));
jessicahandojo05765752016-09-09 19:01:32 -07002684 __ B(&done);
2685 __ Bind(&uncompressed_load);
2686 __ Ldrh(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002687 HeapOperand(obj, offset + (Int64FromLocation(index) << 1)));
jessicahandojo05765752016-09-09 19:01:32 -07002688 __ Bind(&done);
2689 } else {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002690 offset += Int64FromLocation(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002691 source = HeapOperand(obj, offset);
2692 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002693 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002694 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002695 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002696 // We do not need to compute the intermediate address from the array: the
2697 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002698 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002699 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002700 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002701 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2702 }
2703 temp = obj;
2704 } else {
2705 __ Add(temp, obj, offset);
2706 }
jessicahandojo05765752016-09-09 19:01:32 -07002707 if (maybe_compressed_char_at) {
2708 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002709 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2710 "Expecting 0=compressed, 1=uncompressed");
2711 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002712 __ Ldrb(Register(OutputCPURegister(instruction)),
2713 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2714 __ B(&done);
2715 __ Bind(&uncompressed_load);
2716 __ Ldrh(Register(OutputCPURegister(instruction)),
2717 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2718 __ Bind(&done);
2719 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002720 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002721 }
Roland Levillain44015862016-01-22 11:47:17 +00002722 }
jessicahandojo05765752016-09-09 19:01:32 -07002723 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002724 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2725 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002726 codegen_->Load(type, OutputCPURegister(instruction), source);
2727 codegen_->MaybeRecordImplicitNullCheck(instruction);
2728 }
Roland Levillain44015862016-01-22 11:47:17 +00002729
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002730 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002731 static_assert(
2732 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2733 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2734 Location obj_loc = locations->InAt(0);
2735 if (index.IsConstant()) {
2736 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2737 } else {
2738 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2739 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002740 }
Roland Levillain4d027112015-07-01 15:41:14 +01002741 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002742}
2743
Alexandre Rames5319def2014-10-23 10:03:10 +01002744void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002745 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002746 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002747 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002748}
2749
2750void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002751 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002752 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002753 {
2754 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2755 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2756 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2757 codegen_->MaybeRecordImplicitNullCheck(instruction);
2758 }
jessicahandojo05765752016-09-09 19:01:32 -07002759 // Mask out compression flag from String's array length.
2760 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002761 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002762 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002763}
2764
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002765void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002766 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002767
2768 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002769 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002770 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002771 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002772 LocationSummary::kCallOnSlowPath :
2773 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002774 locations->SetInAt(0, Location::RequiresRegister());
2775 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002776 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2777 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002778 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002779 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002780 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002781 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002782 }
2783}
2784
2785void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002786 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002787 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002788 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002789 bool needs_write_barrier =
2790 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002791
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002792 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002793 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002794 CPURegister source = value;
2795 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002796 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002797 MemOperand destination = HeapOperand(array);
2798 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002799
2800 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002801 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002802 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002803 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002804 destination = HeapOperand(array, offset);
2805 } else {
2806 UseScratchRegisterScope temps(masm);
2807 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002808 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002809 // We do not need to compute the intermediate address from the array: the
2810 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002811 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002812 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002813 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002814 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2815 }
2816 temp = array;
2817 } else {
2818 __ Add(temp, array, offset);
2819 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002820 destination = HeapOperand(temp,
2821 XRegisterFrom(index),
2822 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002823 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002824 }
Artem Serov914d7a82017-02-07 14:33:49 +00002825 {
2826 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2827 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2828 codegen_->Store(value_type, value, destination);
2829 codegen_->MaybeRecordImplicitNullCheck(instruction);
2830 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002831 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002832 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002833 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002834 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002835 {
2836 // We use a block to end the scratch scope before the write barrier, thus
2837 // freeing the temporary registers so they can be used in `MarkGCCard`.
2838 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002839 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002840 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002841 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002842 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002843 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002844 destination = HeapOperand(temp,
2845 XRegisterFrom(index),
2846 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002847 DataType::SizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002848 }
2849
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002850 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2851 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2852 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2853
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002854 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002855 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002856 codegen_->AddSlowPath(slow_path);
2857 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002858 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002859 __ Cbnz(Register(value), &non_zero);
2860 if (!index.IsConstant()) {
2861 __ Add(temp, array, offset);
2862 }
Artem Serov914d7a82017-02-07 14:33:49 +00002863 {
2864 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
2865 // emitted.
2866 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2867 __ Str(wzr, destination);
2868 codegen_->MaybeRecordImplicitNullCheck(instruction);
2869 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002870 __ B(&done);
2871 __ Bind(&non_zero);
2872 }
2873
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002874 // Note that when Baker read barriers are enabled, the type
2875 // checks are performed without read barriers. This is fine,
2876 // even in the case where a class object is in the from-space
2877 // after the flip, as a comparison involving such a type would
2878 // not produce a false positive; it may of course produce a
2879 // false negative, in which case we would take the ArraySet
2880 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002881
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002882 Register temp2 = temps.AcquireSameSizeAs(array);
2883 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00002884 {
2885 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2886 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2887 __ Ldr(temp, HeapOperand(array, class_offset));
2888 codegen_->MaybeRecordImplicitNullCheck(instruction);
2889 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002890 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002891
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002892 // /* HeapReference<Class> */ temp = temp->component_type_
2893 __ Ldr(temp, HeapOperand(temp, component_offset));
2894 // /* HeapReference<Class> */ temp2 = value->klass_
2895 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2896 // If heap poisoning is enabled, no need to unpoison `temp`
2897 // nor `temp2`, as we are comparing two poisoned references.
2898 __ Cmp(temp, temp2);
2899 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002900
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002901 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2902 vixl::aarch64::Label do_put;
2903 __ B(eq, &do_put);
2904 // If heap poisoning is enabled, the `temp` reference has
2905 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002906 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2907
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002908 // /* HeapReference<Class> */ temp = temp->super_class_
2909 __ Ldr(temp, HeapOperand(temp, super_offset));
2910 // If heap poisoning is enabled, no need to unpoison
2911 // `temp`, as we are comparing against null below.
2912 __ Cbnz(temp, slow_path->GetEntryLabel());
2913 __ Bind(&do_put);
2914 } else {
2915 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002916 }
2917 }
2918
2919 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002920 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002921 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002922 __ Mov(temp2, value.W());
2923 GetAssembler()->PoisonHeapReference(temp2);
2924 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002925 }
2926
2927 if (!index.IsConstant()) {
2928 __ Add(temp, array, offset);
Vladimir Markod1ef8732017-04-18 13:55:13 +01002929 } else {
2930 // We no longer need the `temp` here so release it as the store below may
2931 // need a scratch register (if the constant index makes the offset too large)
2932 // and the poisoned `source` could be using the other scratch register.
2933 temps.Release(temp);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002934 }
Artem Serov914d7a82017-02-07 14:33:49 +00002935 {
2936 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2937 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2938 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002939
Artem Serov914d7a82017-02-07 14:33:49 +00002940 if (!may_need_runtime_call_for_type_check) {
2941 codegen_->MaybeRecordImplicitNullCheck(instruction);
2942 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002943 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002944 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002945
2946 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2947
2948 if (done.IsLinked()) {
2949 __ Bind(&done);
2950 }
2951
2952 if (slow_path != nullptr) {
2953 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002954 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002955 }
2956}
2957
Alexandre Rames67555f72014-11-18 10:55:16 +00002958void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002959 RegisterSet caller_saves = RegisterSet::Empty();
2960 InvokeRuntimeCallingConvention calling_convention;
2961 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2962 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2963 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002964 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002965 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002966}
2967
2968void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002969 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002970 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002971 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002972 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2973 __ B(slow_path->GetEntryLabel(), hs);
2974}
2975
Alexandre Rames67555f72014-11-18 10:55:16 +00002976void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2977 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002978 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00002979 locations->SetInAt(0, Location::RequiresRegister());
2980 if (check->HasUses()) {
2981 locations->SetOut(Location::SameAsFirstInput());
2982 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01002983 // Rely on the type initialization to save everything we need.
2984 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Alexandre Rames67555f72014-11-18 10:55:16 +00002985}
2986
2987void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2988 // We assume the class is not null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01002989 SlowPathCodeARM64* slow_path =
2990 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(check->GetLoadClass(), check);
Alexandre Rames67555f72014-11-18 10:55:16 +00002991 codegen_->AddSlowPath(slow_path);
2992 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2993}
2994
Roland Levillain1a653882016-03-18 18:05:57 +00002995static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2996 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2997 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2998}
2999
3000void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3001 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3002 Location rhs_loc = instruction->GetLocations()->InAt(1);
3003 if (rhs_loc.IsConstant()) {
3004 // 0.0 is the only immediate that can be encoded directly in
3005 // an FCMP instruction.
3006 //
3007 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3008 // specify that in a floating-point comparison, positive zero
3009 // and negative zero are considered equal, so we can use the
3010 // literal 0.0 for both cases here.
3011 //
3012 // Note however that some methods (Float.equal, Float.compare,
3013 // Float.compareTo, Double.equal, Double.compare,
3014 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3015 // StrictMath.min) consider 0.0 to be (strictly) greater than
3016 // -0.0. So if we ever translate calls to these methods into a
3017 // HCompare instruction, we must handle the -0.0 case with
3018 // care here.
3019 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3020 __ Fcmp(lhs_reg, 0.0);
3021 } else {
3022 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3023 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003024}
3025
Serban Constantinescu02164b32014-11-13 14:05:07 +00003026void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003027 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003028 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003029 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003030 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003031 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003032 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003033 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003034 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003035 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003036 case DataType::Type::kInt32:
3037 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003038 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003039 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003040 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3041 break;
3042 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003043 case DataType::Type::kFloat32:
3044 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003045 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003046 locations->SetInAt(1,
3047 IsFloatingPointZeroConstant(compare->InputAt(1))
3048 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3049 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003050 locations->SetOut(Location::RequiresRegister());
3051 break;
3052 }
3053 default:
3054 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3055 }
3056}
3057
3058void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003059 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00003060
3061 // 0 if: left == right
3062 // 1 if: left > right
3063 // -1 if: left < right
3064 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003065 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003066 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003067 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003068 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003069 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003070 case DataType::Type::kInt32:
3071 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003072 Register result = OutputRegister(compare);
3073 Register left = InputRegisterAt(compare, 0);
3074 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003075 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003076 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3077 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003078 break;
3079 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003080 case DataType::Type::kFloat32:
3081 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003082 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003083 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003084 __ Cset(result, ne);
3085 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003086 break;
3087 }
3088 default:
3089 LOG(FATAL) << "Unimplemented compare type " << in_type;
3090 }
3091}
3092
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003093void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003094 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003095
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003096 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003097 locations->SetInAt(0, Location::RequiresFpuRegister());
3098 locations->SetInAt(1,
3099 IsFloatingPointZeroConstant(instruction->InputAt(1))
3100 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3101 : Location::RequiresFpuRegister());
3102 } else {
3103 // Integer cases.
3104 locations->SetInAt(0, Location::RequiresRegister());
3105 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3106 }
3107
David Brazdilb3e773e2016-01-26 11:28:37 +00003108 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003109 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003110 }
3111}
3112
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003113void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003114 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003115 return;
3116 }
3117
3118 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003119 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003120 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003121
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003122 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003123 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003124 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003125 } else {
3126 // Integer cases.
3127 Register lhs = InputRegisterAt(instruction, 0);
3128 Operand rhs = InputOperandAt(instruction, 1);
3129 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003130 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003131 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003132}
3133
3134#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3135 M(Equal) \
3136 M(NotEqual) \
3137 M(LessThan) \
3138 M(LessThanOrEqual) \
3139 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003140 M(GreaterThanOrEqual) \
3141 M(Below) \
3142 M(BelowOrEqual) \
3143 M(Above) \
3144 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003145#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003146void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3147void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003148FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003149#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003150#undef FOR_EACH_CONDITION_INSTRUCTION
3151
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003152void InstructionCodeGeneratorARM64::GenerateIntDivForPower2Denom(HDiv* instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003153 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003154 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003155 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
3156
3157 Register out = OutputRegister(instruction);
3158 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003159
3160 if (abs_imm == 2) {
3161 int bits = DataType::Size(instruction->GetResultType()) * kBitsPerByte;
3162 __ Add(out, dividend, Operand(dividend, LSR, bits - 1));
3163 } else {
3164 UseScratchRegisterScope temps(GetVIXLAssembler());
3165 Register temp = temps.AcquireSameSizeAs(out);
3166 __ Add(temp, dividend, abs_imm - 1);
3167 __ Cmp(dividend, 0);
3168 __ Csel(out, temp, dividend, lt);
3169 }
3170
Zheng Xuc6667102015-05-15 16:08:45 +08003171 int ctz_imm = CTZ(abs_imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003172 if (imm > 0) {
3173 __ Asr(out, out, ctz_imm);
Zheng Xuc6667102015-05-15 16:08:45 +08003174 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003175 __ Neg(out, Operand(out, ASR, ctz_imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003176 }
3177}
3178
3179void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3180 DCHECK(instruction->IsDiv() || instruction->IsRem());
3181
3182 LocationSummary* locations = instruction->GetLocations();
3183 Location second = locations->InAt(1);
3184 DCHECK(second.IsConstant());
3185
3186 Register out = OutputRegister(instruction);
3187 Register dividend = InputRegisterAt(instruction, 0);
3188 int64_t imm = Int64FromConstant(second.GetConstant());
3189
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003190 DataType::Type type = instruction->GetResultType();
3191 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003192
3193 int64_t magic;
3194 int shift;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003195 CalculateMagicAndShiftForDivRem(
3196 imm, type == DataType::Type::kInt64 /* is_long */, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08003197
3198 UseScratchRegisterScope temps(GetVIXLAssembler());
3199 Register temp = temps.AcquireSameSizeAs(out);
3200
3201 // temp = get_high(dividend * magic)
3202 __ Mov(temp, magic);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003203 if (type == DataType::Type::kInt64) {
Zheng Xuc6667102015-05-15 16:08:45 +08003204 __ Smulh(temp, dividend, temp);
3205 } else {
3206 __ Smull(temp.X(), dividend, temp);
3207 __ Lsr(temp.X(), temp.X(), 32);
3208 }
3209
3210 if (imm > 0 && magic < 0) {
3211 __ Add(temp, temp, dividend);
3212 } else if (imm < 0 && magic > 0) {
3213 __ Sub(temp, temp, dividend);
3214 }
3215
3216 if (shift != 0) {
3217 __ Asr(temp, temp, shift);
3218 }
3219
3220 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003221 __ Sub(out, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08003222 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003223 __ Sub(temp, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08003224 // TODO: Strength reduction for msub.
3225 Register temp_imm = temps.AcquireSameSizeAs(out);
3226 __ Mov(temp_imm, imm);
3227 __ Msub(out, temp, temp_imm, dividend);
3228 }
3229}
3230
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003231void InstructionCodeGeneratorARM64::GenerateIntDivForConstDenom(HDiv *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003232 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Zheng Xuc6667102015-05-15 16:08:45 +08003233
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003234 if (imm == 0) {
3235 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3236 return;
3237 }
Zheng Xuc6667102015-05-15 16:08:45 +08003238
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003239 if (IsPowerOfTwo(AbsOrMin(imm))) {
3240 GenerateIntDivForPower2Denom(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003241 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003242 // Cases imm == -1 or imm == 1 are handled by InstructionSimplifier.
3243 DCHECK(imm < -2 || imm > 2) << imm;
3244 GenerateDivRemWithAnyConstant(instruction);
3245 }
3246}
3247
3248void InstructionCodeGeneratorARM64::GenerateIntDiv(HDiv *instruction) {
3249 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
3250 << instruction->GetResultType();
3251
3252 if (instruction->GetLocations()->InAt(1).IsConstant()) {
3253 GenerateIntDivForConstDenom(instruction);
3254 } else {
3255 Register out = OutputRegister(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003256 Register dividend = InputRegisterAt(instruction, 0);
3257 Register divisor = InputRegisterAt(instruction, 1);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003258 __ Sdiv(out, dividend, divisor);
Zheng Xuc6667102015-05-15 16:08:45 +08003259 }
3260}
3261
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003262void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3263 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003264 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003265 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003266 case DataType::Type::kInt32:
3267 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003268 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003269 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003270 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3271 break;
3272
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003273 case DataType::Type::kFloat32:
3274 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003275 locations->SetInAt(0, Location::RequiresFpuRegister());
3276 locations->SetInAt(1, Location::RequiresFpuRegister());
3277 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3278 break;
3279
3280 default:
3281 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3282 }
3283}
3284
3285void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003286 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003287 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003288 case DataType::Type::kInt32:
3289 case DataType::Type::kInt64:
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003290 GenerateIntDiv(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003291 break;
3292
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003293 case DataType::Type::kFloat32:
3294 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003295 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3296 break;
3297
3298 default:
3299 LOG(FATAL) << "Unexpected div type " << type;
3300 }
3301}
3302
Alexandre Rames67555f72014-11-18 10:55:16 +00003303void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003304 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003305 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003306}
3307
3308void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3309 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003310 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003311 codegen_->AddSlowPath(slow_path);
3312 Location value = instruction->GetLocations()->InAt(0);
3313
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003314 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003315
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003316 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003317 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003318 return;
3319 }
3320
Alexandre Rames67555f72014-11-18 10:55:16 +00003321 if (value.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003322 int64_t divisor = Int64FromLocation(value);
Alexandre Rames67555f72014-11-18 10:55:16 +00003323 if (divisor == 0) {
3324 __ B(slow_path->GetEntryLabel());
3325 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003326 // A division by a non-null constant is valid. We don't need to perform
3327 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003328 }
3329 } else {
3330 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3331 }
3332}
3333
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003334void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3335 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003336 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003337 locations->SetOut(Location::ConstantLocation(constant));
3338}
3339
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003340void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3341 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003342 // Will be generated at use site.
3343}
3344
Alexandre Rames5319def2014-10-23 10:03:10 +01003345void LocationsBuilderARM64::VisitExit(HExit* exit) {
3346 exit->SetLocations(nullptr);
3347}
3348
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003349void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003350}
3351
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003352void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3353 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003354 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003355 locations->SetOut(Location::ConstantLocation(constant));
3356}
3357
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003358void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003359 // Will be generated at use site.
3360}
3361
David Brazdilfc6a86a2015-06-26 10:33:45 +00003362void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003363 if (successor->IsExitBlock()) {
3364 DCHECK(got->GetPrevious()->AlwaysThrows());
3365 return; // no code needed
3366 }
3367
Serban Constantinescu02164b32014-11-13 14:05:07 +00003368 HBasicBlock* block = got->GetBlock();
3369 HInstruction* previous = got->GetPrevious();
3370 HLoopInformation* info = block->GetLoopInformation();
3371
David Brazdil46e2a392015-03-16 17:31:52 +00003372 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00003373 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
3374 UseScratchRegisterScope temps(GetVIXLAssembler());
3375 Register temp1 = temps.AcquireX();
3376 Register temp2 = temps.AcquireX();
3377 __ Ldr(temp1, MemOperand(sp, 0));
3378 __ Ldrh(temp2, MemOperand(temp1, ArtMethod::HotnessCountOffset().Int32Value()));
3379 __ Add(temp2, temp2, 1);
3380 __ Strh(temp2, MemOperand(temp1, ArtMethod::HotnessCountOffset().Int32Value()));
3381 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003382 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3383 return;
3384 }
3385 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3386 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01003387 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003388 }
3389 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003390 __ B(codegen_->GetLabelOf(successor));
3391 }
3392}
3393
David Brazdilfc6a86a2015-06-26 10:33:45 +00003394void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3395 got->SetLocations(nullptr);
3396}
3397
3398void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3399 HandleGoto(got, got->GetSuccessor());
3400}
3401
3402void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3403 try_boundary->SetLocations(nullptr);
3404}
3405
3406void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3407 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3408 if (!successor->IsExitBlock()) {
3409 HandleGoto(try_boundary, successor);
3410 }
3411}
3412
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003413void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003414 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003415 vixl::aarch64::Label* true_target,
3416 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003417 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003418
David Brazdil0debae72015-11-12 18:37:00 +00003419 if (true_target == nullptr && false_target == nullptr) {
3420 // Nothing to do. The code always falls through.
3421 return;
3422 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003423 // Constant condition, statically compared against "true" (integer value 1).
3424 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003425 if (true_target != nullptr) {
3426 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003427 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003428 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003429 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003430 if (false_target != nullptr) {
3431 __ B(false_target);
3432 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003433 }
David Brazdil0debae72015-11-12 18:37:00 +00003434 return;
3435 }
3436
3437 // The following code generates these patterns:
3438 // (1) true_target == nullptr && false_target != nullptr
3439 // - opposite condition true => branch to false_target
3440 // (2) true_target != nullptr && false_target == nullptr
3441 // - condition true => branch to true_target
3442 // (3) true_target != nullptr && false_target != nullptr
3443 // - condition true => branch to true_target
3444 // - branch to false_target
3445 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003446 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003447 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003448 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003449 if (true_target == nullptr) {
3450 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3451 } else {
3452 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3453 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003454 } else {
3455 // The condition instruction has not been materialized, use its inputs as
3456 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003457 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003458
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003459 DataType::Type type = condition->InputAt(0)->GetType();
3460 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003461 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003462 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003463 IfCondition opposite_condition = condition->GetOppositeCondition();
3464 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003465 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003466 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003467 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003468 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003469 // Integer cases.
3470 Register lhs = InputRegisterAt(condition, 0);
3471 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003472
3473 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003474 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003475 if (true_target == nullptr) {
3476 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3477 non_fallthrough_target = false_target;
3478 } else {
3479 arm64_cond = ARM64Condition(condition->GetCondition());
3480 non_fallthrough_target = true_target;
3481 }
3482
Aart Bik086d27e2016-01-20 17:02:00 -08003483 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003484 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003485 switch (arm64_cond) {
3486 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003487 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003488 break;
3489 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003490 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003491 break;
3492 case lt:
3493 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003494 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003495 break;
3496 case ge:
3497 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003498 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003499 break;
3500 default:
3501 // Without the `static_cast` the compiler throws an error for
3502 // `-Werror=sign-promo`.
3503 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3504 }
3505 } else {
3506 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003507 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003508 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003509 }
3510 }
David Brazdil0debae72015-11-12 18:37:00 +00003511
3512 // If neither branch falls through (case 3), the conditional branch to `true_target`
3513 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3514 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003515 __ B(false_target);
3516 }
3517}
3518
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003519void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003520 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003521 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003522 locations->SetInAt(0, Location::RequiresRegister());
3523 }
3524}
3525
3526void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003527 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3528 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003529 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3530 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3531 true_target = nullptr;
3532 }
3533 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3534 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3535 false_target = nullptr;
3536 }
David Brazdil0debae72015-11-12 18:37:00 +00003537 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003538}
3539
3540void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003541 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003542 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003543 InvokeRuntimeCallingConvention calling_convention;
3544 RegisterSet caller_saves = RegisterSet::Empty();
3545 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3546 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003547 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003548 locations->SetInAt(0, Location::RequiresRegister());
3549 }
3550}
3551
3552void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003553 SlowPathCodeARM64* slow_path =
3554 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003555 GenerateTestAndBranch(deoptimize,
3556 /* condition_input_index */ 0,
3557 slow_path->GetEntryLabel(),
3558 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003559}
3560
Mingyao Yang063fc772016-08-02 11:02:54 -07003561void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003562 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003563 LocationSummary(flag, LocationSummary::kNoCall);
3564 locations->SetOut(Location::RequiresRegister());
3565}
3566
3567void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3568 __ Ldr(OutputRegister(flag),
3569 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3570}
3571
David Brazdilc0b601b2016-02-08 14:20:45 +00003572static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3573 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003574 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003575}
3576
Alexandre Rames880f1192016-06-13 16:04:50 +01003577static inline Condition GetConditionForSelect(HCondition* condition) {
3578 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003579 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3580 : ARM64Condition(cond);
3581}
3582
David Brazdil74eb1b22015-12-14 11:44:01 +00003583void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003584 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003585 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003586 locations->SetInAt(0, Location::RequiresFpuRegister());
3587 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003588 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003589 } else {
3590 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3591 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3592 bool is_true_value_constant = cst_true_value != nullptr;
3593 bool is_false_value_constant = cst_false_value != nullptr;
3594 // Ask VIXL whether we should synthesize constants in registers.
3595 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3596 Operand true_op = is_true_value_constant ?
3597 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3598 Operand false_op = is_false_value_constant ?
3599 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3600 bool true_value_in_register = false;
3601 bool false_value_in_register = false;
3602 MacroAssembler::GetCselSynthesisInformation(
3603 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3604 true_value_in_register |= !is_true_value_constant;
3605 false_value_in_register |= !is_false_value_constant;
3606
3607 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3608 : Location::ConstantLocation(cst_true_value));
3609 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3610 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003611 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003612 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003613
David Brazdil74eb1b22015-12-14 11:44:01 +00003614 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3615 locations->SetInAt(2, Location::RequiresRegister());
3616 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003617}
3618
3619void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003620 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003621 Condition csel_cond;
3622
3623 if (IsBooleanValueOrMaterializedCondition(cond)) {
3624 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003625 // Use the condition flags set by the previous instruction.
3626 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003627 } else {
3628 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003629 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003630 }
3631 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003632 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003633 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003634 } else {
3635 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003636 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003637 }
3638
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003639 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003640 __ Fcsel(OutputFPRegister(select),
3641 InputFPRegisterAt(select, 1),
3642 InputFPRegisterAt(select, 0),
3643 csel_cond);
3644 } else {
3645 __ Csel(OutputRegister(select),
3646 InputOperandAt(select, 1),
3647 InputOperandAt(select, 0),
3648 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003649 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003650}
3651
David Srbecky0cf44932015-12-09 14:09:59 +00003652void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003653 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00003654}
3655
David Srbeckyd28f4a02016-03-14 17:14:24 +00003656void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3657 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003658}
3659
3660void CodeGeneratorARM64::GenerateNop() {
3661 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003662}
3663
Alexandre Rames5319def2014-10-23 10:03:10 +01003664void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003665 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003666}
3667
3668void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003669 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003670}
3671
3672void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003673 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003674}
3675
3676void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003677 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003678}
3679
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003680// Temp is used for read barrier.
3681static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3682 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003683 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003684 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3685 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3686 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3687 return 1;
3688 }
3689 return 0;
3690}
3691
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003692// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003693// interface pointer, one for loading the current interface.
3694// The other checks have one temp for loading the object's class.
3695static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3696 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3697 return 3;
3698 }
3699 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003700}
3701
Alexandre Rames67555f72014-11-18 10:55:16 +00003702void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003703 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003704 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003705 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003706 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003707 case TypeCheckKind::kExactCheck:
3708 case TypeCheckKind::kAbstractClassCheck:
3709 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00003710 case TypeCheckKind::kArrayObjectCheck: {
3711 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
3712 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
3713 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003714 break;
Vladimir Marko87584542017-12-12 17:47:52 +00003715 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003716 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003717 case TypeCheckKind::kUnresolvedCheck:
3718 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003719 call_kind = LocationSummary::kCallOnSlowPath;
3720 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00003721 case TypeCheckKind::kBitstringCheck:
3722 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003723 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003724
Vladimir Markoca6fff82017-10-03 14:49:14 +01003725 LocationSummary* locations =
3726 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003727 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003728 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003729 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003730 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003731 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3732 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3733 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3734 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3735 } else {
3736 locations->SetInAt(1, Location::RequiresRegister());
3737 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003738 // The "out" register is used as a temporary, so it overlaps with the inputs.
3739 // Note that TypeCheckSlowPathARM64 uses this register too.
3740 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003741 // Add temps if necessary for read barriers.
3742 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003743}
3744
3745void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003746 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003747 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003748 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003749 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003750 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3751 ? Register()
3752 : InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003753 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003754 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003755 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3756 DCHECK_LE(num_temps, 1u);
3757 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003758 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3759 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3760 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3761 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003762
Scott Wakeling97c72b72016-06-24 16:19:36 +01003763 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003764 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003765
3766 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003767 // Avoid null check if we know `obj` is not null.
3768 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003769 __ Cbz(obj, &zero);
3770 }
3771
Roland Levillain44015862016-01-22 11:47:17 +00003772 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003773 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003774 ReadBarrierOption read_barrier_option =
3775 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003776 // /* HeapReference<Class> */ out = obj->klass_
3777 GenerateReferenceLoadTwoRegisters(instruction,
3778 out_loc,
3779 obj_loc,
3780 class_offset,
3781 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003782 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003783 __ Cmp(out, cls);
3784 __ Cset(out, eq);
3785 if (zero.IsLinked()) {
3786 __ B(&done);
3787 }
3788 break;
3789 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003790
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003791 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003792 ReadBarrierOption read_barrier_option =
3793 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003794 // /* HeapReference<Class> */ out = obj->klass_
3795 GenerateReferenceLoadTwoRegisters(instruction,
3796 out_loc,
3797 obj_loc,
3798 class_offset,
3799 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003800 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003801 // If the class is abstract, we eagerly fetch the super class of the
3802 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003803 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003804 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003805 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003806 GenerateReferenceLoadOneRegister(instruction,
3807 out_loc,
3808 super_offset,
3809 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003810 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003811 // If `out` is null, we use it for the result, and jump to `done`.
3812 __ Cbz(out, &done);
3813 __ Cmp(out, cls);
3814 __ B(ne, &loop);
3815 __ Mov(out, 1);
3816 if (zero.IsLinked()) {
3817 __ B(&done);
3818 }
3819 break;
3820 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003821
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003822 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003823 ReadBarrierOption read_barrier_option =
3824 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003825 // /* HeapReference<Class> */ out = obj->klass_
3826 GenerateReferenceLoadTwoRegisters(instruction,
3827 out_loc,
3828 obj_loc,
3829 class_offset,
3830 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003831 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003832 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003833 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003834 __ Bind(&loop);
3835 __ Cmp(out, cls);
3836 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003837 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003838 GenerateReferenceLoadOneRegister(instruction,
3839 out_loc,
3840 super_offset,
3841 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003842 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003843 __ Cbnz(out, &loop);
3844 // If `out` is null, we use it for the result, and jump to `done`.
3845 __ B(&done);
3846 __ Bind(&success);
3847 __ Mov(out, 1);
3848 if (zero.IsLinked()) {
3849 __ B(&done);
3850 }
3851 break;
3852 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003853
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003854 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003855 ReadBarrierOption read_barrier_option =
3856 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003857 // /* HeapReference<Class> */ out = obj->klass_
3858 GenerateReferenceLoadTwoRegisters(instruction,
3859 out_loc,
3860 obj_loc,
3861 class_offset,
3862 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003863 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003864 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003865 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003866 __ Cmp(out, cls);
3867 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003868 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003869 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003870 GenerateReferenceLoadOneRegister(instruction,
3871 out_loc,
3872 component_offset,
3873 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003874 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003875 // If `out` is null, we use it for the result, and jump to `done`.
3876 __ Cbz(out, &done);
3877 __ Ldrh(out, HeapOperand(out, primitive_offset));
3878 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3879 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003880 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003881 __ Mov(out, 1);
3882 __ B(&done);
3883 break;
3884 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003885
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003886 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003887 // No read barrier since the slow path will retry upon failure.
3888 // /* HeapReference<Class> */ out = obj->klass_
3889 GenerateReferenceLoadTwoRegisters(instruction,
3890 out_loc,
3891 obj_loc,
3892 class_offset,
3893 maybe_temp_loc,
3894 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003895 __ Cmp(out, cls);
3896 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01003897 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
3898 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003899 codegen_->AddSlowPath(slow_path);
3900 __ B(ne, slow_path->GetEntryLabel());
3901 __ Mov(out, 1);
3902 if (zero.IsLinked()) {
3903 __ B(&done);
3904 }
3905 break;
3906 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003907
Calin Juravle98893e12015-10-02 21:05:03 +01003908 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003909 case TypeCheckKind::kInterfaceCheck: {
3910 // Note that we indeed only call on slow path, but we always go
3911 // into the slow path for the unresolved and interface check
3912 // cases.
3913 //
3914 // We cannot directly call the InstanceofNonTrivial runtime
3915 // entry point without resorting to a type checking slow path
3916 // here (i.e. by calling InvokeRuntime directly), as it would
3917 // require to assign fixed registers for the inputs of this
3918 // HInstanceOf instruction (following the runtime calling
3919 // convention), which might be cluttered by the potential first
3920 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003921 //
3922 // TODO: Introduce a new runtime entry point taking the object
3923 // to test (instead of its class) as argument, and let it deal
3924 // with the read barrier issues. This will let us refactor this
3925 // case of the `switch` code as it was previously (with a direct
3926 // call to the runtime not using a type checking slow path).
3927 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003928 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01003929 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
3930 instruction, /* is_fatal */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003931 codegen_->AddSlowPath(slow_path);
3932 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003933 if (zero.IsLinked()) {
3934 __ B(&done);
3935 }
3936 break;
3937 }
Vladimir Marko175e7862018-03-27 09:03:13 +00003938
3939 case TypeCheckKind::kBitstringCheck: {
3940 // /* HeapReference<Class> */ temp = obj->klass_
3941 GenerateReferenceLoadTwoRegisters(instruction,
3942 out_loc,
3943 obj_loc,
3944 class_offset,
3945 maybe_temp_loc,
3946 kWithoutReadBarrier);
3947
3948 GenerateBitstringTypeCheckCompare(instruction, out);
3949 __ Cset(out, eq);
3950 if (zero.IsLinked()) {
3951 __ B(&done);
3952 }
3953 break;
3954 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003955 }
3956
3957 if (zero.IsLinked()) {
3958 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003959 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003960 }
3961
3962 if (done.IsLinked()) {
3963 __ Bind(&done);
3964 }
3965
3966 if (slow_path != nullptr) {
3967 __ Bind(slow_path->GetExitLabel());
3968 }
3969}
3970
3971void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003972 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00003973 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003974 LocationSummary* locations =
3975 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003976 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003977 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3978 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3979 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3980 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3981 } else {
3982 locations->SetInAt(1, Location::RequiresRegister());
3983 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003984 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
3985 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003986}
3987
3988void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003989 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003990 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003991 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003992 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003993 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3994 ? Register()
3995 : InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003996 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3997 DCHECK_GE(num_temps, 1u);
3998 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003999 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004000 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4001 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004002 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004003 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4004 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4005 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4006 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4007 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4008 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4009 const uint32_t object_array_data_offset =
4010 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004011
Vladimir Marko87584542017-12-12 17:47:52 +00004012 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004013 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004014 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4015 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004016 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004017
Scott Wakeling97c72b72016-06-24 16:19:36 +01004018 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004019 // Avoid null check if we know obj is not null.
4020 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004021 __ Cbz(obj, &done);
4022 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004023
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004024 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004025 case TypeCheckKind::kExactCheck:
4026 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004027 // /* HeapReference<Class> */ temp = obj->klass_
4028 GenerateReferenceLoadTwoRegisters(instruction,
4029 temp_loc,
4030 obj_loc,
4031 class_offset,
4032 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004033 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004034
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004035 __ Cmp(temp, cls);
4036 // Jump to slow path for throwing the exception or doing a
4037 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004038 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004039 break;
4040 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004041
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004042 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004043 // /* HeapReference<Class> */ temp = obj->klass_
4044 GenerateReferenceLoadTwoRegisters(instruction,
4045 temp_loc,
4046 obj_loc,
4047 class_offset,
4048 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004049 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004050
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004051 // If the class is abstract, we eagerly fetch the super class of the
4052 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004053 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004054 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004055 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004056 GenerateReferenceLoadOneRegister(instruction,
4057 temp_loc,
4058 super_offset,
4059 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004060 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004061
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004062 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4063 // exception.
4064 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4065 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004066 __ Cmp(temp, cls);
4067 __ B(ne, &loop);
4068 break;
4069 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004070
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004071 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004072 // /* HeapReference<Class> */ temp = obj->klass_
4073 GenerateReferenceLoadTwoRegisters(instruction,
4074 temp_loc,
4075 obj_loc,
4076 class_offset,
4077 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004078 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004079
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004080 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004081 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004082 __ Bind(&loop);
4083 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004084 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004085
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004086 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004087 GenerateReferenceLoadOneRegister(instruction,
4088 temp_loc,
4089 super_offset,
4090 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004091 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004092
4093 // If the class reference currently in `temp` is not null, jump
4094 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004095 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004096 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004097 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004098 break;
4099 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004100
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004101 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004102 // /* HeapReference<Class> */ temp = obj->klass_
4103 GenerateReferenceLoadTwoRegisters(instruction,
4104 temp_loc,
4105 obj_loc,
4106 class_offset,
4107 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004108 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004109
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004110 // Do an exact check.
4111 __ Cmp(temp, cls);
4112 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004113
4114 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004115 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004116 GenerateReferenceLoadOneRegister(instruction,
4117 temp_loc,
4118 component_offset,
4119 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004120 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004121
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004122 // If the component type is null, jump to the slow path to throw the exception.
4123 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4124 // Otherwise, the object is indeed an array. Further check that this component type is not a
4125 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004126 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4127 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004128 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004129 break;
4130 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004131
Calin Juravle98893e12015-10-02 21:05:03 +01004132 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004133 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004134 //
4135 // We cannot directly call the CheckCast runtime entry point
4136 // without resorting to a type checking slow path here (i.e. by
4137 // calling InvokeRuntime directly), as it would require to
4138 // assign fixed registers for the inputs of this HInstanceOf
4139 // instruction (following the runtime calling convention), which
4140 // might be cluttered by the potential first read barrier
4141 // emission at the beginning of this method.
4142 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004143 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004144 case TypeCheckKind::kInterfaceCheck: {
4145 // /* HeapReference<Class> */ temp = obj->klass_
4146 GenerateReferenceLoadTwoRegisters(instruction,
4147 temp_loc,
4148 obj_loc,
4149 class_offset,
4150 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004151 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004152
4153 // /* HeapReference<Class> */ temp = temp->iftable_
4154 GenerateReferenceLoadTwoRegisters(instruction,
4155 temp_loc,
4156 temp_loc,
4157 iftable_offset,
4158 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004159 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004160 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004161 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004162 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004163 vixl::aarch64::Label start_loop;
4164 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004165 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004166 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4167 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004168 // Go to next interface.
4169 __ Add(temp, temp, 2 * kHeapReferenceSize);
4170 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004171 // Compare the classes and continue the loop if they do not match.
4172 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4173 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004174 break;
4175 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004176
4177 case TypeCheckKind::kBitstringCheck: {
4178 // /* HeapReference<Class> */ temp = obj->klass_
4179 GenerateReferenceLoadTwoRegisters(instruction,
4180 temp_loc,
4181 obj_loc,
4182 class_offset,
4183 maybe_temp2_loc,
4184 kWithoutReadBarrier);
4185
4186 GenerateBitstringTypeCheckCompare(instruction, temp);
4187 __ B(ne, type_check_slow_path->GetEntryLabel());
4188 break;
4189 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004190 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004191 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004192
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004193 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004194}
4195
Alexandre Rames5319def2014-10-23 10:03:10 +01004196void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004197 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004198 locations->SetOut(Location::ConstantLocation(constant));
4199}
4200
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004201void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004202 // Will be generated at use site.
4203}
4204
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004205void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004206 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004207 locations->SetOut(Location::ConstantLocation(constant));
4208}
4209
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004210void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004211 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004212}
4213
Calin Juravle175dc732015-08-25 15:42:32 +01004214void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4215 // The trampoline uses the same calling convention as dex calling conventions,
4216 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4217 // the method_idx.
4218 HandleInvoke(invoke);
4219}
4220
4221void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4222 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004223 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004224}
4225
Alexandre Rames5319def2014-10-23 10:03:10 +01004226void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004227 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004228 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004229}
4230
Alexandre Rames67555f72014-11-18 10:55:16 +00004231void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4232 HandleInvoke(invoke);
4233}
4234
4235void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4236 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004237 LocationSummary* locations = invoke->GetLocations();
4238 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004239 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004240 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004241 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004242
4243 // The register ip1 is required to be used for the hidden argument in
4244 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004245 MacroAssembler* masm = GetVIXLAssembler();
4246 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004247 scratch_scope.Exclude(ip1);
4248 __ Mov(ip1, invoke->GetDexMethodIndex());
4249
Artem Serov914d7a82017-02-07 14:33:49 +00004250 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004251 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004252 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004253 {
4254 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4255 // /* HeapReference<Class> */ temp = temp->klass_
4256 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4257 codegen_->MaybeRecordImplicitNullCheck(invoke);
4258 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004259 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004260 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004261 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004262 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004263 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004264 }
Artem Serov914d7a82017-02-07 14:33:49 +00004265
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004266 // Instead of simply (possibly) unpoisoning `temp` here, we should
4267 // emit a read barrier for the previous class reference load.
4268 // However this is not required in practice, as this is an
4269 // intermediate/temporary reference and because the current
4270 // concurrent copying collector keeps the from-space memory
4271 // intact/accessible until the end of the marking phase (the
4272 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004273 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004274 __ Ldr(temp,
4275 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4276 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004277 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004278 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004279 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004280 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004281 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004282
4283 {
4284 // Ensure the pc position is recorded immediately after the `blr` instruction.
4285 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4286
4287 // lr();
4288 __ blr(lr);
4289 DCHECK(!codegen_->IsLeafMethod());
4290 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4291 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004292
4293 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004294}
4295
4296void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004297 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004298 if (intrinsic.TryDispatch(invoke)) {
4299 return;
4300 }
4301
Alexandre Rames67555f72014-11-18 10:55:16 +00004302 HandleInvoke(invoke);
4303}
4304
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004305void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004306 // Explicit clinit checks triggered by static invokes must have been pruned by
4307 // art::PrepareForRegisterAllocation.
4308 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004309
Vladimir Markoca6fff82017-10-03 14:49:14 +01004310 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004311 if (intrinsic.TryDispatch(invoke)) {
4312 return;
4313 }
4314
Alexandre Rames67555f72014-11-18 10:55:16 +00004315 HandleInvoke(invoke);
4316}
4317
Andreas Gampe878d58c2015-01-15 23:24:00 -08004318static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4319 if (invoke->GetLocations()->Intrinsified()) {
4320 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4321 intrinsic.Dispatch(invoke);
4322 return true;
4323 }
4324 return false;
4325}
4326
Vladimir Markodc151b22015-10-15 18:02:30 +01004327HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4328 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004329 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004330 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004331 return desired_dispatch_info;
4332}
4333
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004334void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4335 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004336 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004337 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4338 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004339 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4340 uint32_t offset =
4341 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004342 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004343 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004344 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004345 }
Vladimir Marko58155012015-08-19 12:49:41 +00004346 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004347 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004348 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004349 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4350 DCHECK(GetCompilerOptions().IsBootImage());
4351 // Add ADRP with its PC-relative method patch.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004352 vixl::aarch64::Label* adrp_label = NewBootImageMethodPatch(invoke->GetTargetMethod());
Vladimir Marko65979462017-05-19 17:25:12 +01004353 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4354 // Add ADD with its PC-relative method patch.
4355 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004356 NewBootImageMethodPatch(invoke->GetTargetMethod(), adrp_label);
Vladimir Marko65979462017-05-19 17:25:12 +01004357 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4358 break;
4359 }
Vladimir Markob066d432018-01-03 13:14:37 +00004360 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4361 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004362 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00004363 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_offset);
4364 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4365 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
4366 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_offset, adrp_label);
4367 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
4368 EmitLdrOffsetPlaceholder(ldr_label, WRegisterFrom(temp), XRegisterFrom(temp));
4369 break;
4370 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004371 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Markob066d432018-01-03 13:14:37 +00004372 // Add ADRP with its PC-relative .bss entry patch.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004373 MethodReference target_method(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4374 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(target_method);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004375 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Markob066d432018-01-03 13:14:37 +00004376 // Add LDR with its PC-relative .bss entry patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004377 vixl::aarch64::Label* ldr_label =
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004378 NewMethodBssEntryPatch(target_method, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004379 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004380 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004381 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004382 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
4383 // Load method address from literal pool.
4384 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
4385 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004386 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4387 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4388 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004389 }
4390 }
4391
4392 switch (invoke->GetCodePtrLocation()) {
4393 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004394 {
4395 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4396 ExactAssemblyScope eas(GetVIXLAssembler(),
4397 kInstructionSize,
4398 CodeBufferCheckScope::kExactSize);
4399 __ bl(&frame_entry_label_);
4400 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4401 }
Vladimir Marko58155012015-08-19 12:49:41 +00004402 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004403 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4404 // LR = callee_method->entry_point_from_quick_compiled_code_;
4405 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004406 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004407 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004408 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004409 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004410 ExactAssemblyScope eas(GetVIXLAssembler(),
4411 kInstructionSize,
4412 CodeBufferCheckScope::kExactSize);
4413 // lr()
4414 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004415 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004416 }
Vladimir Marko58155012015-08-19 12:49:41 +00004417 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004418 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004419
Andreas Gampe878d58c2015-01-15 23:24:00 -08004420 DCHECK(!IsLeafMethod());
4421}
4422
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004423void CodeGeneratorARM64::GenerateVirtualCall(
4424 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004425 // Use the calling convention instead of the location of the receiver, as
4426 // intrinsics may have put the receiver in a different register. In the intrinsics
4427 // slow path, the arguments have been moved to the right place, so here we are
4428 // guaranteed that the receiver is the first register of the calling convention.
4429 InvokeDexCallingConvention calling_convention;
4430 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004431 Register temp = XRegisterFrom(temp_in);
4432 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4433 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4434 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004435 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004436
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004437 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004438
4439 {
4440 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4441 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4442 // /* HeapReference<Class> */ temp = receiver->klass_
4443 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4444 MaybeRecordImplicitNullCheck(invoke);
4445 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004446 // Instead of simply (possibly) unpoisoning `temp` here, we should
4447 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004448 // intermediate/temporary reference and because the current
4449 // concurrent copying collector keeps the from-space memory
4450 // intact/accessible until the end of the marking phase (the
4451 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004452 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4453 // temp = temp->GetMethodAt(method_offset);
4454 __ Ldr(temp, MemOperand(temp, method_offset));
4455 // lr = temp->GetEntryPoint();
4456 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004457 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004458 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004459 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4460 // lr();
4461 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004462 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004463 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004464}
4465
Orion Hodsonac141392017-01-13 11:53:47 +00004466void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4467 HandleInvoke(invoke);
4468}
4469
4470void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4471 codegen_->GenerateInvokePolymorphicCall(invoke);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004472 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004473}
4474
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004475void LocationsBuilderARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4476 HandleInvoke(invoke);
4477}
4478
4479void InstructionCodeGeneratorARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4480 codegen_->GenerateInvokeCustomCall(invoke);
4481 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
4482}
4483
Vladimir Marko6fd16062018-06-26 11:02:04 +01004484vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageIntrinsicPatch(
4485 uint32_t intrinsic_data,
4486 vixl::aarch64::Label* adrp_label) {
4487 return NewPcRelativePatch(
4488 /* dex_file */ nullptr, intrinsic_data, adrp_label, &boot_image_intrinsic_patches_);
4489}
4490
Vladimir Markob066d432018-01-03 13:14:37 +00004491vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageRelRoPatch(
4492 uint32_t boot_image_offset,
4493 vixl::aarch64::Label* adrp_label) {
4494 return NewPcRelativePatch(
4495 /* dex_file */ nullptr, boot_image_offset, adrp_label, &boot_image_method_patches_);
4496}
4497
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004498vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004499 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004500 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004501 return NewPcRelativePatch(
4502 target_method.dex_file, target_method.index, adrp_label, &boot_image_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004503}
4504
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004505vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4506 MethodReference target_method,
4507 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004508 return NewPcRelativePatch(
4509 target_method.dex_file, target_method.index, adrp_label, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004510}
4511
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004512vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageTypePatch(
Scott Wakeling97c72b72016-06-24 16:19:36 +01004513 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004514 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004515 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004516 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &boot_image_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004517}
4518
Vladimir Marko1998cd02017-01-13 13:02:58 +00004519vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4520 const DexFile& dex_file,
4521 dex::TypeIndex type_index,
4522 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004523 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004524}
4525
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004526vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004527 const DexFile& dex_file,
4528 dex::StringIndex string_index,
4529 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004530 return NewPcRelativePatch(
4531 &dex_file, string_index.index_, adrp_label, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01004532}
4533
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004534vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4535 const DexFile& dex_file,
4536 dex::StringIndex string_index,
4537 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004538 return NewPcRelativePatch(&dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004539}
4540
Vladimir Marko966b46f2018-08-03 10:20:19 +00004541void CodeGeneratorARM64::EmitBakerReadBarrierCbnz(uint32_t custom_data) {
4542 ExactAssemblyScope guard(GetVIXLAssembler(), 1 * vixl::aarch64::kInstructionSize);
4543 if (Runtime::Current()->UseJitCompilation()) {
4544 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
4545 vixl::aarch64::Label* slow_path_entry = &it->second.label;
4546 __ cbnz(mr, slow_path_entry);
4547 } else {
4548 baker_read_barrier_patches_.emplace_back(custom_data);
4549 vixl::aarch64::Label* cbnz_label = &baker_read_barrier_patches_.back().label;
4550 __ bind(cbnz_label);
4551 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4552 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004553}
4554
Scott Wakeling97c72b72016-06-24 16:19:36 +01004555vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004556 const DexFile* dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004557 uint32_t offset_or_index,
4558 vixl::aarch64::Label* adrp_label,
4559 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004560 // Add a patch entry and return the label.
4561 patches->emplace_back(dex_file, offset_or_index);
4562 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004563 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004564 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4565 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4566 return label;
4567}
4568
Scott Wakeling97c72b72016-06-24 16:19:36 +01004569vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4570 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004571 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004572}
4573
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004574vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004575 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004576 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004577 return jit_string_patches_.GetOrCreate(
4578 StringReference(&dex_file, string_index),
4579 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4580}
4581
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004582vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004583 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004584 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004585 return jit_class_patches_.GetOrCreate(
4586 TypeReference(&dex_file, type_index),
4587 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4588}
4589
Vladimir Markoaad75c62016-10-03 08:46:48 +00004590void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4591 vixl::aarch64::Register reg) {
4592 DCHECK(reg.IsX());
4593 SingleEmissionCheckScope guard(GetVIXLAssembler());
4594 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004595 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004596}
4597
4598void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4599 vixl::aarch64::Register out,
4600 vixl::aarch64::Register base) {
4601 DCHECK(out.IsX());
4602 DCHECK(base.IsX());
4603 SingleEmissionCheckScope guard(GetVIXLAssembler());
4604 __ Bind(fixup_label);
4605 __ add(out, base, Operand(/* offset placeholder */ 0));
4606}
4607
4608void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4609 vixl::aarch64::Register out,
4610 vixl::aarch64::Register base) {
4611 DCHECK(base.IsX());
4612 SingleEmissionCheckScope guard(GetVIXLAssembler());
4613 __ Bind(fixup_label);
4614 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4615}
4616
Vladimir Markoeebb8212018-06-05 14:57:24 +01004617void CodeGeneratorARM64::LoadBootImageAddress(vixl::aarch64::Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01004618 uint32_t boot_image_reference) {
4619 if (GetCompilerOptions().IsBootImage()) {
4620 // Add ADRP with its PC-relative type patch.
4621 vixl::aarch64::Label* adrp_label = NewBootImageIntrinsicPatch(boot_image_reference);
4622 EmitAdrpPlaceholder(adrp_label, reg.X());
4623 // Add ADD with its PC-relative type patch.
4624 vixl::aarch64::Label* add_label = NewBootImageIntrinsicPatch(boot_image_reference, adrp_label);
4625 EmitAddPlaceholder(add_label, reg.X(), reg.X());
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004626 } else if (Runtime::Current()->IsAotCompiler()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01004627 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004628 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004629 EmitAdrpPlaceholder(adrp_label, reg.X());
4630 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004631 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_reference, adrp_label);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004632 EmitLdrOffsetPlaceholder(ldr_label, reg.W(), reg.X());
4633 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004634 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01004635 gc::Heap* heap = Runtime::Current()->GetHeap();
4636 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004637 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01004638 __ Ldr(reg.W(), DeduplicateBootImageAddressLiteral(reinterpret_cast<uintptr_t>(address)));
4639 }
4640}
4641
Vladimir Marko6fd16062018-06-26 11:02:04 +01004642void CodeGeneratorARM64::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
4643 uint32_t boot_image_offset) {
4644 DCHECK(invoke->IsStatic());
4645 InvokeRuntimeCallingConvention calling_convention;
4646 Register argument = calling_convention.GetRegisterAt(0);
4647 if (GetCompilerOptions().IsBootImage()) {
4648 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
4649 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
4650 MethodReference target_method = invoke->GetTargetMethod();
4651 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
4652 // Add ADRP with its PC-relative type patch.
4653 vixl::aarch64::Label* adrp_label = NewBootImageTypePatch(*target_method.dex_file, type_idx);
4654 EmitAdrpPlaceholder(adrp_label, argument.X());
4655 // Add ADD with its PC-relative type patch.
4656 vixl::aarch64::Label* add_label =
4657 NewBootImageTypePatch(*target_method.dex_file, type_idx, adrp_label);
4658 EmitAddPlaceholder(add_label, argument.X(), argument.X());
4659 } else {
4660 LoadBootImageAddress(argument, boot_image_offset);
4661 }
4662 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
4663 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4664}
4665
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004666template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004667inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4668 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004669 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004670 for (const PcRelativePatchInfo& info : infos) {
4671 linker_patches->push_back(Factory(info.label.GetLocation(),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004672 info.target_dex_file,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004673 info.pc_insn_label->GetLocation(),
4674 info.offset_or_index));
4675 }
4676}
4677
Vladimir Marko6fd16062018-06-26 11:02:04 +01004678template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
4679linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
4680 const DexFile* target_dex_file,
4681 uint32_t pc_insn_offset,
4682 uint32_t boot_image_offset) {
4683 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
4684 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00004685}
4686
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004687void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004688 DCHECK(linker_patches->empty());
4689 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004690 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004691 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004692 boot_image_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004693 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004694 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004695 string_bss_entry_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01004696 boot_image_intrinsic_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004697 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004698 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01004699 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004700 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004701 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004702 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004703 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004704 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004705 boot_image_string_patches_, linker_patches);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004706 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
4707 boot_image_intrinsic_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01004708 } else {
Vladimir Marko6fd16062018-06-26 11:02:04 +01004709 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
Vladimir Markob066d432018-01-03 13:14:37 +00004710 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004711 DCHECK(boot_image_type_patches_.empty());
4712 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004713 DCHECK(boot_image_intrinsic_patches_.empty());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004714 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004715 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4716 method_bss_entry_patches_, linker_patches);
4717 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4718 type_bss_entry_patches_, linker_patches);
4719 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4720 string_bss_entry_patches_, linker_patches);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004721 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004722 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
4723 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004724 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004725 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004726}
4727
Vladimir Markoca1e0382018-04-11 09:58:41 +00004728bool CodeGeneratorARM64::NeedsThunkCode(const linker::LinkerPatch& patch) const {
4729 return patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
4730 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
4731}
4732
4733void CodeGeneratorARM64::EmitThunkCode(const linker::LinkerPatch& patch,
4734 /*out*/ ArenaVector<uint8_t>* code,
4735 /*out*/ std::string* debug_name) {
4736 Arm64Assembler assembler(GetGraph()->GetAllocator());
4737 switch (patch.GetType()) {
4738 case linker::LinkerPatch::Type::kCallRelative: {
4739 // The thunk just uses the entry point in the ArtMethod. This works even for calls
4740 // to the generic JNI and interpreter trampolines.
4741 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4742 kArm64PointerSize).Int32Value());
4743 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
4744 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
4745 *debug_name = "MethodCallThunk";
4746 }
4747 break;
4748 }
4749 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
4750 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
4751 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
4752 break;
4753 }
4754 default:
4755 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
4756 UNREACHABLE();
4757 }
4758
4759 // Ensure we emit the literal pool if any.
4760 assembler.FinalizeCode();
4761 code->resize(assembler.CodeSize());
4762 MemoryRegion code_region(code->data(), code->size());
4763 assembler.FinalizeInstructions(code_region);
4764}
4765
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004766vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
4767 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004768 value,
4769 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4770}
4771
Scott Wakeling97c72b72016-06-24 16:19:36 +01004772vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004773 return uint64_literals_.GetOrCreate(
4774 value,
4775 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004776}
4777
Andreas Gampe878d58c2015-01-15 23:24:00 -08004778void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004779 // Explicit clinit checks triggered by static invokes must have been pruned by
4780 // art::PrepareForRegisterAllocation.
4781 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004782
Andreas Gampe878d58c2015-01-15 23:24:00 -08004783 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004784 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004785 return;
4786 }
4787
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004788 {
4789 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4790 // are no pools emitted.
4791 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4792 LocationSummary* locations = invoke->GetLocations();
4793 codegen_->GenerateStaticOrDirectCall(
4794 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
4795 }
4796
4797 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004798}
4799
4800void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004801 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004802 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004803 return;
4804 }
4805
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004806 {
4807 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4808 // are no pools emitted.
4809 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4810 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
4811 DCHECK(!codegen_->IsLeafMethod());
4812 }
4813
4814 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004815}
4816
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004817HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4818 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004819 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004820 case HLoadClass::LoadKind::kInvalid:
4821 LOG(FATAL) << "UNREACHABLE";
4822 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004823 case HLoadClass::LoadKind::kReferrersClass:
4824 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004825 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004826 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004827 case HLoadClass::LoadKind::kBssEntry:
4828 DCHECK(!Runtime::Current()->UseJitCompilation());
4829 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004830 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004831 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004832 DCHECK(Runtime::Current()->UseJitCompilation());
4833 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004834 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004835 break;
4836 }
4837 return desired_class_load_kind;
4838}
4839
Alexandre Rames67555f72014-11-18 10:55:16 +00004840void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004841 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004842 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004843 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004844 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004845 cls,
4846 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004847 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004848 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004849 return;
4850 }
Vladimir Marko41559982017-01-06 14:04:23 +00004851 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004852
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004853 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4854 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004855 ? LocationSummary::kCallOnSlowPath
4856 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004857 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004858 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004859 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004860 }
4861
Vladimir Marko41559982017-01-06 14:04:23 +00004862 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004863 locations->SetInAt(0, Location::RequiresRegister());
4864 }
4865 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004866 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4867 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4868 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01004869 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004870 } else {
4871 // For non-Baker read barrier we have a temp-clobbering call.
4872 }
4873 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004874}
4875
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004876// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4877// move.
4878void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004879 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004880 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00004881 codegen_->GenerateLoadClassRuntimeCall(cls);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004882 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01004883 return;
4884 }
Vladimir Marko41559982017-01-06 14:04:23 +00004885 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004886
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004887 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004888 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004889
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004890 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4891 ? kWithoutReadBarrier
4892 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004893 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004894 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004895 case HLoadClass::LoadKind::kReferrersClass: {
4896 DCHECK(!cls->CanCallRuntime());
4897 DCHECK(!cls->MustGenerateClinitCheck());
4898 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4899 Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00004900 codegen_->GenerateGcRootFieldLoad(cls,
4901 out_loc,
4902 current_method,
4903 ArtMethod::DeclaringClassOffset().Int32Value(),
4904 /* fixup_label */ nullptr,
4905 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004906 break;
4907 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004908 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004909 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004910 // Add ADRP with its PC-relative type patch.
4911 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004912 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004913 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004914 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004915 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004916 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004917 codegen_->NewBootImageTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004918 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004919 break;
4920 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004921 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004922 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004923 uint32_t boot_image_offset = codegen_->GetBootImageOffset(cls);
4924 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
4925 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004926 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004927 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004928 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004929 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004930 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004931 break;
4932 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004933 case HLoadClass::LoadKind::kBssEntry: {
4934 // Add ADRP with its PC-relative Class .bss entry patch.
4935 const DexFile& dex_file = cls->GetDexFile();
4936 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00004937 vixl::aarch64::Register temp = XRegisterFrom(out_loc);
4938 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4939 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004940 // Add LDR with its PC-relative Class .bss entry patch.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004941 vixl::aarch64::Label* ldr_label =
Vladimir Markof3c52b42017-11-17 17:32:12 +00004942 codegen_->NewBssEntryTypePatch(dex_file, type_index, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004943 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoca1e0382018-04-11 09:58:41 +00004944 codegen_->GenerateGcRootFieldLoad(cls,
4945 out_loc,
4946 temp,
4947 /* offset placeholder */ 0u,
4948 ldr_label,
4949 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004950 generate_null_check = true;
4951 break;
4952 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004953 case HLoadClass::LoadKind::kJitBootImageAddress: {
4954 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
4955 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
4956 DCHECK_NE(address, 0u);
4957 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
4958 break;
4959 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004960 case HLoadClass::LoadKind::kJitTableAddress: {
4961 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4962 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004963 cls->GetClass()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00004964 codegen_->GenerateGcRootFieldLoad(cls,
4965 out_loc,
4966 out.X(),
4967 /* offset */ 0,
4968 /* fixup_label */ nullptr,
4969 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004970 break;
4971 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004972 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004973 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004974 LOG(FATAL) << "UNREACHABLE";
4975 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004976 }
4977
Vladimir Markoea4c1262017-02-06 19:59:33 +00004978 bool do_clinit = cls->MustGenerateClinitCheck();
4979 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004980 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01004981 SlowPathCodeARM64* slow_path =
4982 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004983 codegen_->AddSlowPath(slow_path);
4984 if (generate_null_check) {
4985 __ Cbz(out, slow_path->GetEntryLabel());
4986 }
4987 if (cls->MustGenerateClinitCheck()) {
4988 GenerateClassInitializationCheck(slow_path, out);
4989 } else {
4990 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004991 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004992 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004993 }
4994}
4995
Orion Hodsondbaa5c72018-05-10 08:22:46 +01004996void LocationsBuilderARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
4997 InvokeRuntimeCallingConvention calling_convention;
4998 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
4999 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
5000}
5001
5002void InstructionCodeGeneratorARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5003 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
5004}
5005
Orion Hodson18259d72018-04-12 11:18:23 +01005006void LocationsBuilderARM64::VisitLoadMethodType(HLoadMethodType* load) {
5007 InvokeRuntimeCallingConvention calling_convention;
5008 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5009 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
5010}
5011
5012void InstructionCodeGeneratorARM64::VisitLoadMethodType(HLoadMethodType* load) {
5013 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
5014}
5015
David Brazdilcb1c0552015-08-04 16:22:25 +01005016static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005017 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005018}
5019
Alexandre Rames67555f72014-11-18 10:55:16 +00005020void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5021 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005022 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00005023 locations->SetOut(Location::RequiresRegister());
5024}
5025
5026void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005027 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5028}
5029
5030void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005031 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005032}
5033
5034void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5035 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005036}
5037
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005038HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5039 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005040 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005041 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005042 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005043 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005044 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005045 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005046 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005047 case HLoadString::LoadKind::kJitTableAddress:
5048 DCHECK(Runtime::Current()->UseJitCompilation());
5049 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005050 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005051 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005052 }
5053 return desired_string_load_kind;
5054}
5055
Alexandre Rames67555f72014-11-18 10:55:16 +00005056void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005057 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005058 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005059 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005060 InvokeRuntimeCallingConvention calling_convention;
5061 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5062 } else {
5063 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005064 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5065 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005066 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005067 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005068 } else {
5069 // For non-Baker read barrier we have a temp-clobbering call.
5070 }
5071 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005072 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005073}
5074
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005075// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5076// move.
5077void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005078 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005079 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005080
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005081 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005082 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005083 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005084 // Add ADRP with its PC-relative String patch.
5085 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005086 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005087 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005088 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005089 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005090 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005091 codegen_->NewBootImageStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005092 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005093 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005094 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005095 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005096 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005097 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5098 uint32_t boot_image_offset = codegen_->GetBootImageOffset(load);
5099 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005100 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005101 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005102 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005103 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005104 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
5105 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005106 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005107 case HLoadString::LoadKind::kBssEntry: {
5108 // Add ADRP with its PC-relative String .bss entry patch.
5109 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005110 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005111 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markof3c52b42017-11-17 17:32:12 +00005112 Register temp = XRegisterFrom(out_loc);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005113 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005114 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005115 // Add LDR with its PC-relative String .bss entry patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005116 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005117 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005118 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoca1e0382018-04-11 09:58:41 +00005119 codegen_->GenerateGcRootFieldLoad(load,
5120 out_loc,
5121 temp,
5122 /* offset placeholder */ 0u,
5123 ldr_label,
5124 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005125 SlowPathCodeARM64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005126 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005127 codegen_->AddSlowPath(slow_path);
5128 __ Cbz(out.X(), slow_path->GetEntryLabel());
5129 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005130 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005131 return;
5132 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005133 case HLoadString::LoadKind::kJitBootImageAddress: {
5134 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
5135 DCHECK_NE(address, 0u);
5136 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5137 return;
5138 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005139 case HLoadString::LoadKind::kJitTableAddress: {
5140 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005141 load->GetStringIndex(),
5142 load->GetString()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005143 codegen_->GenerateGcRootFieldLoad(load,
5144 out_loc,
5145 out.X(),
5146 /* offset */ 0,
5147 /* fixup_label */ nullptr,
5148 kCompilerReadBarrierOption);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005149 return;
5150 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005151 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005152 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005153 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005154
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005155 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005156 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005157 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005158 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005159 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5160 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005161 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005162}
5163
Alexandre Rames5319def2014-10-23 10:03:10 +01005164void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005165 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01005166 locations->SetOut(Location::ConstantLocation(constant));
5167}
5168
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005169void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005170 // Will be generated at use site.
5171}
5172
Alexandre Rames67555f72014-11-18 10:55:16 +00005173void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005174 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5175 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005176 InvokeRuntimeCallingConvention calling_convention;
5177 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5178}
5179
5180void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005181 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005182 instruction,
5183 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005184 if (instruction->IsEnter()) {
5185 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5186 } else {
5187 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5188 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005189 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005190}
5191
Alexandre Rames42d641b2014-10-27 14:00:51 +00005192void LocationsBuilderARM64::VisitMul(HMul* mul) {
5193 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005194 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005195 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005196 case DataType::Type::kInt32:
5197 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005198 locations->SetInAt(0, Location::RequiresRegister());
5199 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005200 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005201 break;
5202
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005203 case DataType::Type::kFloat32:
5204 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005205 locations->SetInAt(0, Location::RequiresFpuRegister());
5206 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005207 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005208 break;
5209
5210 default:
5211 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5212 }
5213}
5214
5215void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5216 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005217 case DataType::Type::kInt32:
5218 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005219 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5220 break;
5221
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005222 case DataType::Type::kFloat32:
5223 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005224 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005225 break;
5226
5227 default:
5228 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5229 }
5230}
5231
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005232void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5233 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005234 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005235 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005236 case DataType::Type::kInt32:
5237 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005238 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005239 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005240 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005241
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005242 case DataType::Type::kFloat32:
5243 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005244 locations->SetInAt(0, Location::RequiresFpuRegister());
5245 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005246 break;
5247
5248 default:
5249 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5250 }
5251}
5252
5253void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5254 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005255 case DataType::Type::kInt32:
5256 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005257 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5258 break;
5259
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005260 case DataType::Type::kFloat32:
5261 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005262 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005263 break;
5264
5265 default:
5266 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5267 }
5268}
5269
5270void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005271 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5272 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005273 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005274 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005275 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5276 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005277}
5278
5279void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005280 // Note: if heap poisoning is enabled, the entry point takes cares
5281 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005282 QuickEntrypointEnum entrypoint =
5283 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5284 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005285 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005286 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005287}
5288
Alexandre Rames5319def2014-10-23 10:03:10 +01005289void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005290 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5291 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005292 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07005293 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005294 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005295}
5296
5297void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005298 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5299 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005300 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005301}
5302
5303void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005304 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005305 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005306 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005307}
5308
5309void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005310 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005311 case DataType::Type::kInt32:
5312 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005313 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005314 break;
5315
5316 default:
5317 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5318 }
5319}
5320
David Brazdil66d126e2015-04-03 16:02:44 +01005321void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005322 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005323 locations->SetInAt(0, Location::RequiresRegister());
5324 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5325}
5326
5327void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005328 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005329}
5330
Alexandre Rames5319def2014-10-23 10:03:10 +01005331void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005332 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5333 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005334}
5335
Calin Juravle2ae48182016-03-16 14:05:09 +00005336void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5337 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005338 return;
5339 }
Artem Serov914d7a82017-02-07 14:33:49 +00005340 {
5341 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5342 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5343 Location obj = instruction->GetLocations()->InAt(0);
5344 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5345 RecordPcInfo(instruction, instruction->GetDexPc());
5346 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005347}
5348
Calin Juravle2ae48182016-03-16 14:05:09 +00005349void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005350 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005351 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005352
5353 LocationSummary* locations = instruction->GetLocations();
5354 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005355
5356 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005357}
5358
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005359void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005360 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005361}
5362
Alexandre Rames67555f72014-11-18 10:55:16 +00005363void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5364 HandleBinaryOp(instruction);
5365}
5366
5367void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5368 HandleBinaryOp(instruction);
5369}
5370
Alexandre Rames3e69f162014-12-10 10:36:50 +00005371void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5372 LOG(FATAL) << "Unreachable";
5373}
5374
5375void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005376 if (instruction->GetNext()->IsSuspendCheck() &&
5377 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5378 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5379 // The back edge will generate the suspend check.
5380 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5381 }
5382
Alexandre Rames3e69f162014-12-10 10:36:50 +00005383 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5384}
5385
Alexandre Rames5319def2014-10-23 10:03:10 +01005386void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005387 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005388 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5389 if (location.IsStackSlot()) {
5390 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5391 } else if (location.IsDoubleStackSlot()) {
5392 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5393 }
5394 locations->SetOut(location);
5395}
5396
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005397void InstructionCodeGeneratorARM64::VisitParameterValue(
5398 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005399 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005400}
5401
5402void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5403 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005404 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005405 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005406}
5407
5408void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5409 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5410 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005411}
5412
5413void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005414 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005415 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005416 locations->SetInAt(i, Location::Any());
5417 }
5418 locations->SetOut(Location::Any());
5419}
5420
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005421void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005422 LOG(FATAL) << "Unreachable";
5423}
5424
Serban Constantinescu02164b32014-11-13 14:05:07 +00005425void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005426 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005427 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005428 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005429 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005430 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005431
5432 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005433 case DataType::Type::kInt32:
5434 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005435 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005436 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005437 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5438 break;
5439
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005440 case DataType::Type::kFloat32:
5441 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005442 InvokeRuntimeCallingConvention calling_convention;
5443 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5444 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5445 locations->SetOut(calling_convention.GetReturnLocation(type));
5446
5447 break;
5448 }
5449
Serban Constantinescu02164b32014-11-13 14:05:07 +00005450 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005451 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005452 }
5453}
5454
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005455void InstructionCodeGeneratorARM64::GenerateIntRemForPower2Denom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005456 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005457 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
5458 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
5459
5460 Register out = OutputRegister(instruction);
5461 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005462
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005463 if (abs_imm == 2) {
5464 __ Cmp(dividend, 0);
5465 __ And(out, dividend, 1);
5466 __ Csneg(out, out, out, ge);
5467 } else {
5468 UseScratchRegisterScope temps(GetVIXLAssembler());
5469 Register temp = temps.AcquireSameSizeAs(out);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005470
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005471 __ Negs(temp, dividend);
5472 __ And(out, dividend, abs_imm - 1);
5473 __ And(temp, temp, abs_imm - 1);
5474 __ Csneg(out, out, temp, mi);
5475 }
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005476}
5477
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005478void InstructionCodeGeneratorARM64::GenerateIntRemForConstDenom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005479 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005480
5481 if (imm == 0) {
5482 // Do not generate anything.
5483 // DivZeroCheck would prevent any code to be executed.
5484 return;
5485 }
5486
Evgeny Astigeevichf58dc652018-06-25 17:54:07 +01005487 if (IsPowerOfTwo(AbsOrMin(imm))) {
5488 // Cases imm == -1 or imm == 1 are handled in constant folding by
5489 // InstructionWithAbsorbingInputSimplifier.
5490 // If the cases have survided till code generation they are handled in
5491 // GenerateIntRemForPower2Denom becauses -1 and 1 are the power of 2 (2^0).
5492 // The correct code is generated for them, just more instructions.
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005493 GenerateIntRemForPower2Denom(instruction);
5494 } else {
5495 DCHECK(imm < -2 || imm > 2) << imm;
5496 GenerateDivRemWithAnyConstant(instruction);
5497 }
5498}
5499
5500void InstructionCodeGeneratorARM64::GenerateIntRem(HRem* instruction) {
5501 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
5502 << instruction->GetResultType();
5503
5504 if (instruction->GetLocations()->InAt(1).IsConstant()) {
5505 GenerateIntRemForConstDenom(instruction);
5506 } else {
5507 Register out = OutputRegister(instruction);
5508 Register dividend = InputRegisterAt(instruction, 0);
5509 Register divisor = InputRegisterAt(instruction, 1);
5510 UseScratchRegisterScope temps(GetVIXLAssembler());
5511 Register temp = temps.AcquireSameSizeAs(out);
5512 __ Sdiv(temp, dividend, divisor);
5513 __ Msub(out, temp, divisor, dividend);
5514 }
5515}
5516
Serban Constantinescu02164b32014-11-13 14:05:07 +00005517void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005518 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005519
Serban Constantinescu02164b32014-11-13 14:05:07 +00005520 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005521 case DataType::Type::kInt32:
5522 case DataType::Type::kInt64: {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005523 GenerateIntRem(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005524 break;
5525 }
5526
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005527 case DataType::Type::kFloat32:
5528 case DataType::Type::kFloat64: {
5529 QuickEntrypointEnum entrypoint =
5530 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005531 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005532 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00005533 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5534 } else {
5535 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5536 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005537 break;
5538 }
5539
Serban Constantinescu02164b32014-11-13 14:05:07 +00005540 default:
5541 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005542 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005543 }
5544}
5545
Aart Bik1f8d51b2018-02-15 10:42:37 -08005546void LocationsBuilderARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005547 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005548}
5549
Aart Bik1f8d51b2018-02-15 10:42:37 -08005550void InstructionCodeGeneratorARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005551 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005552}
5553
5554void LocationsBuilderARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005555 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005556}
5557
5558void InstructionCodeGeneratorARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005559 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005560}
5561
Aart Bik3dad3412018-02-28 12:01:46 -08005562void LocationsBuilderARM64::VisitAbs(HAbs* abs) {
5563 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
5564 switch (abs->GetResultType()) {
5565 case DataType::Type::kInt32:
5566 case DataType::Type::kInt64:
5567 locations->SetInAt(0, Location::RequiresRegister());
5568 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5569 break;
5570 case DataType::Type::kFloat32:
5571 case DataType::Type::kFloat64:
5572 locations->SetInAt(0, Location::RequiresFpuRegister());
5573 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5574 break;
5575 default:
5576 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5577 }
5578}
5579
5580void InstructionCodeGeneratorARM64::VisitAbs(HAbs* abs) {
5581 switch (abs->GetResultType()) {
5582 case DataType::Type::kInt32:
5583 case DataType::Type::kInt64: {
5584 Register in_reg = InputRegisterAt(abs, 0);
5585 Register out_reg = OutputRegister(abs);
5586 __ Cmp(in_reg, Operand(0));
5587 __ Cneg(out_reg, in_reg, lt);
5588 break;
5589 }
5590 case DataType::Type::kFloat32:
5591 case DataType::Type::kFloat64: {
5592 FPRegister in_reg = InputFPRegisterAt(abs, 0);
5593 FPRegister out_reg = OutputFPRegister(abs);
5594 __ Fabs(out_reg, in_reg);
5595 break;
5596 }
5597 default:
5598 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5599 }
5600}
5601
Igor Murashkind01745e2017-04-05 16:40:31 -07005602void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5603 constructor_fence->SetLocations(nullptr);
5604}
5605
5606void InstructionCodeGeneratorARM64::VisitConstructorFence(
5607 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5608 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5609}
5610
Calin Juravle27df7582015-04-17 19:12:31 +01005611void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5612 memory_barrier->SetLocations(nullptr);
5613}
5614
5615void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005616 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005617}
5618
Alexandre Rames5319def2014-10-23 10:03:10 +01005619void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005620 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005621 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005622 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005623}
5624
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005625void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005626 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005627}
5628
5629void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5630 instruction->SetLocations(nullptr);
5631}
5632
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005633void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005634 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005635}
5636
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005637void LocationsBuilderARM64::VisitRor(HRor* ror) {
5638 HandleBinaryOp(ror);
5639}
5640
5641void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5642 HandleBinaryOp(ror);
5643}
5644
Serban Constantinescu02164b32014-11-13 14:05:07 +00005645void LocationsBuilderARM64::VisitShl(HShl* shl) {
5646 HandleShift(shl);
5647}
5648
5649void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5650 HandleShift(shl);
5651}
5652
5653void LocationsBuilderARM64::VisitShr(HShr* shr) {
5654 HandleShift(shr);
5655}
5656
5657void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5658 HandleShift(shr);
5659}
5660
Alexandre Rames5319def2014-10-23 10:03:10 +01005661void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005662 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005663}
5664
5665void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005666 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005667}
5668
Alexandre Rames67555f72014-11-18 10:55:16 +00005669void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005670 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005671}
5672
5673void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005674 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005675}
5676
5677void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005678 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005679}
5680
Alexandre Rames67555f72014-11-18 10:55:16 +00005681void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005682 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005683}
5684
Calin Juravlee460d1d2015-09-29 04:52:17 +01005685void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5686 HUnresolvedInstanceFieldGet* instruction) {
5687 FieldAccessCallingConventionARM64 calling_convention;
5688 codegen_->CreateUnresolvedFieldLocationSummary(
5689 instruction, instruction->GetFieldType(), calling_convention);
5690}
5691
5692void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5693 HUnresolvedInstanceFieldGet* instruction) {
5694 FieldAccessCallingConventionARM64 calling_convention;
5695 codegen_->GenerateUnresolvedFieldAccess(instruction,
5696 instruction->GetFieldType(),
5697 instruction->GetFieldIndex(),
5698 instruction->GetDexPc(),
5699 calling_convention);
5700}
5701
5702void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5703 HUnresolvedInstanceFieldSet* instruction) {
5704 FieldAccessCallingConventionARM64 calling_convention;
5705 codegen_->CreateUnresolvedFieldLocationSummary(
5706 instruction, instruction->GetFieldType(), calling_convention);
5707}
5708
5709void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5710 HUnresolvedInstanceFieldSet* instruction) {
5711 FieldAccessCallingConventionARM64 calling_convention;
5712 codegen_->GenerateUnresolvedFieldAccess(instruction,
5713 instruction->GetFieldType(),
5714 instruction->GetFieldIndex(),
5715 instruction->GetDexPc(),
5716 calling_convention);
5717}
5718
5719void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5720 HUnresolvedStaticFieldGet* instruction) {
5721 FieldAccessCallingConventionARM64 calling_convention;
5722 codegen_->CreateUnresolvedFieldLocationSummary(
5723 instruction, instruction->GetFieldType(), calling_convention);
5724}
5725
5726void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5727 HUnresolvedStaticFieldGet* instruction) {
5728 FieldAccessCallingConventionARM64 calling_convention;
5729 codegen_->GenerateUnresolvedFieldAccess(instruction,
5730 instruction->GetFieldType(),
5731 instruction->GetFieldIndex(),
5732 instruction->GetDexPc(),
5733 calling_convention);
5734}
5735
5736void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5737 HUnresolvedStaticFieldSet* instruction) {
5738 FieldAccessCallingConventionARM64 calling_convention;
5739 codegen_->CreateUnresolvedFieldLocationSummary(
5740 instruction, instruction->GetFieldType(), calling_convention);
5741}
5742
5743void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5744 HUnresolvedStaticFieldSet* instruction) {
5745 FieldAccessCallingConventionARM64 calling_convention;
5746 codegen_->GenerateUnresolvedFieldAccess(instruction,
5747 instruction->GetFieldType(),
5748 instruction->GetFieldIndex(),
5749 instruction->GetDexPc(),
5750 calling_convention);
5751}
5752
Alexandre Rames5319def2014-10-23 10:03:10 +01005753void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005754 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5755 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005756 // In suspend check slow path, usually there are no caller-save registers at all.
5757 // If SIMD instructions are present, however, we force spilling all live SIMD
5758 // registers in full width (since the runtime only saves/restores lower part).
5759 locations->SetCustomSlowPathCallerSaves(
5760 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005761}
5762
5763void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005764 HBasicBlock* block = instruction->GetBlock();
5765 if (block->GetLoopInformation() != nullptr) {
5766 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5767 // The back edge will generate the suspend check.
5768 return;
5769 }
5770 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5771 // The goto will generate the suspend check.
5772 return;
5773 }
5774 GenerateSuspendCheck(instruction, nullptr);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005775 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005776}
5777
Alexandre Rames67555f72014-11-18 10:55:16 +00005778void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005779 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5780 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005781 InvokeRuntimeCallingConvention calling_convention;
5782 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5783}
5784
5785void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005786 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005787 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005788}
5789
5790void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5791 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005792 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005793 DataType::Type input_type = conversion->GetInputType();
5794 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005795 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5796 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005797 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
5798 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005799 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5800 }
5801
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005802 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005803 locations->SetInAt(0, Location::RequiresFpuRegister());
5804 } else {
5805 locations->SetInAt(0, Location::RequiresRegister());
5806 }
5807
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005808 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005809 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5810 } else {
5811 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5812 }
5813}
5814
5815void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005816 DataType::Type result_type = conversion->GetResultType();
5817 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00005818
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005819 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5820 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005821
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005822 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
5823 int result_size = DataType::Size(result_type);
5824 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005825 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005826 Register output = OutputRegister(conversion);
5827 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005828 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005829 // 'int' values are used directly as W registers, discarding the top
5830 // bits, so we don't need to sign-extend and can just perform a move.
5831 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5832 // top 32 bits of the target register. We theoretically could leave those
5833 // bits unchanged, but we would have to make sure that no code uses a
5834 // 32bit input value as a 64bit value assuming that the top 32 bits are
5835 // zero.
5836 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005837 } else if (DataType::IsUnsignedType(result_type) ||
5838 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
5839 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005840 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005841 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005842 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005843 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005844 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005845 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
5846 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005847 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005848 } else if (DataType::IsFloatingPointType(result_type) &&
5849 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005850 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5851 } else {
5852 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5853 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005854 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005855}
Alexandre Rames67555f72014-11-18 10:55:16 +00005856
Serban Constantinescu02164b32014-11-13 14:05:07 +00005857void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5858 HandleShift(ushr);
5859}
5860
5861void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5862 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005863}
5864
5865void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5866 HandleBinaryOp(instruction);
5867}
5868
5869void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5870 HandleBinaryOp(instruction);
5871}
5872
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005873void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005874 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005875 LOG(FATAL) << "Unreachable";
5876}
5877
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005878void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005879 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005880 LOG(FATAL) << "Unreachable";
5881}
5882
Mark Mendellfe57faa2015-09-18 09:26:15 -04005883// Simple implementation of packed switch - generate cascaded compare/jumps.
5884void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5885 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005886 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005887 locations->SetInAt(0, Location::RequiresRegister());
5888}
5889
5890void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5891 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005892 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005893 Register value_reg = InputRegisterAt(switch_instr, 0);
5894 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5895
Zheng Xu3927c8b2015-11-18 17:46:25 +08005896 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005897 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005898 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5899 // make sure we don't emit it if the target may run out of range.
5900 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5901 // ranges and emit the tables only as required.
5902 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005903
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005904 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005905 // Current instruction id is an upper bound of the number of HIRs in the graph.
5906 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5907 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005908 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5909 Register temp = temps.AcquireW();
5910 __ Subs(temp, value_reg, Operand(lower_bound));
5911
Zheng Xu3927c8b2015-11-18 17:46:25 +08005912 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005913 // Jump to successors[0] if value == lower_bound.
5914 __ B(eq, codegen_->GetLabelOf(successors[0]));
5915 int32_t last_index = 0;
5916 for (; num_entries - last_index > 2; last_index += 2) {
5917 __ Subs(temp, temp, Operand(2));
5918 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5919 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5920 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5921 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5922 }
5923 if (num_entries - last_index == 2) {
5924 // The last missing case_value.
5925 __ Cmp(temp, Operand(1));
5926 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005927 }
5928
5929 // And the default for any other value.
5930 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5931 __ B(codegen_->GetLabelOf(default_block));
5932 }
5933 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005934 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005935
5936 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5937
5938 // Below instructions should use at most one blocked register. Since there are two blocked
5939 // registers, we are free to block one.
5940 Register temp_w = temps.AcquireW();
5941 Register index;
5942 // Remove the bias.
5943 if (lower_bound != 0) {
5944 index = temp_w;
5945 __ Sub(index, value_reg, Operand(lower_bound));
5946 } else {
5947 index = value_reg;
5948 }
5949
5950 // Jump to default block if index is out of the range.
5951 __ Cmp(index, Operand(num_entries));
5952 __ B(hs, codegen_->GetLabelOf(default_block));
5953
5954 // In current VIXL implementation, it won't require any blocked registers to encode the
5955 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5956 // register pressure.
5957 Register table_base = temps.AcquireX();
5958 // Load jump offset from the table.
5959 __ Adr(table_base, jump_table->GetTableStartLabel());
5960 Register jump_offset = temp_w;
5961 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5962
5963 // Jump to target block by branching to table_base(pc related) + offset.
5964 Register target_address = table_base;
5965 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5966 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005967 }
5968}
5969
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005970void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5971 HInstruction* instruction,
5972 Location out,
5973 uint32_t offset,
5974 Location maybe_temp,
5975 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005976 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00005977 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005978 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005979 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005980 if (kUseBakerReadBarrier) {
5981 // Load with fast path based Baker's read barrier.
5982 // /* HeapReference<Object> */ out = *(out + offset)
5983 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5984 out,
5985 out_reg,
5986 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005987 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005988 /* needs_null_check */ false,
5989 /* use_load_acquire */ false);
5990 } else {
5991 // Load with slow path based read barrier.
5992 // Save the value of `out` into `maybe_temp` before overwriting it
5993 // in the following move operation, as we will need it for the
5994 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005995 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005996 __ Mov(temp_reg, out_reg);
5997 // /* HeapReference<Object> */ out = *(out + offset)
5998 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5999 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6000 }
6001 } else {
6002 // Plain load with no read barrier.
6003 // /* HeapReference<Object> */ out = *(out + offset)
6004 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6005 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6006 }
6007}
6008
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006009void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
6010 HInstruction* instruction,
6011 Location out,
6012 Location obj,
6013 uint32_t offset,
6014 Location maybe_temp,
6015 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006016 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006017 Register out_reg = RegisterFrom(out, type);
6018 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006019 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006020 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006021 if (kUseBakerReadBarrier) {
6022 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00006023 // /* HeapReference<Object> */ out = *(obj + offset)
6024 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6025 out,
6026 obj_reg,
6027 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006028 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006029 /* needs_null_check */ false,
6030 /* use_load_acquire */ false);
6031 } else {
6032 // Load with slow path based read barrier.
6033 // /* HeapReference<Object> */ out = *(obj + offset)
6034 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6035 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6036 }
6037 } else {
6038 // Plain load with no read barrier.
6039 // /* HeapReference<Object> */ out = *(obj + offset)
6040 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6041 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6042 }
6043}
6044
Vladimir Markoca1e0382018-04-11 09:58:41 +00006045void CodeGeneratorARM64::GenerateGcRootFieldLoad(
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006046 HInstruction* instruction,
6047 Location root,
6048 Register obj,
6049 uint32_t offset,
6050 vixl::aarch64::Label* fixup_label,
6051 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006052 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006053 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006054 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006055 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006056 if (kUseBakerReadBarrier) {
6057 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00006058 // Baker's read barrier are used.
Roland Levillain44015862016-01-22 11:47:17 +00006059
Vladimir Marko008e09f32018-08-06 15:42:43 +01006060 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6061 // the Marking Register) to decide whether we need to enter
6062 // the slow path to mark the GC root.
6063 //
6064 // We use shared thunks for the slow path; shared within the method
6065 // for JIT, across methods for AOT. That thunk checks the reference
6066 // and jumps to the entrypoint if needed.
6067 //
6068 // lr = &return_address;
6069 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6070 // if (mr) { // Thread::Current()->GetIsGcMarking()
6071 // goto gc_root_thunk<root_reg>(lr)
6072 // }
6073 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00006074
Vladimir Marko008e09f32018-08-06 15:42:43 +01006075 UseScratchRegisterScope temps(GetVIXLAssembler());
6076 DCHECK(temps.IsAvailable(ip0));
6077 DCHECK(temps.IsAvailable(ip1));
6078 temps.Exclude(ip0, ip1);
6079 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
Roland Levillain44015862016-01-22 11:47:17 +00006080
Vladimir Marko008e09f32018-08-06 15:42:43 +01006081 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6082 vixl::aarch64::Label return_address;
6083 __ adr(lr, &return_address);
6084 if (fixup_label != nullptr) {
6085 __ bind(fixup_label);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006086 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006087 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6088 "GC root LDR must be 2 instruction (8B) before the return address label.");
6089 __ ldr(root_reg, MemOperand(obj.X(), offset));
6090 EmitBakerReadBarrierCbnz(custom_data);
6091 __ bind(&return_address);
Roland Levillain44015862016-01-22 11:47:17 +00006092 } else {
6093 // GC root loaded through a slow path for read barriers other
6094 // than Baker's.
6095 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006096 if (fixup_label == nullptr) {
6097 __ Add(root_reg.X(), obj.X(), offset);
6098 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006099 EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006100 }
Roland Levillain44015862016-01-22 11:47:17 +00006101 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00006102 GenerateReadBarrierForRootSlow(instruction, root, root);
Roland Levillain44015862016-01-22 11:47:17 +00006103 }
6104 } else {
6105 // Plain GC root load with no read barrier.
6106 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006107 if (fixup_label == nullptr) {
6108 __ Ldr(root_reg, MemOperand(obj, offset));
6109 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006110 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006111 }
Roland Levillain44015862016-01-22 11:47:17 +00006112 // Note that GC roots are not affected by heap poisoning, thus we
6113 // do not have to unpoison `root_reg` here.
6114 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006115 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00006116}
6117
6118void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6119 Location ref,
Vladimir Marko248141f2018-08-10 10:40:07 +01006120 vixl::aarch64::Register obj,
6121 const vixl::aarch64::MemOperand& src,
Roland Levillain44015862016-01-22 11:47:17 +00006122 bool needs_null_check,
6123 bool use_load_acquire) {
6124 DCHECK(kEmitCompilerReadBarrier);
6125 DCHECK(kUseBakerReadBarrier);
6126
Vladimir Marko0ecac682018-08-07 10:40:38 +01006127 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6128 // Marking Register) to decide whether we need to enter the slow
6129 // path to mark the reference. Then, in the slow path, check the
6130 // gray bit in the lock word of the reference's holder (`obj`) to
6131 // decide whether to mark `ref` or not.
6132 //
6133 // We use shared thunks for the slow path; shared within the method
6134 // for JIT, across methods for AOT. That thunk checks the holder
6135 // and jumps to the entrypoint if needed. If the holder is not gray,
6136 // it creates a fake dependency and returns to the LDR instruction.
6137 //
6138 // lr = &gray_return_address;
6139 // if (mr) { // Thread::Current()->GetIsGcMarking()
6140 // goto field_thunk<holder_reg, base_reg, use_load_acquire>(lr)
6141 // }
6142 // not_gray_return_address:
6143 // // Original reference load. If the offset is too large to fit
6144 // // into LDR, we use an adjusted base register here.
6145 // HeapReference<mirror::Object> reference = *(obj+offset);
6146 // gray_return_address:
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006147
Vladimir Marko248141f2018-08-10 10:40:07 +01006148 DCHECK(src.GetAddrMode() == vixl::aarch64::Offset);
6149 DCHECK_ALIGNED(src.GetOffset(), sizeof(mirror::HeapReference<mirror::Object>));
6150
6151 UseScratchRegisterScope temps(GetVIXLAssembler());
6152 DCHECK(temps.IsAvailable(ip0));
6153 DCHECK(temps.IsAvailable(ip1));
6154 temps.Exclude(ip0, ip1);
6155 uint32_t custom_data = use_load_acquire
6156 ? EncodeBakerReadBarrierAcquireData(src.GetBaseRegister().GetCode(), obj.GetCode())
6157 : EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode());
6158
6159 {
6160 ExactAssemblyScope guard(GetVIXLAssembler(),
6161 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6162 vixl::aarch64::Label return_address;
6163 __ adr(lr, &return_address);
6164 EmitBakerReadBarrierCbnz(custom_data);
6165 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6166 "Field LDR must be 1 instruction (4B) before the return address label; "
6167 " 2 instructions (8B) for heap poisoning.");
6168 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
6169 if (use_load_acquire) {
6170 DCHECK_EQ(src.GetOffset(), 0);
6171 __ ldar(ref_reg, src);
6172 } else {
6173 __ ldr(ref_reg, src);
6174 }
6175 if (needs_null_check) {
6176 MaybeRecordImplicitNullCheck(instruction);
6177 }
6178 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6179 // macro instructions disallowed in ExactAssemblyScope.
6180 if (kPoisonHeapReferences) {
6181 __ neg(ref_reg, Operand(ref_reg));
6182 }
6183 __ bind(&return_address);
6184 }
6185 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__, /* temp_loc */ LocationFrom(ip1));
6186}
6187
6188void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6189 Location ref,
6190 Register obj,
6191 uint32_t offset,
6192 Location maybe_temp,
6193 bool needs_null_check,
6194 bool use_load_acquire) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006195 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6196 Register base = obj;
6197 if (use_load_acquire) {
6198 DCHECK(maybe_temp.IsRegister());
6199 base = WRegisterFrom(maybe_temp);
6200 __ Add(base, obj, offset);
6201 offset = 0u;
6202 } else if (offset >= kReferenceLoadMinFarOffset) {
6203 DCHECK(maybe_temp.IsRegister());
6204 base = WRegisterFrom(maybe_temp);
6205 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6206 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6207 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006208 }
Vladimir Marko248141f2018-08-10 10:40:07 +01006209 MemOperand src(base.X(), offset);
6210 GenerateFieldLoadWithBakerReadBarrier(
6211 instruction, ref, obj, src, needs_null_check, use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006212}
6213
Vladimir Marko008e09f32018-08-06 15:42:43 +01006214void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006215 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006216 uint32_t data_offset,
6217 Location index,
6218 Register temp,
6219 bool needs_null_check) {
6220 DCHECK(kEmitCompilerReadBarrier);
6221 DCHECK(kUseBakerReadBarrier);
6222
Vladimir Marko66d691d2017-04-07 17:53:39 +01006223 static_assert(
6224 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6225 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006226 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006227
Vladimir Marko008e09f32018-08-06 15:42:43 +01006228 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6229 // Marking Register) to decide whether we need to enter the slow
6230 // path to mark the reference. Then, in the slow path, check the
6231 // gray bit in the lock word of the reference's holder (`obj`) to
6232 // decide whether to mark `ref` or not.
6233 //
6234 // We use shared thunks for the slow path; shared within the method
6235 // for JIT, across methods for AOT. That thunk checks the holder
6236 // and jumps to the entrypoint if needed. If the holder is not gray,
6237 // it creates a fake dependency and returns to the LDR instruction.
6238 //
6239 // lr = &gray_return_address;
6240 // if (mr) { // Thread::Current()->GetIsGcMarking()
6241 // goto array_thunk<base_reg>(lr)
6242 // }
6243 // not_gray_return_address:
6244 // // Original reference load. If the offset is too large to fit
6245 // // into LDR, we use an adjusted base register here.
6246 // HeapReference<mirror::Object> reference = data[index];
6247 // gray_return_address:
Vladimir Marko66d691d2017-04-07 17:53:39 +01006248
Vladimir Marko008e09f32018-08-06 15:42:43 +01006249 DCHECK(index.IsValid());
6250 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
6251 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006252
Vladimir Marko008e09f32018-08-06 15:42:43 +01006253 UseScratchRegisterScope temps(GetVIXLAssembler());
6254 DCHECK(temps.IsAvailable(ip0));
6255 DCHECK(temps.IsAvailable(ip1));
6256 temps.Exclude(ip0, ip1);
6257 uint32_t custom_data = EncodeBakerReadBarrierArrayData(temp.GetCode());
Vladimir Marko66d691d2017-04-07 17:53:39 +01006258
Vladimir Marko008e09f32018-08-06 15:42:43 +01006259 __ Add(temp.X(), obj.X(), Operand(data_offset));
6260 {
6261 ExactAssemblyScope guard(GetVIXLAssembler(),
6262 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6263 vixl::aarch64::Label return_address;
6264 __ adr(lr, &return_address);
6265 EmitBakerReadBarrierCbnz(custom_data);
6266 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6267 "Array LDR must be 1 instruction (4B) before the return address label; "
6268 " 2 instructions (8B) for heap poisoning.");
6269 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6270 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6271 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6272 // macro instructions disallowed in ExactAssemblyScope.
6273 if (kPoisonHeapReferences) {
6274 __ neg(ref_reg, Operand(ref_reg));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006275 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006276 __ bind(&return_address);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006277 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006278 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__, /* temp_loc */ LocationFrom(ip1));
Roland Levillain44015862016-01-22 11:47:17 +00006279}
6280
Roland Levillainff487002017-03-07 16:50:01 +00006281void CodeGeneratorARM64::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
6282 Location ref,
6283 Register obj,
6284 Location field_offset,
6285 Register temp,
6286 bool needs_null_check,
6287 bool use_load_acquire) {
6288 DCHECK(kEmitCompilerReadBarrier);
6289 DCHECK(kUseBakerReadBarrier);
6290 // If we are emitting an array load, we should not be using a
6291 // Load Acquire instruction. In other words:
6292 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6293 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
6294
Roland Levillain97c46462017-05-11 14:04:03 +01006295 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6296 // Marking Register) to decide whether we need to enter the slow
6297 // path to update the reference field within `obj`. Then, in the
6298 // slow path, check the gray bit in the lock word of the reference's
6299 // holder (`obj`) to decide whether to mark `ref` and update the
6300 // field or not.
Roland Levillainff487002017-03-07 16:50:01 +00006301 //
Roland Levillain97c46462017-05-11 14:04:03 +01006302 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainff487002017-03-07 16:50:01 +00006303 // // Slow path.
6304 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6305 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6306 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
6307 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6308 // if (is_gray) {
6309 // old_ref = ref;
Roland Levillain97c46462017-05-11 14:04:03 +01006310 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6311 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00006312 // compareAndSwapObject(obj, field_offset, old_ref, ref);
6313 // }
6314 // }
6315
6316 // Slow path updating the object reference at address `obj + field_offset`
Roland Levillain97c46462017-05-11 14:04:03 +01006317 // when the GC is marking. The entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00006318 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006319 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
Roland Levillainff487002017-03-07 16:50:01 +00006320 instruction,
6321 ref,
6322 obj,
6323 /* offset */ 0u,
6324 /* index */ field_offset,
6325 /* scale_factor */ 0u /* "times 1" */,
6326 needs_null_check,
6327 use_load_acquire,
Roland Levillain97c46462017-05-11 14:04:03 +01006328 temp);
Roland Levillainff487002017-03-07 16:50:01 +00006329 AddSlowPath(slow_path);
6330
Roland Levillain97c46462017-05-11 14:04:03 +01006331 __ Cbnz(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006332 // Fast path: the GC is not marking: nothing to do (the field is
6333 // up-to-date, and we don't need to load the reference).
6334 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006335 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillainff487002017-03-07 16:50:01 +00006336}
6337
Roland Levillainba650a42017-03-06 13:52:32 +00006338void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6339 Location ref,
6340 Register obj,
6341 uint32_t offset,
6342 Location index,
6343 size_t scale_factor,
6344 bool needs_null_check,
6345 bool use_load_acquire) {
6346 DCHECK(obj.IsW());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006347 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006348 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006349
Roland Levillainba650a42017-03-06 13:52:32 +00006350 // If needed, vixl::EmissionCheckScope guards are used to ensure
6351 // that no pools are emitted between the load (macro) instruction
6352 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006353
Roland Levillain44015862016-01-22 11:47:17 +00006354 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006355 // Load types involving an "index": ArrayGet,
6356 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6357 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006358 if (use_load_acquire) {
6359 // UnsafeGetObjectVolatile intrinsic case.
6360 // Register `index` is not an index in an object array, but an
6361 // offset to an object reference field within object `obj`.
6362 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6363 DCHECK(instruction->GetLocations()->Intrinsified());
6364 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6365 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006366 DCHECK_EQ(offset, 0u);
6367 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006368 DCHECK_EQ(needs_null_check, false);
6369 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006370 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6371 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006372 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006373 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6374 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006375 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01006376 uint32_t computed_offset = offset + (Int64FromLocation(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006377 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006378 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006379 if (needs_null_check) {
6380 MaybeRecordImplicitNullCheck(instruction);
6381 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006382 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006383 UseScratchRegisterScope temps(GetVIXLAssembler());
6384 Register temp = temps.AcquireW();
6385 __ Add(temp, obj, offset);
6386 {
6387 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6388 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6389 if (needs_null_check) {
6390 MaybeRecordImplicitNullCheck(instruction);
6391 }
6392 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006393 }
Roland Levillain44015862016-01-22 11:47:17 +00006394 }
Roland Levillain44015862016-01-22 11:47:17 +00006395 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006396 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006397 MemOperand field = HeapOperand(obj, offset);
6398 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006399 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6400 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006401 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006402 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006403 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006404 if (needs_null_check) {
6405 MaybeRecordImplicitNullCheck(instruction);
6406 }
Roland Levillain44015862016-01-22 11:47:17 +00006407 }
6408 }
6409
6410 // Object* ref = ref_addr->AsMirrorPtr()
6411 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006412}
6413
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006414void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6415 // The following condition is a compile-time one, so it does not have a run-time cost.
6416 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
6417 // The following condition is a run-time one; it is executed after the
6418 // previous compile-time test, to avoid penalizing non-debug builds.
6419 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6420 UseScratchRegisterScope temps(GetVIXLAssembler());
6421 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6422 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6423 }
6424 }
6425}
6426
Roland Levillain44015862016-01-22 11:47:17 +00006427void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6428 Location out,
6429 Location ref,
6430 Location obj,
6431 uint32_t offset,
6432 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006433 DCHECK(kEmitCompilerReadBarrier);
6434
Roland Levillain44015862016-01-22 11:47:17 +00006435 // Insert a slow path based read barrier *after* the reference load.
6436 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006437 // If heap poisoning is enabled, the unpoisoning of the loaded
6438 // reference will be carried out by the runtime within the slow
6439 // path.
6440 //
6441 // Note that `ref` currently does not get unpoisoned (when heap
6442 // poisoning is enabled), which is alright as the `ref` argument is
6443 // not used by the artReadBarrierSlow entry point.
6444 //
6445 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006446 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006447 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6448 AddSlowPath(slow_path);
6449
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006450 __ B(slow_path->GetEntryLabel());
6451 __ Bind(slow_path->GetExitLabel());
6452}
6453
Roland Levillain44015862016-01-22 11:47:17 +00006454void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6455 Location out,
6456 Location ref,
6457 Location obj,
6458 uint32_t offset,
6459 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006460 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006461 // Baker's read barriers shall be handled by the fast path
6462 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6463 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006464 // If heap poisoning is enabled, unpoisoning will be taken care of
6465 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006466 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006467 } else if (kPoisonHeapReferences) {
6468 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6469 }
6470}
6471
Roland Levillain44015862016-01-22 11:47:17 +00006472void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6473 Location out,
6474 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006475 DCHECK(kEmitCompilerReadBarrier);
6476
Roland Levillain44015862016-01-22 11:47:17 +00006477 // Insert a slow path based read barrier *after* the GC root load.
6478 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006479 // Note that GC roots are not affected by heap poisoning, so we do
6480 // not need to do anything special for this here.
6481 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006482 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006483 AddSlowPath(slow_path);
6484
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006485 __ B(slow_path->GetEntryLabel());
6486 __ Bind(slow_path->GetExitLabel());
6487}
6488
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006489void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6490 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006491 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006492 locations->SetInAt(0, Location::RequiresRegister());
6493 locations->SetOut(Location::RequiresRegister());
6494}
6495
6496void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6497 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006498 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006499 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006500 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006501 __ Ldr(XRegisterFrom(locations->Out()),
6502 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006503 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006504 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006505 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006506 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6507 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006508 __ Ldr(XRegisterFrom(locations->Out()),
6509 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006510 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006511}
6512
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006513static void PatchJitRootUse(uint8_t* code,
6514 const uint8_t* roots_data,
6515 vixl::aarch64::Literal<uint32_t>* literal,
6516 uint64_t index_in_table) {
6517 uint32_t literal_offset = literal->GetOffset();
6518 uintptr_t address =
6519 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6520 uint8_t* data = code + literal_offset;
6521 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6522}
6523
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006524void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6525 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006526 const StringReference& string_reference = entry.first;
6527 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006528 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006529 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006530 }
6531 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006532 const TypeReference& type_reference = entry.first;
6533 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006534 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006535 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006536 }
6537}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006538
Alexandre Rames67555f72014-11-18 10:55:16 +00006539#undef __
6540#undef QUICK_ENTRY_POINT
6541
Vladimir Markoca1e0382018-04-11 09:58:41 +00006542#define __ assembler.GetVIXLAssembler()->
6543
6544static void EmitGrayCheckAndFastPath(arm64::Arm64Assembler& assembler,
6545 vixl::aarch64::Register base_reg,
6546 vixl::aarch64::MemOperand& lock_word,
Vladimir Marko7a695052018-04-12 10:26:50 +01006547 vixl::aarch64::Label* slow_path,
6548 vixl::aarch64::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006549 // Load the lock word containing the rb_state.
6550 __ Ldr(ip0.W(), lock_word);
6551 // Given the numeric representation, it's enough to check the low bit of the rb_state.
6552 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6553 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6554 __ Tbnz(ip0.W(), LockWord::kReadBarrierStateShift, slow_path);
6555 static_assert(
6556 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET,
6557 "Field and array LDR offsets must be the same to reuse the same code.");
Vladimir Marko7a695052018-04-12 10:26:50 +01006558 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
6559 if (throw_npe != nullptr) {
6560 __ Bind(throw_npe);
6561 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006562 // Adjust the return address back to the LDR (1 instruction; 2 for heap poisoning).
6563 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6564 "Field LDR must be 1 instruction (4B) before the return address label; "
6565 " 2 instructions (8B) for heap poisoning.");
6566 __ Add(lr, lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6567 // Introduce a dependency on the lock_word including rb_state,
6568 // to prevent load-load reordering, and without using
6569 // a memory barrier (which would be more expensive).
6570 __ Add(base_reg, base_reg, Operand(ip0, LSR, 32));
6571 __ Br(lr); // And return back to the function.
6572 // Note: The fake dependency is unnecessary for the slow path.
6573}
6574
6575// Load the read barrier introspection entrypoint in register `entrypoint`.
6576static void LoadReadBarrierMarkIntrospectionEntrypoint(arm64::Arm64Assembler& assembler,
6577 vixl::aarch64::Register entrypoint) {
6578 // entrypoint = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6579 DCHECK_EQ(ip0.GetCode(), 16u);
6580 const int32_t entry_point_offset =
6581 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6582 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
6583}
6584
6585void CodeGeneratorARM64::CompileBakerReadBarrierThunk(Arm64Assembler& assembler,
6586 uint32_t encoded_data,
6587 /*out*/ std::string* debug_name) {
6588 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
6589 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006590 case BakerReadBarrierKind::kField:
6591 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006592 auto base_reg =
6593 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6594 CheckValidReg(base_reg.GetCode());
6595 auto holder_reg =
6596 Register::GetXRegFromCode(BakerReadBarrierSecondRegField::Decode(encoded_data));
6597 CheckValidReg(holder_reg.GetCode());
6598 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6599 temps.Exclude(ip0, ip1);
Vladimir Marko7a695052018-04-12 10:26:50 +01006600 // If base_reg differs from holder_reg, the offset was too large and we must have emitted
6601 // an explicit null check before the load. Otherwise, for implicit null checks, we need to
6602 // null-check the holder as we do not necessarily do that check before going to the thunk.
6603 vixl::aarch64::Label throw_npe_label;
6604 vixl::aarch64::Label* throw_npe = nullptr;
6605 if (GetCompilerOptions().GetImplicitNullChecks() && holder_reg.Is(base_reg)) {
6606 throw_npe = &throw_npe_label;
6607 __ Cbz(holder_reg.W(), throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006608 }
Vladimir Marko7a695052018-04-12 10:26:50 +01006609 // Check if the holder is gray and, if not, add fake dependency to the base register
6610 // and return to the LDR instruction to load the reference. Otherwise, use introspection
6611 // to load the reference and call the entrypoint that performs further checks on the
6612 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006613 vixl::aarch64::Label slow_path;
6614 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
Vladimir Marko7a695052018-04-12 10:26:50 +01006615 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006616 __ Bind(&slow_path);
Vladimir Marko0ecac682018-08-07 10:40:38 +01006617 if (kind == BakerReadBarrierKind::kField) {
6618 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6619 __ Ldr(ip0.W(), ldr_address); // Load the LDR (immediate) unsigned offset.
6620 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6621 __ Ubfx(ip0.W(), ip0.W(), 10, 12); // Extract the offset.
6622 __ Ldr(ip0.W(), MemOperand(base_reg, ip0, LSL, 2)); // Load the reference.
6623 } else {
6624 DCHECK(kind == BakerReadBarrierKind::kAcquire);
6625 DCHECK(!base_reg.Is(holder_reg));
6626 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6627 __ Ldar(ip0.W(), MemOperand(base_reg));
6628 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006629 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
6630 __ Br(ip1); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006631 break;
6632 }
6633 case BakerReadBarrierKind::kArray: {
6634 auto base_reg =
6635 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6636 CheckValidReg(base_reg.GetCode());
6637 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6638 BakerReadBarrierSecondRegField::Decode(encoded_data));
6639 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6640 temps.Exclude(ip0, ip1);
6641 vixl::aarch64::Label slow_path;
6642 int32_t data_offset =
6643 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
6644 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
6645 DCHECK_LT(lock_word.GetOffset(), 0);
6646 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path);
6647 __ Bind(&slow_path);
6648 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
6649 __ Ldr(ip0.W(), ldr_address); // Load the LDR (register) unsigned offset.
6650 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6651 __ Ubfx(ip0, ip0, 16, 6); // Extract the index register, plus 32 (bit 21 is set).
6652 __ Bfi(ip1, ip0, 3, 6); // Insert ip0 to the entrypoint address to create
6653 // a switch case target based on the index register.
6654 __ Mov(ip0, base_reg); // Move the base register to ip0.
6655 __ Br(ip1); // Jump to the entrypoint's array switch case.
6656 break;
6657 }
6658 case BakerReadBarrierKind::kGcRoot: {
6659 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
6660 // and it does not have a forwarding address), call the correct introspection entrypoint;
6661 // otherwise return the reference (or the extracted forwarding address).
6662 // There is no gray bit check for GC roots.
6663 auto root_reg =
6664 Register::GetWRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6665 CheckValidReg(root_reg.GetCode());
6666 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6667 BakerReadBarrierSecondRegField::Decode(encoded_data));
6668 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6669 temps.Exclude(ip0, ip1);
6670 vixl::aarch64::Label return_label, not_marked, forwarding_address;
6671 __ Cbz(root_reg, &return_label);
6672 MemOperand lock_word(root_reg.X(), mirror::Object::MonitorOffset().Int32Value());
6673 __ Ldr(ip0.W(), lock_word);
6674 __ Tbz(ip0.W(), LockWord::kMarkBitStateShift, &not_marked);
6675 __ Bind(&return_label);
6676 __ Br(lr);
6677 __ Bind(&not_marked);
6678 __ Tst(ip0.W(), Operand(ip0.W(), LSL, 1));
6679 __ B(&forwarding_address, mi);
6680 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6681 // Adjust the art_quick_read_barrier_mark_introspection address in IP1 to
6682 // art_quick_read_barrier_mark_introspection_gc_roots.
6683 __ Add(ip1, ip1, Operand(BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRYPOINT_OFFSET));
6684 __ Mov(ip0.W(), root_reg);
6685 __ Br(ip1);
6686 __ Bind(&forwarding_address);
6687 __ Lsl(root_reg, ip0.W(), LockWord::kForwardingAddressShift);
6688 __ Br(lr);
6689 break;
6690 }
6691 default:
6692 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
6693 UNREACHABLE();
6694 }
6695
Vladimir Marko966b46f2018-08-03 10:20:19 +00006696 // For JIT, the slow path is considered part of the compiled method,
6697 // so JIT should pass null as `debug_name`. Tests may not have a runtime.
6698 DCHECK(Runtime::Current() == nullptr ||
6699 !Runtime::Current()->UseJitCompilation() ||
6700 debug_name == nullptr);
6701 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006702 std::ostringstream oss;
6703 oss << "BakerReadBarrierThunk";
6704 switch (kind) {
6705 case BakerReadBarrierKind::kField:
6706 oss << "Field_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
6707 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
6708 break;
Vladimir Marko0ecac682018-08-07 10:40:38 +01006709 case BakerReadBarrierKind::kAcquire:
6710 oss << "Acquire_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
6711 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
6712 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +00006713 case BakerReadBarrierKind::kArray:
6714 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
6715 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6716 BakerReadBarrierSecondRegField::Decode(encoded_data));
6717 break;
6718 case BakerReadBarrierKind::kGcRoot:
6719 oss << "GcRoot_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
6720 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6721 BakerReadBarrierSecondRegField::Decode(encoded_data));
6722 break;
6723 }
6724 *debug_name = oss.str();
6725 }
6726}
6727
6728#undef __
6729
Alexandre Rames5319def2014-10-23 10:03:10 +01006730} // namespace arm64
6731} // namespace art