blob: 6b1180bb137cb58e75d4a735051f44800a123650 [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
Serban Constantinescu579885a2015-02-22 20:51:33 +000019#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080024#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010025#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080026#include "intrinsics.h"
27#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000030#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010031#include "thread.h"
32#include "utils/arm64/assembler_arm64.h"
33#include "utils/assembler.h"
34#include "utils/stack_checks.h"
35
Scott Wakeling97c72b72016-06-24 16:19:36 +010036using namespace vixl::aarch64; // NOLINT(build/namespaces)
Alexandre Rames5319def2014-10-23 10:03:10 +010037
38#ifdef __
39#error "ARM64 Codegen VIXL macro-assembler macro already defined."
40#endif
41
Alexandre Rames5319def2014-10-23 10:03:10 +010042namespace art {
43
Roland Levillain22ccc3a2015-11-24 13:10:05 +000044template<class MirrorType>
45class GcRoot;
46
Alexandre Rames5319def2014-10-23 10:03:10 +010047namespace arm64 {
48
Alexandre Ramesbe919d92016-08-23 18:33:36 +010049using helpers::ARM64EncodableConstantOrRegister;
50using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080051using helpers::CPURegisterFrom;
52using helpers::DRegisterFrom;
53using helpers::FPRegisterFrom;
54using helpers::HeapOperand;
55using helpers::HeapOperandFrom;
56using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010057using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080058using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080059using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010060using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080061using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010062using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080063using helpers::LocationFrom;
64using helpers::OperandFromMemOperand;
65using helpers::OutputCPURegister;
66using helpers::OutputFPRegister;
67using helpers::OutputRegister;
68using helpers::RegisterFrom;
69using helpers::StackOperandFrom;
70using helpers::VIXLRegCodeFromART;
71using helpers::WRegisterFrom;
72using helpers::XRegisterFrom;
73
Alexandre Rames5319def2014-10-23 10:03:10 +010074static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000075// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080076// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
77// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000078static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010079
Alexandre Rames5319def2014-10-23 10:03:10 +010080inline Condition ARM64Condition(IfCondition cond) {
81 switch (cond) {
82 case kCondEQ: return eq;
83 case kCondNE: return ne;
84 case kCondLT: return lt;
85 case kCondLE: return le;
86 case kCondGT: return gt;
87 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070088 case kCondB: return lo;
89 case kCondBE: return ls;
90 case kCondA: return hi;
91 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010092 }
Roland Levillain7f63c522015-07-13 15:54:55 +000093 LOG(FATAL) << "Unreachable";
94 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010095}
96
Vladimir Markod6e069b2016-01-18 11:11:01 +000097inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
98 // The ARM64 condition codes can express all the necessary branches, see the
99 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
100 // There is no dex instruction or HIR that would need the missing conditions
101 // "equal or unordered" or "not equal".
102 switch (cond) {
103 case kCondEQ: return eq;
104 case kCondNE: return ne /* unordered */;
105 case kCondLT: return gt_bias ? cc : lt /* unordered */;
106 case kCondLE: return gt_bias ? ls : le /* unordered */;
107 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
108 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
109 default:
110 LOG(FATAL) << "UNREACHABLE";
111 UNREACHABLE();
112 }
113}
114
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000115Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000116 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
117 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
118 // but we use the exact registers for clarity.
119 if (return_type == Primitive::kPrimFloat) {
120 return LocationFrom(s0);
121 } else if (return_type == Primitive::kPrimDouble) {
122 return LocationFrom(d0);
123 } else if (return_type == Primitive::kPrimLong) {
124 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100125 } else if (return_type == Primitive::kPrimVoid) {
126 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000127 } else {
128 return LocationFrom(w0);
129 }
130}
131
Alexandre Rames5319def2014-10-23 10:03:10 +0100132Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000133 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100134}
135
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100136// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
137#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700138#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100139
Zheng Xuda403092015-04-24 17:35:39 +0800140// Calculate memory accessing operand for save/restore live registers.
141static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100142 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800143 int64_t spill_offset,
144 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100145 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
146 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
147 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800148 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100149 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800150 codegen->GetNumberOfFloatingPointRegisters()));
151
Vladimir Marko804b03f2016-09-14 16:26:36 +0100152 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
153 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800154
155 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
156 UseScratchRegisterScope temps(masm);
157
158 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100159 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
160 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800161 int64_t reg_size = kXRegSizeInBytes;
162 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
163 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100164 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800165 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
166 // If the offset does not fit in the instruction's immediate field, use an alternate register
167 // to compute the base address(float point registers spill base address).
168 Register new_base = temps.AcquireSameSizeAs(base);
169 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
170 base = new_base;
171 spill_offset = -core_spill_size;
172 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
173 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
174 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
175 }
176
177 if (is_save) {
178 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
179 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
180 } else {
181 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
182 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
183 }
184}
185
186void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800187 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100188 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
189 for (uint32_t i : LowToHighBits(core_spills)) {
190 // If the register holds an object, update the stack mask.
191 if (locations->RegisterContainsObject(i)) {
192 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800193 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100194 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
195 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
196 saved_core_stack_offsets_[i] = stack_offset;
197 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800198 }
199
Vladimir Marko804b03f2016-09-14 16:26:36 +0100200 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
201 for (uint32_t i : LowToHighBits(fp_spills)) {
202 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
203 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
204 saved_fpu_stack_offsets_[i] = stack_offset;
205 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800206 }
207
Vladimir Marko804b03f2016-09-14 16:26:36 +0100208 SaveRestoreLiveRegistersHelper(codegen,
209 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800210 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
211}
212
213void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100214 SaveRestoreLiveRegistersHelper(codegen,
215 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800216 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
217}
218
Alexandre Rames5319def2014-10-23 10:03:10 +0100219class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
220 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000221 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100222
Alexandre Rames67555f72014-11-18 10:55:16 +0000223 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100224 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000225 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100226
Alexandre Rames5319def2014-10-23 10:03:10 +0100227 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000228 if (instruction_->CanThrowIntoCatchBlock()) {
229 // Live registers will be restored in the catch block if caught.
230 SaveLiveRegisters(codegen, instruction_->GetLocations());
231 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000232 // We're moving two locations to locations that could overlap, so we need a parallel
233 // move resolver.
234 InvokeRuntimeCallingConvention calling_convention;
235 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100236 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
237 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000238 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
239 ? kQuickThrowStringBounds
240 : kQuickThrowArrayBounds;
241 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100242 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800243 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100244 }
245
Alexandre Rames8158f282015-08-07 10:26:17 +0100246 bool IsFatal() const OVERRIDE { return true; }
247
Alexandre Rames9931f312015-06-19 14:47:01 +0100248 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
249
Alexandre Rames5319def2014-10-23 10:03:10 +0100250 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100251 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
252};
253
Alexandre Rames67555f72014-11-18 10:55:16 +0000254class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
255 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000256 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000257
258 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
259 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
260 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000261 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800262 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000263 }
264
Alexandre Rames8158f282015-08-07 10:26:17 +0100265 bool IsFatal() const OVERRIDE { return true; }
266
Alexandre Rames9931f312015-06-19 14:47:01 +0100267 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
268
Alexandre Rames67555f72014-11-18 10:55:16 +0000269 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000270 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
271};
272
273class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
274 public:
275 LoadClassSlowPathARM64(HLoadClass* cls,
276 HInstruction* at,
277 uint32_t dex_pc,
278 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000279 : SlowPathCodeARM64(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000280 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
281 }
282
283 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000284 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000285 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
286
287 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000288 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000289
290 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000291 dex::TypeIndex type_index = cls_->GetTypeIndex();
292 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000293 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
294 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000295 arm64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800296 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100297 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800298 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100299 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800300 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000301
302 // Move the class to the desired location.
303 Location out = locations->Out();
304 if (out.IsValid()) {
305 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000306 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000307 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000308 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000309 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000310 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
311 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
312 if (cls_ == instruction_ && cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
313 DCHECK(out.IsValid());
314 UseScratchRegisterScope temps(arm64_codegen->GetVIXLAssembler());
315 Register temp = temps.AcquireX();
316 const DexFile& dex_file = cls_->GetDexFile();
317 // TODO: Change art_quick_initialize_type/art_quick_initialize_static_storage to
318 // kSaveEverything and use a temporary for the ADRP in the fast path, so that we
319 // can avoid the ADRP here.
320 vixl::aarch64::Label* adrp_label =
321 arm64_codegen->NewPcRelativeTypePatch(dex_file, type_index);
322 arm64_codegen->EmitAdrpPlaceholder(adrp_label, temp);
323 vixl::aarch64::Label* strp_label =
324 arm64_codegen->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
325 {
326 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
327 __ Bind(strp_label);
328 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
329 MemOperand(temp, /* offset placeholder */ 0));
330 }
331 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000332 __ B(GetExitLabel());
333 }
334
Alexandre Rames9931f312015-06-19 14:47:01 +0100335 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
336
Alexandre Rames67555f72014-11-18 10:55:16 +0000337 private:
338 // The class this slow path will load.
339 HLoadClass* const cls_;
340
Alexandre Rames67555f72014-11-18 10:55:16 +0000341 // The dex PC of `at_`.
342 const uint32_t dex_pc_;
343
344 // Whether to initialize the class.
345 const bool do_clinit_;
346
347 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
348};
349
Vladimir Markoaad75c62016-10-03 08:46:48 +0000350class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
351 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100352 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
353 : SlowPathCodeARM64(instruction),
354 temp_(temp),
355 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000356
357 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
358 LocationSummary* locations = instruction_->GetLocations();
359 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
360 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
361
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100362 // temp_ is a scratch register. Make sure it's not used for saving/restoring registers.
363 UseScratchRegisterScope temps(arm64_codegen->GetVIXLAssembler());
364 temps.Exclude(temp_);
365
Vladimir Markoaad75c62016-10-03 08:46:48 +0000366 __ Bind(GetEntryLabel());
367 SaveLiveRegisters(codegen, locations);
368
369 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000370 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
371 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000372 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
373 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
374 Primitive::Type type = instruction_->GetType();
375 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
376
377 RestoreLiveRegisters(codegen, locations);
378
379 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000380 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100381 if (!kUseReadBarrier || kUseBakerReadBarrier) {
382 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
383 } else {
384 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
385 adrp_label_ = arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
386 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
387 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000388 vixl::aarch64::Label* strp_label =
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100389 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000390 {
391 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
392 __ Bind(strp_label);
393 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100394 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000395 }
396
397 __ B(GetExitLabel());
398 }
399
400 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
401
402 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100403 const Register temp_;
404 vixl::aarch64::Label* adrp_label_;
405
Vladimir Markoaad75c62016-10-03 08:46:48 +0000406 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
407};
408
Alexandre Rames5319def2014-10-23 10:03:10 +0100409class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
410 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000411 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100412
Alexandre Rames67555f72014-11-18 10:55:16 +0000413 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
414 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100415 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000416 if (instruction_->CanThrowIntoCatchBlock()) {
417 // Live registers will be restored in the catch block if caught.
418 SaveLiveRegisters(codegen, instruction_->GetLocations());
419 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000420 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
421 instruction_,
422 instruction_->GetDexPc(),
423 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800424 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100425 }
426
Alexandre Rames8158f282015-08-07 10:26:17 +0100427 bool IsFatal() const OVERRIDE { return true; }
428
Alexandre Rames9931f312015-06-19 14:47:01 +0100429 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
430
Alexandre Rames5319def2014-10-23 10:03:10 +0100431 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100432 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
433};
434
435class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
436 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100437 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000438 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100439
Alexandre Rames67555f72014-11-18 10:55:16 +0000440 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
441 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100442 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000443 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800444 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000445 if (successor_ == nullptr) {
446 __ B(GetReturnLabel());
447 } else {
448 __ B(arm64_codegen->GetLabelOf(successor_));
449 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100450 }
451
Scott Wakeling97c72b72016-06-24 16:19:36 +0100452 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100453 DCHECK(successor_ == nullptr);
454 return &return_label_;
455 }
456
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100457 HBasicBlock* GetSuccessor() const {
458 return successor_;
459 }
460
Alexandre Rames9931f312015-06-19 14:47:01 +0100461 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
462
Alexandre Rames5319def2014-10-23 10:03:10 +0100463 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100464 // If not null, the block to branch to after the suspend check.
465 HBasicBlock* const successor_;
466
467 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100468 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100469
470 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
471};
472
Alexandre Rames67555f72014-11-18 10:55:16 +0000473class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
474 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000475 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000476 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000477
478 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000479 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800480
Alexandre Rames3e69f162014-12-10 10:36:50 +0000481 DCHECK(instruction_->IsCheckCast()
482 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
483 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100484 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000485
Alexandre Rames67555f72014-11-18 10:55:16 +0000486 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000487
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000488 if (!is_fatal_) {
489 SaveLiveRegisters(codegen, locations);
490 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000491
492 // We're moving two locations to locations that could overlap, so we need a parallel
493 // move resolver.
494 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800495 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800496 LocationFrom(calling_convention.GetRegisterAt(0)),
497 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800498 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800499 LocationFrom(calling_convention.GetRegisterAt(1)),
500 Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000501 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000502 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800503 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000504 Primitive::Type ret_type = instruction_->GetType();
505 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
506 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
507 } else {
508 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800509 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
510 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000511 }
512
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000513 if (!is_fatal_) {
514 RestoreLiveRegisters(codegen, locations);
515 __ B(GetExitLabel());
516 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000517 }
518
Alexandre Rames9931f312015-06-19 14:47:01 +0100519 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100520 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100521
Alexandre Rames67555f72014-11-18 10:55:16 +0000522 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000523 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000524
Alexandre Rames67555f72014-11-18 10:55:16 +0000525 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
526};
527
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700528class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
529 public:
Aart Bik42249c32016-01-07 15:33:50 -0800530 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000531 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700532
533 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800534 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700535 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000536 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000537 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700538 }
539
Alexandre Rames9931f312015-06-19 14:47:01 +0100540 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
541
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700542 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700543 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
544};
545
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100546class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
547 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000548 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100549
550 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
551 LocationSummary* locations = instruction_->GetLocations();
552 __ Bind(GetEntryLabel());
553 SaveLiveRegisters(codegen, locations);
554
555 InvokeRuntimeCallingConvention calling_convention;
556 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
557 parallel_move.AddMove(
558 locations->InAt(0),
559 LocationFrom(calling_convention.GetRegisterAt(0)),
560 Primitive::kPrimNot,
561 nullptr);
562 parallel_move.AddMove(
563 locations->InAt(1),
564 LocationFrom(calling_convention.GetRegisterAt(1)),
565 Primitive::kPrimInt,
566 nullptr);
567 parallel_move.AddMove(
568 locations->InAt(2),
569 LocationFrom(calling_convention.GetRegisterAt(2)),
570 Primitive::kPrimNot,
571 nullptr);
572 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
573
574 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000575 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100576 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
577 RestoreLiveRegisters(codegen, locations);
578 __ B(GetExitLabel());
579 }
580
581 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
582
583 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100584 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
585};
586
Zheng Xu3927c8b2015-11-18 17:46:25 +0800587void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
588 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000589 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800590
591 // We are about to use the assembler to place literals directly. Make sure we have enough
592 // underlying code buffer and we have generated the jump table with right size.
Scott Wakelingb77051e2016-11-21 19:46:00 +0000593 vixl::CodeBufferCheckScope scope(codegen->GetVIXLAssembler(),
594 num_entries * sizeof(int32_t),
595 vixl::CodeBufferCheckScope::kReserveBufferSpace,
596 vixl::CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800597
598 __ Bind(&table_start_);
599 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
600 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100601 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800602 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100603 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800604 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
605 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
606 Literal<int32_t> literal(jump_offset);
607 __ place(&literal);
608 }
609}
610
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100611// Slow path marking an object reference `ref` during a read
612// barrier. The field `obj.field` in the object `obj` holding this
613// reference does not get updated by this slow path after marking (see
614// ReadBarrierMarkAndUpdateFieldSlowPathARM64 below for that).
615//
616// This means that after the execution of this slow path, `ref` will
617// always be up-to-date, but `obj.field` may not; i.e., after the
618// flip, `ref` will be a to-space reference, but `obj.field` will
619// probably still be a from-space reference (unless it gets updated by
620// another thread, or if another thread installed another object
621// reference (different from `ref`) in `obj.field`).
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800622// If entrypoint is a valid location it is assumed to already be holding the entrypoint. The case
623// where the entrypoint is passed in is for the GcRoot read barrier.
Roland Levillain44015862016-01-22 11:47:17 +0000624class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
625 public:
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800626 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
627 Location ref,
628 Location entrypoint = Location::NoLocation())
629 : SlowPathCodeARM64(instruction),
630 ref_(ref),
631 entrypoint_(entrypoint) {
Roland Levillain44015862016-01-22 11:47:17 +0000632 DCHECK(kEmitCompilerReadBarrier);
633 }
634
635 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
636
637 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
638 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000639 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100640 DCHECK(ref_.IsRegister()) << ref_;
641 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain44015862016-01-22 11:47:17 +0000642 DCHECK(instruction_->IsInstanceFieldGet() ||
643 instruction_->IsStaticFieldGet() ||
644 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100645 instruction_->IsArraySet() ||
Roland Levillain44015862016-01-22 11:47:17 +0000646 instruction_->IsLoadClass() ||
647 instruction_->IsLoadString() ||
648 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100649 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100650 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
651 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000652 << "Unexpected instruction in read barrier marking slow path: "
653 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000654 // The read barrier instrumentation of object ArrayGet
655 // instructions does not support the HIntermediateAddress
656 // instruction.
657 DCHECK(!(instruction_->IsArrayGet() &&
658 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain44015862016-01-22 11:47:17 +0000659
660 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100661 // No need to save live registers; it's taken care of by the
662 // entrypoint. Also, there is no need to update the stack mask,
663 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000664 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100665 DCHECK_NE(ref_.reg(), LR);
666 DCHECK_NE(ref_.reg(), WSP);
667 DCHECK_NE(ref_.reg(), WZR);
Roland Levillain0b671c02016-08-19 12:02:34 +0100668 // IP0 is used internally by the ReadBarrierMarkRegX entry point
669 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100670 DCHECK_NE(ref_.reg(), IP0);
671 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
Roland Levillain02b75802016-07-13 11:54:35 +0100672 // "Compact" slow path, saving two moves.
673 //
674 // Instead of using the standard runtime calling convention (input
675 // and output in W0):
676 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100677 // W0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100678 // W0 <- ReadBarrierMark(W0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100679 // ref <- W0
Roland Levillain02b75802016-07-13 11:54:35 +0100680 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100681 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100682 // of a dedicated entrypoint:
683 //
684 // rX <- ReadBarrierMarkRegX(rX)
685 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800686 if (entrypoint_.IsValid()) {
687 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
688 __ Blr(XRegisterFrom(entrypoint_));
689 } else {
690 // Entrypoint is not already loaded, load from the thread.
691 int32_t entry_point_offset =
692 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
693 // This runtime call does not require a stack map.
694 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
695 }
Roland Levillain44015862016-01-22 11:47:17 +0000696 __ B(GetExitLabel());
697 }
698
699 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100700 // The location (register) of the marked object reference.
701 const Location ref_;
Roland Levillain44015862016-01-22 11:47:17 +0000702
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800703 // The location of the entrypoint if it is already loaded.
704 const Location entrypoint_;
705
Roland Levillain44015862016-01-22 11:47:17 +0000706 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
707};
708
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100709// Slow path marking an object reference `ref` during a read barrier,
710// and if needed, atomically updating the field `obj.field` in the
711// object `obj` holding this reference after marking (contrary to
712// ReadBarrierMarkSlowPathARM64 above, which never tries to update
713// `obj.field`).
714//
715// This means that after the execution of this slow path, both `ref`
716// and `obj.field` will be up-to-date; i.e., after the flip, both will
717// hold the same to-space reference (unless another thread installed
718// another object reference (different from `ref`) in `obj.field`).
719class ReadBarrierMarkAndUpdateFieldSlowPathARM64 : public SlowPathCodeARM64 {
720 public:
721 ReadBarrierMarkAndUpdateFieldSlowPathARM64(HInstruction* instruction,
722 Location ref,
723 Register obj,
724 Location field_offset,
725 Register temp)
726 : SlowPathCodeARM64(instruction),
727 ref_(ref),
728 obj_(obj),
729 field_offset_(field_offset),
730 temp_(temp) {
731 DCHECK(kEmitCompilerReadBarrier);
732 }
733
734 const char* GetDescription() const OVERRIDE {
735 return "ReadBarrierMarkAndUpdateFieldSlowPathARM64";
736 }
737
738 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
739 LocationSummary* locations = instruction_->GetLocations();
740 Register ref_reg = WRegisterFrom(ref_);
741 DCHECK(locations->CanCall());
742 DCHECK(ref_.IsRegister()) << ref_;
743 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
744 // This slow path is only used by the UnsafeCASObject intrinsic.
745 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
746 << "Unexpected instruction in read barrier marking and field updating slow path: "
747 << instruction_->DebugName();
748 DCHECK(instruction_->GetLocations()->Intrinsified());
749 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
750 DCHECK(field_offset_.IsRegister()) << field_offset_;
751
752 __ Bind(GetEntryLabel());
753
754 // Save the old reference.
755 // Note that we cannot use IP to save the old reference, as IP is
756 // used internally by the ReadBarrierMarkRegX entry point, and we
757 // need the old reference after the call to that entry point.
758 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
759 __ Mov(temp_.W(), ref_reg);
760
761 // No need to save live registers; it's taken care of by the
762 // entrypoint. Also, there is no need to update the stack mask,
763 // as this runtime call will not trigger a garbage collection.
764 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
765 DCHECK_NE(ref_.reg(), LR);
766 DCHECK_NE(ref_.reg(), WSP);
767 DCHECK_NE(ref_.reg(), WZR);
768 // IP0 is used internally by the ReadBarrierMarkRegX entry point
769 // as a temporary, it cannot be the entry point's input/output.
770 DCHECK_NE(ref_.reg(), IP0);
771 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
772 // "Compact" slow path, saving two moves.
773 //
774 // Instead of using the standard runtime calling convention (input
775 // and output in W0):
776 //
777 // W0 <- ref
778 // W0 <- ReadBarrierMark(W0)
779 // ref <- W0
780 //
781 // we just use rX (the register containing `ref`) as input and output
782 // of a dedicated entrypoint:
783 //
784 // rX <- ReadBarrierMarkRegX(rX)
785 //
786 int32_t entry_point_offset =
787 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
788 // This runtime call does not require a stack map.
789 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
790
791 // If the new reference is different from the old reference,
792 // update the field in the holder (`*(obj_ + field_offset_)`).
793 //
794 // Note that this field could also hold a different object, if
795 // another thread had concurrently changed it. In that case, the
796 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
797 // (CAS) operation below would abort the CAS, leaving the field
798 // as-is.
799 vixl::aarch64::Label done;
800 __ Cmp(temp_.W(), ref_reg);
801 __ B(eq, &done);
802
803 // Update the the holder's field atomically. This may fail if
804 // mutator updates before us, but it's OK. This is achieved
805 // using a strong compare-and-set (CAS) operation with relaxed
806 // memory synchronization ordering, where the expected value is
807 // the old reference and the desired value is the new reference.
808
809 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
810 UseScratchRegisterScope temps(masm);
811
812 // Convenience aliases.
813 Register base = obj_.W();
814 Register offset = XRegisterFrom(field_offset_);
815 Register expected = temp_.W();
816 Register value = ref_reg;
817 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
818 Register tmp_value = temps.AcquireW(); // Value in memory.
819
820 __ Add(tmp_ptr, base.X(), Operand(offset));
821
822 if (kPoisonHeapReferences) {
823 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
824 if (value.Is(expected)) {
825 // Do not poison `value`, as it is the same register as
826 // `expected`, which has just been poisoned.
827 } else {
828 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
829 }
830 }
831
832 // do {
833 // tmp_value = [tmp_ptr] - expected;
834 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
835
Roland Levillain24a4d112016-10-26 13:10:46 +0100836 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100837 __ Bind(&loop_head);
838 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
839 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +0100840 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100841 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
842 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +0100843 __ B(&exit_loop);
844 __ Bind(&comparison_failed);
845 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100846 __ Bind(&exit_loop);
847
848 if (kPoisonHeapReferences) {
849 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
850 if (value.Is(expected)) {
851 // Do not unpoison `value`, as it is the same register as
852 // `expected`, which has just been unpoisoned.
853 } else {
854 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
855 }
856 }
857
858 __ Bind(&done);
859 __ B(GetExitLabel());
860 }
861
862 private:
863 // The location (register) of the marked object reference.
864 const Location ref_;
865 // The register containing the object holding the marked object reference field.
866 const Register obj_;
867 // The location of the offset of the marked reference field within `obj_`.
868 Location field_offset_;
869
870 const Register temp_;
871
872 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM64);
873};
874
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000875// Slow path generating a read barrier for a heap reference.
876class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
877 public:
878 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
879 Location out,
880 Location ref,
881 Location obj,
882 uint32_t offset,
883 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000884 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000885 out_(out),
886 ref_(ref),
887 obj_(obj),
888 offset_(offset),
889 index_(index) {
890 DCHECK(kEmitCompilerReadBarrier);
891 // If `obj` is equal to `out` or `ref`, it means the initial object
892 // has been overwritten by (or after) the heap object reference load
893 // to be instrumented, e.g.:
894 //
895 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000896 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000897 //
898 // In that case, we have lost the information about the original
899 // object, and the emitted read barrier cannot work properly.
900 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
901 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
902 }
903
904 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
905 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
906 LocationSummary* locations = instruction_->GetLocations();
907 Primitive::Type type = Primitive::kPrimNot;
908 DCHECK(locations->CanCall());
909 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100910 DCHECK(instruction_->IsInstanceFieldGet() ||
911 instruction_->IsStaticFieldGet() ||
912 instruction_->IsArrayGet() ||
913 instruction_->IsInstanceOf() ||
914 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100915 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000916 << "Unexpected instruction in read barrier for heap reference slow path: "
917 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000918 // The read barrier instrumentation of object ArrayGet
919 // instructions does not support the HIntermediateAddress
920 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000921 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100922 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000923
924 __ Bind(GetEntryLabel());
925
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000926 SaveLiveRegisters(codegen, locations);
927
928 // We may have to change the index's value, but as `index_` is a
929 // constant member (like other "inputs" of this slow path),
930 // introduce a copy of it, `index`.
931 Location index = index_;
932 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100933 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000934 if (instruction_->IsArrayGet()) {
935 // Compute the actual memory offset and store it in `index`.
936 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
937 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
938 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
939 // We are about to change the value of `index_reg` (see the
940 // calls to vixl::MacroAssembler::Lsl and
941 // vixl::MacroAssembler::Mov below), but it has
942 // not been saved by the previous call to
943 // art::SlowPathCode::SaveLiveRegisters, as it is a
944 // callee-save register --
945 // art::SlowPathCode::SaveLiveRegisters does not consider
946 // callee-save registers, as it has been designed with the
947 // assumption that callee-save registers are supposed to be
948 // handled by the called function. So, as a callee-save
949 // register, `index_reg` _would_ eventually be saved onto
950 // the stack, but it would be too late: we would have
951 // changed its value earlier. Therefore, we manually save
952 // it here into another freely available register,
953 // `free_reg`, chosen of course among the caller-save
954 // registers (as a callee-save `free_reg` register would
955 // exhibit the same problem).
956 //
957 // Note we could have requested a temporary register from
958 // the register allocator instead; but we prefer not to, as
959 // this is a slow path, and we know we can find a
960 // caller-save register that is available.
961 Register free_reg = FindAvailableCallerSaveRegister(codegen);
962 __ Mov(free_reg.W(), index_reg);
963 index_reg = free_reg;
964 index = LocationFrom(index_reg);
965 } else {
966 // The initial register stored in `index_` has already been
967 // saved in the call to art::SlowPathCode::SaveLiveRegisters
968 // (as it is not a callee-save register), so we can freely
969 // use it.
970 }
971 // Shifting the index value contained in `index_reg` by the scale
972 // factor (2) cannot overflow in practice, as the runtime is
973 // unable to allocate object arrays with a size larger than
974 // 2^26 - 1 (that is, 2^28 - 4 bytes).
975 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
976 static_assert(
977 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
978 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
979 __ Add(index_reg, index_reg, Operand(offset_));
980 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100981 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
982 // intrinsics, `index_` is not shifted by a scale factor of 2
983 // (as in the case of ArrayGet), as it is actually an offset
984 // to an object field within an object.
985 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000986 DCHECK(instruction_->GetLocations()->Intrinsified());
987 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
988 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
989 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100990 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100991 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000992 }
993 }
994
995 // We're moving two or three locations to locations that could
996 // overlap, so we need a parallel move resolver.
997 InvokeRuntimeCallingConvention calling_convention;
998 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
999 parallel_move.AddMove(ref_,
1000 LocationFrom(calling_convention.GetRegisterAt(0)),
1001 type,
1002 nullptr);
1003 parallel_move.AddMove(obj_,
1004 LocationFrom(calling_convention.GetRegisterAt(1)),
1005 type,
1006 nullptr);
1007 if (index.IsValid()) {
1008 parallel_move.AddMove(index,
1009 LocationFrom(calling_convention.GetRegisterAt(2)),
1010 Primitive::kPrimInt,
1011 nullptr);
1012 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1013 } else {
1014 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1015 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1016 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001017 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001018 instruction_,
1019 instruction_->GetDexPc(),
1020 this);
1021 CheckEntrypointTypes<
1022 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1023 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1024
1025 RestoreLiveRegisters(codegen, locations);
1026
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001027 __ B(GetExitLabel());
1028 }
1029
1030 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1031
1032 private:
1033 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001034 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1035 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001036 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1037 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1038 return Register(VIXLRegCodeFromART(i), kXRegSize);
1039 }
1040 }
1041 // We shall never fail to find a free caller-save register, as
1042 // there are more than two core caller-save registers on ARM64
1043 // (meaning it is possible to find one which is different from
1044 // `ref` and `obj`).
1045 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1046 LOG(FATAL) << "Could not find a free register";
1047 UNREACHABLE();
1048 }
1049
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001050 const Location out_;
1051 const Location ref_;
1052 const Location obj_;
1053 const uint32_t offset_;
1054 // An additional location containing an index to an array.
1055 // Only used for HArrayGet and the UnsafeGetObject &
1056 // UnsafeGetObjectVolatile intrinsics.
1057 const Location index_;
1058
1059 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1060};
1061
1062// Slow path generating a read barrier for a GC root.
1063class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1064 public:
1065 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001066 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001067 DCHECK(kEmitCompilerReadBarrier);
1068 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001069
1070 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1071 LocationSummary* locations = instruction_->GetLocations();
1072 Primitive::Type type = Primitive::kPrimNot;
1073 DCHECK(locations->CanCall());
1074 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001075 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1076 << "Unexpected instruction in read barrier for GC root slow path: "
1077 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001078
1079 __ Bind(GetEntryLabel());
1080 SaveLiveRegisters(codegen, locations);
1081
1082 InvokeRuntimeCallingConvention calling_convention;
1083 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1084 // The argument of the ReadBarrierForRootSlow is not a managed
1085 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1086 // thus we need a 64-bit move here, and we cannot use
1087 //
1088 // arm64_codegen->MoveLocation(
1089 // LocationFrom(calling_convention.GetRegisterAt(0)),
1090 // root_,
1091 // type);
1092 //
1093 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1094 // reference type (`Primitive::kPrimNot`).
1095 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001096 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001097 instruction_,
1098 instruction_->GetDexPc(),
1099 this);
1100 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1101 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1102
1103 RestoreLiveRegisters(codegen, locations);
1104 __ B(GetExitLabel());
1105 }
1106
1107 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1108
1109 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001110 const Location out_;
1111 const Location root_;
1112
1113 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1114};
1115
Alexandre Rames5319def2014-10-23 10:03:10 +01001116#undef __
1117
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001118Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001119 Location next_location;
1120 if (type == Primitive::kPrimVoid) {
1121 LOG(FATAL) << "Unreachable type " << type;
1122 }
1123
Alexandre Rames542361f2015-01-29 16:57:31 +00001124 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001125 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
1126 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +00001127 } else if (!Primitive::IsFloatingPointType(type) &&
1128 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001129 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1130 } else {
1131 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001132 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1133 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001134 }
1135
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001136 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001137 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001138 return next_location;
1139}
1140
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001141Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001142 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001143}
1144
Serban Constantinescu579885a2015-02-22 20:51:33 +00001145CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1146 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001147 const CompilerOptions& compiler_options,
1148 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001149 : CodeGenerator(graph,
1150 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001151 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001152 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001153 callee_saved_core_registers.GetList(),
1154 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001155 compiler_options,
1156 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001157 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001158 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001159 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001160 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001161 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001162 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001163 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001164 uint32_literals_(std::less<uint32_t>(),
1165 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001166 uint64_literals_(std::less<uint64_t>(),
1167 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001168 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1169 boot_image_string_patches_(StringReferenceValueComparator(),
1170 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1171 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001172 boot_image_type_patches_(TypeReferenceValueComparator(),
1173 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1174 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001175 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001176 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1177 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001178 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1179 jit_class_patches_(TypeReferenceValueComparator(),
1180 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001181 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001182 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001183}
Alexandre Rames5319def2014-10-23 10:03:10 +01001184
Alexandre Rames67555f72014-11-18 10:55:16 +00001185#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001186
Zheng Xu3927c8b2015-11-18 17:46:25 +08001187void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001188 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001189 jump_table->EmitTable(this);
1190 }
1191}
1192
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001193void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001194 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001195 // Ensure we emit the literal pool.
1196 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001197
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001198 CodeGenerator::Finalize(allocator);
1199}
1200
Zheng Xuad4450e2015-04-17 18:48:56 +08001201void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1202 // Note: There are 6 kinds of moves:
1203 // 1. constant -> GPR/FPR (non-cycle)
1204 // 2. constant -> stack (non-cycle)
1205 // 3. GPR/FPR -> GPR/FPR
1206 // 4. GPR/FPR -> stack
1207 // 5. stack -> GPR/FPR
1208 // 6. stack -> stack (non-cycle)
1209 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1210 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1211 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1212 // dependency.
1213 vixl_temps_.Open(GetVIXLAssembler());
1214}
1215
1216void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1217 vixl_temps_.Close();
1218}
1219
1220Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
1221 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
1222 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
1223 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
1224 Location scratch = GetScratchLocation(kind);
1225 if (!scratch.Equals(Location::NoLocation())) {
1226 return scratch;
1227 }
1228 // Allocate from VIXL temp registers.
1229 if (kind == Location::kRegister) {
1230 scratch = LocationFrom(vixl_temps_.AcquireX());
1231 } else {
1232 DCHECK(kind == Location::kFpuRegister);
1233 scratch = LocationFrom(vixl_temps_.AcquireD());
1234 }
1235 AddScratchLocation(scratch);
1236 return scratch;
1237}
1238
1239void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1240 if (loc.IsRegister()) {
1241 vixl_temps_.Release(XRegisterFrom(loc));
1242 } else {
1243 DCHECK(loc.IsFpuRegister());
1244 vixl_temps_.Release(DRegisterFrom(loc));
1245 }
1246 RemoveScratchLocation(loc);
1247}
1248
Alexandre Rames3e69f162014-12-10 10:36:50 +00001249void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001250 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001251 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001252}
1253
Alexandre Rames5319def2014-10-23 10:03:10 +01001254void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001255 MacroAssembler* masm = GetVIXLAssembler();
1256 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001257 __ Bind(&frame_entry_label_);
1258
Serban Constantinescu02164b32014-11-13 14:05:07 +00001259 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1260 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001261 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001262 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001263 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001264 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001265 __ Ldr(wzr, MemOperand(temp, 0));
1266 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001267 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001268
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001269 if (!HasEmptyFrame()) {
1270 int frame_size = GetFrameSize();
1271 // Stack layout:
1272 // sp[frame_size - 8] : lr.
1273 // ... : other preserved core registers.
1274 // ... : other preserved fp registers.
1275 // ... : reserved frame space.
1276 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001277
1278 // Save the current method if we need it. Note that we do not
1279 // do this in HCurrentMethod, as the instruction might have been removed
1280 // in the SSA graph.
1281 if (RequiresCurrentMethod()) {
1282 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001283 } else {
1284 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001285 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001286 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001287 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1288 frame_size - GetCoreSpillSize());
1289 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1290 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001291
1292 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1293 // Initialize should_deoptimize flag to 0.
1294 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1295 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1296 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001297 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001298}
1299
1300void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001301 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001302 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001303 if (!HasEmptyFrame()) {
1304 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001305 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1306 frame_size - FrameEntrySpillSize());
1307 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1308 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001309 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001310 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001311 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001312 __ Ret();
1313 GetAssembler()->cfi().RestoreState();
1314 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001315}
1316
Scott Wakeling97c72b72016-06-24 16:19:36 +01001317CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001318 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001319 return CPURegList(CPURegister::kRegister, kXRegSize,
1320 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001321}
1322
Scott Wakeling97c72b72016-06-24 16:19:36 +01001323CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001324 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1325 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001326 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1327 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001328}
1329
Alexandre Rames5319def2014-10-23 10:03:10 +01001330void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1331 __ Bind(GetLabelOf(block));
1332}
1333
Calin Juravle175dc732015-08-25 15:42:32 +01001334void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1335 DCHECK(location.IsRegister());
1336 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1337}
1338
Calin Juravlee460d1d2015-09-29 04:52:17 +01001339void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1340 if (location.IsRegister()) {
1341 locations->AddTemp(location);
1342 } else {
1343 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1344 }
1345}
1346
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001347void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001348 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001349 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001350 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001351 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001352 if (value_can_be_null) {
1353 __ Cbz(value, &done);
1354 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001355 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001356 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001357 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001358 if (value_can_be_null) {
1359 __ Bind(&done);
1360 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001361}
1362
David Brazdil58282f42016-01-14 12:45:10 +00001363void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001364 // Blocked core registers:
1365 // lr : Runtime reserved.
1366 // tr : Runtime reserved.
1367 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1368 // ip1 : VIXL core temp.
1369 // ip0 : VIXL core temp.
1370 //
1371 // Blocked fp registers:
1372 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001373 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1374 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001375 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001376 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001377 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001378
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001379 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001380 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001381 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001382 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001383
David Brazdil58282f42016-01-14 12:45:10 +00001384 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001385 // Stubs do not save callee-save floating point registers. If the graph
1386 // is debuggable, we need to deal with these registers differently. For
1387 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001388 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1389 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001390 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001391 }
1392 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001393}
1394
Alexandre Rames3e69f162014-12-10 10:36:50 +00001395size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1396 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1397 __ Str(reg, MemOperand(sp, stack_index));
1398 return kArm64WordSize;
1399}
1400
1401size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1402 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1403 __ Ldr(reg, MemOperand(sp, stack_index));
1404 return kArm64WordSize;
1405}
1406
1407size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1408 FPRegister reg = FPRegister(reg_id, kDRegSize);
1409 __ Str(reg, MemOperand(sp, stack_index));
1410 return kArm64WordSize;
1411}
1412
1413size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1414 FPRegister reg = FPRegister(reg_id, kDRegSize);
1415 __ Ldr(reg, MemOperand(sp, stack_index));
1416 return kArm64WordSize;
1417}
1418
Alexandre Rames5319def2014-10-23 10:03:10 +01001419void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001420 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001421}
1422
1423void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001424 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001425}
1426
Alexandre Rames67555f72014-11-18 10:55:16 +00001427void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001428 if (constant->IsIntConstant()) {
1429 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1430 } else if (constant->IsLongConstant()) {
1431 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1432 } else if (constant->IsNullConstant()) {
1433 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001434 } else if (constant->IsFloatConstant()) {
1435 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1436 } else {
1437 DCHECK(constant->IsDoubleConstant());
1438 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1439 }
1440}
1441
Alexandre Rames3e69f162014-12-10 10:36:50 +00001442
1443static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1444 DCHECK(constant.IsConstant());
1445 HConstant* cst = constant.GetConstant();
1446 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001447 // Null is mapped to a core W register, which we associate with kPrimInt.
1448 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001449 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1450 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1451 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1452}
1453
Calin Juravlee460d1d2015-09-29 04:52:17 +01001454void CodeGeneratorARM64::MoveLocation(Location destination,
1455 Location source,
1456 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001457 if (source.Equals(destination)) {
1458 return;
1459 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001460
1461 // A valid move can always be inferred from the destination and source
1462 // locations. When moving from and to a register, the argument type can be
1463 // used to generate 32bit instead of 64bit moves. In debug mode we also
1464 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001465 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001466
1467 if (destination.IsRegister() || destination.IsFpuRegister()) {
1468 if (unspecified_type) {
1469 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1470 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001471 (src_cst != nullptr && (src_cst->IsIntConstant()
1472 || src_cst->IsFloatConstant()
1473 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001474 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001475 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001476 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001477 // If the source is a double stack slot or a 64bit constant, a 64bit
1478 // type is appropriate. Else the source is a register, and since the
1479 // type has not been specified, we chose a 64bit type to force a 64bit
1480 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001481 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001482 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001483 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001484 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1485 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1486 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001487 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1488 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1489 __ Ldr(dst, StackOperandFrom(source));
1490 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001491 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001492 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001493 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001494 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001495 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001496 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001497 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001498 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1499 ? Primitive::kPrimLong
1500 : Primitive::kPrimInt;
1501 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1502 }
1503 } else {
1504 DCHECK(source.IsFpuRegister());
1505 if (destination.IsRegister()) {
1506 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1507 ? Primitive::kPrimDouble
1508 : Primitive::kPrimFloat;
1509 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1510 } else {
1511 DCHECK(destination.IsFpuRegister());
1512 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001513 }
1514 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001515 } else { // The destination is not a register. It must be a stack slot.
1516 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1517 if (source.IsRegister() || source.IsFpuRegister()) {
1518 if (unspecified_type) {
1519 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001520 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001521 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001522 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001523 }
1524 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001525 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1526 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1527 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001528 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001529 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1530 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001531 UseScratchRegisterScope temps(GetVIXLAssembler());
1532 HConstant* src_cst = source.GetConstant();
1533 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001534 if (src_cst->IsZeroBitPattern()) {
1535 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant()) ? xzr : wzr;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001536 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001537 if (src_cst->IsIntConstant()) {
1538 temp = temps.AcquireW();
1539 } else if (src_cst->IsLongConstant()) {
1540 temp = temps.AcquireX();
1541 } else if (src_cst->IsFloatConstant()) {
1542 temp = temps.AcquireS();
1543 } else {
1544 DCHECK(src_cst->IsDoubleConstant());
1545 temp = temps.AcquireD();
1546 }
1547 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001548 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001549 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001550 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001551 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001552 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001553 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001554 // Use any scratch register (a core or a floating-point one)
1555 // from VIXL scratch register pools as a temporary.
1556 //
1557 // We used to only use the FP scratch register pool, but in some
1558 // rare cases the only register from this pool (D31) would
1559 // already be used (e.g. within a ParallelMove instruction, when
1560 // a move is blocked by a another move requiring a scratch FP
1561 // register, which would reserve D31). To prevent this issue, we
1562 // ask for a scratch register of any type (core or FP).
1563 CPURegister temp =
1564 temps.AcquireCPURegisterOfSize(destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001565 __ Ldr(temp, StackOperandFrom(source));
1566 __ Str(temp, StackOperandFrom(destination));
1567 }
1568 }
1569}
1570
1571void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001572 CPURegister dst,
1573 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001574 switch (type) {
1575 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001576 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001577 break;
1578 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001579 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001580 break;
1581 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001582 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001583 break;
1584 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001585 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001586 break;
1587 case Primitive::kPrimInt:
1588 case Primitive::kPrimNot:
1589 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001590 case Primitive::kPrimFloat:
1591 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001592 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001593 __ Ldr(dst, src);
1594 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001595 case Primitive::kPrimVoid:
1596 LOG(FATAL) << "Unreachable type " << type;
1597 }
1598}
1599
Calin Juravle77520bc2015-01-12 18:45:46 +00001600void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001601 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001602 const MemOperand& src,
1603 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001604 MacroAssembler* masm = GetVIXLAssembler();
1605 BlockPoolsScope block_pools(masm);
1606 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001607 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001608 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001609
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001610 DCHECK(!src.IsPreIndex());
1611 DCHECK(!src.IsPostIndex());
1612
1613 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001614 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001615 MemOperand base = MemOperand(temp_base);
1616 switch (type) {
1617 case Primitive::kPrimBoolean:
1618 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001619 if (needs_null_check) {
1620 MaybeRecordImplicitNullCheck(instruction);
1621 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001622 break;
1623 case Primitive::kPrimByte:
1624 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001625 if (needs_null_check) {
1626 MaybeRecordImplicitNullCheck(instruction);
1627 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001628 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1629 break;
1630 case Primitive::kPrimChar:
1631 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001632 if (needs_null_check) {
1633 MaybeRecordImplicitNullCheck(instruction);
1634 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001635 break;
1636 case Primitive::kPrimShort:
1637 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001638 if (needs_null_check) {
1639 MaybeRecordImplicitNullCheck(instruction);
1640 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001641 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1642 break;
1643 case Primitive::kPrimInt:
1644 case Primitive::kPrimNot:
1645 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001646 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001647 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001648 if (needs_null_check) {
1649 MaybeRecordImplicitNullCheck(instruction);
1650 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001651 break;
1652 case Primitive::kPrimFloat:
1653 case Primitive::kPrimDouble: {
1654 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001655 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001656
1657 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1658 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001659 if (needs_null_check) {
1660 MaybeRecordImplicitNullCheck(instruction);
1661 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001662 __ Fmov(FPRegister(dst), temp);
1663 break;
1664 }
1665 case Primitive::kPrimVoid:
1666 LOG(FATAL) << "Unreachable type " << type;
1667 }
1668}
1669
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001670void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001671 CPURegister src,
1672 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001673 switch (type) {
1674 case Primitive::kPrimBoolean:
1675 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001676 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001677 break;
1678 case Primitive::kPrimChar:
1679 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001680 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001681 break;
1682 case Primitive::kPrimInt:
1683 case Primitive::kPrimNot:
1684 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001685 case Primitive::kPrimFloat:
1686 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001687 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001688 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001689 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001690 case Primitive::kPrimVoid:
1691 LOG(FATAL) << "Unreachable type " << type;
1692 }
1693}
1694
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001695void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1696 CPURegister src,
1697 const MemOperand& dst) {
1698 UseScratchRegisterScope temps(GetVIXLAssembler());
1699 Register temp_base = temps.AcquireX();
1700
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001701 DCHECK(!dst.IsPreIndex());
1702 DCHECK(!dst.IsPostIndex());
1703
1704 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001705 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001706 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001707 MemOperand base = MemOperand(temp_base);
1708 switch (type) {
1709 case Primitive::kPrimBoolean:
1710 case Primitive::kPrimByte:
1711 __ Stlrb(Register(src), base);
1712 break;
1713 case Primitive::kPrimChar:
1714 case Primitive::kPrimShort:
1715 __ Stlrh(Register(src), base);
1716 break;
1717 case Primitive::kPrimInt:
1718 case Primitive::kPrimNot:
1719 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001720 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001721 __ Stlr(Register(src), base);
1722 break;
1723 case Primitive::kPrimFloat:
1724 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001725 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001726 Register temp_src;
1727 if (src.IsZero()) {
1728 // The zero register is used to avoid synthesizing zero constants.
1729 temp_src = Register(src);
1730 } else {
1731 DCHECK(src.IsFPRegister());
1732 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1733 __ Fmov(temp_src, FPRegister(src));
1734 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001735
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001736 __ Stlr(temp_src, base);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001737 break;
1738 }
1739 case Primitive::kPrimVoid:
1740 LOG(FATAL) << "Unreachable type " << type;
1741 }
1742}
1743
Calin Juravle175dc732015-08-25 15:42:32 +01001744void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1745 HInstruction* instruction,
1746 uint32_t dex_pc,
1747 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001748 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001749 GenerateInvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001750 if (EntrypointRequiresStackMap(entrypoint)) {
1751 RecordPcInfo(instruction, dex_pc, slow_path);
1752 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001753}
1754
Roland Levillaindec8f632016-07-22 17:10:06 +01001755void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1756 HInstruction* instruction,
1757 SlowPathCode* slow_path) {
1758 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001759 GenerateInvokeRuntime(entry_point_offset);
1760}
1761
1762void CodeGeneratorARM64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001763 BlockPoolsScope block_pools(GetVIXLAssembler());
1764 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1765 __ Blr(lr);
1766}
1767
Alexandre Rames67555f72014-11-18 10:55:16 +00001768void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001769 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001770 UseScratchRegisterScope temps(GetVIXLAssembler());
1771 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001772 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1773
Serban Constantinescu02164b32014-11-13 14:05:07 +00001774 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001775 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1776 __ Add(temp, class_reg, status_offset);
1777 __ Ldar(temp, HeapOperand(temp));
1778 __ Cmp(temp, mirror::Class::kStatusInitialized);
1779 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001780 __ Bind(slow_path->GetExitLabel());
1781}
Alexandre Rames5319def2014-10-23 10:03:10 +01001782
Roland Levillain44015862016-01-22 11:47:17 +00001783void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001784 BarrierType type = BarrierAll;
1785
1786 switch (kind) {
1787 case MemBarrierKind::kAnyAny:
1788 case MemBarrierKind::kAnyStore: {
1789 type = BarrierAll;
1790 break;
1791 }
1792 case MemBarrierKind::kLoadAny: {
1793 type = BarrierReads;
1794 break;
1795 }
1796 case MemBarrierKind::kStoreStore: {
1797 type = BarrierWrites;
1798 break;
1799 }
1800 default:
1801 LOG(FATAL) << "Unexpected memory barrier " << kind;
1802 }
1803 __ Dmb(InnerShareable, type);
1804}
1805
Serban Constantinescu02164b32014-11-13 14:05:07 +00001806void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1807 HBasicBlock* successor) {
1808 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001809 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1810 if (slow_path == nullptr) {
1811 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1812 instruction->SetSlowPath(slow_path);
1813 codegen_->AddSlowPath(slow_path);
1814 if (successor != nullptr) {
1815 DCHECK(successor->IsLoopHeader());
1816 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1817 }
1818 } else {
1819 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1820 }
1821
Serban Constantinescu02164b32014-11-13 14:05:07 +00001822 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1823 Register temp = temps.AcquireW();
1824
Andreas Gampe542451c2016-07-26 09:02:02 -07001825 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001826 if (successor == nullptr) {
1827 __ Cbnz(temp, slow_path->GetEntryLabel());
1828 __ Bind(slow_path->GetReturnLabel());
1829 } else {
1830 __ Cbz(temp, codegen_->GetLabelOf(successor));
1831 __ B(slow_path->GetEntryLabel());
1832 // slow_path will return to GetLabelOf(successor).
1833 }
1834}
1835
Alexandre Rames5319def2014-10-23 10:03:10 +01001836InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1837 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001838 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001839 assembler_(codegen->GetAssembler()),
1840 codegen_(codegen) {}
1841
1842#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001843 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001844
1845#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1846
1847enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001848 // Using a base helps identify when we hit such breakpoints.
1849 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001850#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1851 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1852#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1853};
1854
1855#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001856 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001857 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1858 } \
1859 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1860 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1861 locations->SetOut(Location::Any()); \
1862 }
1863 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1864#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1865
1866#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001867#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001868
Alexandre Rames67555f72014-11-18 10:55:16 +00001869void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001870 DCHECK_EQ(instr->InputCount(), 2U);
1871 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1872 Primitive::Type type = instr->GetResultType();
1873 switch (type) {
1874 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001875 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001876 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001877 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001878 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001879 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001880
1881 case Primitive::kPrimFloat:
1882 case Primitive::kPrimDouble:
1883 locations->SetInAt(0, Location::RequiresFpuRegister());
1884 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001885 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001886 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001887
Alexandre Rames5319def2014-10-23 10:03:10 +01001888 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001889 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001890 }
1891}
1892
Alexandre Rames09a99962015-04-15 11:47:56 +01001893void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001894 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1895
1896 bool object_field_get_with_read_barrier =
1897 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001898 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001899 new (GetGraph()->GetArena()) LocationSummary(instruction,
1900 object_field_get_with_read_barrier ?
1901 LocationSummary::kCallOnSlowPath :
1902 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001903 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001904 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01001905 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001906 locations->SetInAt(0, Location::RequiresRegister());
1907 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1908 locations->SetOut(Location::RequiresFpuRegister());
1909 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001910 // The output overlaps for an object field get when read barriers
1911 // are enabled: we do not want the load to overwrite the object's
1912 // location, as we need it to emit the read barrier.
1913 locations->SetOut(
1914 Location::RequiresRegister(),
1915 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001916 }
1917}
1918
1919void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1920 const FieldInfo& field_info) {
1921 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001922 LocationSummary* locations = instruction->GetLocations();
1923 Location base_loc = locations->InAt(0);
1924 Location out = locations->Out();
1925 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001926 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001927 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001928 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001929
Roland Levillain44015862016-01-22 11:47:17 +00001930 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1931 // Object FieldGet with Baker's read barrier case.
1932 MacroAssembler* masm = GetVIXLAssembler();
1933 UseScratchRegisterScope temps(masm);
1934 // /* HeapReference<Object> */ out = *(base + offset)
1935 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1936 Register temp = temps.AcquireW();
1937 // Note that potential implicit null checks are handled in this
1938 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1939 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1940 instruction,
1941 out,
1942 base,
1943 offset,
1944 temp,
1945 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001946 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001947 } else {
1948 // General case.
1949 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001950 // Note that a potential implicit null check is handled in this
1951 // CodeGeneratorARM64::LoadAcquire call.
1952 // NB: LoadAcquire will record the pc info if needed.
1953 codegen_->LoadAcquire(
1954 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001955 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001956 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001957 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001958 }
Roland Levillain44015862016-01-22 11:47:17 +00001959 if (field_type == Primitive::kPrimNot) {
1960 // If read barriers are enabled, emit read barriers other than
1961 // Baker's using a slow path (and also unpoison the loaded
1962 // reference, if heap poisoning is enabled).
1963 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1964 }
Roland Levillain4d027112015-07-01 15:41:14 +01001965 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001966}
1967
1968void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1969 LocationSummary* locations =
1970 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1971 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001972 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1973 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
1974 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001975 locations->SetInAt(1, Location::RequiresFpuRegister());
1976 } else {
1977 locations->SetInAt(1, Location::RequiresRegister());
1978 }
1979}
1980
1981void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001982 const FieldInfo& field_info,
1983 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001984 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001985 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001986
1987 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001988 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001989 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001990 Offset offset = field_info.GetFieldOffset();
1991 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001992
Roland Levillain4d027112015-07-01 15:41:14 +01001993 {
1994 // We use a block to end the scratch scope before the write barrier, thus
1995 // freeing the temporary registers so they can be used in `MarkGCCard`.
1996 UseScratchRegisterScope temps(GetVIXLAssembler());
1997
1998 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1999 DCHECK(value.IsW());
2000 Register temp = temps.AcquireW();
2001 __ Mov(temp, value.W());
2002 GetAssembler()->PoisonHeapReference(temp.W());
2003 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002004 }
Roland Levillain4d027112015-07-01 15:41:14 +01002005
2006 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002007 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
2008 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01002009 } else {
2010 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2011 codegen_->MaybeRecordImplicitNullCheck(instruction);
2012 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002013 }
2014
2015 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002016 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002017 }
2018}
2019
Alexandre Rames67555f72014-11-18 10:55:16 +00002020void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002021 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002022
2023 switch (type) {
2024 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002025 case Primitive::kPrimLong: {
2026 Register dst = OutputRegister(instr);
2027 Register lhs = InputRegisterAt(instr, 0);
2028 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002029 if (instr->IsAdd()) {
2030 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002031 } else if (instr->IsAnd()) {
2032 __ And(dst, lhs, rhs);
2033 } else if (instr->IsOr()) {
2034 __ Orr(dst, lhs, rhs);
2035 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002036 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002037 } else if (instr->IsRor()) {
2038 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002039 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002040 __ Ror(dst, lhs, shift);
2041 } else {
2042 // Ensure shift distance is in the same size register as the result. If
2043 // we are rotating a long and the shift comes in a w register originally,
2044 // we don't need to sxtw for use as an x since the shift distances are
2045 // all & reg_bits - 1.
2046 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2047 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002048 } else {
2049 DCHECK(instr->IsXor());
2050 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002051 }
2052 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002053 }
2054 case Primitive::kPrimFloat:
2055 case Primitive::kPrimDouble: {
2056 FPRegister dst = OutputFPRegister(instr);
2057 FPRegister lhs = InputFPRegisterAt(instr, 0);
2058 FPRegister rhs = InputFPRegisterAt(instr, 1);
2059 if (instr->IsAdd()) {
2060 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002061 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002062 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002063 } else {
2064 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002065 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002066 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002067 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002068 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002069 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002070 }
2071}
2072
Serban Constantinescu02164b32014-11-13 14:05:07 +00002073void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2074 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2075
2076 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2077 Primitive::Type type = instr->GetResultType();
2078 switch (type) {
2079 case Primitive::kPrimInt:
2080 case Primitive::kPrimLong: {
2081 locations->SetInAt(0, Location::RequiresRegister());
2082 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2083 locations->SetOut(Location::RequiresRegister());
2084 break;
2085 }
2086 default:
2087 LOG(FATAL) << "Unexpected shift type " << type;
2088 }
2089}
2090
2091void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2092 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2093
2094 Primitive::Type type = instr->GetType();
2095 switch (type) {
2096 case Primitive::kPrimInt:
2097 case Primitive::kPrimLong: {
2098 Register dst = OutputRegister(instr);
2099 Register lhs = InputRegisterAt(instr, 0);
2100 Operand rhs = InputOperandAt(instr, 1);
2101 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002102 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002103 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002104 if (instr->IsShl()) {
2105 __ Lsl(dst, lhs, shift_value);
2106 } else if (instr->IsShr()) {
2107 __ Asr(dst, lhs, shift_value);
2108 } else {
2109 __ Lsr(dst, lhs, shift_value);
2110 }
2111 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002112 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002113
2114 if (instr->IsShl()) {
2115 __ Lsl(dst, lhs, rhs_reg);
2116 } else if (instr->IsShr()) {
2117 __ Asr(dst, lhs, rhs_reg);
2118 } else {
2119 __ Lsr(dst, lhs, rhs_reg);
2120 }
2121 }
2122 break;
2123 }
2124 default:
2125 LOG(FATAL) << "Unexpected shift operation type " << type;
2126 }
2127}
2128
Alexandre Rames5319def2014-10-23 10:03:10 +01002129void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002130 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002131}
2132
2133void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002134 HandleBinaryOp(instruction);
2135}
2136
2137void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2138 HandleBinaryOp(instruction);
2139}
2140
2141void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2142 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002143}
2144
Artem Serov7fc63502016-02-09 17:15:29 +00002145void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002146 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2147 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2148 locations->SetInAt(0, Location::RequiresRegister());
2149 // There is no immediate variant of negated bitwise instructions in AArch64.
2150 locations->SetInAt(1, Location::RequiresRegister());
2151 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2152}
2153
Artem Serov7fc63502016-02-09 17:15:29 +00002154void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002155 Register dst = OutputRegister(instr);
2156 Register lhs = InputRegisterAt(instr, 0);
2157 Register rhs = InputRegisterAt(instr, 1);
2158
2159 switch (instr->GetOpKind()) {
2160 case HInstruction::kAnd:
2161 __ Bic(dst, lhs, rhs);
2162 break;
2163 case HInstruction::kOr:
2164 __ Orn(dst, lhs, rhs);
2165 break;
2166 case HInstruction::kXor:
2167 __ Eon(dst, lhs, rhs);
2168 break;
2169 default:
2170 LOG(FATAL) << "Unreachable";
2171 }
2172}
2173
Alexandre Rames8626b742015-11-25 16:28:08 +00002174void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
2175 HArm64DataProcWithShifterOp* instruction) {
2176 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2177 instruction->GetType() == Primitive::kPrimLong);
2178 LocationSummary* locations =
2179 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2180 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2181 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2182 } else {
2183 locations->SetInAt(0, Location::RequiresRegister());
2184 }
2185 locations->SetInAt(1, Location::RequiresRegister());
2186 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2187}
2188
2189void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
2190 HArm64DataProcWithShifterOp* instruction) {
2191 Primitive::Type type = instruction->GetType();
2192 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2193 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2194 Register out = OutputRegister(instruction);
2195 Register left;
2196 if (kind != HInstruction::kNeg) {
2197 left = InputRegisterAt(instruction, 0);
2198 }
2199 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
2200 // shifter operand operation, the IR generating `right_reg` (input to the type
2201 // conversion) can have a different type from the current instruction's type,
2202 // so we manually indicate the type.
2203 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00002204 int64_t shift_amount = instruction->GetShiftAmount() &
2205 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00002206
2207 Operand right_operand(0);
2208
2209 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2210 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
2211 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2212 } else {
2213 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
2214 }
2215
2216 // Logical binary operations do not support extension operations in the
2217 // operand. Note that VIXL would still manage if it was passed by generating
2218 // the extension as a separate instruction.
2219 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2220 DCHECK(!right_operand.IsExtendedRegister() ||
2221 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2222 kind != HInstruction::kNeg));
2223 switch (kind) {
2224 case HInstruction::kAdd:
2225 __ Add(out, left, right_operand);
2226 break;
2227 case HInstruction::kAnd:
2228 __ And(out, left, right_operand);
2229 break;
2230 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002231 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002232 __ Neg(out, right_operand);
2233 break;
2234 case HInstruction::kOr:
2235 __ Orr(out, left, right_operand);
2236 break;
2237 case HInstruction::kSub:
2238 __ Sub(out, left, right_operand);
2239 break;
2240 case HInstruction::kXor:
2241 __ Eor(out, left, right_operand);
2242 break;
2243 default:
2244 LOG(FATAL) << "Unexpected operation kind: " << kind;
2245 UNREACHABLE();
2246 }
2247}
2248
Artem Serov328429f2016-07-06 16:23:04 +01002249void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002250 LocationSummary* locations =
2251 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2252 locations->SetInAt(0, Location::RequiresRegister());
2253 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
2254 locations->SetOut(Location::RequiresRegister());
2255}
2256
Roland Levillain19c54192016-11-04 13:44:09 +00002257void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002258 __ Add(OutputRegister(instruction),
2259 InputRegisterAt(instruction, 0),
2260 Operand(InputOperandAt(instruction, 1)));
2261}
2262
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002263void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002264 LocationSummary* locations =
2265 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002266 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2267 if (instr->GetOpKind() == HInstruction::kSub &&
2268 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002269 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002270 // Don't allocate register for Mneg instruction.
2271 } else {
2272 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2273 Location::RequiresRegister());
2274 }
2275 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2276 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002277 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2278}
2279
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002280void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002281 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002282 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2283 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002284
2285 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2286 // This fixup should be carried out for all multiply-accumulate instructions:
2287 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2288 if (instr->GetType() == Primitive::kPrimLong &&
2289 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2290 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002291 vixl::aarch64::Instruction* prev =
2292 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002293 if (prev->IsLoadOrStore()) {
2294 // Make sure we emit only exactly one nop.
Scott Wakelingb77051e2016-11-21 19:46:00 +00002295 vixl::CodeBufferCheckScope scope(masm,
2296 kInstructionSize,
2297 vixl::CodeBufferCheckScope::kReserveBufferSpace,
2298 vixl::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002299 __ nop();
2300 }
2301 }
2302
2303 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002304 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002305 __ Madd(res, mul_left, mul_right, accumulator);
2306 } else {
2307 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002308 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002309 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002310 __ Mneg(res, mul_left, mul_right);
2311 } else {
2312 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2313 __ Msub(res, mul_left, mul_right, accumulator);
2314 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002315 }
2316}
2317
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002318void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002319 bool object_array_get_with_read_barrier =
2320 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002321 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002322 new (GetGraph()->GetArena()) LocationSummary(instruction,
2323 object_array_get_with_read_barrier ?
2324 LocationSummary::kCallOnSlowPath :
2325 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002326 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002327 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002328 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002329 locations->SetInAt(0, Location::RequiresRegister());
2330 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002331 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2332 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2333 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002334 // The output overlaps in the case of an object array get with
2335 // read barriers enabled: we do not want the move to overwrite the
2336 // array's location, as we need it to emit the read barrier.
2337 locations->SetOut(
2338 Location::RequiresRegister(),
2339 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002340 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002341}
2342
2343void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002344 Primitive::Type type = instruction->GetType();
2345 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002346 LocationSummary* locations = instruction->GetLocations();
2347 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002348 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002349 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002350 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2351 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002352 MacroAssembler* masm = GetVIXLAssembler();
2353 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002354 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002355 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002356
Roland Levillain19c54192016-11-04 13:44:09 +00002357 // The read barrier instrumentation of object ArrayGet instructions
2358 // does not support the HIntermediateAddress instruction.
2359 DCHECK(!((type == Primitive::kPrimNot) &&
2360 instruction->GetArray()->IsIntermediateAddress() &&
2361 kEmitCompilerReadBarrier));
2362
Roland Levillain44015862016-01-22 11:47:17 +00002363 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2364 // Object ArrayGet with Baker's read barrier case.
2365 Register temp = temps.AcquireW();
Roland Levillain44015862016-01-22 11:47:17 +00002366 // Note that a potential implicit null check is handled in the
2367 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2368 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2369 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002370 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002371 // General case.
2372 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002373 Register length;
2374 if (maybe_compressed_char_at) {
2375 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2376 length = temps.AcquireW();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002377 if (instruction->GetArray()->IsIntermediateAddress()) {
2378 DCHECK_LT(count_offset, offset);
2379 int64_t adjusted_offset = static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2380 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2381 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2382 } else {
2383 __ Ldr(length, HeapOperand(obj, count_offset));
2384 }
jessicahandojo05765752016-09-09 19:01:32 -07002385 codegen_->MaybeRecordImplicitNullCheck(instruction);
2386 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002387 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002388 if (maybe_compressed_char_at) {
2389 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002390 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2391 "Expecting 0=compressed, 1=uncompressed");
2392 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002393 __ Ldrb(Register(OutputCPURegister(instruction)),
2394 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2395 __ B(&done);
2396 __ Bind(&uncompressed_load);
2397 __ Ldrh(Register(OutputCPURegister(instruction)),
2398 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2399 __ Bind(&done);
2400 } else {
2401 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2402 source = HeapOperand(obj, offset);
2403 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002404 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002405 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002406 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002407 // We do not need to compute the intermediate address from the array: the
2408 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002409 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002410 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002411 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002412 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2413 }
2414 temp = obj;
2415 } else {
2416 __ Add(temp, obj, offset);
2417 }
jessicahandojo05765752016-09-09 19:01:32 -07002418 if (maybe_compressed_char_at) {
2419 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002420 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2421 "Expecting 0=compressed, 1=uncompressed");
2422 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002423 __ Ldrb(Register(OutputCPURegister(instruction)),
2424 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2425 __ B(&done);
2426 __ Bind(&uncompressed_load);
2427 __ Ldrh(Register(OutputCPURegister(instruction)),
2428 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2429 __ Bind(&done);
2430 } else {
2431 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2432 }
Roland Levillain44015862016-01-22 11:47:17 +00002433 }
jessicahandojo05765752016-09-09 19:01:32 -07002434 if (!maybe_compressed_char_at) {
2435 codegen_->Load(type, OutputCPURegister(instruction), source);
2436 codegen_->MaybeRecordImplicitNullCheck(instruction);
2437 }
Roland Levillain44015862016-01-22 11:47:17 +00002438
2439 if (type == Primitive::kPrimNot) {
2440 static_assert(
2441 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2442 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2443 Location obj_loc = locations->InAt(0);
2444 if (index.IsConstant()) {
2445 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2446 } else {
2447 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2448 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002449 }
Roland Levillain4d027112015-07-01 15:41:14 +01002450 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002451}
2452
Alexandre Rames5319def2014-10-23 10:03:10 +01002453void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2454 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2455 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002456 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002457}
2458
2459void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002460 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002461 vixl::aarch64::Register out = OutputRegister(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002462 BlockPoolsScope block_pools(GetVIXLAssembler());
jessicahandojo05765752016-09-09 19:01:32 -07002463 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002464 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002465 // Mask out compression flag from String's array length.
2466 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002467 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002468 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002469}
2470
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002471void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002472 Primitive::Type value_type = instruction->GetComponentType();
2473
2474 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002475 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2476 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002477 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002478 LocationSummary::kCallOnSlowPath :
2479 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002480 locations->SetInAt(0, Location::RequiresRegister());
2481 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002482 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2483 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2484 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002485 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002486 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002487 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002488 }
2489}
2490
2491void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2492 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002493 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002494 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002495 bool needs_write_barrier =
2496 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002497
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002498 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002499 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002500 CPURegister source = value;
2501 Location index = locations->InAt(1);
2502 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2503 MemOperand destination = HeapOperand(array);
2504 MacroAssembler* masm = GetVIXLAssembler();
2505 BlockPoolsScope block_pools(masm);
2506
2507 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002508 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002509 if (index.IsConstant()) {
2510 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2511 destination = HeapOperand(array, offset);
2512 } else {
2513 UseScratchRegisterScope temps(masm);
2514 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002515 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002516 // We do not need to compute the intermediate address from the array: the
2517 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002518 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002519 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002520 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002521 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2522 }
2523 temp = array;
2524 } else {
2525 __ Add(temp, array, offset);
2526 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002527 destination = HeapOperand(temp,
2528 XRegisterFrom(index),
2529 LSL,
2530 Primitive::ComponentSizeShift(value_type));
2531 }
2532 codegen_->Store(value_type, value, destination);
2533 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002534 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002535 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002536 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002537 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002538 {
2539 // We use a block to end the scratch scope before the write barrier, thus
2540 // freeing the temporary registers so they can be used in `MarkGCCard`.
2541 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002542 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002543 if (index.IsConstant()) {
2544 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002545 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002546 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002547 destination = HeapOperand(temp,
2548 XRegisterFrom(index),
2549 LSL,
2550 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002551 }
2552
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002553 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2554 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2555 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2556
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002557 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002558 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2559 codegen_->AddSlowPath(slow_path);
2560 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002561 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002562 __ Cbnz(Register(value), &non_zero);
2563 if (!index.IsConstant()) {
2564 __ Add(temp, array, offset);
2565 }
2566 __ Str(wzr, destination);
2567 codegen_->MaybeRecordImplicitNullCheck(instruction);
2568 __ B(&done);
2569 __ Bind(&non_zero);
2570 }
2571
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002572 // Note that when Baker read barriers are enabled, the type
2573 // checks are performed without read barriers. This is fine,
2574 // even in the case where a class object is in the from-space
2575 // after the flip, as a comparison involving such a type would
2576 // not produce a false positive; it may of course produce a
2577 // false negative, in which case we would take the ArraySet
2578 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002579
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002580 Register temp2 = temps.AcquireSameSizeAs(array);
2581 // /* HeapReference<Class> */ temp = array->klass_
2582 __ Ldr(temp, HeapOperand(array, class_offset));
2583 codegen_->MaybeRecordImplicitNullCheck(instruction);
2584 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002585
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002586 // /* HeapReference<Class> */ temp = temp->component_type_
2587 __ Ldr(temp, HeapOperand(temp, component_offset));
2588 // /* HeapReference<Class> */ temp2 = value->klass_
2589 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2590 // If heap poisoning is enabled, no need to unpoison `temp`
2591 // nor `temp2`, as we are comparing two poisoned references.
2592 __ Cmp(temp, temp2);
2593 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002594
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002595 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2596 vixl::aarch64::Label do_put;
2597 __ B(eq, &do_put);
2598 // If heap poisoning is enabled, the `temp` reference has
2599 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002600 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2601
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002602 // /* HeapReference<Class> */ temp = temp->super_class_
2603 __ Ldr(temp, HeapOperand(temp, super_offset));
2604 // If heap poisoning is enabled, no need to unpoison
2605 // `temp`, as we are comparing against null below.
2606 __ Cbnz(temp, slow_path->GetEntryLabel());
2607 __ Bind(&do_put);
2608 } else {
2609 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002610 }
2611 }
2612
2613 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002614 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002615 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002616 __ Mov(temp2, value.W());
2617 GetAssembler()->PoisonHeapReference(temp2);
2618 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002619 }
2620
2621 if (!index.IsConstant()) {
2622 __ Add(temp, array, offset);
2623 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002624 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002625
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002626 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002627 codegen_->MaybeRecordImplicitNullCheck(instruction);
2628 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002629 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002630
2631 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2632
2633 if (done.IsLinked()) {
2634 __ Bind(&done);
2635 }
2636
2637 if (slow_path != nullptr) {
2638 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002639 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002640 }
2641}
2642
Alexandre Rames67555f72014-11-18 10:55:16 +00002643void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002644 RegisterSet caller_saves = RegisterSet::Empty();
2645 InvokeRuntimeCallingConvention calling_convention;
2646 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2647 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2648 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002649 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002650 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002651}
2652
2653void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002654 BoundsCheckSlowPathARM64* slow_path =
2655 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002656 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002657 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2658 __ B(slow_path->GetEntryLabel(), hs);
2659}
2660
Alexandre Rames67555f72014-11-18 10:55:16 +00002661void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2662 LocationSummary* locations =
2663 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2664 locations->SetInAt(0, Location::RequiresRegister());
2665 if (check->HasUses()) {
2666 locations->SetOut(Location::SameAsFirstInput());
2667 }
2668}
2669
2670void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2671 // We assume the class is not null.
2672 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2673 check->GetLoadClass(), check, check->GetDexPc(), true);
2674 codegen_->AddSlowPath(slow_path);
2675 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2676}
2677
Roland Levillain1a653882016-03-18 18:05:57 +00002678static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2679 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2680 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2681}
2682
2683void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2684 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2685 Location rhs_loc = instruction->GetLocations()->InAt(1);
2686 if (rhs_loc.IsConstant()) {
2687 // 0.0 is the only immediate that can be encoded directly in
2688 // an FCMP instruction.
2689 //
2690 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2691 // specify that in a floating-point comparison, positive zero
2692 // and negative zero are considered equal, so we can use the
2693 // literal 0.0 for both cases here.
2694 //
2695 // Note however that some methods (Float.equal, Float.compare,
2696 // Float.compareTo, Double.equal, Double.compare,
2697 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2698 // StrictMath.min) consider 0.0 to be (strictly) greater than
2699 // -0.0. So if we ever translate calls to these methods into a
2700 // HCompare instruction, we must handle the -0.0 case with
2701 // care here.
2702 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2703 __ Fcmp(lhs_reg, 0.0);
2704 } else {
2705 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2706 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002707}
2708
Serban Constantinescu02164b32014-11-13 14:05:07 +00002709void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002710 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002711 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2712 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002713 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002714 case Primitive::kPrimBoolean:
2715 case Primitive::kPrimByte:
2716 case Primitive::kPrimShort:
2717 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002718 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002719 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002720 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002721 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002722 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2723 break;
2724 }
2725 case Primitive::kPrimFloat:
2726 case Primitive::kPrimDouble: {
2727 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002728 locations->SetInAt(1,
2729 IsFloatingPointZeroConstant(compare->InputAt(1))
2730 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2731 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002732 locations->SetOut(Location::RequiresRegister());
2733 break;
2734 }
2735 default:
2736 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2737 }
2738}
2739
2740void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2741 Primitive::Type in_type = compare->InputAt(0)->GetType();
2742
2743 // 0 if: left == right
2744 // 1 if: left > right
2745 // -1 if: left < right
2746 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002747 case Primitive::kPrimBoolean:
2748 case Primitive::kPrimByte:
2749 case Primitive::kPrimShort:
2750 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002751 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002752 case Primitive::kPrimLong: {
2753 Register result = OutputRegister(compare);
2754 Register left = InputRegisterAt(compare, 0);
2755 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002756 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002757 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2758 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002759 break;
2760 }
2761 case Primitive::kPrimFloat:
2762 case Primitive::kPrimDouble: {
2763 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002764 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002765 __ Cset(result, ne);
2766 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002767 break;
2768 }
2769 default:
2770 LOG(FATAL) << "Unimplemented compare type " << in_type;
2771 }
2772}
2773
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002774void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002775 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002776
2777 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2778 locations->SetInAt(0, Location::RequiresFpuRegister());
2779 locations->SetInAt(1,
2780 IsFloatingPointZeroConstant(instruction->InputAt(1))
2781 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2782 : Location::RequiresFpuRegister());
2783 } else {
2784 // Integer cases.
2785 locations->SetInAt(0, Location::RequiresRegister());
2786 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2787 }
2788
David Brazdilb3e773e2016-01-26 11:28:37 +00002789 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002790 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002791 }
2792}
2793
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002794void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002795 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002796 return;
2797 }
2798
2799 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002800 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002801 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002802
Roland Levillain7f63c522015-07-13 15:54:55 +00002803 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002804 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002805 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002806 } else {
2807 // Integer cases.
2808 Register lhs = InputRegisterAt(instruction, 0);
2809 Operand rhs = InputOperandAt(instruction, 1);
2810 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002811 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002812 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002813}
2814
2815#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2816 M(Equal) \
2817 M(NotEqual) \
2818 M(LessThan) \
2819 M(LessThanOrEqual) \
2820 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002821 M(GreaterThanOrEqual) \
2822 M(Below) \
2823 M(BelowOrEqual) \
2824 M(Above) \
2825 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002826#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002827void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2828void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002829FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002830#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002831#undef FOR_EACH_CONDITION_INSTRUCTION
2832
Zheng Xuc6667102015-05-15 16:08:45 +08002833void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2834 DCHECK(instruction->IsDiv() || instruction->IsRem());
2835
2836 LocationSummary* locations = instruction->GetLocations();
2837 Location second = locations->InAt(1);
2838 DCHECK(second.IsConstant());
2839
2840 Register out = OutputRegister(instruction);
2841 Register dividend = InputRegisterAt(instruction, 0);
2842 int64_t imm = Int64FromConstant(second.GetConstant());
2843 DCHECK(imm == 1 || imm == -1);
2844
2845 if (instruction->IsRem()) {
2846 __ Mov(out, 0);
2847 } else {
2848 if (imm == 1) {
2849 __ Mov(out, dividend);
2850 } else {
2851 __ Neg(out, dividend);
2852 }
2853 }
2854}
2855
2856void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2857 DCHECK(instruction->IsDiv() || instruction->IsRem());
2858
2859 LocationSummary* locations = instruction->GetLocations();
2860 Location second = locations->InAt(1);
2861 DCHECK(second.IsConstant());
2862
2863 Register out = OutputRegister(instruction);
2864 Register dividend = InputRegisterAt(instruction, 0);
2865 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002866 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002867 int ctz_imm = CTZ(abs_imm);
2868
2869 UseScratchRegisterScope temps(GetVIXLAssembler());
2870 Register temp = temps.AcquireSameSizeAs(out);
2871
2872 if (instruction->IsDiv()) {
2873 __ Add(temp, dividend, abs_imm - 1);
2874 __ Cmp(dividend, 0);
2875 __ Csel(out, temp, dividend, lt);
2876 if (imm > 0) {
2877 __ Asr(out, out, ctz_imm);
2878 } else {
2879 __ Neg(out, Operand(out, ASR, ctz_imm));
2880 }
2881 } else {
2882 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2883 __ Asr(temp, dividend, bits - 1);
2884 __ Lsr(temp, temp, bits - ctz_imm);
2885 __ Add(out, dividend, temp);
2886 __ And(out, out, abs_imm - 1);
2887 __ Sub(out, out, temp);
2888 }
2889}
2890
2891void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2892 DCHECK(instruction->IsDiv() || instruction->IsRem());
2893
2894 LocationSummary* locations = instruction->GetLocations();
2895 Location second = locations->InAt(1);
2896 DCHECK(second.IsConstant());
2897
2898 Register out = OutputRegister(instruction);
2899 Register dividend = InputRegisterAt(instruction, 0);
2900 int64_t imm = Int64FromConstant(second.GetConstant());
2901
2902 Primitive::Type type = instruction->GetResultType();
2903 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2904
2905 int64_t magic;
2906 int shift;
2907 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2908
2909 UseScratchRegisterScope temps(GetVIXLAssembler());
2910 Register temp = temps.AcquireSameSizeAs(out);
2911
2912 // temp = get_high(dividend * magic)
2913 __ Mov(temp, magic);
2914 if (type == Primitive::kPrimLong) {
2915 __ Smulh(temp, dividend, temp);
2916 } else {
2917 __ Smull(temp.X(), dividend, temp);
2918 __ Lsr(temp.X(), temp.X(), 32);
2919 }
2920
2921 if (imm > 0 && magic < 0) {
2922 __ Add(temp, temp, dividend);
2923 } else if (imm < 0 && magic > 0) {
2924 __ Sub(temp, temp, dividend);
2925 }
2926
2927 if (shift != 0) {
2928 __ Asr(temp, temp, shift);
2929 }
2930
2931 if (instruction->IsDiv()) {
2932 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2933 } else {
2934 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2935 // TODO: Strength reduction for msub.
2936 Register temp_imm = temps.AcquireSameSizeAs(out);
2937 __ Mov(temp_imm, imm);
2938 __ Msub(out, temp, temp_imm, dividend);
2939 }
2940}
2941
2942void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2943 DCHECK(instruction->IsDiv() || instruction->IsRem());
2944 Primitive::Type type = instruction->GetResultType();
2945 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2946
2947 LocationSummary* locations = instruction->GetLocations();
2948 Register out = OutputRegister(instruction);
2949 Location second = locations->InAt(1);
2950
2951 if (second.IsConstant()) {
2952 int64_t imm = Int64FromConstant(second.GetConstant());
2953
2954 if (imm == 0) {
2955 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2956 } else if (imm == 1 || imm == -1) {
2957 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002958 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002959 DivRemByPowerOfTwo(instruction);
2960 } else {
2961 DCHECK(imm <= -2 || imm >= 2);
2962 GenerateDivRemWithAnyConstant(instruction);
2963 }
2964 } else {
2965 Register dividend = InputRegisterAt(instruction, 0);
2966 Register divisor = InputRegisterAt(instruction, 1);
2967 if (instruction->IsDiv()) {
2968 __ Sdiv(out, dividend, divisor);
2969 } else {
2970 UseScratchRegisterScope temps(GetVIXLAssembler());
2971 Register temp = temps.AcquireSameSizeAs(out);
2972 __ Sdiv(temp, dividend, divisor);
2973 __ Msub(out, temp, divisor, dividend);
2974 }
2975 }
2976}
2977
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002978void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2979 LocationSummary* locations =
2980 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2981 switch (div->GetResultType()) {
2982 case Primitive::kPrimInt:
2983 case Primitive::kPrimLong:
2984 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002985 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002986 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2987 break;
2988
2989 case Primitive::kPrimFloat:
2990 case Primitive::kPrimDouble:
2991 locations->SetInAt(0, Location::RequiresFpuRegister());
2992 locations->SetInAt(1, Location::RequiresFpuRegister());
2993 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2994 break;
2995
2996 default:
2997 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2998 }
2999}
3000
3001void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3002 Primitive::Type type = div->GetResultType();
3003 switch (type) {
3004 case Primitive::kPrimInt:
3005 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003006 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003007 break;
3008
3009 case Primitive::kPrimFloat:
3010 case Primitive::kPrimDouble:
3011 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3012 break;
3013
3014 default:
3015 LOG(FATAL) << "Unexpected div type " << type;
3016 }
3017}
3018
Alexandre Rames67555f72014-11-18 10:55:16 +00003019void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003020 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003021 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003022}
3023
3024void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3025 SlowPathCodeARM64* slow_path =
3026 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3027 codegen_->AddSlowPath(slow_path);
3028 Location value = instruction->GetLocations()->InAt(0);
3029
Alexandre Rames3e69f162014-12-10 10:36:50 +00003030 Primitive::Type type = instruction->GetType();
3031
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003032 if (!Primitive::IsIntegralType(type)) {
3033 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003034 return;
3035 }
3036
Alexandre Rames67555f72014-11-18 10:55:16 +00003037 if (value.IsConstant()) {
3038 int64_t divisor = Int64ConstantFrom(value);
3039 if (divisor == 0) {
3040 __ B(slow_path->GetEntryLabel());
3041 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003042 // A division by a non-null constant is valid. We don't need to perform
3043 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003044 }
3045 } else {
3046 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3047 }
3048}
3049
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003050void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3051 LocationSummary* locations =
3052 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3053 locations->SetOut(Location::ConstantLocation(constant));
3054}
3055
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003056void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3057 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003058 // Will be generated at use site.
3059}
3060
Alexandre Rames5319def2014-10-23 10:03:10 +01003061void LocationsBuilderARM64::VisitExit(HExit* exit) {
3062 exit->SetLocations(nullptr);
3063}
3064
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003065void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003066}
3067
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003068void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3069 LocationSummary* locations =
3070 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3071 locations->SetOut(Location::ConstantLocation(constant));
3072}
3073
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003074void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003075 // Will be generated at use site.
3076}
3077
David Brazdilfc6a86a2015-06-26 10:33:45 +00003078void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003079 DCHECK(!successor->IsExitBlock());
3080 HBasicBlock* block = got->GetBlock();
3081 HInstruction* previous = got->GetPrevious();
3082 HLoopInformation* info = block->GetLoopInformation();
3083
David Brazdil46e2a392015-03-16 17:31:52 +00003084 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003085 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3086 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3087 return;
3088 }
3089 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3090 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3091 }
3092 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003093 __ B(codegen_->GetLabelOf(successor));
3094 }
3095}
3096
David Brazdilfc6a86a2015-06-26 10:33:45 +00003097void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3098 got->SetLocations(nullptr);
3099}
3100
3101void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3102 HandleGoto(got, got->GetSuccessor());
3103}
3104
3105void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3106 try_boundary->SetLocations(nullptr);
3107}
3108
3109void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3110 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3111 if (!successor->IsExitBlock()) {
3112 HandleGoto(try_boundary, successor);
3113 }
3114}
3115
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003116void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003117 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003118 vixl::aarch64::Label* true_target,
3119 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003120 // FP branching requires both targets to be explicit. If either of the targets
3121 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003122 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003123 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003124
David Brazdil0debae72015-11-12 18:37:00 +00003125 if (true_target == nullptr && false_target == nullptr) {
3126 // Nothing to do. The code always falls through.
3127 return;
3128 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003129 // Constant condition, statically compared against "true" (integer value 1).
3130 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003131 if (true_target != nullptr) {
3132 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003133 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003134 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003135 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003136 if (false_target != nullptr) {
3137 __ B(false_target);
3138 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003139 }
David Brazdil0debae72015-11-12 18:37:00 +00003140 return;
3141 }
3142
3143 // The following code generates these patterns:
3144 // (1) true_target == nullptr && false_target != nullptr
3145 // - opposite condition true => branch to false_target
3146 // (2) true_target != nullptr && false_target == nullptr
3147 // - condition true => branch to true_target
3148 // (3) true_target != nullptr && false_target != nullptr
3149 // - condition true => branch to true_target
3150 // - branch to false_target
3151 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003152 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003153 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003154 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003155 if (true_target == nullptr) {
3156 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3157 } else {
3158 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3159 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003160 } else {
3161 // The condition instruction has not been materialized, use its inputs as
3162 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003163 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003164
David Brazdil0debae72015-11-12 18:37:00 +00003165 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003166 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003167 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003168 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003169 IfCondition opposite_condition = condition->GetOppositeCondition();
3170 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003171 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003172 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003173 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003174 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003175 // Integer cases.
3176 Register lhs = InputRegisterAt(condition, 0);
3177 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003178
3179 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003180 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003181 if (true_target == nullptr) {
3182 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3183 non_fallthrough_target = false_target;
3184 } else {
3185 arm64_cond = ARM64Condition(condition->GetCondition());
3186 non_fallthrough_target = true_target;
3187 }
3188
Aart Bik086d27e2016-01-20 17:02:00 -08003189 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003190 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003191 switch (arm64_cond) {
3192 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003193 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003194 break;
3195 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003196 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003197 break;
3198 case lt:
3199 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003200 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003201 break;
3202 case ge:
3203 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003204 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003205 break;
3206 default:
3207 // Without the `static_cast` the compiler throws an error for
3208 // `-Werror=sign-promo`.
3209 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3210 }
3211 } else {
3212 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003213 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003214 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003215 }
3216 }
David Brazdil0debae72015-11-12 18:37:00 +00003217
3218 // If neither branch falls through (case 3), the conditional branch to `true_target`
3219 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3220 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003221 __ B(false_target);
3222 }
David Brazdil0debae72015-11-12 18:37:00 +00003223
3224 if (fallthrough_target.IsLinked()) {
3225 __ Bind(&fallthrough_target);
3226 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003227}
3228
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003229void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3230 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003231 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003232 locations->SetInAt(0, Location::RequiresRegister());
3233 }
3234}
3235
3236void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003237 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3238 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003239 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3240 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3241 true_target = nullptr;
3242 }
3243 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3244 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3245 false_target = nullptr;
3246 }
David Brazdil0debae72015-11-12 18:37:00 +00003247 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003248}
3249
3250void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3251 LocationSummary* locations = new (GetGraph()->GetArena())
3252 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003253 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003254 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003255 locations->SetInAt(0, Location::RequiresRegister());
3256 }
3257}
3258
3259void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003260 SlowPathCodeARM64* slow_path =
3261 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003262 GenerateTestAndBranch(deoptimize,
3263 /* condition_input_index */ 0,
3264 slow_path->GetEntryLabel(),
3265 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003266}
3267
Mingyao Yang063fc772016-08-02 11:02:54 -07003268void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3269 LocationSummary* locations = new (GetGraph()->GetArena())
3270 LocationSummary(flag, LocationSummary::kNoCall);
3271 locations->SetOut(Location::RequiresRegister());
3272}
3273
3274void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3275 __ Ldr(OutputRegister(flag),
3276 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3277}
3278
David Brazdilc0b601b2016-02-08 14:20:45 +00003279static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3280 return condition->IsCondition() &&
3281 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3282}
3283
Alexandre Rames880f1192016-06-13 16:04:50 +01003284static inline Condition GetConditionForSelect(HCondition* condition) {
3285 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003286 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3287 : ARM64Condition(cond);
3288}
3289
David Brazdil74eb1b22015-12-14 11:44:01 +00003290void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3291 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003292 if (Primitive::IsFloatingPointType(select->GetType())) {
3293 locations->SetInAt(0, Location::RequiresFpuRegister());
3294 locations->SetInAt(1, Location::RequiresFpuRegister());
3295 locations->SetOut(Location::RequiresFpuRegister());
3296 } else {
3297 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3298 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3299 bool is_true_value_constant = cst_true_value != nullptr;
3300 bool is_false_value_constant = cst_false_value != nullptr;
3301 // Ask VIXL whether we should synthesize constants in registers.
3302 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3303 Operand true_op = is_true_value_constant ?
3304 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3305 Operand false_op = is_false_value_constant ?
3306 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3307 bool true_value_in_register = false;
3308 bool false_value_in_register = false;
3309 MacroAssembler::GetCselSynthesisInformation(
3310 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3311 true_value_in_register |= !is_true_value_constant;
3312 false_value_in_register |= !is_false_value_constant;
3313
3314 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3315 : Location::ConstantLocation(cst_true_value));
3316 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3317 : Location::ConstantLocation(cst_false_value));
3318 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003319 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003320
David Brazdil74eb1b22015-12-14 11:44:01 +00003321 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3322 locations->SetInAt(2, Location::RequiresRegister());
3323 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003324}
3325
3326void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003327 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003328 Condition csel_cond;
3329
3330 if (IsBooleanValueOrMaterializedCondition(cond)) {
3331 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003332 // Use the condition flags set by the previous instruction.
3333 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003334 } else {
3335 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003336 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003337 }
3338 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003339 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003340 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003341 } else {
3342 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003343 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003344 }
3345
Alexandre Rames880f1192016-06-13 16:04:50 +01003346 if (Primitive::IsFloatingPointType(select->GetType())) {
3347 __ Fcsel(OutputFPRegister(select),
3348 InputFPRegisterAt(select, 1),
3349 InputFPRegisterAt(select, 0),
3350 csel_cond);
3351 } else {
3352 __ Csel(OutputRegister(select),
3353 InputOperandAt(select, 1),
3354 InputOperandAt(select, 0),
3355 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003356 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003357}
3358
David Srbecky0cf44932015-12-09 14:09:59 +00003359void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3360 new (GetGraph()->GetArena()) LocationSummary(info);
3361}
3362
David Srbeckyd28f4a02016-03-14 17:14:24 +00003363void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3364 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003365}
3366
3367void CodeGeneratorARM64::GenerateNop() {
3368 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003369}
3370
Alexandre Rames5319def2014-10-23 10:03:10 +01003371void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003372 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003373}
3374
3375void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003376 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003377}
3378
3379void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003380 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003381}
3382
3383void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003384 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003385}
3386
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003387// Temp is used for read barrier.
3388static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3389 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003390 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003391 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3392 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3393 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3394 return 1;
3395 }
3396 return 0;
3397}
3398
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003399// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003400// interface pointer, one for loading the current interface.
3401// The other checks have one temp for loading the object's class.
3402static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3403 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3404 return 3;
3405 }
3406 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003407}
3408
Alexandre Rames67555f72014-11-18 10:55:16 +00003409void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003410 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003411 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003412 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003413 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003414 case TypeCheckKind::kExactCheck:
3415 case TypeCheckKind::kAbstractClassCheck:
3416 case TypeCheckKind::kClassHierarchyCheck:
3417 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003418 call_kind =
3419 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003420 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003421 break;
3422 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003423 case TypeCheckKind::kUnresolvedCheck:
3424 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003425 call_kind = LocationSummary::kCallOnSlowPath;
3426 break;
3427 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003428
Alexandre Rames67555f72014-11-18 10:55:16 +00003429 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003430 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003431 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003432 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003433 locations->SetInAt(0, Location::RequiresRegister());
3434 locations->SetInAt(1, Location::RequiresRegister());
3435 // The "out" register is used as a temporary, so it overlaps with the inputs.
3436 // Note that TypeCheckSlowPathARM64 uses this register too.
3437 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003438 // Add temps if necessary for read barriers.
3439 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003440}
3441
3442void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003443 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003444 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003445 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003446 Register obj = InputRegisterAt(instruction, 0);
3447 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003448 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003449 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003450 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3451 DCHECK_LE(num_temps, 1u);
3452 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003453 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3454 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3455 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3456 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003457
Scott Wakeling97c72b72016-06-24 16:19:36 +01003458 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003459 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003460
3461 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003462 // Avoid null check if we know `obj` is not null.
3463 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003464 __ Cbz(obj, &zero);
3465 }
3466
Roland Levillain44015862016-01-22 11:47:17 +00003467 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003468 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003469 // /* HeapReference<Class> */ out = obj->klass_
3470 GenerateReferenceLoadTwoRegisters(instruction,
3471 out_loc,
3472 obj_loc,
3473 class_offset,
3474 maybe_temp_loc,
3475 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003476 __ Cmp(out, cls);
3477 __ Cset(out, eq);
3478 if (zero.IsLinked()) {
3479 __ B(&done);
3480 }
3481 break;
3482 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003483
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003484 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003485 // /* HeapReference<Class> */ out = obj->klass_
3486 GenerateReferenceLoadTwoRegisters(instruction,
3487 out_loc,
3488 obj_loc,
3489 class_offset,
3490 maybe_temp_loc,
3491 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003492 // If the class is abstract, we eagerly fetch the super class of the
3493 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003494 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003495 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003496 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003497 GenerateReferenceLoadOneRegister(instruction,
3498 out_loc,
3499 super_offset,
3500 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003501 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003502 // If `out` is null, we use it for the result, and jump to `done`.
3503 __ Cbz(out, &done);
3504 __ Cmp(out, cls);
3505 __ B(ne, &loop);
3506 __ Mov(out, 1);
3507 if (zero.IsLinked()) {
3508 __ B(&done);
3509 }
3510 break;
3511 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003512
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003513 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003514 // /* HeapReference<Class> */ out = obj->klass_
3515 GenerateReferenceLoadTwoRegisters(instruction,
3516 out_loc,
3517 obj_loc,
3518 class_offset,
3519 maybe_temp_loc,
3520 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003521 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003522 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003523 __ Bind(&loop);
3524 __ Cmp(out, cls);
3525 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003526 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003527 GenerateReferenceLoadOneRegister(instruction,
3528 out_loc,
3529 super_offset,
3530 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003531 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003532 __ Cbnz(out, &loop);
3533 // If `out` is null, we use it for the result, and jump to `done`.
3534 __ B(&done);
3535 __ Bind(&success);
3536 __ Mov(out, 1);
3537 if (zero.IsLinked()) {
3538 __ B(&done);
3539 }
3540 break;
3541 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003542
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003543 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003544 // /* HeapReference<Class> */ out = obj->klass_
3545 GenerateReferenceLoadTwoRegisters(instruction,
3546 out_loc,
3547 obj_loc,
3548 class_offset,
3549 maybe_temp_loc,
3550 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003551 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003552 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003553 __ Cmp(out, cls);
3554 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003555 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003556 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003557 GenerateReferenceLoadOneRegister(instruction,
3558 out_loc,
3559 component_offset,
3560 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003561 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003562 // If `out` is null, we use it for the result, and jump to `done`.
3563 __ Cbz(out, &done);
3564 __ Ldrh(out, HeapOperand(out, primitive_offset));
3565 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3566 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003567 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003568 __ Mov(out, 1);
3569 __ B(&done);
3570 break;
3571 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003572
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003573 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003574 // No read barrier since the slow path will retry upon failure.
3575 // /* HeapReference<Class> */ out = obj->klass_
3576 GenerateReferenceLoadTwoRegisters(instruction,
3577 out_loc,
3578 obj_loc,
3579 class_offset,
3580 maybe_temp_loc,
3581 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003582 __ Cmp(out, cls);
3583 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003584 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3585 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003586 codegen_->AddSlowPath(slow_path);
3587 __ B(ne, slow_path->GetEntryLabel());
3588 __ Mov(out, 1);
3589 if (zero.IsLinked()) {
3590 __ B(&done);
3591 }
3592 break;
3593 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003594
Calin Juravle98893e12015-10-02 21:05:03 +01003595 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003596 case TypeCheckKind::kInterfaceCheck: {
3597 // Note that we indeed only call on slow path, but we always go
3598 // into the slow path for the unresolved and interface check
3599 // cases.
3600 //
3601 // We cannot directly call the InstanceofNonTrivial runtime
3602 // entry point without resorting to a type checking slow path
3603 // here (i.e. by calling InvokeRuntime directly), as it would
3604 // require to assign fixed registers for the inputs of this
3605 // HInstanceOf instruction (following the runtime calling
3606 // convention), which might be cluttered by the potential first
3607 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003608 //
3609 // TODO: Introduce a new runtime entry point taking the object
3610 // to test (instead of its class) as argument, and let it deal
3611 // with the read barrier issues. This will let us refactor this
3612 // case of the `switch` code as it was previously (with a direct
3613 // call to the runtime not using a type checking slow path).
3614 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003615 DCHECK(locations->OnlyCallsOnSlowPath());
3616 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3617 /* is_fatal */ false);
3618 codegen_->AddSlowPath(slow_path);
3619 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003620 if (zero.IsLinked()) {
3621 __ B(&done);
3622 }
3623 break;
3624 }
3625 }
3626
3627 if (zero.IsLinked()) {
3628 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003629 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003630 }
3631
3632 if (done.IsLinked()) {
3633 __ Bind(&done);
3634 }
3635
3636 if (slow_path != nullptr) {
3637 __ Bind(slow_path->GetExitLabel());
3638 }
3639}
3640
3641void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3642 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3643 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3644
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003645 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3646 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003647 case TypeCheckKind::kExactCheck:
3648 case TypeCheckKind::kAbstractClassCheck:
3649 case TypeCheckKind::kClassHierarchyCheck:
3650 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003651 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3652 LocationSummary::kCallOnSlowPath :
3653 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003654 break;
3655 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003656 case TypeCheckKind::kUnresolvedCheck:
3657 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003658 call_kind = LocationSummary::kCallOnSlowPath;
3659 break;
3660 }
3661
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003662 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3663 locations->SetInAt(0, Location::RequiresRegister());
3664 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003665 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
3666 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003667}
3668
3669void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003670 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003671 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003672 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003673 Register obj = InputRegisterAt(instruction, 0);
3674 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003675 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3676 DCHECK_GE(num_temps, 1u);
3677 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003678 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003679 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
3680 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003681 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003682 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3683 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3684 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3685 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3686 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3687 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3688 const uint32_t object_array_data_offset =
3689 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003690
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003691 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003692 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3693 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3694 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003695 if (!kEmitCompilerReadBarrier) {
3696 is_type_check_slow_path_fatal =
3697 (type_check_kind == TypeCheckKind::kExactCheck ||
3698 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3699 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3700 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3701 !instruction->CanThrowIntoCatchBlock();
3702 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003703 SlowPathCodeARM64* type_check_slow_path =
3704 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3705 is_type_check_slow_path_fatal);
3706 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003707
Scott Wakeling97c72b72016-06-24 16:19:36 +01003708 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003709 // Avoid null check if we know obj is not null.
3710 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003711 __ Cbz(obj, &done);
3712 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003713
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003714 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003715 case TypeCheckKind::kExactCheck:
3716 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003717 // /* HeapReference<Class> */ temp = obj->klass_
3718 GenerateReferenceLoadTwoRegisters(instruction,
3719 temp_loc,
3720 obj_loc,
3721 class_offset,
3722 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003723 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003724
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003725 __ Cmp(temp, cls);
3726 // Jump to slow path for throwing the exception or doing a
3727 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003728 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003729 break;
3730 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003731
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003732 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003733 // /* HeapReference<Class> */ temp = obj->klass_
3734 GenerateReferenceLoadTwoRegisters(instruction,
3735 temp_loc,
3736 obj_loc,
3737 class_offset,
3738 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003739 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003740
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003741 // If the class is abstract, we eagerly fetch the super class of the
3742 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003743 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003744 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003745 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003746 GenerateReferenceLoadOneRegister(instruction,
3747 temp_loc,
3748 super_offset,
3749 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003750 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003751
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003752 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3753 // exception.
3754 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3755 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003756 __ Cmp(temp, cls);
3757 __ B(ne, &loop);
3758 break;
3759 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003760
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003761 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003762 // /* HeapReference<Class> */ temp = obj->klass_
3763 GenerateReferenceLoadTwoRegisters(instruction,
3764 temp_loc,
3765 obj_loc,
3766 class_offset,
3767 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003768 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003769
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003770 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003771 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003772 __ Bind(&loop);
3773 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003774 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003775
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003776 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003777 GenerateReferenceLoadOneRegister(instruction,
3778 temp_loc,
3779 super_offset,
3780 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003781 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003782
3783 // If the class reference currently in `temp` is not null, jump
3784 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003785 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003786 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003787 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003788 break;
3789 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003790
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003791 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003792 // /* HeapReference<Class> */ temp = obj->klass_
3793 GenerateReferenceLoadTwoRegisters(instruction,
3794 temp_loc,
3795 obj_loc,
3796 class_offset,
3797 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003798 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003799
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003800 // Do an exact check.
3801 __ Cmp(temp, cls);
3802 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003803
3804 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003805 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003806 GenerateReferenceLoadOneRegister(instruction,
3807 temp_loc,
3808 component_offset,
3809 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003810 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003811
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003812 // If the component type is null, jump to the slow path to throw the exception.
3813 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3814 // Otherwise, the object is indeed an array. Further check that this component type is not a
3815 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003816 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3817 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003818 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003819 break;
3820 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003821
Calin Juravle98893e12015-10-02 21:05:03 +01003822 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003823 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003824 //
3825 // We cannot directly call the CheckCast runtime entry point
3826 // without resorting to a type checking slow path here (i.e. by
3827 // calling InvokeRuntime directly), as it would require to
3828 // assign fixed registers for the inputs of this HInstanceOf
3829 // instruction (following the runtime calling convention), which
3830 // might be cluttered by the potential first read barrier
3831 // emission at the beginning of this method.
3832 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003833 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003834 case TypeCheckKind::kInterfaceCheck: {
3835 // /* HeapReference<Class> */ temp = obj->klass_
3836 GenerateReferenceLoadTwoRegisters(instruction,
3837 temp_loc,
3838 obj_loc,
3839 class_offset,
3840 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003841 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003842
3843 // /* HeapReference<Class> */ temp = temp->iftable_
3844 GenerateReferenceLoadTwoRegisters(instruction,
3845 temp_loc,
3846 temp_loc,
3847 iftable_offset,
3848 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003849 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003850 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003851 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08003852 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003853 vixl::aarch64::Label start_loop;
3854 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003855 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003856 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
3857 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003858 // Go to next interface.
3859 __ Add(temp, temp, 2 * kHeapReferenceSize);
3860 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003861 // Compare the classes and continue the loop if they do not match.
3862 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
3863 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003864 break;
3865 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003866 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003867 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003868
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003869 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003870}
3871
Alexandre Rames5319def2014-10-23 10:03:10 +01003872void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3873 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3874 locations->SetOut(Location::ConstantLocation(constant));
3875}
3876
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003877void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003878 // Will be generated at use site.
3879}
3880
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003881void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3882 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3883 locations->SetOut(Location::ConstantLocation(constant));
3884}
3885
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003886void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003887 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003888}
3889
Calin Juravle175dc732015-08-25 15:42:32 +01003890void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3891 // The trampoline uses the same calling convention as dex calling conventions,
3892 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3893 // the method_idx.
3894 HandleInvoke(invoke);
3895}
3896
3897void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3898 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3899}
3900
Alexandre Rames5319def2014-10-23 10:03:10 +01003901void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003902 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003903 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003904}
3905
Alexandre Rames67555f72014-11-18 10:55:16 +00003906void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3907 HandleInvoke(invoke);
3908}
3909
3910void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3911 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003912 LocationSummary* locations = invoke->GetLocations();
3913 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003914 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003915 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003916 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003917
3918 // The register ip1 is required to be used for the hidden argument in
3919 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003920 MacroAssembler* masm = GetVIXLAssembler();
3921 UseScratchRegisterScope scratch_scope(masm);
3922 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003923 scratch_scope.Exclude(ip1);
3924 __ Mov(ip1, invoke->GetDexMethodIndex());
3925
Alexandre Rames67555f72014-11-18 10:55:16 +00003926 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003927 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003928 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003929 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003930 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003931 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003932 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003933 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003934 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003935 // Instead of simply (possibly) unpoisoning `temp` here, we should
3936 // emit a read barrier for the previous class reference load.
3937 // However this is not required in practice, as this is an
3938 // intermediate/temporary reference and because the current
3939 // concurrent copying collector keeps the from-space memory
3940 // intact/accessible until the end of the marking phase (the
3941 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003942 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003943 __ Ldr(temp,
3944 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3945 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003946 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003947 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003948 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003949 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003950 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003951 // lr();
3952 __ Blr(lr);
3953 DCHECK(!codegen_->IsLeafMethod());
3954 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3955}
3956
3957void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003958 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3959 if (intrinsic.TryDispatch(invoke)) {
3960 return;
3961 }
3962
Alexandre Rames67555f72014-11-18 10:55:16 +00003963 HandleInvoke(invoke);
3964}
3965
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003966void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003967 // Explicit clinit checks triggered by static invokes must have been pruned by
3968 // art::PrepareForRegisterAllocation.
3969 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003970
Andreas Gampe878d58c2015-01-15 23:24:00 -08003971 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3972 if (intrinsic.TryDispatch(invoke)) {
3973 return;
3974 }
3975
Alexandre Rames67555f72014-11-18 10:55:16 +00003976 HandleInvoke(invoke);
3977}
3978
Andreas Gampe878d58c2015-01-15 23:24:00 -08003979static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3980 if (invoke->GetLocations()->Intrinsified()) {
3981 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3982 intrinsic.Dispatch(invoke);
3983 return true;
3984 }
3985 return false;
3986}
3987
Vladimir Markodc151b22015-10-15 18:02:30 +01003988HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3989 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003990 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003991 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003992 return desired_dispatch_info;
3993}
3994
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003995void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003996 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003997 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3998 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003999 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4000 uint32_t offset =
4001 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004002 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004003 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004004 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004005 }
Vladimir Marko58155012015-08-19 12:49:41 +00004006 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004007 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004008 break;
4009 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4010 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004011 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004012 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004013 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4014 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004015 const DexFile& dex_file = invoke->GetDexFile();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004016 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004017 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004018 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004019 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004020 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004021 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004022 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004023 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004024 }
Vladimir Marko58155012015-08-19 12:49:41 +00004025 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004026 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004027 Register reg = XRegisterFrom(temp);
4028 Register method_reg;
4029 if (current_method.IsRegister()) {
4030 method_reg = XRegisterFrom(current_method);
4031 } else {
4032 DCHECK(invoke->GetLocations()->Intrinsified());
4033 DCHECK(!current_method.IsValid());
4034 method_reg = reg;
4035 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4036 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004037
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004038 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004039 __ Ldr(reg.X(),
4040 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004041 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004042 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004043 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4044 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004045 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4046 break;
4047 }
4048 }
4049
4050 switch (invoke->GetCodePtrLocation()) {
4051 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4052 __ Bl(&frame_entry_label_);
4053 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004054 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4055 // LR = callee_method->entry_point_from_quick_compiled_code_;
4056 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004057 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004058 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004059 // lr()
4060 __ Blr(lr);
4061 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004062 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004063
Andreas Gampe878d58c2015-01-15 23:24:00 -08004064 DCHECK(!IsLeafMethod());
4065}
4066
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004067void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004068 // Use the calling convention instead of the location of the receiver, as
4069 // intrinsics may have put the receiver in a different register. In the intrinsics
4070 // slow path, the arguments have been moved to the right place, so here we are
4071 // guaranteed that the receiver is the first register of the calling convention.
4072 InvokeDexCallingConvention calling_convention;
4073 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004074 Register temp = XRegisterFrom(temp_in);
4075 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4076 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4077 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004078 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004079
4080 BlockPoolsScope block_pools(GetVIXLAssembler());
4081
4082 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004083 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004084 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004085 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004086 // Instead of simply (possibly) unpoisoning `temp` here, we should
4087 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004088 // intermediate/temporary reference and because the current
4089 // concurrent copying collector keeps the from-space memory
4090 // intact/accessible until the end of the marking phase (the
4091 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004092 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4093 // temp = temp->GetMethodAt(method_offset);
4094 __ Ldr(temp, MemOperand(temp, method_offset));
4095 // lr = temp->GetEntryPoint();
4096 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
4097 // lr();
4098 __ Blr(lr);
4099}
4100
Orion Hodsonac141392017-01-13 11:53:47 +00004101void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4102 HandleInvoke(invoke);
4103}
4104
4105void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4106 codegen_->GenerateInvokePolymorphicCall(invoke);
4107}
4108
Scott Wakeling97c72b72016-06-24 16:19:36 +01004109vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4110 const DexFile& dex_file,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004111 dex::StringIndex string_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004112 vixl::aarch64::Label* adrp_label) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004113 return
4114 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004115}
4116
Scott Wakeling97c72b72016-06-24 16:19:36 +01004117vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4118 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004119 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004120 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004121 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004122}
4123
Scott Wakeling97c72b72016-06-24 16:19:36 +01004124vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4125 const DexFile& dex_file,
4126 uint32_t element_offset,
4127 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004128 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4129}
4130
Scott Wakeling97c72b72016-06-24 16:19:36 +01004131vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4132 const DexFile& dex_file,
4133 uint32_t offset_or_index,
4134 vixl::aarch64::Label* adrp_label,
4135 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004136 // Add a patch entry and return the label.
4137 patches->emplace_back(dex_file, offset_or_index);
4138 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004139 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004140 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4141 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4142 return label;
4143}
4144
Scott Wakeling97c72b72016-06-24 16:19:36 +01004145vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004146 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004147 return boot_image_string_patches_.GetOrCreate(
4148 StringReference(&dex_file, string_index),
4149 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4150}
4151
Scott Wakeling97c72b72016-06-24 16:19:36 +01004152vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004153 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004154 return boot_image_type_patches_.GetOrCreate(
4155 TypeReference(&dex_file, type_index),
4156 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4157}
4158
Scott Wakeling97c72b72016-06-24 16:19:36 +01004159vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4160 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004161 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
4162 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
4163 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
4164}
4165
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004166vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004167 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4168 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4169 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004170 return jit_string_patches_.GetOrCreate(
4171 StringReference(&dex_file, string_index),
4172 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4173}
4174
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004175vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
4176 const DexFile& dex_file, dex::TypeIndex type_index, uint64_t address) {
4177 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index), address);
4178 return jit_class_patches_.GetOrCreate(
4179 TypeReference(&dex_file, type_index),
4180 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4181}
4182
Vladimir Markoaad75c62016-10-03 08:46:48 +00004183void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4184 vixl::aarch64::Register reg) {
4185 DCHECK(reg.IsX());
4186 SingleEmissionCheckScope guard(GetVIXLAssembler());
4187 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004188 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004189}
4190
4191void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4192 vixl::aarch64::Register out,
4193 vixl::aarch64::Register base) {
4194 DCHECK(out.IsX());
4195 DCHECK(base.IsX());
4196 SingleEmissionCheckScope guard(GetVIXLAssembler());
4197 __ Bind(fixup_label);
4198 __ add(out, base, Operand(/* offset placeholder */ 0));
4199}
4200
4201void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4202 vixl::aarch64::Register out,
4203 vixl::aarch64::Register base) {
4204 DCHECK(base.IsX());
4205 SingleEmissionCheckScope guard(GetVIXLAssembler());
4206 __ Bind(fixup_label);
4207 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4208}
4209
4210template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4211inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4212 const ArenaDeque<PcRelativePatchInfo>& infos,
4213 ArenaVector<LinkerPatch>* linker_patches) {
4214 for (const PcRelativePatchInfo& info : infos) {
4215 linker_patches->push_back(Factory(info.label.GetLocation(),
4216 &info.target_dex_file,
4217 info.pc_insn_label->GetLocation(),
4218 info.offset_or_index));
4219 }
4220}
4221
Vladimir Marko58155012015-08-19 12:49:41 +00004222void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4223 DCHECK(linker_patches->empty());
4224 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004225 pc_relative_dex_cache_patches_.size() +
4226 boot_image_string_patches_.size() +
4227 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004228 boot_image_type_patches_.size() +
4229 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004230 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004231 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004232 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004233 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004234 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004235 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004236 info.offset_or_index));
4237 }
4238 for (const auto& entry : boot_image_string_patches_) {
4239 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004240 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4241 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004242 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004243 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004244 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004245 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004246 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(pc_relative_type_patches_,
4247 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004248 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4249 linker_patches);
4250 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004251 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4252 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004253 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4254 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004255 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004256 for (const auto& entry : boot_image_type_patches_) {
4257 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004258 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4259 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004260 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004261 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004262 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004263 for (const auto& entry : boot_image_address_patches_) {
4264 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004265 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4266 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00004267 }
4268}
4269
Scott Wakeling97c72b72016-06-24 16:19:36 +01004270vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004271 Uint32ToLiteralMap* map) {
4272 return map->GetOrCreate(
4273 value,
4274 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4275}
4276
Scott Wakeling97c72b72016-06-24 16:19:36 +01004277vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004278 return uint64_literals_.GetOrCreate(
4279 value,
4280 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004281}
4282
Scott Wakeling97c72b72016-06-24 16:19:36 +01004283vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004284 MethodReference target_method,
4285 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004286 return map->GetOrCreate(
4287 target_method,
4288 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004289}
4290
Andreas Gampe878d58c2015-01-15 23:24:00 -08004291void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004292 // Explicit clinit checks triggered by static invokes must have been pruned by
4293 // art::PrepareForRegisterAllocation.
4294 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004295
Andreas Gampe878d58c2015-01-15 23:24:00 -08004296 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4297 return;
4298 }
4299
Alexandre Ramesd921d642015-04-16 15:07:16 +01004300 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004301 LocationSummary* locations = invoke->GetLocations();
4302 codegen_->GenerateStaticOrDirectCall(
4303 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004304 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004305}
4306
4307void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004308 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4309 return;
4310 }
4311
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004312 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004313 DCHECK(!codegen_->IsLeafMethod());
4314 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4315}
4316
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004317HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4318 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004319 switch (desired_class_load_kind) {
4320 case HLoadClass::LoadKind::kReferrersClass:
4321 break;
4322 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4323 DCHECK(!GetCompilerOptions().GetCompilePic());
4324 break;
4325 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4326 DCHECK(GetCompilerOptions().GetCompilePic());
4327 break;
4328 case HLoadClass::LoadKind::kBootImageAddress:
4329 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004330 case HLoadClass::LoadKind::kBssEntry:
4331 DCHECK(!Runtime::Current()->UseJitCompilation());
4332 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004333 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004334 DCHECK(Runtime::Current()->UseJitCompilation());
4335 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004336 case HLoadClass::LoadKind::kDexCacheViaMethod:
4337 break;
4338 }
4339 return desired_class_load_kind;
4340}
4341
Alexandre Rames67555f72014-11-18 10:55:16 +00004342void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004343 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4344 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004345 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004346 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004347 cls,
4348 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004349 LocationFrom(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004350 return;
4351 }
Vladimir Marko41559982017-01-06 14:04:23 +00004352 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004353
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004354 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4355 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004356 ? LocationSummary::kCallOnSlowPath
4357 : LocationSummary::kNoCall;
4358 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004359 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004360 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004361 }
4362
Vladimir Marko41559982017-01-06 14:04:23 +00004363 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004364 locations->SetInAt(0, Location::RequiresRegister());
4365 }
4366 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004367}
4368
4369void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004370 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4371 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4372 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004373 return;
4374 }
Vladimir Marko41559982017-01-06 14:04:23 +00004375 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004376
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004377 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004378 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004379
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004380 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4381 ? kWithoutReadBarrier
4382 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004383 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004384 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004385 case HLoadClass::LoadKind::kReferrersClass: {
4386 DCHECK(!cls->CanCallRuntime());
4387 DCHECK(!cls->MustGenerateClinitCheck());
4388 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4389 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004390 GenerateGcRootFieldLoad(cls,
4391 out_loc,
4392 current_method,
4393 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004394 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004395 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004396 break;
4397 }
4398 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004399 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004400 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4401 cls->GetTypeIndex()));
4402 break;
4403 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004404 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004405 // Add ADRP with its PC-relative type patch.
4406 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004407 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004408 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004409 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004410 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004411 vixl::aarch64::Label* add_label =
4412 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004413 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004414 break;
4415 }
4416 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004417 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004418 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4419 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4420 break;
4421 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004422 case HLoadClass::LoadKind::kBssEntry: {
4423 // Add ADRP with its PC-relative Class .bss entry patch.
4424 const DexFile& dex_file = cls->GetDexFile();
4425 dex::TypeIndex type_index = cls->GetTypeIndex();
4426 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
4427 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
4428 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
4429 // Add LDR with its PC-relative Class patch.
4430 vixl::aarch64::Label* ldr_label =
4431 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
4432 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4433 GenerateGcRootFieldLoad(cls,
4434 cls->GetLocations()->Out(),
4435 out.X(),
4436 /* placeholder */ 0u,
4437 ldr_label,
4438 kCompilerReadBarrierOption);
4439 generate_null_check = true;
4440 break;
4441 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004442 case HLoadClass::LoadKind::kJitTableAddress: {
4443 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4444 cls->GetTypeIndex(),
4445 cls->GetAddress()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004446 GenerateGcRootFieldLoad(cls,
4447 out_loc,
4448 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004449 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004450 /* fixup_label */ nullptr,
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004451 kCompilerReadBarrierOption);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004452 break;
4453 }
Vladimir Marko41559982017-01-06 14:04:23 +00004454 case HLoadClass::LoadKind::kDexCacheViaMethod:
4455 LOG(FATAL) << "UNREACHABLE";
4456 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004457 }
4458
4459 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4460 DCHECK(cls->CanCallRuntime());
4461 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4462 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4463 codegen_->AddSlowPath(slow_path);
4464 if (generate_null_check) {
4465 __ Cbz(out, slow_path->GetEntryLabel());
4466 }
4467 if (cls->MustGenerateClinitCheck()) {
4468 GenerateClassInitializationCheck(slow_path, out);
4469 } else {
4470 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004471 }
4472 }
4473}
4474
David Brazdilcb1c0552015-08-04 16:22:25 +01004475static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004476 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004477}
4478
Alexandre Rames67555f72014-11-18 10:55:16 +00004479void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4480 LocationSummary* locations =
4481 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4482 locations->SetOut(Location::RequiresRegister());
4483}
4484
4485void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004486 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4487}
4488
4489void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4490 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4491}
4492
4493void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4494 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004495}
4496
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004497HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4498 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004499 switch (desired_string_load_kind) {
4500 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4501 DCHECK(!GetCompilerOptions().GetCompilePic());
4502 break;
4503 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4504 DCHECK(GetCompilerOptions().GetCompilePic());
4505 break;
4506 case HLoadString::LoadKind::kBootImageAddress:
4507 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004508 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004509 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004510 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004511 case HLoadString::LoadKind::kJitTableAddress:
4512 DCHECK(Runtime::Current()->UseJitCompilation());
4513 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004514 case HLoadString::LoadKind::kDexCacheViaMethod:
4515 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004516 }
4517 return desired_string_load_kind;
4518}
4519
Alexandre Rames67555f72014-11-18 10:55:16 +00004520void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004521 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004522 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004523 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004524 InvokeRuntimeCallingConvention calling_convention;
4525 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4526 } else {
4527 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004528 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4529 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4530 // Rely on the pResolveString and/or marking to save everything, including temps.
4531 RegisterSet caller_saves = RegisterSet::Empty();
4532 InvokeRuntimeCallingConvention calling_convention;
4533 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4534 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4535 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4536 Primitive::kPrimNot).GetCode());
4537 locations->SetCustomSlowPathCallerSaves(caller_saves);
4538 } else {
4539 // For non-Baker read barrier we have a temp-clobbering call.
4540 }
4541 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004542 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004543}
4544
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004545// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4546// move.
4547void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00004548 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004549 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004550
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004551 switch (load->GetLoadKind()) {
4552 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004553 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4554 load->GetStringIndex()));
4555 return; // No dex cache slow path.
4556 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004557 // Add ADRP with its PC-relative String patch.
4558 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004559 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004560 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004561 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004562 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004563 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004564 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004565 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004566 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004567 return; // No dex cache slow path.
4568 }
4569 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004570 uint32_t address = dchecked_integral_cast<uint32_t>(
4571 reinterpret_cast<uintptr_t>(load->GetString().Get()));
4572 DCHECK_NE(address, 0u);
4573 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004574 return; // No dex cache slow path.
4575 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004576 case HLoadString::LoadKind::kBssEntry: {
4577 // Add ADRP with its PC-relative String .bss entry patch.
4578 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004579 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004580 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004581 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4582 Register temp = temps.AcquireX();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004583 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004584 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004585 // Add LDR with its PC-relative String patch.
4586 vixl::aarch64::Label* ldr_label =
4587 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004588 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00004589 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004590 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004591 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01004592 /* offset placeholder */ 0u,
4593 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004594 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004595 SlowPathCodeARM64* slow_path =
4596 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004597 codegen_->AddSlowPath(slow_path);
4598 __ Cbz(out.X(), slow_path->GetEntryLabel());
4599 __ Bind(slow_path->GetExitLabel());
4600 return;
4601 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004602 case HLoadString::LoadKind::kJitTableAddress: {
4603 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004604 load->GetStringIndex(),
4605 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004606 GenerateGcRootFieldLoad(load,
4607 out_loc,
4608 out.X(),
4609 /* offset */ 0,
4610 /* fixup_label */ nullptr,
4611 kCompilerReadBarrierOption);
4612 return;
4613 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004614 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004615 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004616 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004617
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004618 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004619 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004620 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004621 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004622 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4623 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004624}
4625
Alexandre Rames5319def2014-10-23 10:03:10 +01004626void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4627 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4628 locations->SetOut(Location::ConstantLocation(constant));
4629}
4630
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004631void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004632 // Will be generated at use site.
4633}
4634
Alexandre Rames67555f72014-11-18 10:55:16 +00004635void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4636 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004637 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004638 InvokeRuntimeCallingConvention calling_convention;
4639 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4640}
4641
4642void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004643 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject: kQuickUnlockObject,
4644 instruction,
4645 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004646 if (instruction->IsEnter()) {
4647 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4648 } else {
4649 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4650 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004651}
4652
Alexandre Rames42d641b2014-10-27 14:00:51 +00004653void LocationsBuilderARM64::VisitMul(HMul* mul) {
4654 LocationSummary* locations =
4655 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4656 switch (mul->GetResultType()) {
4657 case Primitive::kPrimInt:
4658 case Primitive::kPrimLong:
4659 locations->SetInAt(0, Location::RequiresRegister());
4660 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004661 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004662 break;
4663
4664 case Primitive::kPrimFloat:
4665 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004666 locations->SetInAt(0, Location::RequiresFpuRegister());
4667 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004668 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004669 break;
4670
4671 default:
4672 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4673 }
4674}
4675
4676void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4677 switch (mul->GetResultType()) {
4678 case Primitive::kPrimInt:
4679 case Primitive::kPrimLong:
4680 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4681 break;
4682
4683 case Primitive::kPrimFloat:
4684 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004685 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004686 break;
4687
4688 default:
4689 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4690 }
4691}
4692
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004693void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4694 LocationSummary* locations =
4695 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4696 switch (neg->GetResultType()) {
4697 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004698 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004699 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004700 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004701 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004702
4703 case Primitive::kPrimFloat:
4704 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004705 locations->SetInAt(0, Location::RequiresFpuRegister());
4706 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004707 break;
4708
4709 default:
4710 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4711 }
4712}
4713
4714void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4715 switch (neg->GetResultType()) {
4716 case Primitive::kPrimInt:
4717 case Primitive::kPrimLong:
4718 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4719 break;
4720
4721 case Primitive::kPrimFloat:
4722 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004723 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004724 break;
4725
4726 default:
4727 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4728 }
4729}
4730
4731void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4732 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004733 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004734 InvokeRuntimeCallingConvention calling_convention;
4735 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004736 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004737 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004738 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004739}
4740
4741void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4742 LocationSummary* locations = instruction->GetLocations();
4743 InvokeRuntimeCallingConvention calling_convention;
4744 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4745 DCHECK(type_index.Is(w0));
Andreas Gampea5b09a62016-11-17 15:21:22 -08004746 __ Mov(type_index, instruction->GetTypeIndex().index_);
Roland Levillain4d027112015-07-01 15:41:14 +01004747 // Note: if heap poisoning is enabled, the entry point takes cares
4748 // of poisoning the reference.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004749 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004750 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004751}
4752
Alexandre Rames5319def2014-10-23 10:03:10 +01004753void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4754 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004755 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004756 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004757 if (instruction->IsStringAlloc()) {
4758 locations->AddTemp(LocationFrom(kArtMethodRegister));
4759 } else {
4760 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004761 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004762 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4763}
4764
4765void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004766 // Note: if heap poisoning is enabled, the entry point takes cares
4767 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004768 if (instruction->IsStringAlloc()) {
4769 // String is allocated through StringFactory. Call NewEmptyString entry point.
4770 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004771 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004772 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4773 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4774 __ Blr(lr);
4775 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4776 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004777 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004778 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004779 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004780}
4781
4782void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4783 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004784 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004785 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004786}
4787
4788void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004789 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004790 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004791 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004792 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004793 break;
4794
4795 default:
4796 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4797 }
4798}
4799
David Brazdil66d126e2015-04-03 16:02:44 +01004800void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4801 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4802 locations->SetInAt(0, Location::RequiresRegister());
4803 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4804}
4805
4806void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004807 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004808}
4809
Alexandre Rames5319def2014-10-23 10:03:10 +01004810void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004811 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4812 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01004813}
4814
Calin Juravle2ae48182016-03-16 14:05:09 +00004815void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4816 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004817 return;
4818 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004819
Alexandre Ramesd921d642015-04-16 15:07:16 +01004820 BlockPoolsScope block_pools(GetVIXLAssembler());
4821 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004822 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004823 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004824}
4825
Calin Juravle2ae48182016-03-16 14:05:09 +00004826void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004827 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004828 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004829
4830 LocationSummary* locations = instruction->GetLocations();
4831 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004832
4833 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004834}
4835
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004836void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004837 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004838}
4839
Alexandre Rames67555f72014-11-18 10:55:16 +00004840void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4841 HandleBinaryOp(instruction);
4842}
4843
4844void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4845 HandleBinaryOp(instruction);
4846}
4847
Alexandre Rames3e69f162014-12-10 10:36:50 +00004848void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4849 LOG(FATAL) << "Unreachable";
4850}
4851
4852void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4853 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4854}
4855
Alexandre Rames5319def2014-10-23 10:03:10 +01004856void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4857 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4858 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4859 if (location.IsStackSlot()) {
4860 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4861 } else if (location.IsDoubleStackSlot()) {
4862 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4863 }
4864 locations->SetOut(location);
4865}
4866
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004867void InstructionCodeGeneratorARM64::VisitParameterValue(
4868 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004869 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004870}
4871
4872void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4873 LocationSummary* locations =
4874 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004875 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004876}
4877
4878void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4879 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4880 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004881}
4882
4883void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4884 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004885 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004886 locations->SetInAt(i, Location::Any());
4887 }
4888 locations->SetOut(Location::Any());
4889}
4890
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004891void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004892 LOG(FATAL) << "Unreachable";
4893}
4894
Serban Constantinescu02164b32014-11-13 14:05:07 +00004895void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004896 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004897 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004898 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4899 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004900 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4901
4902 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004903 case Primitive::kPrimInt:
4904 case Primitive::kPrimLong:
4905 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004906 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004907 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4908 break;
4909
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004910 case Primitive::kPrimFloat:
4911 case Primitive::kPrimDouble: {
4912 InvokeRuntimeCallingConvention calling_convention;
4913 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4914 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4915 locations->SetOut(calling_convention.GetReturnLocation(type));
4916
4917 break;
4918 }
4919
Serban Constantinescu02164b32014-11-13 14:05:07 +00004920 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004921 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004922 }
4923}
4924
4925void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4926 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004927
Serban Constantinescu02164b32014-11-13 14:05:07 +00004928 switch (type) {
4929 case Primitive::kPrimInt:
4930 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004931 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004932 break;
4933 }
4934
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004935 case Primitive::kPrimFloat:
4936 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004937 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4938 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004939 if (type == Primitive::kPrimFloat) {
4940 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4941 } else {
4942 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4943 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004944 break;
4945 }
4946
Serban Constantinescu02164b32014-11-13 14:05:07 +00004947 default:
4948 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004949 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004950 }
4951}
4952
Calin Juravle27df7582015-04-17 19:12:31 +01004953void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4954 memory_barrier->SetLocations(nullptr);
4955}
4956
4957void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004958 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004959}
4960
Alexandre Rames5319def2014-10-23 10:03:10 +01004961void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4962 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4963 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004964 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004965}
4966
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004967void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004968 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004969}
4970
4971void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4972 instruction->SetLocations(nullptr);
4973}
4974
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004975void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004976 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004977}
4978
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004979void LocationsBuilderARM64::VisitRor(HRor* ror) {
4980 HandleBinaryOp(ror);
4981}
4982
4983void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4984 HandleBinaryOp(ror);
4985}
4986
Serban Constantinescu02164b32014-11-13 14:05:07 +00004987void LocationsBuilderARM64::VisitShl(HShl* shl) {
4988 HandleShift(shl);
4989}
4990
4991void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4992 HandleShift(shl);
4993}
4994
4995void LocationsBuilderARM64::VisitShr(HShr* shr) {
4996 HandleShift(shr);
4997}
4998
4999void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5000 HandleShift(shr);
5001}
5002
Alexandre Rames5319def2014-10-23 10:03:10 +01005003void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005004 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005005}
5006
5007void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005008 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005009}
5010
Alexandre Rames67555f72014-11-18 10:55:16 +00005011void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005012 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00005013}
5014
5015void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005016 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005017}
5018
5019void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005020 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005021}
5022
Alexandre Rames67555f72014-11-18 10:55:16 +00005023void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005024 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005025}
5026
Calin Juravlee460d1d2015-09-29 04:52:17 +01005027void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5028 HUnresolvedInstanceFieldGet* instruction) {
5029 FieldAccessCallingConventionARM64 calling_convention;
5030 codegen_->CreateUnresolvedFieldLocationSummary(
5031 instruction, instruction->GetFieldType(), calling_convention);
5032}
5033
5034void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5035 HUnresolvedInstanceFieldGet* instruction) {
5036 FieldAccessCallingConventionARM64 calling_convention;
5037 codegen_->GenerateUnresolvedFieldAccess(instruction,
5038 instruction->GetFieldType(),
5039 instruction->GetFieldIndex(),
5040 instruction->GetDexPc(),
5041 calling_convention);
5042}
5043
5044void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5045 HUnresolvedInstanceFieldSet* instruction) {
5046 FieldAccessCallingConventionARM64 calling_convention;
5047 codegen_->CreateUnresolvedFieldLocationSummary(
5048 instruction, instruction->GetFieldType(), calling_convention);
5049}
5050
5051void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5052 HUnresolvedInstanceFieldSet* instruction) {
5053 FieldAccessCallingConventionARM64 calling_convention;
5054 codegen_->GenerateUnresolvedFieldAccess(instruction,
5055 instruction->GetFieldType(),
5056 instruction->GetFieldIndex(),
5057 instruction->GetDexPc(),
5058 calling_convention);
5059}
5060
5061void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5062 HUnresolvedStaticFieldGet* instruction) {
5063 FieldAccessCallingConventionARM64 calling_convention;
5064 codegen_->CreateUnresolvedFieldLocationSummary(
5065 instruction, instruction->GetFieldType(), calling_convention);
5066}
5067
5068void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5069 HUnresolvedStaticFieldGet* instruction) {
5070 FieldAccessCallingConventionARM64 calling_convention;
5071 codegen_->GenerateUnresolvedFieldAccess(instruction,
5072 instruction->GetFieldType(),
5073 instruction->GetFieldIndex(),
5074 instruction->GetDexPc(),
5075 calling_convention);
5076}
5077
5078void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5079 HUnresolvedStaticFieldSet* instruction) {
5080 FieldAccessCallingConventionARM64 calling_convention;
5081 codegen_->CreateUnresolvedFieldLocationSummary(
5082 instruction, instruction->GetFieldType(), calling_convention);
5083}
5084
5085void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5086 HUnresolvedStaticFieldSet* instruction) {
5087 FieldAccessCallingConventionARM64 calling_convention;
5088 codegen_->GenerateUnresolvedFieldAccess(instruction,
5089 instruction->GetFieldType(),
5090 instruction->GetFieldIndex(),
5091 instruction->GetDexPc(),
5092 calling_convention);
5093}
5094
Alexandre Rames5319def2014-10-23 10:03:10 +01005095void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005096 LocationSummary* locations =
5097 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005098 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01005099}
5100
5101void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005102 HBasicBlock* block = instruction->GetBlock();
5103 if (block->GetLoopInformation() != nullptr) {
5104 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5105 // The back edge will generate the suspend check.
5106 return;
5107 }
5108 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5109 // The goto will generate the suspend check.
5110 return;
5111 }
5112 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005113}
5114
Alexandre Rames67555f72014-11-18 10:55:16 +00005115void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5116 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005117 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005118 InvokeRuntimeCallingConvention calling_convention;
5119 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5120}
5121
5122void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005123 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005124 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005125}
5126
5127void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5128 LocationSummary* locations =
5129 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5130 Primitive::Type input_type = conversion->GetInputType();
5131 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005132 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005133 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5134 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5135 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5136 }
5137
Alexandre Rames542361f2015-01-29 16:57:31 +00005138 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005139 locations->SetInAt(0, Location::RequiresFpuRegister());
5140 } else {
5141 locations->SetInAt(0, Location::RequiresRegister());
5142 }
5143
Alexandre Rames542361f2015-01-29 16:57:31 +00005144 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005145 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5146 } else {
5147 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5148 }
5149}
5150
5151void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5152 Primitive::Type result_type = conversion->GetResultType();
5153 Primitive::Type input_type = conversion->GetInputType();
5154
5155 DCHECK_NE(input_type, result_type);
5156
Alexandre Rames542361f2015-01-29 16:57:31 +00005157 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005158 int result_size = Primitive::ComponentSize(result_type);
5159 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005160 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005161 Register output = OutputRegister(conversion);
5162 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005163 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005164 // 'int' values are used directly as W registers, discarding the top
5165 // bits, so we don't need to sign-extend and can just perform a move.
5166 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5167 // top 32 bits of the target register. We theoretically could leave those
5168 // bits unchanged, but we would have to make sure that no code uses a
5169 // 32bit input value as a 64bit value assuming that the top 32 bits are
5170 // zero.
5171 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005172 } else if (result_type == Primitive::kPrimChar ||
5173 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5174 __ Ubfx(output,
5175 output.IsX() ? source.X() : source.W(),
5176 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005177 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005178 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005179 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005180 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005181 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005182 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005183 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5184 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005185 } else if (Primitive::IsFloatingPointType(result_type) &&
5186 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005187 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5188 } else {
5189 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5190 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005191 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005192}
Alexandre Rames67555f72014-11-18 10:55:16 +00005193
Serban Constantinescu02164b32014-11-13 14:05:07 +00005194void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5195 HandleShift(ushr);
5196}
5197
5198void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5199 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005200}
5201
5202void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5203 HandleBinaryOp(instruction);
5204}
5205
5206void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5207 HandleBinaryOp(instruction);
5208}
5209
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005210void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005211 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005212 LOG(FATAL) << "Unreachable";
5213}
5214
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005215void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005216 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005217 LOG(FATAL) << "Unreachable";
5218}
5219
Mark Mendellfe57faa2015-09-18 09:26:15 -04005220// Simple implementation of packed switch - generate cascaded compare/jumps.
5221void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5222 LocationSummary* locations =
5223 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5224 locations->SetInAt(0, Location::RequiresRegister());
5225}
5226
5227void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5228 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005229 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005230 Register value_reg = InputRegisterAt(switch_instr, 0);
5231 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5232
Zheng Xu3927c8b2015-11-18 17:46:25 +08005233 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005234 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005235 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5236 // make sure we don't emit it if the target may run out of range.
5237 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5238 // ranges and emit the tables only as required.
5239 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005240
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005241 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005242 // Current instruction id is an upper bound of the number of HIRs in the graph.
5243 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5244 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005245 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5246 Register temp = temps.AcquireW();
5247 __ Subs(temp, value_reg, Operand(lower_bound));
5248
Zheng Xu3927c8b2015-11-18 17:46:25 +08005249 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005250 // Jump to successors[0] if value == lower_bound.
5251 __ B(eq, codegen_->GetLabelOf(successors[0]));
5252 int32_t last_index = 0;
5253 for (; num_entries - last_index > 2; last_index += 2) {
5254 __ Subs(temp, temp, Operand(2));
5255 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5256 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5257 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5258 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5259 }
5260 if (num_entries - last_index == 2) {
5261 // The last missing case_value.
5262 __ Cmp(temp, Operand(1));
5263 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005264 }
5265
5266 // And the default for any other value.
5267 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5268 __ B(codegen_->GetLabelOf(default_block));
5269 }
5270 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005271 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005272
5273 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5274
5275 // Below instructions should use at most one blocked register. Since there are two blocked
5276 // registers, we are free to block one.
5277 Register temp_w = temps.AcquireW();
5278 Register index;
5279 // Remove the bias.
5280 if (lower_bound != 0) {
5281 index = temp_w;
5282 __ Sub(index, value_reg, Operand(lower_bound));
5283 } else {
5284 index = value_reg;
5285 }
5286
5287 // Jump to default block if index is out of the range.
5288 __ Cmp(index, Operand(num_entries));
5289 __ B(hs, codegen_->GetLabelOf(default_block));
5290
5291 // In current VIXL implementation, it won't require any blocked registers to encode the
5292 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5293 // register pressure.
5294 Register table_base = temps.AcquireX();
5295 // Load jump offset from the table.
5296 __ Adr(table_base, jump_table->GetTableStartLabel());
5297 Register jump_offset = temp_w;
5298 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5299
5300 // Jump to target block by branching to table_base(pc related) + offset.
5301 Register target_address = table_base;
5302 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5303 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005304 }
5305}
5306
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005307void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5308 HInstruction* instruction,
5309 Location out,
5310 uint32_t offset,
5311 Location maybe_temp,
5312 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005313 Primitive::Type type = Primitive::kPrimNot;
5314 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005315 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005316 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005317 Register temp_reg = RegisterFrom(maybe_temp, type);
5318 if (kUseBakerReadBarrier) {
5319 // Load with fast path based Baker's read barrier.
5320 // /* HeapReference<Object> */ out = *(out + offset)
5321 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5322 out,
5323 out_reg,
5324 offset,
5325 temp_reg,
5326 /* needs_null_check */ false,
5327 /* use_load_acquire */ false);
5328 } else {
5329 // Load with slow path based read barrier.
5330 // Save the value of `out` into `maybe_temp` before overwriting it
5331 // in the following move operation, as we will need it for the
5332 // read barrier below.
5333 __ Mov(temp_reg, out_reg);
5334 // /* HeapReference<Object> */ out = *(out + offset)
5335 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5336 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5337 }
5338 } else {
5339 // Plain load with no read barrier.
5340 // /* HeapReference<Object> */ out = *(out + offset)
5341 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5342 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5343 }
5344}
5345
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005346void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5347 HInstruction* instruction,
5348 Location out,
5349 Location obj,
5350 uint32_t offset,
5351 Location maybe_temp,
5352 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005353 Primitive::Type type = Primitive::kPrimNot;
5354 Register out_reg = RegisterFrom(out, type);
5355 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005356 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005357 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005358 if (kUseBakerReadBarrier) {
5359 // Load with fast path based Baker's read barrier.
5360 Register temp_reg = RegisterFrom(maybe_temp, type);
5361 // /* HeapReference<Object> */ out = *(obj + offset)
5362 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5363 out,
5364 obj_reg,
5365 offset,
5366 temp_reg,
5367 /* needs_null_check */ false,
5368 /* use_load_acquire */ false);
5369 } else {
5370 // Load with slow path based read barrier.
5371 // /* HeapReference<Object> */ out = *(obj + offset)
5372 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5373 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5374 }
5375 } else {
5376 // Plain load with no read barrier.
5377 // /* HeapReference<Object> */ out = *(obj + offset)
5378 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5379 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5380 }
5381}
5382
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005383void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5384 HInstruction* instruction,
5385 Location root,
5386 Register obj,
5387 uint32_t offset,
5388 vixl::aarch64::Label* fixup_label,
5389 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005390 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005391 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005392 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005393 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005394 if (kUseBakerReadBarrier) {
5395 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5396 // Baker's read barrier are used:
5397 //
5398 // root = obj.field;
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005399 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5400 // if (temp != null) {
5401 // root = temp(root)
Roland Levillain44015862016-01-22 11:47:17 +00005402 // }
5403
5404 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005405 if (fixup_label == nullptr) {
5406 __ Ldr(root_reg, MemOperand(obj, offset));
5407 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005408 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005409 }
Roland Levillain44015862016-01-22 11:47:17 +00005410 static_assert(
5411 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5412 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5413 "have different sizes.");
5414 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5415 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5416 "have different sizes.");
5417
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005418 Register temp = lr;
Roland Levillain44015862016-01-22 11:47:17 +00005419
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005420 // Slow path marking the GC root `root`. The entrypoint will alrady be loaded in temp.
5421 SlowPathCodeARM64* slow_path =
5422 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction,
5423 root,
5424 LocationFrom(temp));
5425 codegen_->AddSlowPath(slow_path);
5426 const int32_t entry_point_offset =
5427 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5428 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5429 // Loading the entrypoint does not require a load acquire since it is only changed when
5430 // threads are suspended or running a checkpoint.
5431 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5432 // The entrypoint is null when the GC is not marking, this prevents one load compared to
5433 // checking GetIsGcMarking.
Roland Levillain44015862016-01-22 11:47:17 +00005434 __ Cbnz(temp, slow_path->GetEntryLabel());
5435 __ Bind(slow_path->GetExitLabel());
5436 } else {
5437 // GC root loaded through a slow path for read barriers other
5438 // than Baker's.
5439 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005440 if (fixup_label == nullptr) {
5441 __ Add(root_reg.X(), obj.X(), offset);
5442 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005443 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005444 }
Roland Levillain44015862016-01-22 11:47:17 +00005445 // /* mirror::Object* */ root = root->Read()
5446 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5447 }
5448 } else {
5449 // Plain GC root load with no read barrier.
5450 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005451 if (fixup_label == nullptr) {
5452 __ Ldr(root_reg, MemOperand(obj, offset));
5453 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005454 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005455 }
Roland Levillain44015862016-01-22 11:47:17 +00005456 // Note that GC roots are not affected by heap poisoning, thus we
5457 // do not have to unpoison `root_reg` here.
5458 }
5459}
5460
5461void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5462 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005463 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005464 uint32_t offset,
5465 Register temp,
5466 bool needs_null_check,
5467 bool use_load_acquire) {
5468 DCHECK(kEmitCompilerReadBarrier);
5469 DCHECK(kUseBakerReadBarrier);
5470
5471 // /* HeapReference<Object> */ ref = *(obj + offset)
5472 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005473 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01005474 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5475 ref,
5476 obj,
5477 offset,
5478 no_index,
5479 no_scale_factor,
5480 temp,
5481 needs_null_check,
5482 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005483}
5484
5485void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5486 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005487 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005488 uint32_t data_offset,
5489 Location index,
5490 Register temp,
5491 bool needs_null_check) {
5492 DCHECK(kEmitCompilerReadBarrier);
5493 DCHECK(kUseBakerReadBarrier);
5494
5495 // Array cells are never volatile variables, therefore array loads
5496 // never use Load-Acquire instructions on ARM64.
5497 const bool use_load_acquire = false;
5498
Roland Levillainbfea3352016-06-23 13:48:47 +01005499 static_assert(
5500 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5501 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005502 // /* HeapReference<Object> */ ref =
5503 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005504 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5505 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5506 ref,
5507 obj,
5508 data_offset,
5509 index,
5510 scale_factor,
5511 temp,
5512 needs_null_check,
5513 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005514}
5515
5516void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5517 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005518 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005519 uint32_t offset,
5520 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005521 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005522 Register temp,
5523 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005524 bool use_load_acquire,
5525 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00005526 DCHECK(kEmitCompilerReadBarrier);
5527 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005528 // If we are emitting an array load, we should not be using a
5529 // Load Acquire instruction. In other words:
5530 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5531 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005532
5533 MacroAssembler* masm = GetVIXLAssembler();
5534 UseScratchRegisterScope temps(masm);
5535
5536 // In slow path based read barriers, the read barrier call is
5537 // inserted after the original load. However, in fast path based
5538 // Baker's read barriers, we need to perform the load of
5539 // mirror::Object::monitor_ *before* the original reference load.
5540 // This load-load ordering is required by the read barrier.
5541 // The fast path/slow path (for Baker's algorithm) should look like:
5542 //
5543 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5544 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5545 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005546 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain44015862016-01-22 11:47:17 +00005547 // if (is_gray) {
5548 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5549 // }
5550 //
5551 // Note: the original implementation in ReadBarrier::Barrier is
5552 // slightly more complex as it performs additional checks that we do
5553 // not do here for performance reasons.
5554
5555 Primitive::Type type = Primitive::kPrimNot;
5556 Register ref_reg = RegisterFrom(ref, type);
5557 DCHECK(obj.IsW());
5558 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5559
5560 // /* int32_t */ monitor = obj->monitor_
5561 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5562 if (needs_null_check) {
5563 MaybeRecordImplicitNullCheck(instruction);
5564 }
5565 // /* LockWord */ lock_word = LockWord(monitor)
5566 static_assert(sizeof(LockWord) == sizeof(int32_t),
5567 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005568
Vladimir Marko877a0332016-07-11 19:30:56 +01005569 // Introduce a dependency on the lock_word including rb_state,
5570 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005571 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005572 // `obj` is unchanged by this operation, but its value now depends
5573 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005574 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005575
5576 // The actual reference load.
5577 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005578 // Load types involving an "index": ArrayGet,
5579 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5580 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01005581 if (use_load_acquire) {
5582 // UnsafeGetObjectVolatile intrinsic case.
5583 // Register `index` is not an index in an object array, but an
5584 // offset to an object reference field within object `obj`.
5585 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5586 DCHECK(instruction->GetLocations()->Intrinsified());
5587 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5588 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005589 DCHECK_EQ(offset, 0u);
5590 DCHECK_EQ(scale_factor, 0u);
5591 DCHECK_EQ(needs_null_check, 0u);
Roland Levillainbfea3352016-06-23 13:48:47 +01005592 // /* HeapReference<Object> */ ref = *(obj + index)
5593 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5594 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005595 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005596 // ArrayGet and UnsafeGetObject intrinsics cases.
5597 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5598 if (index.IsConstant()) {
5599 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5600 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5601 } else {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005602 Register temp3 = temps.AcquireW();
5603 __ Add(temp3, obj, offset);
5604 Load(type, ref_reg, HeapOperand(temp3, XRegisterFrom(index), LSL, scale_factor));
5605 temps.Release(temp3);
Roland Levillainbfea3352016-06-23 13:48:47 +01005606 }
Roland Levillain44015862016-01-22 11:47:17 +00005607 }
Roland Levillain44015862016-01-22 11:47:17 +00005608 } else {
5609 // /* HeapReference<Object> */ ref = *(obj + offset)
5610 MemOperand field = HeapOperand(obj, offset);
5611 if (use_load_acquire) {
5612 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5613 } else {
5614 Load(type, ref_reg, field);
5615 }
5616 }
5617
5618 // Object* ref = ref_addr->AsMirrorPtr()
5619 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5620
Vladimir Marko953437b2016-08-24 08:30:46 +00005621 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005622 SlowPathCodeARM64* slow_path;
5623 if (always_update_field) {
5624 // ReadBarrierMarkAndUpdateFieldSlowPathARM64 only supports
5625 // address of the form `obj + field_offset`, where `obj` is a
5626 // register and `field_offset` is a register. Thus `offset` and
5627 // `scale_factor` above are expected to be null in this code path.
5628 DCHECK_EQ(offset, 0u);
5629 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
5630 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM64(
5631 instruction, ref, obj, /* field_offset */ index, temp);
5632 } else {
5633 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
5634 }
Roland Levillain44015862016-01-22 11:47:17 +00005635 AddSlowPath(slow_path);
5636
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005637 // if (rb_state == ReadBarrier::GrayState())
Roland Levillain44015862016-01-22 11:47:17 +00005638 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005639 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005640 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5641 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko877a0332016-07-11 19:30:56 +01005642 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005643 __ Bind(slow_path->GetExitLabel());
5644}
5645
5646void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5647 Location out,
5648 Location ref,
5649 Location obj,
5650 uint32_t offset,
5651 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005652 DCHECK(kEmitCompilerReadBarrier);
5653
Roland Levillain44015862016-01-22 11:47:17 +00005654 // Insert a slow path based read barrier *after* the reference load.
5655 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005656 // If heap poisoning is enabled, the unpoisoning of the loaded
5657 // reference will be carried out by the runtime within the slow
5658 // path.
5659 //
5660 // Note that `ref` currently does not get unpoisoned (when heap
5661 // poisoning is enabled), which is alright as the `ref` argument is
5662 // not used by the artReadBarrierSlow entry point.
5663 //
5664 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5665 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5666 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5667 AddSlowPath(slow_path);
5668
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005669 __ B(slow_path->GetEntryLabel());
5670 __ Bind(slow_path->GetExitLabel());
5671}
5672
Roland Levillain44015862016-01-22 11:47:17 +00005673void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5674 Location out,
5675 Location ref,
5676 Location obj,
5677 uint32_t offset,
5678 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005679 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005680 // Baker's read barriers shall be handled by the fast path
5681 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5682 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005683 // If heap poisoning is enabled, unpoisoning will be taken care of
5684 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005685 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005686 } else if (kPoisonHeapReferences) {
5687 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5688 }
5689}
5690
Roland Levillain44015862016-01-22 11:47:17 +00005691void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5692 Location out,
5693 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005694 DCHECK(kEmitCompilerReadBarrier);
5695
Roland Levillain44015862016-01-22 11:47:17 +00005696 // Insert a slow path based read barrier *after* the GC root load.
5697 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005698 // Note that GC roots are not affected by heap poisoning, so we do
5699 // not need to do anything special for this here.
5700 SlowPathCodeARM64* slow_path =
5701 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5702 AddSlowPath(slow_path);
5703
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005704 __ B(slow_path->GetEntryLabel());
5705 __ Bind(slow_path->GetExitLabel());
5706}
5707
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005708void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5709 LocationSummary* locations =
5710 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5711 locations->SetInAt(0, Location::RequiresRegister());
5712 locations->SetOut(Location::RequiresRegister());
5713}
5714
5715void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5716 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005717 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005718 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005719 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005720 __ Ldr(XRegisterFrom(locations->Out()),
5721 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005722 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005723 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005724 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005725 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5726 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005727 __ Ldr(XRegisterFrom(locations->Out()),
5728 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005729 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005730}
5731
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005732static void PatchJitRootUse(uint8_t* code,
5733 const uint8_t* roots_data,
5734 vixl::aarch64::Literal<uint32_t>* literal,
5735 uint64_t index_in_table) {
5736 uint32_t literal_offset = literal->GetOffset();
5737 uintptr_t address =
5738 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
5739 uint8_t* data = code + literal_offset;
5740 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
5741}
5742
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005743void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
5744 for (const auto& entry : jit_string_patches_) {
5745 const auto& it = jit_string_roots_.find(entry.first);
5746 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005747 PatchJitRootUse(code, roots_data, entry.second, it->second);
5748 }
5749 for (const auto& entry : jit_class_patches_) {
5750 const auto& it = jit_class_roots_.find(entry.first);
5751 DCHECK(it != jit_class_roots_.end());
5752 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005753 }
5754}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005755
Alexandre Rames67555f72014-11-18 10:55:16 +00005756#undef __
5757#undef QUICK_ENTRY_POINT
5758
Alexandre Rames5319def2014-10-23 10:03:10 +01005759} // namespace arm64
5760} // namespace art