blob: 5d002674d8c7daba9cf51ea62a6ce7989c34b334 [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)
David Srbecky9cd6d372016-02-09 15:24:47 +0000279 : SlowPathCodeARM64(at), cls_(cls), at_(at), 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 {
284 LocationSummary* locations = at_->GetLocations();
285 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;
291 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000292 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
293 : kQuickInitializeType;
294 arm64_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800295 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100296 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800297 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100298 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800299 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000300
301 // Move the class to the desired location.
302 Location out = locations->Out();
303 if (out.IsValid()) {
304 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
305 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000306 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000307 }
308
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000309 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000310 __ B(GetExitLabel());
311 }
312
Alexandre Rames9931f312015-06-19 14:47:01 +0100313 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
314
Alexandre Rames67555f72014-11-18 10:55:16 +0000315 private:
316 // The class this slow path will load.
317 HLoadClass* const cls_;
318
319 // The instruction where this slow path is happening.
320 // (Might be the load class or an initialization check).
321 HInstruction* const at_;
322
323 // The dex PC of `at_`.
324 const uint32_t dex_pc_;
325
326 // Whether to initialize the class.
327 const bool do_clinit_;
328
329 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
330};
331
Vladimir Marko63dccbb2016-09-21 13:51:10 +0100332class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
333 public:
334 explicit LoadStringSlowPathARM64(HLoadString* instruction) : SlowPathCodeARM64(instruction) {}
335
336 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
337 LocationSummary* locations = instruction_->GetLocations();
338 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
339 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
340
341 __ Bind(GetEntryLabel());
342 SaveLiveRegisters(codegen, locations);
343
344 InvokeRuntimeCallingConvention calling_convention;
345 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
346 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index);
347 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
348 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
349 Primitive::Type type = instruction_->GetType();
350 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
351
352 RestoreLiveRegisters(codegen, locations);
353
354 // Store the resolved String to the BSS entry.
355 UseScratchRegisterScope temps(arm64_codegen->GetVIXLAssembler());
356 Register temp = temps.AcquireX();
357 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
358 // TODO: Change art_quick_resolve_string to kSaveEverything and use a temporary
359 // for the ADRP in the fast path, so that we can avoid the ADRP here.
360 vixl::aarch64::Label* adrp_label =
361 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
362 arm64_codegen->EmitAdrpPlaceholder(adrp_label, temp);
363 vixl::aarch64::Label* strp_label =
364 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
365 {
366 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
367 __ Bind(strp_label);
368 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
369 MemOperand(temp, /* offset placeholder */ 0));
370 }
371
372 __ B(GetExitLabel());
373 }
374
375 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
376
377 private:
378 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
379};
380
Alexandre Rames5319def2014-10-23 10:03:10 +0100381class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
382 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000383 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100384
Alexandre Rames67555f72014-11-18 10:55:16 +0000385 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
386 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100387 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000388 if (instruction_->CanThrowIntoCatchBlock()) {
389 // Live registers will be restored in the catch block if caught.
390 SaveLiveRegisters(codegen, instruction_->GetLocations());
391 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000392 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
393 instruction_,
394 instruction_->GetDexPc(),
395 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800396 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100397 }
398
Alexandre Rames8158f282015-08-07 10:26:17 +0100399 bool IsFatal() const OVERRIDE { return true; }
400
Alexandre Rames9931f312015-06-19 14:47:01 +0100401 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
402
Alexandre Rames5319def2014-10-23 10:03:10 +0100403 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100404 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
405};
406
407class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
408 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100409 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000410 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100411
Alexandre Rames67555f72014-11-18 10:55:16 +0000412 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
413 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100414 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000415 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800416 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000417 if (successor_ == nullptr) {
418 __ B(GetReturnLabel());
419 } else {
420 __ B(arm64_codegen->GetLabelOf(successor_));
421 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100422 }
423
Scott Wakeling97c72b72016-06-24 16:19:36 +0100424 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100425 DCHECK(successor_ == nullptr);
426 return &return_label_;
427 }
428
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100429 HBasicBlock* GetSuccessor() const {
430 return successor_;
431 }
432
Alexandre Rames9931f312015-06-19 14:47:01 +0100433 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
434
Alexandre Rames5319def2014-10-23 10:03:10 +0100435 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100436 // If not null, the block to branch to after the suspend check.
437 HBasicBlock* const successor_;
438
439 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100440 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100441
442 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
443};
444
Alexandre Rames67555f72014-11-18 10:55:16 +0000445class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
446 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000447 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000448 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000449
450 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000451 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100452 Location class_to_check = locations->InAt(1);
453 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
454 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000455 DCHECK(instruction_->IsCheckCast()
456 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
457 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100458 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000459
Alexandre Rames67555f72014-11-18 10:55:16 +0000460 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000461
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000462 if (!is_fatal_) {
463 SaveLiveRegisters(codegen, locations);
464 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000465
466 // We're moving two locations to locations that could overlap, so we need a parallel
467 // move resolver.
468 InvokeRuntimeCallingConvention calling_convention;
469 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100470 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
471 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000472
473 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000474 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Andreas Gampe67409972016-07-19 22:34:53 -0700475 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t,
Roland Levillain888d0672015-11-23 18:53:50 +0000476 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000477 Primitive::Type ret_type = instruction_->GetType();
478 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
479 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
480 } else {
481 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000482 arm64_codegen->InvokeRuntime(kQuickCheckCast, instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800483 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000484 }
485
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000486 if (!is_fatal_) {
487 RestoreLiveRegisters(codegen, locations);
488 __ B(GetExitLabel());
489 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000490 }
491
Alexandre Rames9931f312015-06-19 14:47:01 +0100492 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100493 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100494
Alexandre Rames67555f72014-11-18 10:55:16 +0000495 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000496 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000497
Alexandre Rames67555f72014-11-18 10:55:16 +0000498 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
499};
500
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700501class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
502 public:
Aart Bik42249c32016-01-07 15:33:50 -0800503 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000504 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700505
506 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800507 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700508 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000509 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000510 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700511 }
512
Alexandre Rames9931f312015-06-19 14:47:01 +0100513 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
514
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700515 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700516 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
517};
518
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100519class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
520 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000521 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100522
523 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
524 LocationSummary* locations = instruction_->GetLocations();
525 __ Bind(GetEntryLabel());
526 SaveLiveRegisters(codegen, locations);
527
528 InvokeRuntimeCallingConvention calling_convention;
529 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
530 parallel_move.AddMove(
531 locations->InAt(0),
532 LocationFrom(calling_convention.GetRegisterAt(0)),
533 Primitive::kPrimNot,
534 nullptr);
535 parallel_move.AddMove(
536 locations->InAt(1),
537 LocationFrom(calling_convention.GetRegisterAt(1)),
538 Primitive::kPrimInt,
539 nullptr);
540 parallel_move.AddMove(
541 locations->InAt(2),
542 LocationFrom(calling_convention.GetRegisterAt(2)),
543 Primitive::kPrimNot,
544 nullptr);
545 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
546
547 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000548 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100549 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
550 RestoreLiveRegisters(codegen, locations);
551 __ B(GetExitLabel());
552 }
553
554 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
555
556 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100557 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
558};
559
Zheng Xu3927c8b2015-11-18 17:46:25 +0800560void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
561 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000562 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800563
564 // We are about to use the assembler to place literals directly. Make sure we have enough
565 // underlying code buffer and we have generated the jump table with right size.
566 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
567 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
568
569 __ Bind(&table_start_);
570 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
571 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100572 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800573 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100574 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800575 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
576 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
577 Literal<int32_t> literal(jump_offset);
578 __ place(&literal);
579 }
580}
581
Roland Levillain44015862016-01-22 11:47:17 +0000582// Slow path marking an object during a read barrier.
583class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
584 public:
Roland Levillain02b75802016-07-13 11:54:35 +0100585 ReadBarrierMarkSlowPathARM64(HInstruction* instruction, Location obj)
586 : SlowPathCodeARM64(instruction), obj_(obj) {
Roland Levillain44015862016-01-22 11:47:17 +0000587 DCHECK(kEmitCompilerReadBarrier);
588 }
589
590 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
591
592 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
593 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000594 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100595 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(obj_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000596 DCHECK(instruction_->IsInstanceFieldGet() ||
597 instruction_->IsStaticFieldGet() ||
598 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100599 instruction_->IsArraySet() ||
Roland Levillain44015862016-01-22 11:47:17 +0000600 instruction_->IsLoadClass() ||
601 instruction_->IsLoadString() ||
602 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100603 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100604 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
605 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000606 << "Unexpected instruction in read barrier marking slow path: "
607 << instruction_->DebugName();
608
609 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100610 // No need to save live registers; it's taken care of by the
611 // entrypoint. Also, there is no need to update the stack mask,
612 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000613 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100614 DCHECK_NE(obj_.reg(), LR);
615 DCHECK_NE(obj_.reg(), WSP);
616 DCHECK_NE(obj_.reg(), WZR);
Roland Levillain0b671c02016-08-19 12:02:34 +0100617 // IP0 is used internally by the ReadBarrierMarkRegX entry point
618 // as a temporary, it cannot be the entry point's input/output.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700619 DCHECK_NE(obj_.reg(), IP0);
Roland Levillain02b75802016-07-13 11:54:35 +0100620 DCHECK(0 <= obj_.reg() && obj_.reg() < kNumberOfWRegisters) << obj_.reg();
621 // "Compact" slow path, saving two moves.
622 //
623 // Instead of using the standard runtime calling convention (input
624 // and output in W0):
625 //
626 // W0 <- obj
627 // W0 <- ReadBarrierMark(W0)
628 // obj <- W0
629 //
630 // we just use rX (the register holding `obj`) as input and output
631 // of a dedicated entrypoint:
632 //
633 // rX <- ReadBarrierMarkRegX(rX)
634 //
635 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700636 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(obj_.reg());
Roland Levillaindec8f632016-07-22 17:10:06 +0100637 // This runtime call does not require a stack map.
638 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain44015862016-01-22 11:47:17 +0000639 __ B(GetExitLabel());
640 }
641
642 private:
Roland Levillain44015862016-01-22 11:47:17 +0000643 const Location obj_;
644
645 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
646};
647
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000648// Slow path generating a read barrier for a heap reference.
649class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
650 public:
651 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
652 Location out,
653 Location ref,
654 Location obj,
655 uint32_t offset,
656 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000657 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000658 out_(out),
659 ref_(ref),
660 obj_(obj),
661 offset_(offset),
662 index_(index) {
663 DCHECK(kEmitCompilerReadBarrier);
664 // If `obj` is equal to `out` or `ref`, it means the initial object
665 // has been overwritten by (or after) the heap object reference load
666 // to be instrumented, e.g.:
667 //
668 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000669 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000670 //
671 // In that case, we have lost the information about the original
672 // object, and the emitted read barrier cannot work properly.
673 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
674 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
675 }
676
677 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
678 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
679 LocationSummary* locations = instruction_->GetLocations();
680 Primitive::Type type = Primitive::kPrimNot;
681 DCHECK(locations->CanCall());
682 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100683 DCHECK(instruction_->IsInstanceFieldGet() ||
684 instruction_->IsStaticFieldGet() ||
685 instruction_->IsArrayGet() ||
686 instruction_->IsInstanceOf() ||
687 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100688 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000689 << "Unexpected instruction in read barrier for heap reference slow path: "
690 << instruction_->DebugName();
Roland Levillain4a3aa572016-08-15 13:17:06 +0000691 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000692 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100693 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000694
695 __ Bind(GetEntryLabel());
696
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000697 SaveLiveRegisters(codegen, locations);
698
699 // We may have to change the index's value, but as `index_` is a
700 // constant member (like other "inputs" of this slow path),
701 // introduce a copy of it, `index`.
702 Location index = index_;
703 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100704 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000705 if (instruction_->IsArrayGet()) {
706 // Compute the actual memory offset and store it in `index`.
707 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
708 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
709 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
710 // We are about to change the value of `index_reg` (see the
711 // calls to vixl::MacroAssembler::Lsl and
712 // vixl::MacroAssembler::Mov below), but it has
713 // not been saved by the previous call to
714 // art::SlowPathCode::SaveLiveRegisters, as it is a
715 // callee-save register --
716 // art::SlowPathCode::SaveLiveRegisters does not consider
717 // callee-save registers, as it has been designed with the
718 // assumption that callee-save registers are supposed to be
719 // handled by the called function. So, as a callee-save
720 // register, `index_reg` _would_ eventually be saved onto
721 // the stack, but it would be too late: we would have
722 // changed its value earlier. Therefore, we manually save
723 // it here into another freely available register,
724 // `free_reg`, chosen of course among the caller-save
725 // registers (as a callee-save `free_reg` register would
726 // exhibit the same problem).
727 //
728 // Note we could have requested a temporary register from
729 // the register allocator instead; but we prefer not to, as
730 // this is a slow path, and we know we can find a
731 // caller-save register that is available.
732 Register free_reg = FindAvailableCallerSaveRegister(codegen);
733 __ Mov(free_reg.W(), index_reg);
734 index_reg = free_reg;
735 index = LocationFrom(index_reg);
736 } else {
737 // The initial register stored in `index_` has already been
738 // saved in the call to art::SlowPathCode::SaveLiveRegisters
739 // (as it is not a callee-save register), so we can freely
740 // use it.
741 }
742 // Shifting the index value contained in `index_reg` by the scale
743 // factor (2) cannot overflow in practice, as the runtime is
744 // unable to allocate object arrays with a size larger than
745 // 2^26 - 1 (that is, 2^28 - 4 bytes).
746 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
747 static_assert(
748 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
749 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
750 __ Add(index_reg, index_reg, Operand(offset_));
751 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100752 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
753 // intrinsics, `index_` is not shifted by a scale factor of 2
754 // (as in the case of ArrayGet), as it is actually an offset
755 // to an object field within an object.
756 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000757 DCHECK(instruction_->GetLocations()->Intrinsified());
758 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
759 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
760 << instruction_->AsInvoke()->GetIntrinsic();
761 DCHECK_EQ(offset_, 0U);
Roland Levillaina7426c62016-08-03 15:02:10 +0100762 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000763 }
764 }
765
766 // We're moving two or three locations to locations that could
767 // overlap, so we need a parallel move resolver.
768 InvokeRuntimeCallingConvention calling_convention;
769 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
770 parallel_move.AddMove(ref_,
771 LocationFrom(calling_convention.GetRegisterAt(0)),
772 type,
773 nullptr);
774 parallel_move.AddMove(obj_,
775 LocationFrom(calling_convention.GetRegisterAt(1)),
776 type,
777 nullptr);
778 if (index.IsValid()) {
779 parallel_move.AddMove(index,
780 LocationFrom(calling_convention.GetRegisterAt(2)),
781 Primitive::kPrimInt,
782 nullptr);
783 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
784 } else {
785 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
786 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
787 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000788 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000789 instruction_,
790 instruction_->GetDexPc(),
791 this);
792 CheckEntrypointTypes<
793 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
794 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
795
796 RestoreLiveRegisters(codegen, locations);
797
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000798 __ B(GetExitLabel());
799 }
800
801 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
802
803 private:
804 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100805 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
806 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000807 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
808 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
809 return Register(VIXLRegCodeFromART(i), kXRegSize);
810 }
811 }
812 // We shall never fail to find a free caller-save register, as
813 // there are more than two core caller-save registers on ARM64
814 // (meaning it is possible to find one which is different from
815 // `ref` and `obj`).
816 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
817 LOG(FATAL) << "Could not find a free register";
818 UNREACHABLE();
819 }
820
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000821 const Location out_;
822 const Location ref_;
823 const Location obj_;
824 const uint32_t offset_;
825 // An additional location containing an index to an array.
826 // Only used for HArrayGet and the UnsafeGetObject &
827 // UnsafeGetObjectVolatile intrinsics.
828 const Location index_;
829
830 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
831};
832
833// Slow path generating a read barrier for a GC root.
834class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
835 public:
836 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000837 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000838 DCHECK(kEmitCompilerReadBarrier);
839 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000840
841 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
842 LocationSummary* locations = instruction_->GetLocations();
843 Primitive::Type type = Primitive::kPrimNot;
844 DCHECK(locations->CanCall());
845 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000846 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
847 << "Unexpected instruction in read barrier for GC root slow path: "
848 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000849
850 __ Bind(GetEntryLabel());
851 SaveLiveRegisters(codegen, locations);
852
853 InvokeRuntimeCallingConvention calling_convention;
854 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
855 // The argument of the ReadBarrierForRootSlow is not a managed
856 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
857 // thus we need a 64-bit move here, and we cannot use
858 //
859 // arm64_codegen->MoveLocation(
860 // LocationFrom(calling_convention.GetRegisterAt(0)),
861 // root_,
862 // type);
863 //
864 // which would emit a 32-bit move, as `type` is a (32-bit wide)
865 // reference type (`Primitive::kPrimNot`).
866 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000867 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000868 instruction_,
869 instruction_->GetDexPc(),
870 this);
871 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
872 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
873
874 RestoreLiveRegisters(codegen, locations);
875 __ B(GetExitLabel());
876 }
877
878 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
879
880 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000881 const Location out_;
882 const Location root_;
883
884 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
885};
886
Alexandre Rames5319def2014-10-23 10:03:10 +0100887#undef __
888
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100889Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100890 Location next_location;
891 if (type == Primitive::kPrimVoid) {
892 LOG(FATAL) << "Unreachable type " << type;
893 }
894
Alexandre Rames542361f2015-01-29 16:57:31 +0000895 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100896 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
897 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000898 } else if (!Primitive::IsFloatingPointType(type) &&
899 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000900 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
901 } else {
902 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000903 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
904 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100905 }
906
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000907 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000908 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100909 return next_location;
910}
911
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100912Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100913 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100914}
915
Serban Constantinescu579885a2015-02-22 20:51:33 +0000916CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
917 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100918 const CompilerOptions& compiler_options,
919 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100920 : CodeGenerator(graph,
921 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000922 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000923 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100924 callee_saved_core_registers.GetList(),
925 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100926 compiler_options,
927 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100928 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800929 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100930 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000931 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000932 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100933 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000934 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000935 uint32_literals_(std::less<uint32_t>(),
936 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100937 uint64_literals_(std::less<uint64_t>(),
938 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
939 method_patches_(MethodReferenceComparator(),
940 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
941 call_patches_(MethodReferenceComparator(),
942 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
943 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000944 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
945 boot_image_string_patches_(StringReferenceValueComparator(),
946 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
947 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100948 boot_image_type_patches_(TypeReferenceValueComparator(),
949 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
950 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000951 boot_image_address_patches_(std::less<uint32_t>(),
952 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000953 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000954 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000955}
Alexandre Rames5319def2014-10-23 10:03:10 +0100956
Alexandre Rames67555f72014-11-18 10:55:16 +0000957#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100958
Zheng Xu3927c8b2015-11-18 17:46:25 +0800959void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100960 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800961 jump_table->EmitTable(this);
962 }
963}
964
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000965void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800966 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000967 // Ensure we emit the literal pool.
968 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000969
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000970 CodeGenerator::Finalize(allocator);
971}
972
Zheng Xuad4450e2015-04-17 18:48:56 +0800973void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
974 // Note: There are 6 kinds of moves:
975 // 1. constant -> GPR/FPR (non-cycle)
976 // 2. constant -> stack (non-cycle)
977 // 3. GPR/FPR -> GPR/FPR
978 // 4. GPR/FPR -> stack
979 // 5. stack -> GPR/FPR
980 // 6. stack -> stack (non-cycle)
981 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
982 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
983 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
984 // dependency.
985 vixl_temps_.Open(GetVIXLAssembler());
986}
987
988void ParallelMoveResolverARM64::FinishEmitNativeCode() {
989 vixl_temps_.Close();
990}
991
992Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
993 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
994 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
995 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
996 Location scratch = GetScratchLocation(kind);
997 if (!scratch.Equals(Location::NoLocation())) {
998 return scratch;
999 }
1000 // Allocate from VIXL temp registers.
1001 if (kind == Location::kRegister) {
1002 scratch = LocationFrom(vixl_temps_.AcquireX());
1003 } else {
1004 DCHECK(kind == Location::kFpuRegister);
1005 scratch = LocationFrom(vixl_temps_.AcquireD());
1006 }
1007 AddScratchLocation(scratch);
1008 return scratch;
1009}
1010
1011void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1012 if (loc.IsRegister()) {
1013 vixl_temps_.Release(XRegisterFrom(loc));
1014 } else {
1015 DCHECK(loc.IsFpuRegister());
1016 vixl_temps_.Release(DRegisterFrom(loc));
1017 }
1018 RemoveScratchLocation(loc);
1019}
1020
Alexandre Rames3e69f162014-12-10 10:36:50 +00001021void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001022 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001023 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001024}
1025
Alexandre Rames5319def2014-10-23 10:03:10 +01001026void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001027 MacroAssembler* masm = GetVIXLAssembler();
1028 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001029 __ Bind(&frame_entry_label_);
1030
Serban Constantinescu02164b32014-11-13 14:05:07 +00001031 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1032 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001033 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001034 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001035 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001036 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001037 __ Ldr(wzr, MemOperand(temp, 0));
1038 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001039 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001040
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001041 if (!HasEmptyFrame()) {
1042 int frame_size = GetFrameSize();
1043 // Stack layout:
1044 // sp[frame_size - 8] : lr.
1045 // ... : other preserved core registers.
1046 // ... : other preserved fp registers.
1047 // ... : reserved frame space.
1048 // sp[0] : current method.
1049 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001050 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001051 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1052 frame_size - GetCoreSpillSize());
1053 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1054 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001055 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001056}
1057
1058void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001059 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001060 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001061 if (!HasEmptyFrame()) {
1062 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001063 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1064 frame_size - FrameEntrySpillSize());
1065 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1066 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001067 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001068 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001069 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001070 __ Ret();
1071 GetAssembler()->cfi().RestoreState();
1072 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001073}
1074
Scott Wakeling97c72b72016-06-24 16:19:36 +01001075CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001076 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001077 return CPURegList(CPURegister::kRegister, kXRegSize,
1078 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001079}
1080
Scott Wakeling97c72b72016-06-24 16:19:36 +01001081CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001082 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1083 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001084 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1085 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001086}
1087
Alexandre Rames5319def2014-10-23 10:03:10 +01001088void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1089 __ Bind(GetLabelOf(block));
1090}
1091
Calin Juravle175dc732015-08-25 15:42:32 +01001092void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1093 DCHECK(location.IsRegister());
1094 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1095}
1096
Calin Juravlee460d1d2015-09-29 04:52:17 +01001097void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1098 if (location.IsRegister()) {
1099 locations->AddTemp(location);
1100 } else {
1101 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1102 }
1103}
1104
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001105void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001106 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001107 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001108 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001109 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001110 if (value_can_be_null) {
1111 __ Cbz(value, &done);
1112 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001113 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001114 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001115 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001116 if (value_can_be_null) {
1117 __ Bind(&done);
1118 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001119}
1120
David Brazdil58282f42016-01-14 12:45:10 +00001121void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001122 // Blocked core registers:
1123 // lr : Runtime reserved.
1124 // tr : Runtime reserved.
1125 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1126 // ip1 : VIXL core temp.
1127 // ip0 : VIXL core temp.
1128 //
1129 // Blocked fp registers:
1130 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001131 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1132 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001133 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001134 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001135 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001136
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001137 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001138 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001139 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001140 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001141
David Brazdil58282f42016-01-14 12:45:10 +00001142 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001143 // Stubs do not save callee-save floating point registers. If the graph
1144 // is debuggable, we need to deal with these registers differently. For
1145 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001146 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1147 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001148 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001149 }
1150 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001151}
1152
Alexandre Rames3e69f162014-12-10 10:36:50 +00001153size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1154 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1155 __ Str(reg, MemOperand(sp, stack_index));
1156 return kArm64WordSize;
1157}
1158
1159size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1160 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1161 __ Ldr(reg, MemOperand(sp, stack_index));
1162 return kArm64WordSize;
1163}
1164
1165size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1166 FPRegister reg = FPRegister(reg_id, kDRegSize);
1167 __ Str(reg, MemOperand(sp, stack_index));
1168 return kArm64WordSize;
1169}
1170
1171size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1172 FPRegister reg = FPRegister(reg_id, kDRegSize);
1173 __ Ldr(reg, MemOperand(sp, stack_index));
1174 return kArm64WordSize;
1175}
1176
Alexandre Rames5319def2014-10-23 10:03:10 +01001177void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001178 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001179}
1180
1181void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001182 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001183}
1184
Alexandre Rames67555f72014-11-18 10:55:16 +00001185void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001186 if (constant->IsIntConstant()) {
1187 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1188 } else if (constant->IsLongConstant()) {
1189 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1190 } else if (constant->IsNullConstant()) {
1191 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001192 } else if (constant->IsFloatConstant()) {
1193 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1194 } else {
1195 DCHECK(constant->IsDoubleConstant());
1196 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1197 }
1198}
1199
Alexandre Rames3e69f162014-12-10 10:36:50 +00001200
1201static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1202 DCHECK(constant.IsConstant());
1203 HConstant* cst = constant.GetConstant();
1204 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001205 // Null is mapped to a core W register, which we associate with kPrimInt.
1206 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001207 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1208 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1209 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1210}
1211
Calin Juravlee460d1d2015-09-29 04:52:17 +01001212void CodeGeneratorARM64::MoveLocation(Location destination,
1213 Location source,
1214 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001215 if (source.Equals(destination)) {
1216 return;
1217 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001218
1219 // A valid move can always be inferred from the destination and source
1220 // locations. When moving from and to a register, the argument type can be
1221 // used to generate 32bit instead of 64bit moves. In debug mode we also
1222 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001223 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001224
1225 if (destination.IsRegister() || destination.IsFpuRegister()) {
1226 if (unspecified_type) {
1227 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1228 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001229 (src_cst != nullptr && (src_cst->IsIntConstant()
1230 || src_cst->IsFloatConstant()
1231 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001232 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001233 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001234 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001235 // If the source is a double stack slot or a 64bit constant, a 64bit
1236 // type is appropriate. Else the source is a register, and since the
1237 // type has not been specified, we chose a 64bit type to force a 64bit
1238 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001239 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001240 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001241 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001242 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1243 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1244 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001245 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1246 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1247 __ Ldr(dst, StackOperandFrom(source));
1248 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001249 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001250 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001251 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001252 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001253 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001254 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001255 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001256 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1257 ? Primitive::kPrimLong
1258 : Primitive::kPrimInt;
1259 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1260 }
1261 } else {
1262 DCHECK(source.IsFpuRegister());
1263 if (destination.IsRegister()) {
1264 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1265 ? Primitive::kPrimDouble
1266 : Primitive::kPrimFloat;
1267 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1268 } else {
1269 DCHECK(destination.IsFpuRegister());
1270 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001271 }
1272 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001273 } else { // The destination is not a register. It must be a stack slot.
1274 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1275 if (source.IsRegister() || source.IsFpuRegister()) {
1276 if (unspecified_type) {
1277 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001278 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001279 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001280 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001281 }
1282 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001283 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1284 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1285 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001286 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001287 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1288 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001289 UseScratchRegisterScope temps(GetVIXLAssembler());
1290 HConstant* src_cst = source.GetConstant();
1291 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001292 if (src_cst->IsZeroBitPattern()) {
1293 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant()) ? xzr : wzr;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001294 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001295 if (src_cst->IsIntConstant()) {
1296 temp = temps.AcquireW();
1297 } else if (src_cst->IsLongConstant()) {
1298 temp = temps.AcquireX();
1299 } else if (src_cst->IsFloatConstant()) {
1300 temp = temps.AcquireS();
1301 } else {
1302 DCHECK(src_cst->IsDoubleConstant());
1303 temp = temps.AcquireD();
1304 }
1305 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001306 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001307 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001308 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001309 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001310 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001311 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001312 // There is generally less pressure on FP registers.
1313 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001314 __ Ldr(temp, StackOperandFrom(source));
1315 __ Str(temp, StackOperandFrom(destination));
1316 }
1317 }
1318}
1319
1320void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001321 CPURegister dst,
1322 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001323 switch (type) {
1324 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001325 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001326 break;
1327 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001328 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001329 break;
1330 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001331 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001332 break;
1333 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001334 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001335 break;
1336 case Primitive::kPrimInt:
1337 case Primitive::kPrimNot:
1338 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001339 case Primitive::kPrimFloat:
1340 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001341 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001342 __ Ldr(dst, src);
1343 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001344 case Primitive::kPrimVoid:
1345 LOG(FATAL) << "Unreachable type " << type;
1346 }
1347}
1348
Calin Juravle77520bc2015-01-12 18:45:46 +00001349void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001350 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001351 const MemOperand& src,
1352 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001353 MacroAssembler* masm = GetVIXLAssembler();
1354 BlockPoolsScope block_pools(masm);
1355 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001356 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001357 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001358
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001359 DCHECK(!src.IsPreIndex());
1360 DCHECK(!src.IsPostIndex());
1361
1362 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001363 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001364 MemOperand base = MemOperand(temp_base);
1365 switch (type) {
1366 case Primitive::kPrimBoolean:
1367 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001368 if (needs_null_check) {
1369 MaybeRecordImplicitNullCheck(instruction);
1370 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001371 break;
1372 case Primitive::kPrimByte:
1373 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001374 if (needs_null_check) {
1375 MaybeRecordImplicitNullCheck(instruction);
1376 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001377 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1378 break;
1379 case Primitive::kPrimChar:
1380 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001381 if (needs_null_check) {
1382 MaybeRecordImplicitNullCheck(instruction);
1383 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001384 break;
1385 case Primitive::kPrimShort:
1386 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001387 if (needs_null_check) {
1388 MaybeRecordImplicitNullCheck(instruction);
1389 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001390 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1391 break;
1392 case Primitive::kPrimInt:
1393 case Primitive::kPrimNot:
1394 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001395 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001396 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001397 if (needs_null_check) {
1398 MaybeRecordImplicitNullCheck(instruction);
1399 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001400 break;
1401 case Primitive::kPrimFloat:
1402 case Primitive::kPrimDouble: {
1403 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001404 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001405
1406 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1407 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001408 if (needs_null_check) {
1409 MaybeRecordImplicitNullCheck(instruction);
1410 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001411 __ Fmov(FPRegister(dst), temp);
1412 break;
1413 }
1414 case Primitive::kPrimVoid:
1415 LOG(FATAL) << "Unreachable type " << type;
1416 }
1417}
1418
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001419void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001420 CPURegister src,
1421 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001422 switch (type) {
1423 case Primitive::kPrimBoolean:
1424 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001425 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001426 break;
1427 case Primitive::kPrimChar:
1428 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001429 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001430 break;
1431 case Primitive::kPrimInt:
1432 case Primitive::kPrimNot:
1433 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001434 case Primitive::kPrimFloat:
1435 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001436 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001437 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001438 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001439 case Primitive::kPrimVoid:
1440 LOG(FATAL) << "Unreachable type " << type;
1441 }
1442}
1443
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001444void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1445 CPURegister src,
1446 const MemOperand& dst) {
1447 UseScratchRegisterScope temps(GetVIXLAssembler());
1448 Register temp_base = temps.AcquireX();
1449
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001450 DCHECK(!dst.IsPreIndex());
1451 DCHECK(!dst.IsPostIndex());
1452
1453 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001454 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001455 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001456 MemOperand base = MemOperand(temp_base);
1457 switch (type) {
1458 case Primitive::kPrimBoolean:
1459 case Primitive::kPrimByte:
1460 __ Stlrb(Register(src), base);
1461 break;
1462 case Primitive::kPrimChar:
1463 case Primitive::kPrimShort:
1464 __ Stlrh(Register(src), base);
1465 break;
1466 case Primitive::kPrimInt:
1467 case Primitive::kPrimNot:
1468 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001469 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001470 __ Stlr(Register(src), base);
1471 break;
1472 case Primitive::kPrimFloat:
1473 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001474 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001475 Register temp_src;
1476 if (src.IsZero()) {
1477 // The zero register is used to avoid synthesizing zero constants.
1478 temp_src = Register(src);
1479 } else {
1480 DCHECK(src.IsFPRegister());
1481 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1482 __ Fmov(temp_src, FPRegister(src));
1483 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001484
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001485 __ Stlr(temp_src, base);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001486 break;
1487 }
1488 case Primitive::kPrimVoid:
1489 LOG(FATAL) << "Unreachable type " << type;
1490 }
1491}
1492
Calin Juravle175dc732015-08-25 15:42:32 +01001493void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1494 HInstruction* instruction,
1495 uint32_t dex_pc,
1496 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001497 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001498 GenerateInvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001499 if (EntrypointRequiresStackMap(entrypoint)) {
1500 RecordPcInfo(instruction, dex_pc, slow_path);
1501 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001502}
1503
Roland Levillaindec8f632016-07-22 17:10:06 +01001504void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1505 HInstruction* instruction,
1506 SlowPathCode* slow_path) {
1507 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001508 GenerateInvokeRuntime(entry_point_offset);
1509}
1510
1511void CodeGeneratorARM64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001512 BlockPoolsScope block_pools(GetVIXLAssembler());
1513 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1514 __ Blr(lr);
1515}
1516
Alexandre Rames67555f72014-11-18 10:55:16 +00001517void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001518 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001519 UseScratchRegisterScope temps(GetVIXLAssembler());
1520 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001521 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1522
Serban Constantinescu02164b32014-11-13 14:05:07 +00001523 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001524 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1525 __ Add(temp, class_reg, status_offset);
1526 __ Ldar(temp, HeapOperand(temp));
1527 __ Cmp(temp, mirror::Class::kStatusInitialized);
1528 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001529 __ Bind(slow_path->GetExitLabel());
1530}
Alexandre Rames5319def2014-10-23 10:03:10 +01001531
Roland Levillain44015862016-01-22 11:47:17 +00001532void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001533 BarrierType type = BarrierAll;
1534
1535 switch (kind) {
1536 case MemBarrierKind::kAnyAny:
1537 case MemBarrierKind::kAnyStore: {
1538 type = BarrierAll;
1539 break;
1540 }
1541 case MemBarrierKind::kLoadAny: {
1542 type = BarrierReads;
1543 break;
1544 }
1545 case MemBarrierKind::kStoreStore: {
1546 type = BarrierWrites;
1547 break;
1548 }
1549 default:
1550 LOG(FATAL) << "Unexpected memory barrier " << kind;
1551 }
1552 __ Dmb(InnerShareable, type);
1553}
1554
Serban Constantinescu02164b32014-11-13 14:05:07 +00001555void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1556 HBasicBlock* successor) {
1557 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001558 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1559 if (slow_path == nullptr) {
1560 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1561 instruction->SetSlowPath(slow_path);
1562 codegen_->AddSlowPath(slow_path);
1563 if (successor != nullptr) {
1564 DCHECK(successor->IsLoopHeader());
1565 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1566 }
1567 } else {
1568 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1569 }
1570
Serban Constantinescu02164b32014-11-13 14:05:07 +00001571 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1572 Register temp = temps.AcquireW();
1573
Andreas Gampe542451c2016-07-26 09:02:02 -07001574 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001575 if (successor == nullptr) {
1576 __ Cbnz(temp, slow_path->GetEntryLabel());
1577 __ Bind(slow_path->GetReturnLabel());
1578 } else {
1579 __ Cbz(temp, codegen_->GetLabelOf(successor));
1580 __ B(slow_path->GetEntryLabel());
1581 // slow_path will return to GetLabelOf(successor).
1582 }
1583}
1584
Alexandre Rames5319def2014-10-23 10:03:10 +01001585InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1586 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001587 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001588 assembler_(codegen->GetAssembler()),
1589 codegen_(codegen) {}
1590
1591#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001592 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001593
1594#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1595
1596enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001597 // Using a base helps identify when we hit such breakpoints.
1598 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001599#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1600 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1601#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1602};
1603
1604#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001605 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001606 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1607 } \
1608 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1609 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1610 locations->SetOut(Location::Any()); \
1611 }
1612 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1613#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1614
1615#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001616#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001617
Alexandre Rames67555f72014-11-18 10:55:16 +00001618void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001619 DCHECK_EQ(instr->InputCount(), 2U);
1620 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1621 Primitive::Type type = instr->GetResultType();
1622 switch (type) {
1623 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001624 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001625 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001626 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001627 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001628 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001629
1630 case Primitive::kPrimFloat:
1631 case Primitive::kPrimDouble:
1632 locations->SetInAt(0, Location::RequiresFpuRegister());
1633 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001634 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001635 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001636
Alexandre Rames5319def2014-10-23 10:03:10 +01001637 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001638 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001639 }
1640}
1641
Alexandre Rames09a99962015-04-15 11:47:56 +01001642void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001643 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1644
1645 bool object_field_get_with_read_barrier =
1646 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001647 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001648 new (GetGraph()->GetArena()) LocationSummary(instruction,
1649 object_field_get_with_read_barrier ?
1650 LocationSummary::kCallOnSlowPath :
1651 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001652 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001653 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01001654 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001655 locations->SetInAt(0, Location::RequiresRegister());
1656 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1657 locations->SetOut(Location::RequiresFpuRegister());
1658 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001659 // The output overlaps for an object field get when read barriers
1660 // are enabled: we do not want the load to overwrite the object's
1661 // location, as we need it to emit the read barrier.
1662 locations->SetOut(
1663 Location::RequiresRegister(),
1664 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001665 }
1666}
1667
1668void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1669 const FieldInfo& field_info) {
1670 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001671 LocationSummary* locations = instruction->GetLocations();
1672 Location base_loc = locations->InAt(0);
1673 Location out = locations->Out();
1674 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001675 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001676 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001677 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001678
Roland Levillain44015862016-01-22 11:47:17 +00001679 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1680 // Object FieldGet with Baker's read barrier case.
1681 MacroAssembler* masm = GetVIXLAssembler();
1682 UseScratchRegisterScope temps(masm);
1683 // /* HeapReference<Object> */ out = *(base + offset)
1684 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1685 Register temp = temps.AcquireW();
1686 // Note that potential implicit null checks are handled in this
1687 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1688 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1689 instruction,
1690 out,
1691 base,
1692 offset,
1693 temp,
1694 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001695 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001696 } else {
1697 // General case.
1698 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001699 // Note that a potential implicit null check is handled in this
1700 // CodeGeneratorARM64::LoadAcquire call.
1701 // NB: LoadAcquire will record the pc info if needed.
1702 codegen_->LoadAcquire(
1703 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001704 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001705 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001706 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001707 }
Roland Levillain44015862016-01-22 11:47:17 +00001708 if (field_type == Primitive::kPrimNot) {
1709 // If read barriers are enabled, emit read barriers other than
1710 // Baker's using a slow path (and also unpoison the loaded
1711 // reference, if heap poisoning is enabled).
1712 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1713 }
Roland Levillain4d027112015-07-01 15:41:14 +01001714 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001715}
1716
1717void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1718 LocationSummary* locations =
1719 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1720 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001721 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1722 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
1723 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001724 locations->SetInAt(1, Location::RequiresFpuRegister());
1725 } else {
1726 locations->SetInAt(1, Location::RequiresRegister());
1727 }
1728}
1729
1730void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001731 const FieldInfo& field_info,
1732 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001733 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001734 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001735
1736 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001737 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001738 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001739 Offset offset = field_info.GetFieldOffset();
1740 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001741
Roland Levillain4d027112015-07-01 15:41:14 +01001742 {
1743 // We use a block to end the scratch scope before the write barrier, thus
1744 // freeing the temporary registers so they can be used in `MarkGCCard`.
1745 UseScratchRegisterScope temps(GetVIXLAssembler());
1746
1747 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1748 DCHECK(value.IsW());
1749 Register temp = temps.AcquireW();
1750 __ Mov(temp, value.W());
1751 GetAssembler()->PoisonHeapReference(temp.W());
1752 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001753 }
Roland Levillain4d027112015-07-01 15:41:14 +01001754
1755 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001756 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1757 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001758 } else {
1759 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1760 codegen_->MaybeRecordImplicitNullCheck(instruction);
1761 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001762 }
1763
1764 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001765 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001766 }
1767}
1768
Alexandre Rames67555f72014-11-18 10:55:16 +00001769void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001770 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001771
1772 switch (type) {
1773 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001774 case Primitive::kPrimLong: {
1775 Register dst = OutputRegister(instr);
1776 Register lhs = InputRegisterAt(instr, 0);
1777 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001778 if (instr->IsAdd()) {
1779 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001780 } else if (instr->IsAnd()) {
1781 __ And(dst, lhs, rhs);
1782 } else if (instr->IsOr()) {
1783 __ Orr(dst, lhs, rhs);
1784 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001785 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001786 } else if (instr->IsRor()) {
1787 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001788 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001789 __ Ror(dst, lhs, shift);
1790 } else {
1791 // Ensure shift distance is in the same size register as the result. If
1792 // we are rotating a long and the shift comes in a w register originally,
1793 // we don't need to sxtw for use as an x since the shift distances are
1794 // all & reg_bits - 1.
1795 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1796 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001797 } else {
1798 DCHECK(instr->IsXor());
1799 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001800 }
1801 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001802 }
1803 case Primitive::kPrimFloat:
1804 case Primitive::kPrimDouble: {
1805 FPRegister dst = OutputFPRegister(instr);
1806 FPRegister lhs = InputFPRegisterAt(instr, 0);
1807 FPRegister rhs = InputFPRegisterAt(instr, 1);
1808 if (instr->IsAdd()) {
1809 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001810 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001811 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001812 } else {
1813 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001814 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001815 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001816 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001817 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001818 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001819 }
1820}
1821
Serban Constantinescu02164b32014-11-13 14:05:07 +00001822void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1823 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1824
1825 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1826 Primitive::Type type = instr->GetResultType();
1827 switch (type) {
1828 case Primitive::kPrimInt:
1829 case Primitive::kPrimLong: {
1830 locations->SetInAt(0, Location::RequiresRegister());
1831 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1832 locations->SetOut(Location::RequiresRegister());
1833 break;
1834 }
1835 default:
1836 LOG(FATAL) << "Unexpected shift type " << type;
1837 }
1838}
1839
1840void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1841 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1842
1843 Primitive::Type type = instr->GetType();
1844 switch (type) {
1845 case Primitive::kPrimInt:
1846 case Primitive::kPrimLong: {
1847 Register dst = OutputRegister(instr);
1848 Register lhs = InputRegisterAt(instr, 0);
1849 Operand rhs = InputOperandAt(instr, 1);
1850 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001851 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00001852 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001853 if (instr->IsShl()) {
1854 __ Lsl(dst, lhs, shift_value);
1855 } else if (instr->IsShr()) {
1856 __ Asr(dst, lhs, shift_value);
1857 } else {
1858 __ Lsr(dst, lhs, shift_value);
1859 }
1860 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001861 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001862
1863 if (instr->IsShl()) {
1864 __ Lsl(dst, lhs, rhs_reg);
1865 } else if (instr->IsShr()) {
1866 __ Asr(dst, lhs, rhs_reg);
1867 } else {
1868 __ Lsr(dst, lhs, rhs_reg);
1869 }
1870 }
1871 break;
1872 }
1873 default:
1874 LOG(FATAL) << "Unexpected shift operation type " << type;
1875 }
1876}
1877
Alexandre Rames5319def2014-10-23 10:03:10 +01001878void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001879 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001880}
1881
1882void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001883 HandleBinaryOp(instruction);
1884}
1885
1886void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1887 HandleBinaryOp(instruction);
1888}
1889
1890void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1891 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001892}
1893
Artem Serov7fc63502016-02-09 17:15:29 +00001894void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001895 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
1896 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1897 locations->SetInAt(0, Location::RequiresRegister());
1898 // There is no immediate variant of negated bitwise instructions in AArch64.
1899 locations->SetInAt(1, Location::RequiresRegister());
1900 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1901}
1902
Artem Serov7fc63502016-02-09 17:15:29 +00001903void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001904 Register dst = OutputRegister(instr);
1905 Register lhs = InputRegisterAt(instr, 0);
1906 Register rhs = InputRegisterAt(instr, 1);
1907
1908 switch (instr->GetOpKind()) {
1909 case HInstruction::kAnd:
1910 __ Bic(dst, lhs, rhs);
1911 break;
1912 case HInstruction::kOr:
1913 __ Orn(dst, lhs, rhs);
1914 break;
1915 case HInstruction::kXor:
1916 __ Eon(dst, lhs, rhs);
1917 break;
1918 default:
1919 LOG(FATAL) << "Unreachable";
1920 }
1921}
1922
Alexandre Rames8626b742015-11-25 16:28:08 +00001923void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1924 HArm64DataProcWithShifterOp* instruction) {
1925 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1926 instruction->GetType() == Primitive::kPrimLong);
1927 LocationSummary* locations =
1928 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1929 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1930 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1931 } else {
1932 locations->SetInAt(0, Location::RequiresRegister());
1933 }
1934 locations->SetInAt(1, Location::RequiresRegister());
1935 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1936}
1937
1938void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1939 HArm64DataProcWithShifterOp* instruction) {
1940 Primitive::Type type = instruction->GetType();
1941 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1942 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1943 Register out = OutputRegister(instruction);
1944 Register left;
1945 if (kind != HInstruction::kNeg) {
1946 left = InputRegisterAt(instruction, 0);
1947 }
1948 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1949 // shifter operand operation, the IR generating `right_reg` (input to the type
1950 // conversion) can have a different type from the current instruction's type,
1951 // so we manually indicate the type.
1952 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00001953 int64_t shift_amount = instruction->GetShiftAmount() &
1954 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00001955
1956 Operand right_operand(0);
1957
1958 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1959 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1960 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1961 } else {
1962 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1963 }
1964
1965 // Logical binary operations do not support extension operations in the
1966 // operand. Note that VIXL would still manage if it was passed by generating
1967 // the extension as a separate instruction.
1968 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1969 DCHECK(!right_operand.IsExtendedRegister() ||
1970 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1971 kind != HInstruction::kNeg));
1972 switch (kind) {
1973 case HInstruction::kAdd:
1974 __ Add(out, left, right_operand);
1975 break;
1976 case HInstruction::kAnd:
1977 __ And(out, left, right_operand);
1978 break;
1979 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00001980 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00001981 __ Neg(out, right_operand);
1982 break;
1983 case HInstruction::kOr:
1984 __ Orr(out, left, right_operand);
1985 break;
1986 case HInstruction::kSub:
1987 __ Sub(out, left, right_operand);
1988 break;
1989 case HInstruction::kXor:
1990 __ Eor(out, left, right_operand);
1991 break;
1992 default:
1993 LOG(FATAL) << "Unexpected operation kind: " << kind;
1994 UNREACHABLE();
1995 }
1996}
1997
Artem Serov328429f2016-07-06 16:23:04 +01001998void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00001999 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2000 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002001 LocationSummary* locations =
2002 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2003 locations->SetInAt(0, Location::RequiresRegister());
2004 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
2005 locations->SetOut(Location::RequiresRegister());
2006}
2007
Roland Levillain4a3aa572016-08-15 13:17:06 +00002008void InstructionCodeGeneratorARM64::VisitIntermediateAddress(
2009 HIntermediateAddress* instruction) {
2010 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2011 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002012 __ Add(OutputRegister(instruction),
2013 InputRegisterAt(instruction, 0),
2014 Operand(InputOperandAt(instruction, 1)));
2015}
2016
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002017void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002018 LocationSummary* locations =
2019 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002020 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2021 if (instr->GetOpKind() == HInstruction::kSub &&
2022 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002023 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002024 // Don't allocate register for Mneg instruction.
2025 } else {
2026 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2027 Location::RequiresRegister());
2028 }
2029 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2030 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002031 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2032}
2033
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002034void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002035 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002036 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2037 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002038
2039 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2040 // This fixup should be carried out for all multiply-accumulate instructions:
2041 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2042 if (instr->GetType() == Primitive::kPrimLong &&
2043 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2044 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002045 vixl::aarch64::Instruction* prev =
2046 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002047 if (prev->IsLoadOrStore()) {
2048 // Make sure we emit only exactly one nop.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002049 vixl::aarch64::CodeBufferCheckScope scope(masm,
2050 kInstructionSize,
2051 vixl::aarch64::CodeBufferCheckScope::kCheck,
2052 vixl::aarch64::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002053 __ nop();
2054 }
2055 }
2056
2057 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002058 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002059 __ Madd(res, mul_left, mul_right, accumulator);
2060 } else {
2061 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002062 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002063 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002064 __ Mneg(res, mul_left, mul_right);
2065 } else {
2066 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2067 __ Msub(res, mul_left, mul_right, accumulator);
2068 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002069 }
2070}
2071
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002072void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002073 bool object_array_get_with_read_barrier =
2074 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002075 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002076 new (GetGraph()->GetArena()) LocationSummary(instruction,
2077 object_array_get_with_read_barrier ?
2078 LocationSummary::kCallOnSlowPath :
2079 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002080 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002081 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002082 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002083 locations->SetInAt(0, Location::RequiresRegister());
2084 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002085 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2086 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2087 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002088 // The output overlaps in the case of an object array get with
2089 // read barriers enabled: we do not want the move to overwrite the
2090 // array's location, as we need it to emit the read barrier.
2091 locations->SetOut(
2092 Location::RequiresRegister(),
2093 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002094 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002095}
2096
2097void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002098 Primitive::Type type = instruction->GetType();
2099 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002100 LocationSummary* locations = instruction->GetLocations();
2101 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002102 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002103 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002104
Alexandre Ramesd921d642015-04-16 15:07:16 +01002105 MacroAssembler* masm = GetVIXLAssembler();
2106 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002107 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002108 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002109
Roland Levillain44015862016-01-22 11:47:17 +00002110 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2111 // Object ArrayGet with Baker's read barrier case.
2112 Register temp = temps.AcquireW();
Roland Levillain4a3aa572016-08-15 13:17:06 +00002113 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2114 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Roland Levillain44015862016-01-22 11:47:17 +00002115 // Note that a potential implicit null check is handled in the
2116 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2117 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2118 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002119 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002120 // General case.
2121 MemOperand source = HeapOperand(obj);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002122 if (index.IsConstant()) {
Roland Levillain44015862016-01-22 11:47:17 +00002123 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2124 source = HeapOperand(obj, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002125 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002126 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002127 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002128 // The read barrier instrumentation does not support the
2129 // HIntermediateAddress instruction yet.
2130 DCHECK(!kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00002131 // We do not need to compute the intermediate address from the array: the
2132 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002133 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002134 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002135 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002136 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2137 }
2138 temp = obj;
2139 } else {
2140 __ Add(temp, obj, offset);
2141 }
2142 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2143 }
2144
2145 codegen_->Load(type, OutputCPURegister(instruction), source);
2146 codegen_->MaybeRecordImplicitNullCheck(instruction);
2147
2148 if (type == Primitive::kPrimNot) {
2149 static_assert(
2150 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2151 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2152 Location obj_loc = locations->InAt(0);
2153 if (index.IsConstant()) {
2154 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2155 } else {
2156 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2157 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002158 }
Roland Levillain4d027112015-07-01 15:41:14 +01002159 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002160}
2161
Alexandre Rames5319def2014-10-23 10:03:10 +01002162void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2163 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2164 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002165 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002166}
2167
2168void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002169 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002170 BlockPoolsScope block_pools(GetVIXLAssembler());
Vladimir Markodce016e2016-04-28 13:10:02 +01002171 __ Ldr(OutputRegister(instruction), HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002172 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002173}
2174
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002175void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002176 Primitive::Type value_type = instruction->GetComponentType();
2177
2178 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002179 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2180 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002181 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002182 LocationSummary::kCallOnSlowPath :
2183 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002184 locations->SetInAt(0, Location::RequiresRegister());
2185 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002186 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2187 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2188 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002189 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002190 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002191 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002192 }
2193}
2194
2195void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2196 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002197 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002198 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002199 bool needs_write_barrier =
2200 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002201
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002202 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002203 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002204 CPURegister source = value;
2205 Location index = locations->InAt(1);
2206 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2207 MemOperand destination = HeapOperand(array);
2208 MacroAssembler* masm = GetVIXLAssembler();
2209 BlockPoolsScope block_pools(masm);
2210
2211 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002212 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002213 if (index.IsConstant()) {
2214 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2215 destination = HeapOperand(array, offset);
2216 } else {
2217 UseScratchRegisterScope temps(masm);
2218 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002219 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002220 // The read barrier instrumentation does not support the
2221 // HIntermediateAddress instruction yet.
2222 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002223 // We do not need to compute the intermediate address from the array: the
2224 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002225 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002226 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002227 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002228 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2229 }
2230 temp = array;
2231 } else {
2232 __ Add(temp, array, offset);
2233 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002234 destination = HeapOperand(temp,
2235 XRegisterFrom(index),
2236 LSL,
2237 Primitive::ComponentSizeShift(value_type));
2238 }
2239 codegen_->Store(value_type, value, destination);
2240 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002241 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002242 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002243 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002244 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002245 {
2246 // We use a block to end the scratch scope before the write barrier, thus
2247 // freeing the temporary registers so they can be used in `MarkGCCard`.
2248 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002249 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002250 if (index.IsConstant()) {
2251 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002252 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002253 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002254 destination = HeapOperand(temp,
2255 XRegisterFrom(index),
2256 LSL,
2257 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002258 }
2259
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002260 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2261 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2262 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2263
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002264 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002265 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2266 codegen_->AddSlowPath(slow_path);
2267 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002268 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002269 __ Cbnz(Register(value), &non_zero);
2270 if (!index.IsConstant()) {
2271 __ Add(temp, array, offset);
2272 }
2273 __ Str(wzr, destination);
2274 codegen_->MaybeRecordImplicitNullCheck(instruction);
2275 __ B(&done);
2276 __ Bind(&non_zero);
2277 }
2278
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002279 // Note that when Baker read barriers are enabled, the type
2280 // checks are performed without read barriers. This is fine,
2281 // even in the case where a class object is in the from-space
2282 // after the flip, as a comparison involving such a type would
2283 // not produce a false positive; it may of course produce a
2284 // false negative, in which case we would take the ArraySet
2285 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002286
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002287 Register temp2 = temps.AcquireSameSizeAs(array);
2288 // /* HeapReference<Class> */ temp = array->klass_
2289 __ Ldr(temp, HeapOperand(array, class_offset));
2290 codegen_->MaybeRecordImplicitNullCheck(instruction);
2291 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002292
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002293 // /* HeapReference<Class> */ temp = temp->component_type_
2294 __ Ldr(temp, HeapOperand(temp, component_offset));
2295 // /* HeapReference<Class> */ temp2 = value->klass_
2296 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2297 // If heap poisoning is enabled, no need to unpoison `temp`
2298 // nor `temp2`, as we are comparing two poisoned references.
2299 __ Cmp(temp, temp2);
2300 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002301
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002302 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2303 vixl::aarch64::Label do_put;
2304 __ B(eq, &do_put);
2305 // If heap poisoning is enabled, the `temp` reference has
2306 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002307 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2308
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002309 // /* HeapReference<Class> */ temp = temp->super_class_
2310 __ Ldr(temp, HeapOperand(temp, super_offset));
2311 // If heap poisoning is enabled, no need to unpoison
2312 // `temp`, as we are comparing against null below.
2313 __ Cbnz(temp, slow_path->GetEntryLabel());
2314 __ Bind(&do_put);
2315 } else {
2316 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002317 }
2318 }
2319
2320 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002321 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002322 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002323 __ Mov(temp2, value.W());
2324 GetAssembler()->PoisonHeapReference(temp2);
2325 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002326 }
2327
2328 if (!index.IsConstant()) {
2329 __ Add(temp, array, offset);
2330 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002331 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002332
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002333 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002334 codegen_->MaybeRecordImplicitNullCheck(instruction);
2335 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002336 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002337
2338 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2339
2340 if (done.IsLinked()) {
2341 __ Bind(&done);
2342 }
2343
2344 if (slow_path != nullptr) {
2345 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002346 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002347 }
2348}
2349
Alexandre Rames67555f72014-11-18 10:55:16 +00002350void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002351 RegisterSet caller_saves = RegisterSet::Empty();
2352 InvokeRuntimeCallingConvention calling_convention;
2353 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2354 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2355 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002356 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002357 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002358}
2359
2360void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002361 BoundsCheckSlowPathARM64* slow_path =
2362 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002363 codegen_->AddSlowPath(slow_path);
2364
2365 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2366 __ B(slow_path->GetEntryLabel(), hs);
2367}
2368
Alexandre Rames67555f72014-11-18 10:55:16 +00002369void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2370 LocationSummary* locations =
2371 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2372 locations->SetInAt(0, Location::RequiresRegister());
2373 if (check->HasUses()) {
2374 locations->SetOut(Location::SameAsFirstInput());
2375 }
2376}
2377
2378void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2379 // We assume the class is not null.
2380 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2381 check->GetLoadClass(), check, check->GetDexPc(), true);
2382 codegen_->AddSlowPath(slow_path);
2383 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2384}
2385
Roland Levillain1a653882016-03-18 18:05:57 +00002386static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2387 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2388 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2389}
2390
2391void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2392 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2393 Location rhs_loc = instruction->GetLocations()->InAt(1);
2394 if (rhs_loc.IsConstant()) {
2395 // 0.0 is the only immediate that can be encoded directly in
2396 // an FCMP instruction.
2397 //
2398 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2399 // specify that in a floating-point comparison, positive zero
2400 // and negative zero are considered equal, so we can use the
2401 // literal 0.0 for both cases here.
2402 //
2403 // Note however that some methods (Float.equal, Float.compare,
2404 // Float.compareTo, Double.equal, Double.compare,
2405 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2406 // StrictMath.min) consider 0.0 to be (strictly) greater than
2407 // -0.0. So if we ever translate calls to these methods into a
2408 // HCompare instruction, we must handle the -0.0 case with
2409 // care here.
2410 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2411 __ Fcmp(lhs_reg, 0.0);
2412 } else {
2413 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2414 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002415}
2416
Serban Constantinescu02164b32014-11-13 14:05:07 +00002417void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002418 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002419 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2420 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002421 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002422 case Primitive::kPrimBoolean:
2423 case Primitive::kPrimByte:
2424 case Primitive::kPrimShort:
2425 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002426 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002427 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002428 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002429 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002430 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2431 break;
2432 }
2433 case Primitive::kPrimFloat:
2434 case Primitive::kPrimDouble: {
2435 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002436 locations->SetInAt(1,
2437 IsFloatingPointZeroConstant(compare->InputAt(1))
2438 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2439 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002440 locations->SetOut(Location::RequiresRegister());
2441 break;
2442 }
2443 default:
2444 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2445 }
2446}
2447
2448void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2449 Primitive::Type in_type = compare->InputAt(0)->GetType();
2450
2451 // 0 if: left == right
2452 // 1 if: left > right
2453 // -1 if: left < right
2454 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002455 case Primitive::kPrimBoolean:
2456 case Primitive::kPrimByte:
2457 case Primitive::kPrimShort:
2458 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002459 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002460 case Primitive::kPrimLong: {
2461 Register result = OutputRegister(compare);
2462 Register left = InputRegisterAt(compare, 0);
2463 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002464 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002465 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2466 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002467 break;
2468 }
2469 case Primitive::kPrimFloat:
2470 case Primitive::kPrimDouble: {
2471 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002472 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002473 __ Cset(result, ne);
2474 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002475 break;
2476 }
2477 default:
2478 LOG(FATAL) << "Unimplemented compare type " << in_type;
2479 }
2480}
2481
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002482void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002483 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002484
2485 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2486 locations->SetInAt(0, Location::RequiresFpuRegister());
2487 locations->SetInAt(1,
2488 IsFloatingPointZeroConstant(instruction->InputAt(1))
2489 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2490 : Location::RequiresFpuRegister());
2491 } else {
2492 // Integer cases.
2493 locations->SetInAt(0, Location::RequiresRegister());
2494 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2495 }
2496
David Brazdilb3e773e2016-01-26 11:28:37 +00002497 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002498 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002499 }
2500}
2501
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002502void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002503 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002504 return;
2505 }
2506
2507 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002508 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002509 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002510
Roland Levillain7f63c522015-07-13 15:54:55 +00002511 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002512 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002513 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002514 } else {
2515 // Integer cases.
2516 Register lhs = InputRegisterAt(instruction, 0);
2517 Operand rhs = InputOperandAt(instruction, 1);
2518 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002519 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002520 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002521}
2522
2523#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2524 M(Equal) \
2525 M(NotEqual) \
2526 M(LessThan) \
2527 M(LessThanOrEqual) \
2528 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002529 M(GreaterThanOrEqual) \
2530 M(Below) \
2531 M(BelowOrEqual) \
2532 M(Above) \
2533 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002534#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002535void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2536void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002537FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002538#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002539#undef FOR_EACH_CONDITION_INSTRUCTION
2540
Zheng Xuc6667102015-05-15 16:08:45 +08002541void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2542 DCHECK(instruction->IsDiv() || instruction->IsRem());
2543
2544 LocationSummary* locations = instruction->GetLocations();
2545 Location second = locations->InAt(1);
2546 DCHECK(second.IsConstant());
2547
2548 Register out = OutputRegister(instruction);
2549 Register dividend = InputRegisterAt(instruction, 0);
2550 int64_t imm = Int64FromConstant(second.GetConstant());
2551 DCHECK(imm == 1 || imm == -1);
2552
2553 if (instruction->IsRem()) {
2554 __ Mov(out, 0);
2555 } else {
2556 if (imm == 1) {
2557 __ Mov(out, dividend);
2558 } else {
2559 __ Neg(out, dividend);
2560 }
2561 }
2562}
2563
2564void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2565 DCHECK(instruction->IsDiv() || instruction->IsRem());
2566
2567 LocationSummary* locations = instruction->GetLocations();
2568 Location second = locations->InAt(1);
2569 DCHECK(second.IsConstant());
2570
2571 Register out = OutputRegister(instruction);
2572 Register dividend = InputRegisterAt(instruction, 0);
2573 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002574 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002575 int ctz_imm = CTZ(abs_imm);
2576
2577 UseScratchRegisterScope temps(GetVIXLAssembler());
2578 Register temp = temps.AcquireSameSizeAs(out);
2579
2580 if (instruction->IsDiv()) {
2581 __ Add(temp, dividend, abs_imm - 1);
2582 __ Cmp(dividend, 0);
2583 __ Csel(out, temp, dividend, lt);
2584 if (imm > 0) {
2585 __ Asr(out, out, ctz_imm);
2586 } else {
2587 __ Neg(out, Operand(out, ASR, ctz_imm));
2588 }
2589 } else {
2590 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2591 __ Asr(temp, dividend, bits - 1);
2592 __ Lsr(temp, temp, bits - ctz_imm);
2593 __ Add(out, dividend, temp);
2594 __ And(out, out, abs_imm - 1);
2595 __ Sub(out, out, temp);
2596 }
2597}
2598
2599void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2600 DCHECK(instruction->IsDiv() || instruction->IsRem());
2601
2602 LocationSummary* locations = instruction->GetLocations();
2603 Location second = locations->InAt(1);
2604 DCHECK(second.IsConstant());
2605
2606 Register out = OutputRegister(instruction);
2607 Register dividend = InputRegisterAt(instruction, 0);
2608 int64_t imm = Int64FromConstant(second.GetConstant());
2609
2610 Primitive::Type type = instruction->GetResultType();
2611 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2612
2613 int64_t magic;
2614 int shift;
2615 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2616
2617 UseScratchRegisterScope temps(GetVIXLAssembler());
2618 Register temp = temps.AcquireSameSizeAs(out);
2619
2620 // temp = get_high(dividend * magic)
2621 __ Mov(temp, magic);
2622 if (type == Primitive::kPrimLong) {
2623 __ Smulh(temp, dividend, temp);
2624 } else {
2625 __ Smull(temp.X(), dividend, temp);
2626 __ Lsr(temp.X(), temp.X(), 32);
2627 }
2628
2629 if (imm > 0 && magic < 0) {
2630 __ Add(temp, temp, dividend);
2631 } else if (imm < 0 && magic > 0) {
2632 __ Sub(temp, temp, dividend);
2633 }
2634
2635 if (shift != 0) {
2636 __ Asr(temp, temp, shift);
2637 }
2638
2639 if (instruction->IsDiv()) {
2640 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2641 } else {
2642 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2643 // TODO: Strength reduction for msub.
2644 Register temp_imm = temps.AcquireSameSizeAs(out);
2645 __ Mov(temp_imm, imm);
2646 __ Msub(out, temp, temp_imm, dividend);
2647 }
2648}
2649
2650void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2651 DCHECK(instruction->IsDiv() || instruction->IsRem());
2652 Primitive::Type type = instruction->GetResultType();
2653 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2654
2655 LocationSummary* locations = instruction->GetLocations();
2656 Register out = OutputRegister(instruction);
2657 Location second = locations->InAt(1);
2658
2659 if (second.IsConstant()) {
2660 int64_t imm = Int64FromConstant(second.GetConstant());
2661
2662 if (imm == 0) {
2663 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2664 } else if (imm == 1 || imm == -1) {
2665 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002666 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002667 DivRemByPowerOfTwo(instruction);
2668 } else {
2669 DCHECK(imm <= -2 || imm >= 2);
2670 GenerateDivRemWithAnyConstant(instruction);
2671 }
2672 } else {
2673 Register dividend = InputRegisterAt(instruction, 0);
2674 Register divisor = InputRegisterAt(instruction, 1);
2675 if (instruction->IsDiv()) {
2676 __ Sdiv(out, dividend, divisor);
2677 } else {
2678 UseScratchRegisterScope temps(GetVIXLAssembler());
2679 Register temp = temps.AcquireSameSizeAs(out);
2680 __ Sdiv(temp, dividend, divisor);
2681 __ Msub(out, temp, divisor, dividend);
2682 }
2683 }
2684}
2685
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002686void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2687 LocationSummary* locations =
2688 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2689 switch (div->GetResultType()) {
2690 case Primitive::kPrimInt:
2691 case Primitive::kPrimLong:
2692 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002693 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002694 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2695 break;
2696
2697 case Primitive::kPrimFloat:
2698 case Primitive::kPrimDouble:
2699 locations->SetInAt(0, Location::RequiresFpuRegister());
2700 locations->SetInAt(1, Location::RequiresFpuRegister());
2701 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2702 break;
2703
2704 default:
2705 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2706 }
2707}
2708
2709void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2710 Primitive::Type type = div->GetResultType();
2711 switch (type) {
2712 case Primitive::kPrimInt:
2713 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002714 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002715 break;
2716
2717 case Primitive::kPrimFloat:
2718 case Primitive::kPrimDouble:
2719 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2720 break;
2721
2722 default:
2723 LOG(FATAL) << "Unexpected div type " << type;
2724 }
2725}
2726
Alexandre Rames67555f72014-11-18 10:55:16 +00002727void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002728 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002729 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00002730}
2731
2732void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2733 SlowPathCodeARM64* slow_path =
2734 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2735 codegen_->AddSlowPath(slow_path);
2736 Location value = instruction->GetLocations()->InAt(0);
2737
Alexandre Rames3e69f162014-12-10 10:36:50 +00002738 Primitive::Type type = instruction->GetType();
2739
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002740 if (!Primitive::IsIntegralType(type)) {
2741 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002742 return;
2743 }
2744
Alexandre Rames67555f72014-11-18 10:55:16 +00002745 if (value.IsConstant()) {
2746 int64_t divisor = Int64ConstantFrom(value);
2747 if (divisor == 0) {
2748 __ B(slow_path->GetEntryLabel());
2749 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002750 // A division by a non-null constant is valid. We don't need to perform
2751 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002752 }
2753 } else {
2754 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2755 }
2756}
2757
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002758void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2759 LocationSummary* locations =
2760 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2761 locations->SetOut(Location::ConstantLocation(constant));
2762}
2763
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002764void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2765 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002766 // Will be generated at use site.
2767}
2768
Alexandre Rames5319def2014-10-23 10:03:10 +01002769void LocationsBuilderARM64::VisitExit(HExit* exit) {
2770 exit->SetLocations(nullptr);
2771}
2772
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002773void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002774}
2775
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002776void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2777 LocationSummary* locations =
2778 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2779 locations->SetOut(Location::ConstantLocation(constant));
2780}
2781
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002782void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002783 // Will be generated at use site.
2784}
2785
David Brazdilfc6a86a2015-06-26 10:33:45 +00002786void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002787 DCHECK(!successor->IsExitBlock());
2788 HBasicBlock* block = got->GetBlock();
2789 HInstruction* previous = got->GetPrevious();
2790 HLoopInformation* info = block->GetLoopInformation();
2791
David Brazdil46e2a392015-03-16 17:31:52 +00002792 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002793 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2794 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2795 return;
2796 }
2797 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2798 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2799 }
2800 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002801 __ B(codegen_->GetLabelOf(successor));
2802 }
2803}
2804
David Brazdilfc6a86a2015-06-26 10:33:45 +00002805void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2806 got->SetLocations(nullptr);
2807}
2808
2809void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2810 HandleGoto(got, got->GetSuccessor());
2811}
2812
2813void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2814 try_boundary->SetLocations(nullptr);
2815}
2816
2817void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2818 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2819 if (!successor->IsExitBlock()) {
2820 HandleGoto(try_boundary, successor);
2821 }
2822}
2823
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002824void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002825 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002826 vixl::aarch64::Label* true_target,
2827 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002828 // FP branching requires both targets to be explicit. If either of the targets
2829 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002830 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002831 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002832
David Brazdil0debae72015-11-12 18:37:00 +00002833 if (true_target == nullptr && false_target == nullptr) {
2834 // Nothing to do. The code always falls through.
2835 return;
2836 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002837 // Constant condition, statically compared against "true" (integer value 1).
2838 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002839 if (true_target != nullptr) {
2840 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002841 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002842 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002843 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002844 if (false_target != nullptr) {
2845 __ B(false_target);
2846 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002847 }
David Brazdil0debae72015-11-12 18:37:00 +00002848 return;
2849 }
2850
2851 // The following code generates these patterns:
2852 // (1) true_target == nullptr && false_target != nullptr
2853 // - opposite condition true => branch to false_target
2854 // (2) true_target != nullptr && false_target == nullptr
2855 // - condition true => branch to true_target
2856 // (3) true_target != nullptr && false_target != nullptr
2857 // - condition true => branch to true_target
2858 // - branch to false_target
2859 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002860 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002861 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002862 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002863 if (true_target == nullptr) {
2864 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2865 } else {
2866 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2867 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002868 } else {
2869 // The condition instruction has not been materialized, use its inputs as
2870 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002871 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002872
David Brazdil0debae72015-11-12 18:37:00 +00002873 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002874 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00002875 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00002876 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002877 IfCondition opposite_condition = condition->GetOppositeCondition();
2878 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002879 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002880 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00002881 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002882 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002883 // Integer cases.
2884 Register lhs = InputRegisterAt(condition, 0);
2885 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002886
2887 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01002888 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002889 if (true_target == nullptr) {
2890 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2891 non_fallthrough_target = false_target;
2892 } else {
2893 arm64_cond = ARM64Condition(condition->GetCondition());
2894 non_fallthrough_target = true_target;
2895 }
2896
Aart Bik086d27e2016-01-20 17:02:00 -08002897 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01002898 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002899 switch (arm64_cond) {
2900 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002901 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002902 break;
2903 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002904 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002905 break;
2906 case lt:
2907 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002908 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002909 break;
2910 case ge:
2911 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002912 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002913 break;
2914 default:
2915 // Without the `static_cast` the compiler throws an error for
2916 // `-Werror=sign-promo`.
2917 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2918 }
2919 } else {
2920 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002921 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002922 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002923 }
2924 }
David Brazdil0debae72015-11-12 18:37:00 +00002925
2926 // If neither branch falls through (case 3), the conditional branch to `true_target`
2927 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2928 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002929 __ B(false_target);
2930 }
David Brazdil0debae72015-11-12 18:37:00 +00002931
2932 if (fallthrough_target.IsLinked()) {
2933 __ Bind(&fallthrough_target);
2934 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002935}
2936
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002937void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2938 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002939 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002940 locations->SetInAt(0, Location::RequiresRegister());
2941 }
2942}
2943
2944void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002945 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2946 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002947 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
2948 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
2949 true_target = nullptr;
2950 }
2951 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
2952 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
2953 false_target = nullptr;
2954 }
David Brazdil0debae72015-11-12 18:37:00 +00002955 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002956}
2957
2958void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2959 LocationSummary* locations = new (GetGraph()->GetArena())
2960 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01002961 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00002962 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002963 locations->SetInAt(0, Location::RequiresRegister());
2964 }
2965}
2966
2967void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002968 SlowPathCodeARM64* slow_path =
2969 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002970 GenerateTestAndBranch(deoptimize,
2971 /* condition_input_index */ 0,
2972 slow_path->GetEntryLabel(),
2973 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002974}
2975
David Brazdilc0b601b2016-02-08 14:20:45 +00002976static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
2977 return condition->IsCondition() &&
2978 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
2979}
2980
Alexandre Rames880f1192016-06-13 16:04:50 +01002981static inline Condition GetConditionForSelect(HCondition* condition) {
2982 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00002983 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
2984 : ARM64Condition(cond);
2985}
2986
David Brazdil74eb1b22015-12-14 11:44:01 +00002987void LocationsBuilderARM64::VisitSelect(HSelect* select) {
2988 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01002989 if (Primitive::IsFloatingPointType(select->GetType())) {
2990 locations->SetInAt(0, Location::RequiresFpuRegister());
2991 locations->SetInAt(1, Location::RequiresFpuRegister());
2992 locations->SetOut(Location::RequiresFpuRegister());
2993 } else {
2994 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
2995 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
2996 bool is_true_value_constant = cst_true_value != nullptr;
2997 bool is_false_value_constant = cst_false_value != nullptr;
2998 // Ask VIXL whether we should synthesize constants in registers.
2999 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3000 Operand true_op = is_true_value_constant ?
3001 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3002 Operand false_op = is_false_value_constant ?
3003 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3004 bool true_value_in_register = false;
3005 bool false_value_in_register = false;
3006 MacroAssembler::GetCselSynthesisInformation(
3007 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3008 true_value_in_register |= !is_true_value_constant;
3009 false_value_in_register |= !is_false_value_constant;
3010
3011 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3012 : Location::ConstantLocation(cst_true_value));
3013 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3014 : Location::ConstantLocation(cst_false_value));
3015 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003016 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003017
David Brazdil74eb1b22015-12-14 11:44:01 +00003018 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3019 locations->SetInAt(2, Location::RequiresRegister());
3020 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003021}
3022
3023void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003024 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003025 Condition csel_cond;
3026
3027 if (IsBooleanValueOrMaterializedCondition(cond)) {
3028 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003029 // Use the condition flags set by the previous instruction.
3030 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003031 } else {
3032 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003033 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003034 }
3035 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003036 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003037 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003038 } else {
3039 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003040 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003041 }
3042
Alexandre Rames880f1192016-06-13 16:04:50 +01003043 if (Primitive::IsFloatingPointType(select->GetType())) {
3044 __ Fcsel(OutputFPRegister(select),
3045 InputFPRegisterAt(select, 1),
3046 InputFPRegisterAt(select, 0),
3047 csel_cond);
3048 } else {
3049 __ Csel(OutputRegister(select),
3050 InputOperandAt(select, 1),
3051 InputOperandAt(select, 0),
3052 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003053 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003054}
3055
David Srbecky0cf44932015-12-09 14:09:59 +00003056void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3057 new (GetGraph()->GetArena()) LocationSummary(info);
3058}
3059
David Srbeckyd28f4a02016-03-14 17:14:24 +00003060void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3061 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003062}
3063
3064void CodeGeneratorARM64::GenerateNop() {
3065 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003066}
3067
Alexandre Rames5319def2014-10-23 10:03:10 +01003068void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003069 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003070}
3071
3072void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003073 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003074}
3075
3076void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003077 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003078}
3079
3080void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003081 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003082}
3083
Roland Levillain44015862016-01-22 11:47:17 +00003084static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
3085 return kEmitCompilerReadBarrier &&
3086 (kUseBakerReadBarrier ||
3087 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3088 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3089 type_check_kind == TypeCheckKind::kArrayObjectCheck);
3090}
3091
Alexandre Rames67555f72014-11-18 10:55:16 +00003092void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003093 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003094 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003095 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003096 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003097 case TypeCheckKind::kExactCheck:
3098 case TypeCheckKind::kAbstractClassCheck:
3099 case TypeCheckKind::kClassHierarchyCheck:
3100 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003101 call_kind =
3102 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003103 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003104 break;
3105 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003106 case TypeCheckKind::kUnresolvedCheck:
3107 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003108 call_kind = LocationSummary::kCallOnSlowPath;
3109 break;
3110 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003111
Alexandre Rames67555f72014-11-18 10:55:16 +00003112 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003113 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003114 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003115 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003116 locations->SetInAt(0, Location::RequiresRegister());
3117 locations->SetInAt(1, Location::RequiresRegister());
3118 // The "out" register is used as a temporary, so it overlaps with the inputs.
3119 // Note that TypeCheckSlowPathARM64 uses this register too.
3120 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3121 // When read barriers are enabled, we need a temporary register for
3122 // some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003123 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003124 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003125 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003126}
3127
3128void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003129 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003130 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003131 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003132 Register obj = InputRegisterAt(instruction, 0);
3133 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003134 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003135 Register out = OutputRegister(instruction);
Roland Levillain44015862016-01-22 11:47:17 +00003136 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3137 locations->GetTemp(0) :
3138 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003139 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3140 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3141 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3142 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003143
Scott Wakeling97c72b72016-06-24 16:19:36 +01003144 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003145 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003146
3147 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003148 // Avoid null check if we know `obj` is not null.
3149 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003150 __ Cbz(obj, &zero);
3151 }
3152
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003153 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003154 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003155
Roland Levillain44015862016-01-22 11:47:17 +00003156 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003157 case TypeCheckKind::kExactCheck: {
3158 __ Cmp(out, cls);
3159 __ Cset(out, eq);
3160 if (zero.IsLinked()) {
3161 __ B(&done);
3162 }
3163 break;
3164 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003165
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003166 case TypeCheckKind::kAbstractClassCheck: {
3167 // If the class is abstract, we eagerly fetch the super class of the
3168 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003169 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003170 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003171 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003172 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003173 // If `out` is null, we use it for the result, and jump to `done`.
3174 __ Cbz(out, &done);
3175 __ Cmp(out, cls);
3176 __ B(ne, &loop);
3177 __ Mov(out, 1);
3178 if (zero.IsLinked()) {
3179 __ B(&done);
3180 }
3181 break;
3182 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003183
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003184 case TypeCheckKind::kClassHierarchyCheck: {
3185 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003186 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003187 __ Bind(&loop);
3188 __ Cmp(out, cls);
3189 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003190 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003191 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003192 __ Cbnz(out, &loop);
3193 // If `out` is null, we use it for the result, and jump to `done`.
3194 __ B(&done);
3195 __ Bind(&success);
3196 __ Mov(out, 1);
3197 if (zero.IsLinked()) {
3198 __ B(&done);
3199 }
3200 break;
3201 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003202
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003203 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003204 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003205 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003206 __ Cmp(out, cls);
3207 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003208 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003209 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003210 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003211 // If `out` is null, we use it for the result, and jump to `done`.
3212 __ Cbz(out, &done);
3213 __ Ldrh(out, HeapOperand(out, primitive_offset));
3214 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3215 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003216 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003217 __ Mov(out, 1);
3218 __ B(&done);
3219 break;
3220 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003221
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003222 case TypeCheckKind::kArrayCheck: {
3223 __ Cmp(out, cls);
3224 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003225 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3226 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003227 codegen_->AddSlowPath(slow_path);
3228 __ B(ne, slow_path->GetEntryLabel());
3229 __ Mov(out, 1);
3230 if (zero.IsLinked()) {
3231 __ B(&done);
3232 }
3233 break;
3234 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003235
Calin Juravle98893e12015-10-02 21:05:03 +01003236 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003237 case TypeCheckKind::kInterfaceCheck: {
3238 // Note that we indeed only call on slow path, but we always go
3239 // into the slow path for the unresolved and interface check
3240 // cases.
3241 //
3242 // We cannot directly call the InstanceofNonTrivial runtime
3243 // entry point without resorting to a type checking slow path
3244 // here (i.e. by calling InvokeRuntime directly), as it would
3245 // require to assign fixed registers for the inputs of this
3246 // HInstanceOf instruction (following the runtime calling
3247 // convention), which might be cluttered by the potential first
3248 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003249 //
3250 // TODO: Introduce a new runtime entry point taking the object
3251 // to test (instead of its class) as argument, and let it deal
3252 // with the read barrier issues. This will let us refactor this
3253 // case of the `switch` code as it was previously (with a direct
3254 // call to the runtime not using a type checking slow path).
3255 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003256 DCHECK(locations->OnlyCallsOnSlowPath());
3257 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3258 /* is_fatal */ false);
3259 codegen_->AddSlowPath(slow_path);
3260 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003261 if (zero.IsLinked()) {
3262 __ B(&done);
3263 }
3264 break;
3265 }
3266 }
3267
3268 if (zero.IsLinked()) {
3269 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003270 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003271 }
3272
3273 if (done.IsLinked()) {
3274 __ Bind(&done);
3275 }
3276
3277 if (slow_path != nullptr) {
3278 __ Bind(slow_path->GetExitLabel());
3279 }
3280}
3281
3282void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3283 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3284 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3285
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003286 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3287 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003288 case TypeCheckKind::kExactCheck:
3289 case TypeCheckKind::kAbstractClassCheck:
3290 case TypeCheckKind::kClassHierarchyCheck:
3291 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003292 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3293 LocationSummary::kCallOnSlowPath :
3294 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003295 break;
3296 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003297 case TypeCheckKind::kUnresolvedCheck:
3298 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003299 call_kind = LocationSummary::kCallOnSlowPath;
3300 break;
3301 }
3302
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003303 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3304 locations->SetInAt(0, Location::RequiresRegister());
3305 locations->SetInAt(1, Location::RequiresRegister());
3306 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3307 locations->AddTemp(Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003308 // When read barriers are enabled, we need an additional temporary
3309 // register for some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003310 if (TypeCheckNeedsATemporary(type_check_kind)) {
3311 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003312 }
3313}
3314
3315void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003316 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003317 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003318 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003319 Register obj = InputRegisterAt(instruction, 0);
3320 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003321 Location temp_loc = locations->GetTemp(0);
Roland Levillain44015862016-01-22 11:47:17 +00003322 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3323 locations->GetTemp(1) :
3324 Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003325 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003326 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3327 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3328 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3329 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003330
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003331 bool is_type_check_slow_path_fatal =
3332 (type_check_kind == TypeCheckKind::kExactCheck ||
3333 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3334 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3335 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3336 !instruction->CanThrowIntoCatchBlock();
3337 SlowPathCodeARM64* type_check_slow_path =
3338 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3339 is_type_check_slow_path_fatal);
3340 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003341
Scott Wakeling97c72b72016-06-24 16:19:36 +01003342 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003343 // Avoid null check if we know obj is not null.
3344 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003345 __ Cbz(obj, &done);
3346 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003347
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003348 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003349 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003350
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003351 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003352 case TypeCheckKind::kExactCheck:
3353 case TypeCheckKind::kArrayCheck: {
3354 __ Cmp(temp, cls);
3355 // Jump to slow path for throwing the exception or doing a
3356 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003357 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003358 break;
3359 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003360
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003361 case TypeCheckKind::kAbstractClassCheck: {
3362 // If the class is abstract, we eagerly fetch the super class of the
3363 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003364 vixl::aarch64::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003365 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003366 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003367 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003368
3369 // If the class reference currently in `temp` is not null, jump
3370 // to the `compare_classes` label to compare it with the checked
3371 // class.
3372 __ Cbnz(temp, &compare_classes);
3373 // Otherwise, jump to the slow path to throw the exception.
3374 //
3375 // But before, move back the object's class into `temp` before
3376 // going into the slow path, as it has been overwritten in the
3377 // meantime.
3378 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003379 GenerateReferenceLoadTwoRegisters(
3380 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003381 __ B(type_check_slow_path->GetEntryLabel());
3382
3383 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003384 __ Cmp(temp, cls);
3385 __ B(ne, &loop);
3386 break;
3387 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003388
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003389 case TypeCheckKind::kClassHierarchyCheck: {
3390 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003391 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003392 __ Bind(&loop);
3393 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003394 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003395
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003396 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003397 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003398
3399 // If the class reference currently in `temp` is not null, jump
3400 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003401 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003402 // Otherwise, jump to the slow path to throw the exception.
3403 //
3404 // But before, move back the object's class into `temp` before
3405 // going into the slow path, as it has been overwritten in the
3406 // meantime.
3407 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003408 GenerateReferenceLoadTwoRegisters(
3409 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003410 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003411 break;
3412 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003413
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003414 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003415 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003416 vixl::aarch64::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003417 __ Cmp(temp, cls);
3418 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003419
3420 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003421 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003422 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003423
3424 // If the component type is not null (i.e. the object is indeed
3425 // an array), jump to label `check_non_primitive_component_type`
3426 // to further check that this component type is not a primitive
3427 // type.
3428 __ Cbnz(temp, &check_non_primitive_component_type);
3429 // Otherwise, jump to the slow path to throw the exception.
3430 //
3431 // But before, move back the object's class into `temp` before
3432 // going into the slow path, as it has been overwritten in the
3433 // meantime.
3434 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003435 GenerateReferenceLoadTwoRegisters(
3436 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003437 __ B(type_check_slow_path->GetEntryLabel());
3438
3439 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003440 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3441 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003442 __ Cbz(temp, &done);
3443 // Same comment as above regarding `temp` and the slow path.
3444 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003445 GenerateReferenceLoadTwoRegisters(
3446 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003447 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003448 break;
3449 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003450
Calin Juravle98893e12015-10-02 21:05:03 +01003451 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003452 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003453 // We always go into the type check slow path for the unresolved
3454 // and interface check cases.
3455 //
3456 // We cannot directly call the CheckCast runtime entry point
3457 // without resorting to a type checking slow path here (i.e. by
3458 // calling InvokeRuntime directly), as it would require to
3459 // assign fixed registers for the inputs of this HInstanceOf
3460 // instruction (following the runtime calling convention), which
3461 // might be cluttered by the potential first read barrier
3462 // emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003463 //
3464 // TODO: Introduce a new runtime entry point taking the object
3465 // to test (instead of its class) as argument, and let it deal
3466 // with the read barrier issues. This will let us refactor this
3467 // case of the `switch` code as it was previously (with a direct
3468 // call to the runtime not using a type checking slow path).
3469 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003470 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003471 break;
3472 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003473 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003474
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003475 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003476}
3477
Alexandre Rames5319def2014-10-23 10:03:10 +01003478void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3479 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3480 locations->SetOut(Location::ConstantLocation(constant));
3481}
3482
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003483void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003484 // Will be generated at use site.
3485}
3486
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003487void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3488 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3489 locations->SetOut(Location::ConstantLocation(constant));
3490}
3491
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003492void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003493 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003494}
3495
Calin Juravle175dc732015-08-25 15:42:32 +01003496void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3497 // The trampoline uses the same calling convention as dex calling conventions,
3498 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3499 // the method_idx.
3500 HandleInvoke(invoke);
3501}
3502
3503void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3504 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3505}
3506
Alexandre Rames5319def2014-10-23 10:03:10 +01003507void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003508 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003509 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003510}
3511
Alexandre Rames67555f72014-11-18 10:55:16 +00003512void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3513 HandleInvoke(invoke);
3514}
3515
3516void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3517 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003518 LocationSummary* locations = invoke->GetLocations();
3519 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003520 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003521 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003522 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003523
3524 // The register ip1 is required to be used for the hidden argument in
3525 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003526 MacroAssembler* masm = GetVIXLAssembler();
3527 UseScratchRegisterScope scratch_scope(masm);
3528 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003529 scratch_scope.Exclude(ip1);
3530 __ Mov(ip1, invoke->GetDexMethodIndex());
3531
Alexandre Rames67555f72014-11-18 10:55:16 +00003532 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003533 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003534 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003535 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003536 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003537 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003538 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003539 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003540 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003541 // Instead of simply (possibly) unpoisoning `temp` here, we should
3542 // emit a read barrier for the previous class reference load.
3543 // However this is not required in practice, as this is an
3544 // intermediate/temporary reference and because the current
3545 // concurrent copying collector keeps the from-space memory
3546 // intact/accessible until the end of the marking phase (the
3547 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003548 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003549 __ Ldr(temp,
3550 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3551 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003552 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003553 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003554 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003555 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003556 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003557 // lr();
3558 __ Blr(lr);
3559 DCHECK(!codegen_->IsLeafMethod());
3560 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3561}
3562
3563void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003564 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3565 if (intrinsic.TryDispatch(invoke)) {
3566 return;
3567 }
3568
Alexandre Rames67555f72014-11-18 10:55:16 +00003569 HandleInvoke(invoke);
3570}
3571
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003572void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003573 // Explicit clinit checks triggered by static invokes must have been pruned by
3574 // art::PrepareForRegisterAllocation.
3575 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003576
Andreas Gampe878d58c2015-01-15 23:24:00 -08003577 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3578 if (intrinsic.TryDispatch(invoke)) {
3579 return;
3580 }
3581
Alexandre Rames67555f72014-11-18 10:55:16 +00003582 HandleInvoke(invoke);
3583}
3584
Andreas Gampe878d58c2015-01-15 23:24:00 -08003585static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3586 if (invoke->GetLocations()->Intrinsified()) {
3587 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3588 intrinsic.Dispatch(invoke);
3589 return true;
3590 }
3591 return false;
3592}
3593
Vladimir Markodc151b22015-10-15 18:02:30 +01003594HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3595 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003596 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003597 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003598 return desired_dispatch_info;
3599}
3600
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003601void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003602 // For better instruction scheduling we load the direct code pointer before the method pointer.
3603 bool direct_code_loaded = false;
3604 switch (invoke->GetCodePtrLocation()) {
3605 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3606 // LR = code address from literal pool with link-time patch.
3607 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3608 direct_code_loaded = true;
3609 break;
3610 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3611 // LR = invoke->GetDirectCodePtr();
3612 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3613 direct_code_loaded = true;
3614 break;
3615 default:
3616 break;
3617 }
3618
Andreas Gampe878d58c2015-01-15 23:24:00 -08003619 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003620 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3621 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003622 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
3623 uint32_t offset =
3624 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003625 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003626 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00003627 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003628 }
Vladimir Marko58155012015-08-19 12:49:41 +00003629 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003630 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003631 break;
3632 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3633 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003634 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003635 break;
3636 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3637 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003638 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003639 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3640 break;
3641 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3642 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003643 const DexFile& dex_file = invoke->GetDexFile();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003644 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003645 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Marko63dccbb2016-09-21 13:51:10 +01003646 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00003647 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003648 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003649 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Marko63dccbb2016-09-21 13:51:10 +01003650 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00003651 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003652 }
Vladimir Marko58155012015-08-19 12:49:41 +00003653 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003654 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003655 Register reg = XRegisterFrom(temp);
3656 Register method_reg;
3657 if (current_method.IsRegister()) {
3658 method_reg = XRegisterFrom(current_method);
3659 } else {
3660 DCHECK(invoke->GetLocations()->Intrinsified());
3661 DCHECK(!current_method.IsValid());
3662 method_reg = reg;
3663 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3664 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003665
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003666 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003667 __ Ldr(reg.X(),
3668 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07003669 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003670 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01003671 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3672 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003673 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3674 break;
3675 }
3676 }
3677
3678 switch (invoke->GetCodePtrLocation()) {
3679 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3680 __ Bl(&frame_entry_label_);
3681 break;
3682 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Marko63dccbb2016-09-21 13:51:10 +01003683 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3684 invoke->GetTargetMethod().dex_method_index);
Scott Wakeling97c72b72016-06-24 16:19:36 +01003685 vixl::aarch64::Label* label = &relative_call_patches_.back().label;
3686 SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003687 __ Bind(label);
3688 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003689 break;
3690 }
3691 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3692 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3693 // LR prepared above for better instruction scheduling.
3694 DCHECK(direct_code_loaded);
3695 // lr()
3696 __ Blr(lr);
3697 break;
3698 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3699 // LR = callee_method->entry_point_from_quick_compiled_code_;
3700 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003701 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07003702 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003703 // lr()
3704 __ Blr(lr);
3705 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003706 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003707
Andreas Gampe878d58c2015-01-15 23:24:00 -08003708 DCHECK(!IsLeafMethod());
3709}
3710
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003711void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003712 // Use the calling convention instead of the location of the receiver, as
3713 // intrinsics may have put the receiver in a different register. In the intrinsics
3714 // slow path, the arguments have been moved to the right place, so here we are
3715 // guaranteed that the receiver is the first register of the calling convention.
3716 InvokeDexCallingConvention calling_convention;
3717 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003718 Register temp = XRegisterFrom(temp_in);
3719 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3720 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3721 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003722 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003723
3724 BlockPoolsScope block_pools(GetVIXLAssembler());
3725
3726 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003727 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003728 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003729 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003730 // Instead of simply (possibly) unpoisoning `temp` here, we should
3731 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003732 // intermediate/temporary reference and because the current
3733 // concurrent copying collector keeps the from-space memory
3734 // intact/accessible until the end of the marking phase (the
3735 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003736 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3737 // temp = temp->GetMethodAt(method_offset);
3738 __ Ldr(temp, MemOperand(temp, method_offset));
3739 // lr = temp->GetEntryPoint();
3740 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3741 // lr();
3742 __ Blr(lr);
3743}
3744
Scott Wakeling97c72b72016-06-24 16:19:36 +01003745vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
3746 const DexFile& dex_file,
3747 uint32_t string_index,
3748 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003749 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
3750}
3751
Scott Wakeling97c72b72016-06-24 16:19:36 +01003752vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
3753 const DexFile& dex_file,
3754 uint32_t type_index,
3755 vixl::aarch64::Label* adrp_label) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003756 return NewPcRelativePatch(dex_file, type_index, adrp_label, &pc_relative_type_patches_);
3757}
3758
Scott Wakeling97c72b72016-06-24 16:19:36 +01003759vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
3760 const DexFile& dex_file,
3761 uint32_t element_offset,
3762 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003763 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
3764}
3765
Scott Wakeling97c72b72016-06-24 16:19:36 +01003766vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
3767 const DexFile& dex_file,
3768 uint32_t offset_or_index,
3769 vixl::aarch64::Label* adrp_label,
3770 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003771 // Add a patch entry and return the label.
3772 patches->emplace_back(dex_file, offset_or_index);
3773 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003774 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003775 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
3776 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
3777 return label;
3778}
3779
Scott Wakeling97c72b72016-06-24 16:19:36 +01003780vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003781 const DexFile& dex_file, uint32_t string_index) {
3782 return boot_image_string_patches_.GetOrCreate(
3783 StringReference(&dex_file, string_index),
3784 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3785}
3786
Scott Wakeling97c72b72016-06-24 16:19:36 +01003787vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003788 const DexFile& dex_file, uint32_t type_index) {
3789 return boot_image_type_patches_.GetOrCreate(
3790 TypeReference(&dex_file, type_index),
3791 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3792}
3793
Scott Wakeling97c72b72016-06-24 16:19:36 +01003794vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
3795 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003796 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
3797 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
3798 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
3799}
3800
Scott Wakeling97c72b72016-06-24 16:19:36 +01003801vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(
3802 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003803 return DeduplicateUint64Literal(address);
3804}
3805
Vladimir Marko63dccbb2016-09-21 13:51:10 +01003806void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
3807 vixl::aarch64::Register reg) {
3808 DCHECK(reg.IsX());
3809 SingleEmissionCheckScope guard(GetVIXLAssembler());
3810 __ Bind(fixup_label);
3811 __ adrp(reg, /* offset placeholder */ 0);
3812}
3813
3814void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
3815 vixl::aarch64::Register out,
3816 vixl::aarch64::Register base) {
3817 DCHECK(out.IsX());
3818 DCHECK(base.IsX());
3819 SingleEmissionCheckScope guard(GetVIXLAssembler());
3820 __ Bind(fixup_label);
3821 __ add(out, base, Operand(/* offset placeholder */ 0));
3822}
3823
3824void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
3825 vixl::aarch64::Register out,
3826 vixl::aarch64::Register base) {
3827 DCHECK(base.IsX());
3828 SingleEmissionCheckScope guard(GetVIXLAssembler());
3829 __ Bind(fixup_label);
3830 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
3831}
3832
3833template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
3834inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
3835 const ArenaDeque<PcRelativePatchInfo>& infos,
3836 ArenaVector<LinkerPatch>* linker_patches) {
3837 for (const PcRelativePatchInfo& info : infos) {
3838 linker_patches->push_back(Factory(info.label.GetLocation(),
3839 &info.target_dex_file,
3840 info.pc_insn_label->GetLocation(),
3841 info.offset_or_index));
3842 }
3843}
3844
Vladimir Marko58155012015-08-19 12:49:41 +00003845void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3846 DCHECK(linker_patches->empty());
3847 size_t size =
3848 method_patches_.size() +
3849 call_patches_.size() +
3850 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003851 pc_relative_dex_cache_patches_.size() +
3852 boot_image_string_patches_.size() +
3853 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003854 boot_image_type_patches_.size() +
3855 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003856 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003857 linker_patches->reserve(size);
3858 for (const auto& entry : method_patches_) {
3859 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003860 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3861 linker_patches->push_back(LinkerPatch::MethodPatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003862 target_method.dex_file,
3863 target_method.dex_method_index));
3864 }
3865 for (const auto& entry : call_patches_) {
3866 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003867 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3868 linker_patches->push_back(LinkerPatch::CodePatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003869 target_method.dex_file,
3870 target_method.dex_method_index));
3871 }
Vladimir Marko63dccbb2016-09-21 13:51:10 +01003872 for (const PatchInfo<vixl::aarch64::Label>& info : relative_call_patches_) {
3873 linker_patches->push_back(
3874 LinkerPatch::RelativeCodePatch(info.label.GetLocation(), &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00003875 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003876 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003877 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003878 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003879 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003880 info.offset_or_index));
3881 }
3882 for (const auto& entry : boot_image_string_patches_) {
3883 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003884 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3885 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003886 target_string.dex_file,
3887 target_string.string_index));
3888 }
Vladimir Marko63dccbb2016-09-21 13:51:10 +01003889 if (!GetCompilerOptions().IsBootImage()) {
3890 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
3891 linker_patches);
3892 } else {
3893 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
3894 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003895 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003896 for (const auto& entry : boot_image_type_patches_) {
3897 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003898 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3899 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003900 target_type.dex_file,
3901 target_type.type_index));
3902 }
Vladimir Marko63dccbb2016-09-21 13:51:10 +01003903 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
3904 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003905 for (const auto& entry : boot_image_address_patches_) {
3906 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003907 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3908 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003909 }
3910}
3911
Scott Wakeling97c72b72016-06-24 16:19:36 +01003912vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003913 Uint32ToLiteralMap* map) {
3914 return map->GetOrCreate(
3915 value,
3916 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
3917}
3918
Scott Wakeling97c72b72016-06-24 16:19:36 +01003919vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003920 return uint64_literals_.GetOrCreate(
3921 value,
3922 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00003923}
3924
Scott Wakeling97c72b72016-06-24 16:19:36 +01003925vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003926 MethodReference target_method,
3927 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003928 return map->GetOrCreate(
3929 target_method,
3930 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00003931}
3932
Scott Wakeling97c72b72016-06-24 16:19:36 +01003933vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003934 MethodReference target_method) {
3935 return DeduplicateMethodLiteral(target_method, &method_patches_);
3936}
3937
Scott Wakeling97c72b72016-06-24 16:19:36 +01003938vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003939 MethodReference target_method) {
3940 return DeduplicateMethodLiteral(target_method, &call_patches_);
3941}
3942
3943
Andreas Gampe878d58c2015-01-15 23:24:00 -08003944void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003945 // Explicit clinit checks triggered by static invokes must have been pruned by
3946 // art::PrepareForRegisterAllocation.
3947 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003948
Andreas Gampe878d58c2015-01-15 23:24:00 -08003949 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3950 return;
3951 }
3952
Alexandre Ramesd921d642015-04-16 15:07:16 +01003953 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003954 LocationSummary* locations = invoke->GetLocations();
3955 codegen_->GenerateStaticOrDirectCall(
3956 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003957 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003958}
3959
3960void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003961 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3962 return;
3963 }
3964
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003965 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003966 DCHECK(!codegen_->IsLeafMethod());
3967 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3968}
3969
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003970HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
3971 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003972 switch (desired_class_load_kind) {
3973 case HLoadClass::LoadKind::kReferrersClass:
3974 break;
3975 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3976 DCHECK(!GetCompilerOptions().GetCompilePic());
3977 break;
3978 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3979 DCHECK(GetCompilerOptions().GetCompilePic());
3980 break;
3981 case HLoadClass::LoadKind::kBootImageAddress:
3982 break;
3983 case HLoadClass::LoadKind::kDexCacheAddress:
3984 DCHECK(Runtime::Current()->UseJitCompilation());
3985 break;
3986 case HLoadClass::LoadKind::kDexCachePcRelative:
3987 DCHECK(!Runtime::Current()->UseJitCompilation());
3988 break;
3989 case HLoadClass::LoadKind::kDexCacheViaMethod:
3990 break;
3991 }
3992 return desired_class_load_kind;
3993}
3994
Alexandre Rames67555f72014-11-18 10:55:16 +00003995void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003996 if (cls->NeedsAccessCheck()) {
3997 InvokeRuntimeCallingConvention calling_convention;
3998 CodeGenerator::CreateLoadClassLocationSummary(
3999 cls,
4000 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01004001 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004002 /* code_generator_supports_read_barrier */ true);
4003 return;
4004 }
4005
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004006 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4007 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004008 ? LocationSummary::kCallOnSlowPath
4009 : LocationSummary::kNoCall;
4010 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004011 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004012 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004013 }
4014
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004015 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4016 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
4017 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4018 locations->SetInAt(0, Location::RequiresRegister());
4019 }
4020 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004021}
4022
4023void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004024 if (cls->NeedsAccessCheck()) {
4025 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004026 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004027 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01004028 return;
4029 }
4030
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004031 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004032 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004033
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004034 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004035 bool generate_null_check = false;
4036 switch (cls->GetLoadKind()) {
4037 case HLoadClass::LoadKind::kReferrersClass: {
4038 DCHECK(!cls->CanCallRuntime());
4039 DCHECK(!cls->MustGenerateClinitCheck());
4040 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4041 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004042 GenerateGcRootFieldLoad(cls,
4043 out_loc,
4044 current_method,
4045 ArtMethod::DeclaringClassOffset().Int32Value(),
4046 /*fixup_label*/ nullptr,
4047 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004048 break;
4049 }
4050 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004051 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004052 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4053 cls->GetTypeIndex()));
4054 break;
4055 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004056 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004057 // Add ADRP with its PC-relative type patch.
4058 const DexFile& dex_file = cls->GetDexFile();
4059 uint32_t type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004060 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Marko63dccbb2016-09-21 13:51:10 +01004061 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004062 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004063 vixl::aarch64::Label* add_label =
4064 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Marko63dccbb2016-09-21 13:51:10 +01004065 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004066 break;
4067 }
4068 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004069 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004070 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4071 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4072 break;
4073 }
4074 case HLoadClass::LoadKind::kDexCacheAddress: {
4075 DCHECK_NE(cls->GetAddress(), 0u);
4076 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4077 // that gives a 16KiB range. To try and reduce the number of literals if we load
4078 // multiple types, simply split the dex cache address to a 16KiB aligned base
4079 // loaded from a literal and the remaining offset embedded in the load.
4080 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4081 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4082 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4083 uint64_t base_address = cls->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4084 uint32_t offset = cls->GetAddress() & MaxInt<uint64_t>(offset_bits);
4085 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4086 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004087 GenerateGcRootFieldLoad(cls,
4088 out_loc,
4089 out.X(),
4090 offset,
4091 /*fixup_label*/ nullptr,
4092 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004093 generate_null_check = !cls->IsInDexCache();
4094 break;
4095 }
4096 case HLoadClass::LoadKind::kDexCachePcRelative: {
4097 // Add ADRP with its PC-relative DexCache access patch.
4098 const DexFile& dex_file = cls->GetDexFile();
4099 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004100 vixl::aarch64::Label* adrp_label =
4101 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Marko63dccbb2016-09-21 13:51:10 +01004102 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004103 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004104 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004105 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4106 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004107 GenerateGcRootFieldLoad(cls,
4108 out_loc,
4109 out.X(),
4110 /* offset placeholder */ 0,
4111 ldr_label,
4112 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004113 generate_null_check = !cls->IsInDexCache();
4114 break;
4115 }
4116 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4117 MemberOffset resolved_types_offset =
4118 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4119 // /* GcRoot<mirror::Class>[] */ out =
4120 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4121 Register current_method = InputRegisterAt(cls, 0);
4122 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4123 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004124 GenerateGcRootFieldLoad(cls,
4125 out_loc,
4126 out.X(),
4127 CodeGenerator::GetCacheOffset(cls->GetTypeIndex()),
4128 /*fixup_label*/ nullptr,
4129 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004130 generate_null_check = !cls->IsInDexCache();
4131 break;
4132 }
4133 }
4134
4135 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4136 DCHECK(cls->CanCallRuntime());
4137 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4138 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4139 codegen_->AddSlowPath(slow_path);
4140 if (generate_null_check) {
4141 __ Cbz(out, slow_path->GetEntryLabel());
4142 }
4143 if (cls->MustGenerateClinitCheck()) {
4144 GenerateClassInitializationCheck(slow_path, out);
4145 } else {
4146 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004147 }
4148 }
4149}
4150
David Brazdilcb1c0552015-08-04 16:22:25 +01004151static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004152 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004153}
4154
Alexandre Rames67555f72014-11-18 10:55:16 +00004155void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4156 LocationSummary* locations =
4157 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4158 locations->SetOut(Location::RequiresRegister());
4159}
4160
4161void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004162 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4163}
4164
4165void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4166 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4167}
4168
4169void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4170 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004171}
4172
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004173HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4174 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004175 switch (desired_string_load_kind) {
4176 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4177 DCHECK(!GetCompilerOptions().GetCompilePic());
4178 break;
4179 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4180 DCHECK(GetCompilerOptions().GetCompilePic());
4181 break;
4182 case HLoadString::LoadKind::kBootImageAddress:
4183 break;
4184 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01004185 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004186 break;
Vladimir Marko63dccbb2016-09-21 13:51:10 +01004187 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004188 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004189 break;
4190 case HLoadString::LoadKind::kDexCacheViaMethod:
4191 break;
4192 }
4193 return desired_string_load_kind;
4194}
4195
Alexandre Rames67555f72014-11-18 10:55:16 +00004196void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004197 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
Vladimir Marko63dccbb2016-09-21 13:51:10 +01004198 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
4199 ? LocationSummary::kCallOnMainOnly
4200 : LocationSummary::kCallOnSlowPath)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004201 : LocationSummary::kNoCall;
4202 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004203 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
4204 locations->SetInAt(0, Location::RequiresRegister());
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004205 InvokeRuntimeCallingConvention calling_convention;
4206 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4207 } else {
4208 locations->SetOut(Location::RequiresRegister());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004209 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004210}
4211
4212void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004213 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004214
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004215 switch (load->GetLoadKind()) {
4216 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004217 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4218 load->GetStringIndex()));
4219 return; // No dex cache slow path.
4220 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004221 // Add ADRP with its PC-relative String patch.
4222 const DexFile& dex_file = load->GetDexFile();
4223 uint32_t string_index = load->GetStringIndex();
Vladimir Marko63dccbb2016-09-21 13:51:10 +01004224 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004225 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko63dccbb2016-09-21 13:51:10 +01004226 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004227 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004228 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004229 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Marko63dccbb2016-09-21 13:51:10 +01004230 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004231 return; // No dex cache slow path.
4232 }
4233 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004234 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4235 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4236 return; // No dex cache slow path.
4237 }
Vladimir Marko63dccbb2016-09-21 13:51:10 +01004238 case HLoadString::LoadKind::kBssEntry: {
4239 // Add ADRP with its PC-relative String .bss entry patch.
4240 const DexFile& dex_file = load->GetDexFile();
4241 uint32_t string_index = load->GetStringIndex();
4242 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
4243 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
4244 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
4245 // Add LDR with its PC-relative String patch.
4246 vixl::aarch64::Label* ldr_label =
4247 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
4248 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4249 GenerateGcRootFieldLoad(load,
4250 load->GetLocations()->Out(),
4251 out.X(),
4252 /* placeholder */ 0u,
4253 ldr_label);
4254 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
4255 codegen_->AddSlowPath(slow_path);
4256 __ Cbz(out.X(), slow_path->GetEntryLabel());
4257 __ Bind(slow_path->GetExitLabel());
4258 return;
4259 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004260 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004261 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004262 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004263
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004264 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004265 InvokeRuntimeCallingConvention calling_convention;
4266 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex());
4267 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4268 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004269}
4270
Alexandre Rames5319def2014-10-23 10:03:10 +01004271void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4272 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4273 locations->SetOut(Location::ConstantLocation(constant));
4274}
4275
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004276void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004277 // Will be generated at use site.
4278}
4279
Alexandre Rames67555f72014-11-18 10:55:16 +00004280void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4281 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004282 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004283 InvokeRuntimeCallingConvention calling_convention;
4284 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4285}
4286
4287void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004288 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject: kQuickUnlockObject,
4289 instruction,
4290 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004291 if (instruction->IsEnter()) {
4292 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4293 } else {
4294 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4295 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004296}
4297
Alexandre Rames42d641b2014-10-27 14:00:51 +00004298void LocationsBuilderARM64::VisitMul(HMul* mul) {
4299 LocationSummary* locations =
4300 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4301 switch (mul->GetResultType()) {
4302 case Primitive::kPrimInt:
4303 case Primitive::kPrimLong:
4304 locations->SetInAt(0, Location::RequiresRegister());
4305 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004306 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004307 break;
4308
4309 case Primitive::kPrimFloat:
4310 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004311 locations->SetInAt(0, Location::RequiresFpuRegister());
4312 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004313 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004314 break;
4315
4316 default:
4317 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4318 }
4319}
4320
4321void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4322 switch (mul->GetResultType()) {
4323 case Primitive::kPrimInt:
4324 case Primitive::kPrimLong:
4325 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4326 break;
4327
4328 case Primitive::kPrimFloat:
4329 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004330 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004331 break;
4332
4333 default:
4334 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4335 }
4336}
4337
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004338void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4339 LocationSummary* locations =
4340 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4341 switch (neg->GetResultType()) {
4342 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004343 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004344 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004345 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004346 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004347
4348 case Primitive::kPrimFloat:
4349 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004350 locations->SetInAt(0, Location::RequiresFpuRegister());
4351 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004352 break;
4353
4354 default:
4355 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4356 }
4357}
4358
4359void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4360 switch (neg->GetResultType()) {
4361 case Primitive::kPrimInt:
4362 case Primitive::kPrimLong:
4363 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4364 break;
4365
4366 case Primitive::kPrimFloat:
4367 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004368 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004369 break;
4370
4371 default:
4372 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4373 }
4374}
4375
4376void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4377 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004378 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004379 InvokeRuntimeCallingConvention calling_convention;
4380 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004381 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004382 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004383 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004384}
4385
4386void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4387 LocationSummary* locations = instruction->GetLocations();
4388 InvokeRuntimeCallingConvention calling_convention;
4389 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4390 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004391 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004392 // Note: if heap poisoning is enabled, the entry point takes cares
4393 // of poisoning the reference.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004394 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004395 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004396}
4397
Alexandre Rames5319def2014-10-23 10:03:10 +01004398void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4399 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004400 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004401 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004402 if (instruction->IsStringAlloc()) {
4403 locations->AddTemp(LocationFrom(kArtMethodRegister));
4404 } else {
4405 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4406 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4407 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004408 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4409}
4410
4411void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004412 // Note: if heap poisoning is enabled, the entry point takes cares
4413 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004414 if (instruction->IsStringAlloc()) {
4415 // String is allocated through StringFactory. Call NewEmptyString entry point.
4416 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004417 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004418 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4419 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4420 __ Blr(lr);
4421 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4422 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004423 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004424 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4425 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004426}
4427
4428void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4429 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004430 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004431 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004432}
4433
4434void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004435 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004436 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004437 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004438 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004439 break;
4440
4441 default:
4442 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4443 }
4444}
4445
David Brazdil66d126e2015-04-03 16:02:44 +01004446void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4447 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4448 locations->SetInAt(0, Location::RequiresRegister());
4449 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4450}
4451
4452void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004453 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004454}
4455
Alexandre Rames5319def2014-10-23 10:03:10 +01004456void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004457 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4458 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01004459}
4460
Calin Juravle2ae48182016-03-16 14:05:09 +00004461void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4462 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004463 return;
4464 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004465
Alexandre Ramesd921d642015-04-16 15:07:16 +01004466 BlockPoolsScope block_pools(GetVIXLAssembler());
4467 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004468 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004469 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004470}
4471
Calin Juravle2ae48182016-03-16 14:05:09 +00004472void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004473 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004474 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004475
4476 LocationSummary* locations = instruction->GetLocations();
4477 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004478
4479 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004480}
4481
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004482void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004483 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004484}
4485
Alexandre Rames67555f72014-11-18 10:55:16 +00004486void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4487 HandleBinaryOp(instruction);
4488}
4489
4490void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4491 HandleBinaryOp(instruction);
4492}
4493
Alexandre Rames3e69f162014-12-10 10:36:50 +00004494void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4495 LOG(FATAL) << "Unreachable";
4496}
4497
4498void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4499 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4500}
4501
Alexandre Rames5319def2014-10-23 10:03:10 +01004502void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4503 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4504 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4505 if (location.IsStackSlot()) {
4506 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4507 } else if (location.IsDoubleStackSlot()) {
4508 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4509 }
4510 locations->SetOut(location);
4511}
4512
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004513void InstructionCodeGeneratorARM64::VisitParameterValue(
4514 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004515 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004516}
4517
4518void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4519 LocationSummary* locations =
4520 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004521 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004522}
4523
4524void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4525 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4526 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004527}
4528
4529void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4530 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004531 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004532 locations->SetInAt(i, Location::Any());
4533 }
4534 locations->SetOut(Location::Any());
4535}
4536
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004537void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004538 LOG(FATAL) << "Unreachable";
4539}
4540
Serban Constantinescu02164b32014-11-13 14:05:07 +00004541void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004542 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004543 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004544 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4545 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004546 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4547
4548 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004549 case Primitive::kPrimInt:
4550 case Primitive::kPrimLong:
4551 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004552 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004553 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4554 break;
4555
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004556 case Primitive::kPrimFloat:
4557 case Primitive::kPrimDouble: {
4558 InvokeRuntimeCallingConvention calling_convention;
4559 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4560 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4561 locations->SetOut(calling_convention.GetReturnLocation(type));
4562
4563 break;
4564 }
4565
Serban Constantinescu02164b32014-11-13 14:05:07 +00004566 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004567 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004568 }
4569}
4570
4571void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4572 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004573
Serban Constantinescu02164b32014-11-13 14:05:07 +00004574 switch (type) {
4575 case Primitive::kPrimInt:
4576 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004577 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004578 break;
4579 }
4580
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004581 case Primitive::kPrimFloat:
4582 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004583 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4584 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004585 if (type == Primitive::kPrimFloat) {
4586 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4587 } else {
4588 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4589 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004590 break;
4591 }
4592
Serban Constantinescu02164b32014-11-13 14:05:07 +00004593 default:
4594 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004595 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004596 }
4597}
4598
Calin Juravle27df7582015-04-17 19:12:31 +01004599void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4600 memory_barrier->SetLocations(nullptr);
4601}
4602
4603void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004604 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004605}
4606
Alexandre Rames5319def2014-10-23 10:03:10 +01004607void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4608 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4609 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004610 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004611}
4612
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004613void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004614 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004615}
4616
4617void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4618 instruction->SetLocations(nullptr);
4619}
4620
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004621void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004622 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004623}
4624
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004625void LocationsBuilderARM64::VisitRor(HRor* ror) {
4626 HandleBinaryOp(ror);
4627}
4628
4629void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4630 HandleBinaryOp(ror);
4631}
4632
Serban Constantinescu02164b32014-11-13 14:05:07 +00004633void LocationsBuilderARM64::VisitShl(HShl* shl) {
4634 HandleShift(shl);
4635}
4636
4637void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4638 HandleShift(shl);
4639}
4640
4641void LocationsBuilderARM64::VisitShr(HShr* shr) {
4642 HandleShift(shr);
4643}
4644
4645void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4646 HandleShift(shr);
4647}
4648
Alexandre Rames5319def2014-10-23 10:03:10 +01004649void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004650 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004651}
4652
4653void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004654 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004655}
4656
Alexandre Rames67555f72014-11-18 10:55:16 +00004657void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004658 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004659}
4660
4661void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004662 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004663}
4664
4665void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004666 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004667}
4668
Alexandre Rames67555f72014-11-18 10:55:16 +00004669void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004670 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004671}
4672
Calin Juravlee460d1d2015-09-29 04:52:17 +01004673void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4674 HUnresolvedInstanceFieldGet* instruction) {
4675 FieldAccessCallingConventionARM64 calling_convention;
4676 codegen_->CreateUnresolvedFieldLocationSummary(
4677 instruction, instruction->GetFieldType(), calling_convention);
4678}
4679
4680void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4681 HUnresolvedInstanceFieldGet* instruction) {
4682 FieldAccessCallingConventionARM64 calling_convention;
4683 codegen_->GenerateUnresolvedFieldAccess(instruction,
4684 instruction->GetFieldType(),
4685 instruction->GetFieldIndex(),
4686 instruction->GetDexPc(),
4687 calling_convention);
4688}
4689
4690void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4691 HUnresolvedInstanceFieldSet* instruction) {
4692 FieldAccessCallingConventionARM64 calling_convention;
4693 codegen_->CreateUnresolvedFieldLocationSummary(
4694 instruction, instruction->GetFieldType(), calling_convention);
4695}
4696
4697void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4698 HUnresolvedInstanceFieldSet* instruction) {
4699 FieldAccessCallingConventionARM64 calling_convention;
4700 codegen_->GenerateUnresolvedFieldAccess(instruction,
4701 instruction->GetFieldType(),
4702 instruction->GetFieldIndex(),
4703 instruction->GetDexPc(),
4704 calling_convention);
4705}
4706
4707void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4708 HUnresolvedStaticFieldGet* instruction) {
4709 FieldAccessCallingConventionARM64 calling_convention;
4710 codegen_->CreateUnresolvedFieldLocationSummary(
4711 instruction, instruction->GetFieldType(), calling_convention);
4712}
4713
4714void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4715 HUnresolvedStaticFieldGet* instruction) {
4716 FieldAccessCallingConventionARM64 calling_convention;
4717 codegen_->GenerateUnresolvedFieldAccess(instruction,
4718 instruction->GetFieldType(),
4719 instruction->GetFieldIndex(),
4720 instruction->GetDexPc(),
4721 calling_convention);
4722}
4723
4724void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4725 HUnresolvedStaticFieldSet* instruction) {
4726 FieldAccessCallingConventionARM64 calling_convention;
4727 codegen_->CreateUnresolvedFieldLocationSummary(
4728 instruction, instruction->GetFieldType(), calling_convention);
4729}
4730
4731void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4732 HUnresolvedStaticFieldSet* instruction) {
4733 FieldAccessCallingConventionARM64 calling_convention;
4734 codegen_->GenerateUnresolvedFieldAccess(instruction,
4735 instruction->GetFieldType(),
4736 instruction->GetFieldIndex(),
4737 instruction->GetDexPc(),
4738 calling_convention);
4739}
4740
Alexandre Rames5319def2014-10-23 10:03:10 +01004741void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01004742 LocationSummary* locations =
4743 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004744 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01004745}
4746
4747void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004748 HBasicBlock* block = instruction->GetBlock();
4749 if (block->GetLoopInformation() != nullptr) {
4750 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4751 // The back edge will generate the suspend check.
4752 return;
4753 }
4754 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4755 // The goto will generate the suspend check.
4756 return;
4757 }
4758 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004759}
4760
Alexandre Rames67555f72014-11-18 10:55:16 +00004761void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4762 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004763 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004764 InvokeRuntimeCallingConvention calling_convention;
4765 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4766}
4767
4768void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004769 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004770 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004771}
4772
4773void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4774 LocationSummary* locations =
4775 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4776 Primitive::Type input_type = conversion->GetInputType();
4777 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004778 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004779 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4780 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4781 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4782 }
4783
Alexandre Rames542361f2015-01-29 16:57:31 +00004784 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004785 locations->SetInAt(0, Location::RequiresFpuRegister());
4786 } else {
4787 locations->SetInAt(0, Location::RequiresRegister());
4788 }
4789
Alexandre Rames542361f2015-01-29 16:57:31 +00004790 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004791 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4792 } else {
4793 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4794 }
4795}
4796
4797void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4798 Primitive::Type result_type = conversion->GetResultType();
4799 Primitive::Type input_type = conversion->GetInputType();
4800
4801 DCHECK_NE(input_type, result_type);
4802
Alexandre Rames542361f2015-01-29 16:57:31 +00004803 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004804 int result_size = Primitive::ComponentSize(result_type);
4805 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004806 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004807 Register output = OutputRegister(conversion);
4808 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004809 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004810 // 'int' values are used directly as W registers, discarding the top
4811 // bits, so we don't need to sign-extend and can just perform a move.
4812 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4813 // top 32 bits of the target register. We theoretically could leave those
4814 // bits unchanged, but we would have to make sure that no code uses a
4815 // 32bit input value as a 64bit value assuming that the top 32 bits are
4816 // zero.
4817 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004818 } else if (result_type == Primitive::kPrimChar ||
4819 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4820 __ Ubfx(output,
4821 output.IsX() ? source.X() : source.W(),
4822 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004823 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004824 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004825 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004826 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004827 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004828 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004829 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4830 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004831 } else if (Primitive::IsFloatingPointType(result_type) &&
4832 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004833 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4834 } else {
4835 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4836 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004837 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004838}
Alexandre Rames67555f72014-11-18 10:55:16 +00004839
Serban Constantinescu02164b32014-11-13 14:05:07 +00004840void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4841 HandleShift(ushr);
4842}
4843
4844void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4845 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004846}
4847
4848void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4849 HandleBinaryOp(instruction);
4850}
4851
4852void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4853 HandleBinaryOp(instruction);
4854}
4855
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004856void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004857 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004858 LOG(FATAL) << "Unreachable";
4859}
4860
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004861void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004862 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004863 LOG(FATAL) << "Unreachable";
4864}
4865
Mark Mendellfe57faa2015-09-18 09:26:15 -04004866// Simple implementation of packed switch - generate cascaded compare/jumps.
4867void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4868 LocationSummary* locations =
4869 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4870 locations->SetInAt(0, Location::RequiresRegister());
4871}
4872
4873void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4874 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004875 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004876 Register value_reg = InputRegisterAt(switch_instr, 0);
4877 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4878
Zheng Xu3927c8b2015-11-18 17:46:25 +08004879 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004880 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08004881 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4882 // make sure we don't emit it if the target may run out of range.
4883 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4884 // ranges and emit the tables only as required.
4885 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004886
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004887 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004888 // Current instruction id is an upper bound of the number of HIRs in the graph.
4889 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4890 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004891 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4892 Register temp = temps.AcquireW();
4893 __ Subs(temp, value_reg, Operand(lower_bound));
4894
Zheng Xu3927c8b2015-11-18 17:46:25 +08004895 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004896 // Jump to successors[0] if value == lower_bound.
4897 __ B(eq, codegen_->GetLabelOf(successors[0]));
4898 int32_t last_index = 0;
4899 for (; num_entries - last_index > 2; last_index += 2) {
4900 __ Subs(temp, temp, Operand(2));
4901 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4902 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4903 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4904 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4905 }
4906 if (num_entries - last_index == 2) {
4907 // The last missing case_value.
4908 __ Cmp(temp, Operand(1));
4909 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004910 }
4911
4912 // And the default for any other value.
4913 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4914 __ B(codegen_->GetLabelOf(default_block));
4915 }
4916 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01004917 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08004918
4919 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4920
4921 // Below instructions should use at most one blocked register. Since there are two blocked
4922 // registers, we are free to block one.
4923 Register temp_w = temps.AcquireW();
4924 Register index;
4925 // Remove the bias.
4926 if (lower_bound != 0) {
4927 index = temp_w;
4928 __ Sub(index, value_reg, Operand(lower_bound));
4929 } else {
4930 index = value_reg;
4931 }
4932
4933 // Jump to default block if index is out of the range.
4934 __ Cmp(index, Operand(num_entries));
4935 __ B(hs, codegen_->GetLabelOf(default_block));
4936
4937 // In current VIXL implementation, it won't require any blocked registers to encode the
4938 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4939 // register pressure.
4940 Register table_base = temps.AcquireX();
4941 // Load jump offset from the table.
4942 __ Adr(table_base, jump_table->GetTableStartLabel());
4943 Register jump_offset = temp_w;
4944 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4945
4946 // Jump to target block by branching to table_base(pc related) + offset.
4947 Register target_address = table_base;
4948 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4949 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004950 }
4951}
4952
Roland Levillain44015862016-01-22 11:47:17 +00004953void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
4954 Location out,
4955 uint32_t offset,
4956 Location maybe_temp) {
4957 Primitive::Type type = Primitive::kPrimNot;
4958 Register out_reg = RegisterFrom(out, type);
4959 if (kEmitCompilerReadBarrier) {
4960 Register temp_reg = RegisterFrom(maybe_temp, type);
4961 if (kUseBakerReadBarrier) {
4962 // Load with fast path based Baker's read barrier.
4963 // /* HeapReference<Object> */ out = *(out + offset)
4964 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4965 out,
4966 out_reg,
4967 offset,
4968 temp_reg,
4969 /* needs_null_check */ false,
4970 /* use_load_acquire */ false);
4971 } else {
4972 // Load with slow path based read barrier.
4973 // Save the value of `out` into `maybe_temp` before overwriting it
4974 // in the following move operation, as we will need it for the
4975 // read barrier below.
4976 __ Mov(temp_reg, out_reg);
4977 // /* HeapReference<Object> */ out = *(out + offset)
4978 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4979 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4980 }
4981 } else {
4982 // Plain load with no read barrier.
4983 // /* HeapReference<Object> */ out = *(out + offset)
4984 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4985 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
4986 }
4987}
4988
4989void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
4990 Location out,
4991 Location obj,
4992 uint32_t offset,
4993 Location maybe_temp) {
4994 Primitive::Type type = Primitive::kPrimNot;
4995 Register out_reg = RegisterFrom(out, type);
4996 Register obj_reg = RegisterFrom(obj, type);
4997 if (kEmitCompilerReadBarrier) {
4998 if (kUseBakerReadBarrier) {
4999 // Load with fast path based Baker's read barrier.
5000 Register temp_reg = RegisterFrom(maybe_temp, type);
5001 // /* HeapReference<Object> */ out = *(obj + offset)
5002 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5003 out,
5004 obj_reg,
5005 offset,
5006 temp_reg,
5007 /* needs_null_check */ false,
5008 /* use_load_acquire */ false);
5009 } else {
5010 // Load with slow path based read barrier.
5011 // /* HeapReference<Object> */ out = *(obj + offset)
5012 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5013 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5014 }
5015 } else {
5016 // Plain load with no read barrier.
5017 // /* HeapReference<Object> */ out = *(obj + offset)
5018 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5019 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5020 }
5021}
5022
5023void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(HInstruction* instruction,
5024 Location root,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005025 Register obj,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005026 uint32_t offset,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005027 vixl::aarch64::Label* fixup_label,
5028 bool requires_read_barrier) {
Vladimir Marko63dccbb2016-09-21 13:51:10 +01005029 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005030 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005031 if (requires_read_barrier) {
5032 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005033 if (kUseBakerReadBarrier) {
5034 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5035 // Baker's read barrier are used:
5036 //
5037 // root = obj.field;
5038 // if (Thread::Current()->GetIsGcMarking()) {
5039 // root = ReadBarrier::Mark(root)
5040 // }
5041
5042 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005043 if (fixup_label == nullptr) {
5044 __ Ldr(root_reg, MemOperand(obj, offset));
5045 } else {
Vladimir Marko63dccbb2016-09-21 13:51:10 +01005046 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005047 }
Roland Levillain44015862016-01-22 11:47:17 +00005048 static_assert(
5049 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5050 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5051 "have different sizes.");
5052 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5053 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5054 "have different sizes.");
5055
Vladimir Marko953437b2016-08-24 08:30:46 +00005056 // Slow path marking the GC root `root`.
Roland Levillain44015862016-01-22 11:47:17 +00005057 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005058 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root);
Roland Levillain44015862016-01-22 11:47:17 +00005059 codegen_->AddSlowPath(slow_path);
5060
5061 MacroAssembler* masm = GetVIXLAssembler();
5062 UseScratchRegisterScope temps(masm);
5063 Register temp = temps.AcquireW();
5064 // temp = Thread::Current()->GetIsGcMarking()
Andreas Gampe542451c2016-07-26 09:02:02 -07005065 __ Ldr(temp, MemOperand(tr, Thread::IsGcMarkingOffset<kArm64PointerSize>().Int32Value()));
Roland Levillain44015862016-01-22 11:47:17 +00005066 __ Cbnz(temp, slow_path->GetEntryLabel());
5067 __ Bind(slow_path->GetExitLabel());
5068 } else {
5069 // GC root loaded through a slow path for read barriers other
5070 // than Baker's.
5071 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005072 if (fixup_label == nullptr) {
5073 __ Add(root_reg.X(), obj.X(), offset);
5074 } else {
Vladimir Marko63dccbb2016-09-21 13:51:10 +01005075 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005076 }
Roland Levillain44015862016-01-22 11:47:17 +00005077 // /* mirror::Object* */ root = root->Read()
5078 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5079 }
5080 } else {
5081 // Plain GC root load with no read barrier.
5082 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005083 if (fixup_label == nullptr) {
5084 __ Ldr(root_reg, MemOperand(obj, offset));
5085 } else {
Vladimir Marko63dccbb2016-09-21 13:51:10 +01005086 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005087 }
Roland Levillain44015862016-01-22 11:47:17 +00005088 // Note that GC roots are not affected by heap poisoning, thus we
5089 // do not have to unpoison `root_reg` here.
5090 }
5091}
5092
5093void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5094 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005095 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005096 uint32_t offset,
5097 Register temp,
5098 bool needs_null_check,
5099 bool use_load_acquire) {
5100 DCHECK(kEmitCompilerReadBarrier);
5101 DCHECK(kUseBakerReadBarrier);
5102
5103 // /* HeapReference<Object> */ ref = *(obj + offset)
5104 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01005105 size_t no_scale_factor = 0U;
5106 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5107 ref,
5108 obj,
5109 offset,
5110 no_index,
5111 no_scale_factor,
5112 temp,
5113 needs_null_check,
5114 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005115}
5116
5117void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5118 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005119 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005120 uint32_t data_offset,
5121 Location index,
5122 Register temp,
5123 bool needs_null_check) {
5124 DCHECK(kEmitCompilerReadBarrier);
5125 DCHECK(kUseBakerReadBarrier);
5126
5127 // Array cells are never volatile variables, therefore array loads
5128 // never use Load-Acquire instructions on ARM64.
5129 const bool use_load_acquire = false;
5130
Roland Levillainbfea3352016-06-23 13:48:47 +01005131 static_assert(
5132 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5133 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005134 // /* HeapReference<Object> */ ref =
5135 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005136 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5137 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5138 ref,
5139 obj,
5140 data_offset,
5141 index,
5142 scale_factor,
5143 temp,
5144 needs_null_check,
5145 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005146}
5147
5148void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5149 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005150 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005151 uint32_t offset,
5152 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005153 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005154 Register temp,
5155 bool needs_null_check,
5156 bool use_load_acquire) {
5157 DCHECK(kEmitCompilerReadBarrier);
5158 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005159 // If we are emitting an array load, we should not be using a
5160 // Load Acquire instruction. In other words:
5161 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5162 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005163
5164 MacroAssembler* masm = GetVIXLAssembler();
5165 UseScratchRegisterScope temps(masm);
5166
5167 // In slow path based read barriers, the read barrier call is
5168 // inserted after the original load. However, in fast path based
5169 // Baker's read barriers, we need to perform the load of
5170 // mirror::Object::monitor_ *before* the original reference load.
5171 // This load-load ordering is required by the read barrier.
5172 // The fast path/slow path (for Baker's algorithm) should look like:
5173 //
5174 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5175 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5176 // HeapReference<Object> ref = *src; // Original reference load.
5177 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
5178 // if (is_gray) {
5179 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5180 // }
5181 //
5182 // Note: the original implementation in ReadBarrier::Barrier is
5183 // slightly more complex as it performs additional checks that we do
5184 // not do here for performance reasons.
5185
5186 Primitive::Type type = Primitive::kPrimNot;
5187 Register ref_reg = RegisterFrom(ref, type);
5188 DCHECK(obj.IsW());
5189 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5190
5191 // /* int32_t */ monitor = obj->monitor_
5192 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5193 if (needs_null_check) {
5194 MaybeRecordImplicitNullCheck(instruction);
5195 }
5196 // /* LockWord */ lock_word = LockWord(monitor)
5197 static_assert(sizeof(LockWord) == sizeof(int32_t),
5198 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005199
Vladimir Marko877a0332016-07-11 19:30:56 +01005200 // Introduce a dependency on the lock_word including rb_state,
5201 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005202 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005203 // `obj` is unchanged by this operation, but its value now depends
5204 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005205 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005206
5207 // The actual reference load.
5208 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01005209 // Load types involving an "index".
5210 if (use_load_acquire) {
5211 // UnsafeGetObjectVolatile intrinsic case.
5212 // Register `index` is not an index in an object array, but an
5213 // offset to an object reference field within object `obj`.
5214 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5215 DCHECK(instruction->GetLocations()->Intrinsified());
5216 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5217 << instruction->AsInvoke()->GetIntrinsic();
5218 DCHECK_EQ(offset, 0U);
5219 DCHECK_EQ(scale_factor, 0U);
5220 DCHECK_EQ(needs_null_check, 0U);
5221 // /* HeapReference<Object> */ ref = *(obj + index)
5222 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5223 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005224 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005225 // ArrayGet and UnsafeGetObject intrinsics cases.
5226 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5227 if (index.IsConstant()) {
5228 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5229 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5230 } else {
Vladimir Marko877a0332016-07-11 19:30:56 +01005231 Register temp2 = temps.AcquireW();
Roland Levillainbfea3352016-06-23 13:48:47 +01005232 __ Add(temp2, obj, offset);
5233 Load(type, ref_reg, HeapOperand(temp2, XRegisterFrom(index), LSL, scale_factor));
5234 temps.Release(temp2);
5235 }
Roland Levillain44015862016-01-22 11:47:17 +00005236 }
Roland Levillain44015862016-01-22 11:47:17 +00005237 } else {
5238 // /* HeapReference<Object> */ ref = *(obj + offset)
5239 MemOperand field = HeapOperand(obj, offset);
5240 if (use_load_acquire) {
5241 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5242 } else {
5243 Load(type, ref_reg, field);
5244 }
5245 }
5246
5247 // Object* ref = ref_addr->AsMirrorPtr()
5248 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5249
Vladimir Marko953437b2016-08-24 08:30:46 +00005250 // Slow path marking the object `ref` when it is gray.
Roland Levillain44015862016-01-22 11:47:17 +00005251 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005252 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
Roland Levillain44015862016-01-22 11:47:17 +00005253 AddSlowPath(slow_path);
5254
5255 // if (rb_state == ReadBarrier::gray_ptr_)
5256 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005257 // Given the numeric representation, it's enough to check the low bit of the rb_state.
5258 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
5259 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
5260 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
5261 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005262 __ Bind(slow_path->GetExitLabel());
5263}
5264
5265void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5266 Location out,
5267 Location ref,
5268 Location obj,
5269 uint32_t offset,
5270 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005271 DCHECK(kEmitCompilerReadBarrier);
5272
Roland Levillain44015862016-01-22 11:47:17 +00005273 // Insert a slow path based read barrier *after* the reference load.
5274 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005275 // If heap poisoning is enabled, the unpoisoning of the loaded
5276 // reference will be carried out by the runtime within the slow
5277 // path.
5278 //
5279 // Note that `ref` currently does not get unpoisoned (when heap
5280 // poisoning is enabled), which is alright as the `ref` argument is
5281 // not used by the artReadBarrierSlow entry point.
5282 //
5283 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5284 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5285 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5286 AddSlowPath(slow_path);
5287
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005288 __ B(slow_path->GetEntryLabel());
5289 __ Bind(slow_path->GetExitLabel());
5290}
5291
Roland Levillain44015862016-01-22 11:47:17 +00005292void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5293 Location out,
5294 Location ref,
5295 Location obj,
5296 uint32_t offset,
5297 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005298 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005299 // Baker's read barriers shall be handled by the fast path
5300 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5301 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005302 // If heap poisoning is enabled, unpoisoning will be taken care of
5303 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005304 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005305 } else if (kPoisonHeapReferences) {
5306 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5307 }
5308}
5309
Roland Levillain44015862016-01-22 11:47:17 +00005310void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5311 Location out,
5312 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005313 DCHECK(kEmitCompilerReadBarrier);
5314
Roland Levillain44015862016-01-22 11:47:17 +00005315 // Insert a slow path based read barrier *after* the GC root load.
5316 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005317 // Note that GC roots are not affected by heap poisoning, so we do
5318 // not need to do anything special for this here.
5319 SlowPathCodeARM64* slow_path =
5320 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5321 AddSlowPath(slow_path);
5322
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005323 __ B(slow_path->GetEntryLabel());
5324 __ Bind(slow_path->GetExitLabel());
5325}
5326
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005327void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5328 LocationSummary* locations =
5329 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5330 locations->SetInAt(0, Location::RequiresRegister());
5331 locations->SetOut(Location::RequiresRegister());
5332}
5333
5334void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5335 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005336 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005337 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005338 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005339 __ Ldr(XRegisterFrom(locations->Out()),
5340 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005341 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005342 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005343 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005344 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5345 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005346 __ Ldr(XRegisterFrom(locations->Out()),
5347 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005348 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005349}
5350
5351
5352
Alexandre Rames67555f72014-11-18 10:55:16 +00005353#undef __
5354#undef QUICK_ENTRY_POINT
5355
Alexandre Rames5319def2014-10-23 10:03:10 +01005356} // namespace arm64
5357} // namespace art