blob: 60d7faf738bedd6f5ff423a09b4c5305e3be9ffd [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 Markoaad75c62016-10-03 08:46:48 +0000332class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
333 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100334 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
335 : SlowPathCodeARM64(instruction),
336 temp_(temp),
337 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000338
339 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
340 LocationSummary* locations = instruction_->GetLocations();
341 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
342 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
343
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100344 // temp_ is a scratch register. Make sure it's not used for saving/restoring registers.
345 UseScratchRegisterScope temps(arm64_codegen->GetVIXLAssembler());
346 temps.Exclude(temp_);
347
Vladimir Markoaad75c62016-10-03 08:46:48 +0000348 __ Bind(GetEntryLabel());
349 SaveLiveRegisters(codegen, locations);
350
351 InvokeRuntimeCallingConvention calling_convention;
352 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
353 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index);
354 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
355 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
356 Primitive::Type type = instruction_->GetType();
357 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
358
359 RestoreLiveRegisters(codegen, locations);
360
361 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000362 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100363 if (!kUseReadBarrier || kUseBakerReadBarrier) {
364 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
365 } else {
366 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
367 adrp_label_ = arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
368 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
369 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000370 vixl::aarch64::Label* strp_label =
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100371 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000372 {
373 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
374 __ Bind(strp_label);
375 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100376 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000377 }
378
379 __ B(GetExitLabel());
380 }
381
382 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
383
384 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100385 const Register temp_;
386 vixl::aarch64::Label* adrp_label_;
387
Vladimir Markoaad75c62016-10-03 08:46:48 +0000388 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
389};
390
Alexandre Rames5319def2014-10-23 10:03:10 +0100391class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
392 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000393 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100394
Alexandre Rames67555f72014-11-18 10:55:16 +0000395 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
396 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100397 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000398 if (instruction_->CanThrowIntoCatchBlock()) {
399 // Live registers will be restored in the catch block if caught.
400 SaveLiveRegisters(codegen, instruction_->GetLocations());
401 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000402 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
403 instruction_,
404 instruction_->GetDexPc(),
405 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800406 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100407 }
408
Alexandre Rames8158f282015-08-07 10:26:17 +0100409 bool IsFatal() const OVERRIDE { return true; }
410
Alexandre Rames9931f312015-06-19 14:47:01 +0100411 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
412
Alexandre Rames5319def2014-10-23 10:03:10 +0100413 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100414 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
415};
416
417class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
418 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100419 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000420 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100421
Alexandre Rames67555f72014-11-18 10:55:16 +0000422 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
423 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100424 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000425 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800426 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000427 if (successor_ == nullptr) {
428 __ B(GetReturnLabel());
429 } else {
430 __ B(arm64_codegen->GetLabelOf(successor_));
431 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100432 }
433
Scott Wakeling97c72b72016-06-24 16:19:36 +0100434 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100435 DCHECK(successor_ == nullptr);
436 return &return_label_;
437 }
438
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100439 HBasicBlock* GetSuccessor() const {
440 return successor_;
441 }
442
Alexandre Rames9931f312015-06-19 14:47:01 +0100443 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
444
Alexandre Rames5319def2014-10-23 10:03:10 +0100445 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100446 // If not null, the block to branch to after the suspend check.
447 HBasicBlock* const successor_;
448
449 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100450 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100451
452 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
453};
454
Alexandre Rames67555f72014-11-18 10:55:16 +0000455class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
456 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000457 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000458 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000459
460 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000461 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100462 Location class_to_check = locations->InAt(1);
463 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
464 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000465 DCHECK(instruction_->IsCheckCast()
466 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
467 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100468 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000469
Alexandre Rames67555f72014-11-18 10:55:16 +0000470 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000471
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000472 if (!is_fatal_) {
473 SaveLiveRegisters(codegen, locations);
474 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000475
476 // We're moving two locations to locations that could overlap, so we need a parallel
477 // move resolver.
478 InvokeRuntimeCallingConvention calling_convention;
479 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100480 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
481 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000482
483 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000484 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Andreas Gampe67409972016-07-19 22:34:53 -0700485 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t,
Roland Levillain888d0672015-11-23 18:53:50 +0000486 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000487 Primitive::Type ret_type = instruction_->GetType();
488 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
489 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
490 } else {
491 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000492 arm64_codegen->InvokeRuntime(kQuickCheckCast, instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800493 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000494 }
495
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000496 if (!is_fatal_) {
497 RestoreLiveRegisters(codegen, locations);
498 __ B(GetExitLabel());
499 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000500 }
501
Alexandre Rames9931f312015-06-19 14:47:01 +0100502 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100503 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100504
Alexandre Rames67555f72014-11-18 10:55:16 +0000505 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000506 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000507
Alexandre Rames67555f72014-11-18 10:55:16 +0000508 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
509};
510
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700511class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
512 public:
Aart Bik42249c32016-01-07 15:33:50 -0800513 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000514 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700515
516 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800517 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700518 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000519 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000520 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700521 }
522
Alexandre Rames9931f312015-06-19 14:47:01 +0100523 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
524
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700525 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700526 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
527};
528
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100529class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
530 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000531 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100532
533 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
534 LocationSummary* locations = instruction_->GetLocations();
535 __ Bind(GetEntryLabel());
536 SaveLiveRegisters(codegen, locations);
537
538 InvokeRuntimeCallingConvention calling_convention;
539 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
540 parallel_move.AddMove(
541 locations->InAt(0),
542 LocationFrom(calling_convention.GetRegisterAt(0)),
543 Primitive::kPrimNot,
544 nullptr);
545 parallel_move.AddMove(
546 locations->InAt(1),
547 LocationFrom(calling_convention.GetRegisterAt(1)),
548 Primitive::kPrimInt,
549 nullptr);
550 parallel_move.AddMove(
551 locations->InAt(2),
552 LocationFrom(calling_convention.GetRegisterAt(2)),
553 Primitive::kPrimNot,
554 nullptr);
555 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
556
557 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000558 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100559 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
560 RestoreLiveRegisters(codegen, locations);
561 __ B(GetExitLabel());
562 }
563
564 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
565
566 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100567 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
568};
569
Zheng Xu3927c8b2015-11-18 17:46:25 +0800570void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
571 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000572 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800573
574 // We are about to use the assembler to place literals directly. Make sure we have enough
575 // underlying code buffer and we have generated the jump table with right size.
576 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
577 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
578
579 __ Bind(&table_start_);
580 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
581 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100582 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800583 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100584 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800585 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
586 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
587 Literal<int32_t> literal(jump_offset);
588 __ place(&literal);
589 }
590}
591
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100592// Slow path marking an object reference `ref` during a read
593// barrier. The field `obj.field` in the object `obj` holding this
594// reference does not get updated by this slow path after marking (see
595// ReadBarrierMarkAndUpdateFieldSlowPathARM64 below for that).
596//
597// This means that after the execution of this slow path, `ref` will
598// always be up-to-date, but `obj.field` may not; i.e., after the
599// flip, `ref` will be a to-space reference, but `obj.field` will
600// probably still be a from-space reference (unless it gets updated by
601// another thread, or if another thread installed another object
602// reference (different from `ref`) in `obj.field`).
Roland Levillain44015862016-01-22 11:47:17 +0000603class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
604 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100605 ReadBarrierMarkSlowPathARM64(HInstruction* instruction, Location ref)
606 : SlowPathCodeARM64(instruction), ref_(ref) {
Roland Levillain44015862016-01-22 11:47:17 +0000607 DCHECK(kEmitCompilerReadBarrier);
608 }
609
610 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
611
612 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
613 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000614 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100615 DCHECK(ref_.IsRegister()) << ref_;
616 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain44015862016-01-22 11:47:17 +0000617 DCHECK(instruction_->IsInstanceFieldGet() ||
618 instruction_->IsStaticFieldGet() ||
619 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100620 instruction_->IsArraySet() ||
Roland Levillain44015862016-01-22 11:47:17 +0000621 instruction_->IsLoadClass() ||
622 instruction_->IsLoadString() ||
623 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100624 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100625 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
626 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000627 << "Unexpected instruction in read barrier marking slow path: "
628 << instruction_->DebugName();
629
630 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100631 // No need to save live registers; it's taken care of by the
632 // entrypoint. Also, there is no need to update the stack mask,
633 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000634 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100635 DCHECK_NE(ref_.reg(), LR);
636 DCHECK_NE(ref_.reg(), WSP);
637 DCHECK_NE(ref_.reg(), WZR);
Roland Levillain0b671c02016-08-19 12:02:34 +0100638 // IP0 is used internally by the ReadBarrierMarkRegX entry point
639 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100640 DCHECK_NE(ref_.reg(), IP0);
641 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
Roland Levillain02b75802016-07-13 11:54:35 +0100642 // "Compact" slow path, saving two moves.
643 //
644 // Instead of using the standard runtime calling convention (input
645 // and output in W0):
646 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100647 // W0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100648 // W0 <- ReadBarrierMark(W0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100649 // ref <- W0
Roland Levillain02b75802016-07-13 11:54:35 +0100650 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100651 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100652 // of a dedicated entrypoint:
653 //
654 // rX <- ReadBarrierMarkRegX(rX)
655 //
656 int32_t entry_point_offset =
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100657 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
Roland Levillaindec8f632016-07-22 17:10:06 +0100658 // This runtime call does not require a stack map.
659 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain44015862016-01-22 11:47:17 +0000660 __ B(GetExitLabel());
661 }
662
663 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100664 // The location (register) of the marked object reference.
665 const Location ref_;
Roland Levillain44015862016-01-22 11:47:17 +0000666
667 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
668};
669
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100670// Slow path marking an object reference `ref` during a read barrier,
671// and if needed, atomically updating the field `obj.field` in the
672// object `obj` holding this reference after marking (contrary to
673// ReadBarrierMarkSlowPathARM64 above, which never tries to update
674// `obj.field`).
675//
676// This means that after the execution of this slow path, both `ref`
677// and `obj.field` will be up-to-date; i.e., after the flip, both will
678// hold the same to-space reference (unless another thread installed
679// another object reference (different from `ref`) in `obj.field`).
680class ReadBarrierMarkAndUpdateFieldSlowPathARM64 : public SlowPathCodeARM64 {
681 public:
682 ReadBarrierMarkAndUpdateFieldSlowPathARM64(HInstruction* instruction,
683 Location ref,
684 Register obj,
685 Location field_offset,
686 Register temp)
687 : SlowPathCodeARM64(instruction),
688 ref_(ref),
689 obj_(obj),
690 field_offset_(field_offset),
691 temp_(temp) {
692 DCHECK(kEmitCompilerReadBarrier);
693 }
694
695 const char* GetDescription() const OVERRIDE {
696 return "ReadBarrierMarkAndUpdateFieldSlowPathARM64";
697 }
698
699 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
700 LocationSummary* locations = instruction_->GetLocations();
701 Register ref_reg = WRegisterFrom(ref_);
702 DCHECK(locations->CanCall());
703 DCHECK(ref_.IsRegister()) << ref_;
704 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
705 // This slow path is only used by the UnsafeCASObject intrinsic.
706 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
707 << "Unexpected instruction in read barrier marking and field updating slow path: "
708 << instruction_->DebugName();
709 DCHECK(instruction_->GetLocations()->Intrinsified());
710 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
711 DCHECK(field_offset_.IsRegister()) << field_offset_;
712
713 __ Bind(GetEntryLabel());
714
715 // Save the old reference.
716 // Note that we cannot use IP to save the old reference, as IP is
717 // used internally by the ReadBarrierMarkRegX entry point, and we
718 // need the old reference after the call to that entry point.
719 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
720 __ Mov(temp_.W(), ref_reg);
721
722 // No need to save live registers; it's taken care of by the
723 // entrypoint. Also, there is no need to update the stack mask,
724 // as this runtime call will not trigger a garbage collection.
725 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
726 DCHECK_NE(ref_.reg(), LR);
727 DCHECK_NE(ref_.reg(), WSP);
728 DCHECK_NE(ref_.reg(), WZR);
729 // IP0 is used internally by the ReadBarrierMarkRegX entry point
730 // as a temporary, it cannot be the entry point's input/output.
731 DCHECK_NE(ref_.reg(), IP0);
732 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
733 // "Compact" slow path, saving two moves.
734 //
735 // Instead of using the standard runtime calling convention (input
736 // and output in W0):
737 //
738 // W0 <- ref
739 // W0 <- ReadBarrierMark(W0)
740 // ref <- W0
741 //
742 // we just use rX (the register containing `ref`) as input and output
743 // of a dedicated entrypoint:
744 //
745 // rX <- ReadBarrierMarkRegX(rX)
746 //
747 int32_t entry_point_offset =
748 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
749 // This runtime call does not require a stack map.
750 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
751
752 // If the new reference is different from the old reference,
753 // update the field in the holder (`*(obj_ + field_offset_)`).
754 //
755 // Note that this field could also hold a different object, if
756 // another thread had concurrently changed it. In that case, the
757 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
758 // (CAS) operation below would abort the CAS, leaving the field
759 // as-is.
760 vixl::aarch64::Label done;
761 __ Cmp(temp_.W(), ref_reg);
762 __ B(eq, &done);
763
764 // Update the the holder's field atomically. This may fail if
765 // mutator updates before us, but it's OK. This is achieved
766 // using a strong compare-and-set (CAS) operation with relaxed
767 // memory synchronization ordering, where the expected value is
768 // the old reference and the desired value is the new reference.
769
770 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
771 UseScratchRegisterScope temps(masm);
772
773 // Convenience aliases.
774 Register base = obj_.W();
775 Register offset = XRegisterFrom(field_offset_);
776 Register expected = temp_.W();
777 Register value = ref_reg;
778 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
779 Register tmp_value = temps.AcquireW(); // Value in memory.
780
781 __ Add(tmp_ptr, base.X(), Operand(offset));
782
783 if (kPoisonHeapReferences) {
784 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
785 if (value.Is(expected)) {
786 // Do not poison `value`, as it is the same register as
787 // `expected`, which has just been poisoned.
788 } else {
789 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
790 }
791 }
792
793 // do {
794 // tmp_value = [tmp_ptr] - expected;
795 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
796
797 vixl::aarch64::Label loop_head, exit_loop;
798 __ Bind(&loop_head);
799 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
800 __ Cmp(tmp_value, expected);
801 __ B(&exit_loop, ne);
802 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
803 __ Cbnz(tmp_value, &loop_head);
804 __ Bind(&exit_loop);
805
806 if (kPoisonHeapReferences) {
807 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
808 if (value.Is(expected)) {
809 // Do not unpoison `value`, as it is the same register as
810 // `expected`, which has just been unpoisoned.
811 } else {
812 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
813 }
814 }
815
816 __ Bind(&done);
817 __ B(GetExitLabel());
818 }
819
820 private:
821 // The location (register) of the marked object reference.
822 const Location ref_;
823 // The register containing the object holding the marked object reference field.
824 const Register obj_;
825 // The location of the offset of the marked reference field within `obj_`.
826 Location field_offset_;
827
828 const Register temp_;
829
830 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM64);
831};
832
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000833// Slow path generating a read barrier for a heap reference.
834class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
835 public:
836 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
837 Location out,
838 Location ref,
839 Location obj,
840 uint32_t offset,
841 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000842 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000843 out_(out),
844 ref_(ref),
845 obj_(obj),
846 offset_(offset),
847 index_(index) {
848 DCHECK(kEmitCompilerReadBarrier);
849 // If `obj` is equal to `out` or `ref`, it means the initial object
850 // has been overwritten by (or after) the heap object reference load
851 // to be instrumented, e.g.:
852 //
853 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000854 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000855 //
856 // In that case, we have lost the information about the original
857 // object, and the emitted read barrier cannot work properly.
858 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
859 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
860 }
861
862 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
863 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
864 LocationSummary* locations = instruction_->GetLocations();
865 Primitive::Type type = Primitive::kPrimNot;
866 DCHECK(locations->CanCall());
867 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100868 DCHECK(instruction_->IsInstanceFieldGet() ||
869 instruction_->IsStaticFieldGet() ||
870 instruction_->IsArrayGet() ||
871 instruction_->IsInstanceOf() ||
872 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100873 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000874 << "Unexpected instruction in read barrier for heap reference slow path: "
875 << instruction_->DebugName();
Roland Levillain4a3aa572016-08-15 13:17:06 +0000876 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000877 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100878 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000879
880 __ Bind(GetEntryLabel());
881
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000882 SaveLiveRegisters(codegen, locations);
883
884 // We may have to change the index's value, but as `index_` is a
885 // constant member (like other "inputs" of this slow path),
886 // introduce a copy of it, `index`.
887 Location index = index_;
888 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100889 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000890 if (instruction_->IsArrayGet()) {
891 // Compute the actual memory offset and store it in `index`.
892 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
893 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
894 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
895 // We are about to change the value of `index_reg` (see the
896 // calls to vixl::MacroAssembler::Lsl and
897 // vixl::MacroAssembler::Mov below), but it has
898 // not been saved by the previous call to
899 // art::SlowPathCode::SaveLiveRegisters, as it is a
900 // callee-save register --
901 // art::SlowPathCode::SaveLiveRegisters does not consider
902 // callee-save registers, as it has been designed with the
903 // assumption that callee-save registers are supposed to be
904 // handled by the called function. So, as a callee-save
905 // register, `index_reg` _would_ eventually be saved onto
906 // the stack, but it would be too late: we would have
907 // changed its value earlier. Therefore, we manually save
908 // it here into another freely available register,
909 // `free_reg`, chosen of course among the caller-save
910 // registers (as a callee-save `free_reg` register would
911 // exhibit the same problem).
912 //
913 // Note we could have requested a temporary register from
914 // the register allocator instead; but we prefer not to, as
915 // this is a slow path, and we know we can find a
916 // caller-save register that is available.
917 Register free_reg = FindAvailableCallerSaveRegister(codegen);
918 __ Mov(free_reg.W(), index_reg);
919 index_reg = free_reg;
920 index = LocationFrom(index_reg);
921 } else {
922 // The initial register stored in `index_` has already been
923 // saved in the call to art::SlowPathCode::SaveLiveRegisters
924 // (as it is not a callee-save register), so we can freely
925 // use it.
926 }
927 // Shifting the index value contained in `index_reg` by the scale
928 // factor (2) cannot overflow in practice, as the runtime is
929 // unable to allocate object arrays with a size larger than
930 // 2^26 - 1 (that is, 2^28 - 4 bytes).
931 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
932 static_assert(
933 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
934 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
935 __ Add(index_reg, index_reg, Operand(offset_));
936 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100937 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
938 // intrinsics, `index_` is not shifted by a scale factor of 2
939 // (as in the case of ArrayGet), as it is actually an offset
940 // to an object field within an object.
941 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000942 DCHECK(instruction_->GetLocations()->Intrinsified());
943 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
944 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
945 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100946 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100947 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000948 }
949 }
950
951 // We're moving two or three locations to locations that could
952 // overlap, so we need a parallel move resolver.
953 InvokeRuntimeCallingConvention calling_convention;
954 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
955 parallel_move.AddMove(ref_,
956 LocationFrom(calling_convention.GetRegisterAt(0)),
957 type,
958 nullptr);
959 parallel_move.AddMove(obj_,
960 LocationFrom(calling_convention.GetRegisterAt(1)),
961 type,
962 nullptr);
963 if (index.IsValid()) {
964 parallel_move.AddMove(index,
965 LocationFrom(calling_convention.GetRegisterAt(2)),
966 Primitive::kPrimInt,
967 nullptr);
968 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
969 } else {
970 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
971 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
972 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000973 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000974 instruction_,
975 instruction_->GetDexPc(),
976 this);
977 CheckEntrypointTypes<
978 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
979 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
980
981 RestoreLiveRegisters(codegen, locations);
982
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000983 __ B(GetExitLabel());
984 }
985
986 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
987
988 private:
989 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100990 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
991 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000992 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
993 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
994 return Register(VIXLRegCodeFromART(i), kXRegSize);
995 }
996 }
997 // We shall never fail to find a free caller-save register, as
998 // there are more than two core caller-save registers on ARM64
999 // (meaning it is possible to find one which is different from
1000 // `ref` and `obj`).
1001 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1002 LOG(FATAL) << "Could not find a free register";
1003 UNREACHABLE();
1004 }
1005
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001006 const Location out_;
1007 const Location ref_;
1008 const Location obj_;
1009 const uint32_t offset_;
1010 // An additional location containing an index to an array.
1011 // Only used for HArrayGet and the UnsafeGetObject &
1012 // UnsafeGetObjectVolatile intrinsics.
1013 const Location index_;
1014
1015 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1016};
1017
1018// Slow path generating a read barrier for a GC root.
1019class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1020 public:
1021 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001022 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001023 DCHECK(kEmitCompilerReadBarrier);
1024 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001025
1026 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1027 LocationSummary* locations = instruction_->GetLocations();
1028 Primitive::Type type = Primitive::kPrimNot;
1029 DCHECK(locations->CanCall());
1030 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001031 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1032 << "Unexpected instruction in read barrier for GC root slow path: "
1033 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001034
1035 __ Bind(GetEntryLabel());
1036 SaveLiveRegisters(codegen, locations);
1037
1038 InvokeRuntimeCallingConvention calling_convention;
1039 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1040 // The argument of the ReadBarrierForRootSlow is not a managed
1041 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1042 // thus we need a 64-bit move here, and we cannot use
1043 //
1044 // arm64_codegen->MoveLocation(
1045 // LocationFrom(calling_convention.GetRegisterAt(0)),
1046 // root_,
1047 // type);
1048 //
1049 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1050 // reference type (`Primitive::kPrimNot`).
1051 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001052 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001053 instruction_,
1054 instruction_->GetDexPc(),
1055 this);
1056 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1057 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1058
1059 RestoreLiveRegisters(codegen, locations);
1060 __ B(GetExitLabel());
1061 }
1062
1063 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1064
1065 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001066 const Location out_;
1067 const Location root_;
1068
1069 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1070};
1071
Alexandre Rames5319def2014-10-23 10:03:10 +01001072#undef __
1073
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001074Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001075 Location next_location;
1076 if (type == Primitive::kPrimVoid) {
1077 LOG(FATAL) << "Unreachable type " << type;
1078 }
1079
Alexandre Rames542361f2015-01-29 16:57:31 +00001080 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001081 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
1082 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +00001083 } else if (!Primitive::IsFloatingPointType(type) &&
1084 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001085 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1086 } else {
1087 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001088 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1089 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001090 }
1091
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001092 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001093 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001094 return next_location;
1095}
1096
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001097Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001098 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001099}
1100
Serban Constantinescu579885a2015-02-22 20:51:33 +00001101CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1102 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001103 const CompilerOptions& compiler_options,
1104 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001105 : CodeGenerator(graph,
1106 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001107 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001108 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001109 callee_saved_core_registers.GetList(),
1110 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001111 compiler_options,
1112 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001113 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001114 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001115 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001116 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001117 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001118 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001119 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001120 uint32_literals_(std::less<uint32_t>(),
1121 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001122 uint64_literals_(std::less<uint64_t>(),
1123 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1124 method_patches_(MethodReferenceComparator(),
1125 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1126 call_patches_(MethodReferenceComparator(),
1127 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1128 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001129 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1130 boot_image_string_patches_(StringReferenceValueComparator(),
1131 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1132 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001133 boot_image_type_patches_(TypeReferenceValueComparator(),
1134 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1135 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001136 boot_image_address_patches_(std::less<uint32_t>(),
1137 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001138 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001139 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001140}
Alexandre Rames5319def2014-10-23 10:03:10 +01001141
Alexandre Rames67555f72014-11-18 10:55:16 +00001142#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001143
Zheng Xu3927c8b2015-11-18 17:46:25 +08001144void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001145 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001146 jump_table->EmitTable(this);
1147 }
1148}
1149
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001150void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001151 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001152 // Ensure we emit the literal pool.
1153 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001154
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001155 CodeGenerator::Finalize(allocator);
1156}
1157
Zheng Xuad4450e2015-04-17 18:48:56 +08001158void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1159 // Note: There are 6 kinds of moves:
1160 // 1. constant -> GPR/FPR (non-cycle)
1161 // 2. constant -> stack (non-cycle)
1162 // 3. GPR/FPR -> GPR/FPR
1163 // 4. GPR/FPR -> stack
1164 // 5. stack -> GPR/FPR
1165 // 6. stack -> stack (non-cycle)
1166 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1167 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1168 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1169 // dependency.
1170 vixl_temps_.Open(GetVIXLAssembler());
1171}
1172
1173void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1174 vixl_temps_.Close();
1175}
1176
1177Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
1178 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
1179 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
1180 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
1181 Location scratch = GetScratchLocation(kind);
1182 if (!scratch.Equals(Location::NoLocation())) {
1183 return scratch;
1184 }
1185 // Allocate from VIXL temp registers.
1186 if (kind == Location::kRegister) {
1187 scratch = LocationFrom(vixl_temps_.AcquireX());
1188 } else {
1189 DCHECK(kind == Location::kFpuRegister);
1190 scratch = LocationFrom(vixl_temps_.AcquireD());
1191 }
1192 AddScratchLocation(scratch);
1193 return scratch;
1194}
1195
1196void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1197 if (loc.IsRegister()) {
1198 vixl_temps_.Release(XRegisterFrom(loc));
1199 } else {
1200 DCHECK(loc.IsFpuRegister());
1201 vixl_temps_.Release(DRegisterFrom(loc));
1202 }
1203 RemoveScratchLocation(loc);
1204}
1205
Alexandre Rames3e69f162014-12-10 10:36:50 +00001206void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001207 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001208 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001209}
1210
Alexandre Rames5319def2014-10-23 10:03:10 +01001211void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001212 MacroAssembler* masm = GetVIXLAssembler();
1213 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001214 __ Bind(&frame_entry_label_);
1215
Serban Constantinescu02164b32014-11-13 14:05:07 +00001216 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1217 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001218 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001219 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001220 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001221 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001222 __ Ldr(wzr, MemOperand(temp, 0));
1223 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001224 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001225
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001226 if (!HasEmptyFrame()) {
1227 int frame_size = GetFrameSize();
1228 // Stack layout:
1229 // sp[frame_size - 8] : lr.
1230 // ... : other preserved core registers.
1231 // ... : other preserved fp registers.
1232 // ... : reserved frame space.
1233 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001234
1235 // Save the current method if we need it. Note that we do not
1236 // do this in HCurrentMethod, as the instruction might have been removed
1237 // in the SSA graph.
1238 if (RequiresCurrentMethod()) {
1239 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001240 } else {
1241 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001242 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001243 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001244 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1245 frame_size - GetCoreSpillSize());
1246 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1247 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001248 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001249}
1250
1251void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001252 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001253 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001254 if (!HasEmptyFrame()) {
1255 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001256 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1257 frame_size - FrameEntrySpillSize());
1258 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1259 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001260 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001261 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001262 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001263 __ Ret();
1264 GetAssembler()->cfi().RestoreState();
1265 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001266}
1267
Scott Wakeling97c72b72016-06-24 16:19:36 +01001268CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001269 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001270 return CPURegList(CPURegister::kRegister, kXRegSize,
1271 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001272}
1273
Scott Wakeling97c72b72016-06-24 16:19:36 +01001274CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001275 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1276 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001277 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1278 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001279}
1280
Alexandre Rames5319def2014-10-23 10:03:10 +01001281void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1282 __ Bind(GetLabelOf(block));
1283}
1284
Calin Juravle175dc732015-08-25 15:42:32 +01001285void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1286 DCHECK(location.IsRegister());
1287 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1288}
1289
Calin Juravlee460d1d2015-09-29 04:52:17 +01001290void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1291 if (location.IsRegister()) {
1292 locations->AddTemp(location);
1293 } else {
1294 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1295 }
1296}
1297
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001298void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001299 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001300 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001301 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001302 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001303 if (value_can_be_null) {
1304 __ Cbz(value, &done);
1305 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001306 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001307 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001308 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001309 if (value_can_be_null) {
1310 __ Bind(&done);
1311 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001312}
1313
David Brazdil58282f42016-01-14 12:45:10 +00001314void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001315 // Blocked core registers:
1316 // lr : Runtime reserved.
1317 // tr : Runtime reserved.
1318 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1319 // ip1 : VIXL core temp.
1320 // ip0 : VIXL core temp.
1321 //
1322 // Blocked fp registers:
1323 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001324 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1325 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001326 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001327 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001328 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001329
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001330 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001331 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001332 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001333 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001334
David Brazdil58282f42016-01-14 12:45:10 +00001335 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001336 // Stubs do not save callee-save floating point registers. If the graph
1337 // is debuggable, we need to deal with these registers differently. For
1338 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001339 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1340 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001341 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001342 }
1343 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001344}
1345
Alexandre Rames3e69f162014-12-10 10:36:50 +00001346size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1347 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1348 __ Str(reg, MemOperand(sp, stack_index));
1349 return kArm64WordSize;
1350}
1351
1352size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1353 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1354 __ Ldr(reg, MemOperand(sp, stack_index));
1355 return kArm64WordSize;
1356}
1357
1358size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1359 FPRegister reg = FPRegister(reg_id, kDRegSize);
1360 __ Str(reg, MemOperand(sp, stack_index));
1361 return kArm64WordSize;
1362}
1363
1364size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1365 FPRegister reg = FPRegister(reg_id, kDRegSize);
1366 __ Ldr(reg, MemOperand(sp, stack_index));
1367 return kArm64WordSize;
1368}
1369
Alexandre Rames5319def2014-10-23 10:03:10 +01001370void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001371 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001372}
1373
1374void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001375 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001376}
1377
Alexandre Rames67555f72014-11-18 10:55:16 +00001378void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001379 if (constant->IsIntConstant()) {
1380 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1381 } else if (constant->IsLongConstant()) {
1382 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1383 } else if (constant->IsNullConstant()) {
1384 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001385 } else if (constant->IsFloatConstant()) {
1386 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1387 } else {
1388 DCHECK(constant->IsDoubleConstant());
1389 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1390 }
1391}
1392
Alexandre Rames3e69f162014-12-10 10:36:50 +00001393
1394static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1395 DCHECK(constant.IsConstant());
1396 HConstant* cst = constant.GetConstant();
1397 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001398 // Null is mapped to a core W register, which we associate with kPrimInt.
1399 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001400 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1401 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1402 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1403}
1404
Calin Juravlee460d1d2015-09-29 04:52:17 +01001405void CodeGeneratorARM64::MoveLocation(Location destination,
1406 Location source,
1407 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001408 if (source.Equals(destination)) {
1409 return;
1410 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001411
1412 // A valid move can always be inferred from the destination and source
1413 // locations. When moving from and to a register, the argument type can be
1414 // used to generate 32bit instead of 64bit moves. In debug mode we also
1415 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001416 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001417
1418 if (destination.IsRegister() || destination.IsFpuRegister()) {
1419 if (unspecified_type) {
1420 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1421 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001422 (src_cst != nullptr && (src_cst->IsIntConstant()
1423 || src_cst->IsFloatConstant()
1424 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001425 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001426 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001427 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001428 // If the source is a double stack slot or a 64bit constant, a 64bit
1429 // type is appropriate. Else the source is a register, and since the
1430 // type has not been specified, we chose a 64bit type to force a 64bit
1431 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001432 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001433 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001434 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001435 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1436 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1437 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001438 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1439 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1440 __ Ldr(dst, StackOperandFrom(source));
1441 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001442 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001443 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001444 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001445 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001446 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001447 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001448 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001449 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1450 ? Primitive::kPrimLong
1451 : Primitive::kPrimInt;
1452 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1453 }
1454 } else {
1455 DCHECK(source.IsFpuRegister());
1456 if (destination.IsRegister()) {
1457 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1458 ? Primitive::kPrimDouble
1459 : Primitive::kPrimFloat;
1460 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1461 } else {
1462 DCHECK(destination.IsFpuRegister());
1463 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001464 }
1465 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001466 } else { // The destination is not a register. It must be a stack slot.
1467 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1468 if (source.IsRegister() || source.IsFpuRegister()) {
1469 if (unspecified_type) {
1470 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001471 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001472 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001473 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001474 }
1475 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001476 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1477 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1478 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001479 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001480 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1481 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001482 UseScratchRegisterScope temps(GetVIXLAssembler());
1483 HConstant* src_cst = source.GetConstant();
1484 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001485 if (src_cst->IsZeroBitPattern()) {
1486 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant()) ? xzr : wzr;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001487 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001488 if (src_cst->IsIntConstant()) {
1489 temp = temps.AcquireW();
1490 } else if (src_cst->IsLongConstant()) {
1491 temp = temps.AcquireX();
1492 } else if (src_cst->IsFloatConstant()) {
1493 temp = temps.AcquireS();
1494 } else {
1495 DCHECK(src_cst->IsDoubleConstant());
1496 temp = temps.AcquireD();
1497 }
1498 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001499 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001500 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001501 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001502 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001503 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001504 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001505 // There is generally less pressure on FP registers.
1506 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001507 __ Ldr(temp, StackOperandFrom(source));
1508 __ Str(temp, StackOperandFrom(destination));
1509 }
1510 }
1511}
1512
1513void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001514 CPURegister dst,
1515 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001516 switch (type) {
1517 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001518 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001519 break;
1520 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001521 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001522 break;
1523 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001524 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001525 break;
1526 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001527 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001528 break;
1529 case Primitive::kPrimInt:
1530 case Primitive::kPrimNot:
1531 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001532 case Primitive::kPrimFloat:
1533 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001534 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001535 __ Ldr(dst, src);
1536 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001537 case Primitive::kPrimVoid:
1538 LOG(FATAL) << "Unreachable type " << type;
1539 }
1540}
1541
Calin Juravle77520bc2015-01-12 18:45:46 +00001542void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001543 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001544 const MemOperand& src,
1545 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001546 MacroAssembler* masm = GetVIXLAssembler();
1547 BlockPoolsScope block_pools(masm);
1548 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001549 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001550 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001551
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001552 DCHECK(!src.IsPreIndex());
1553 DCHECK(!src.IsPostIndex());
1554
1555 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001556 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001557 MemOperand base = MemOperand(temp_base);
1558 switch (type) {
1559 case Primitive::kPrimBoolean:
1560 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001561 if (needs_null_check) {
1562 MaybeRecordImplicitNullCheck(instruction);
1563 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001564 break;
1565 case Primitive::kPrimByte:
1566 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001567 if (needs_null_check) {
1568 MaybeRecordImplicitNullCheck(instruction);
1569 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001570 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1571 break;
1572 case Primitive::kPrimChar:
1573 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001574 if (needs_null_check) {
1575 MaybeRecordImplicitNullCheck(instruction);
1576 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001577 break;
1578 case Primitive::kPrimShort:
1579 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001580 if (needs_null_check) {
1581 MaybeRecordImplicitNullCheck(instruction);
1582 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001583 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1584 break;
1585 case Primitive::kPrimInt:
1586 case Primitive::kPrimNot:
1587 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001588 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001589 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001590 if (needs_null_check) {
1591 MaybeRecordImplicitNullCheck(instruction);
1592 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001593 break;
1594 case Primitive::kPrimFloat:
1595 case Primitive::kPrimDouble: {
1596 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001597 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001598
1599 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1600 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001601 if (needs_null_check) {
1602 MaybeRecordImplicitNullCheck(instruction);
1603 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001604 __ Fmov(FPRegister(dst), temp);
1605 break;
1606 }
1607 case Primitive::kPrimVoid:
1608 LOG(FATAL) << "Unreachable type " << type;
1609 }
1610}
1611
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001612void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001613 CPURegister src,
1614 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001615 switch (type) {
1616 case Primitive::kPrimBoolean:
1617 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001618 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001619 break;
1620 case Primitive::kPrimChar:
1621 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001622 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001623 break;
1624 case Primitive::kPrimInt:
1625 case Primitive::kPrimNot:
1626 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001627 case Primitive::kPrimFloat:
1628 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001629 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001630 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001631 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001632 case Primitive::kPrimVoid:
1633 LOG(FATAL) << "Unreachable type " << type;
1634 }
1635}
1636
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001637void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1638 CPURegister src,
1639 const MemOperand& dst) {
1640 UseScratchRegisterScope temps(GetVIXLAssembler());
1641 Register temp_base = temps.AcquireX();
1642
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001643 DCHECK(!dst.IsPreIndex());
1644 DCHECK(!dst.IsPostIndex());
1645
1646 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001647 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001648 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001649 MemOperand base = MemOperand(temp_base);
1650 switch (type) {
1651 case Primitive::kPrimBoolean:
1652 case Primitive::kPrimByte:
1653 __ Stlrb(Register(src), base);
1654 break;
1655 case Primitive::kPrimChar:
1656 case Primitive::kPrimShort:
1657 __ Stlrh(Register(src), base);
1658 break;
1659 case Primitive::kPrimInt:
1660 case Primitive::kPrimNot:
1661 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001662 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001663 __ Stlr(Register(src), base);
1664 break;
1665 case Primitive::kPrimFloat:
1666 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001667 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001668 Register temp_src;
1669 if (src.IsZero()) {
1670 // The zero register is used to avoid synthesizing zero constants.
1671 temp_src = Register(src);
1672 } else {
1673 DCHECK(src.IsFPRegister());
1674 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1675 __ Fmov(temp_src, FPRegister(src));
1676 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001677
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001678 __ Stlr(temp_src, base);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001679 break;
1680 }
1681 case Primitive::kPrimVoid:
1682 LOG(FATAL) << "Unreachable type " << type;
1683 }
1684}
1685
Calin Juravle175dc732015-08-25 15:42:32 +01001686void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1687 HInstruction* instruction,
1688 uint32_t dex_pc,
1689 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001690 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001691 GenerateInvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001692 if (EntrypointRequiresStackMap(entrypoint)) {
1693 RecordPcInfo(instruction, dex_pc, slow_path);
1694 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001695}
1696
Roland Levillaindec8f632016-07-22 17:10:06 +01001697void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1698 HInstruction* instruction,
1699 SlowPathCode* slow_path) {
1700 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001701 GenerateInvokeRuntime(entry_point_offset);
1702}
1703
1704void CodeGeneratorARM64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001705 BlockPoolsScope block_pools(GetVIXLAssembler());
1706 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1707 __ Blr(lr);
1708}
1709
Alexandre Rames67555f72014-11-18 10:55:16 +00001710void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001711 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001712 UseScratchRegisterScope temps(GetVIXLAssembler());
1713 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001714 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1715
Serban Constantinescu02164b32014-11-13 14:05:07 +00001716 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001717 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1718 __ Add(temp, class_reg, status_offset);
1719 __ Ldar(temp, HeapOperand(temp));
1720 __ Cmp(temp, mirror::Class::kStatusInitialized);
1721 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001722 __ Bind(slow_path->GetExitLabel());
1723}
Alexandre Rames5319def2014-10-23 10:03:10 +01001724
Roland Levillain44015862016-01-22 11:47:17 +00001725void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001726 BarrierType type = BarrierAll;
1727
1728 switch (kind) {
1729 case MemBarrierKind::kAnyAny:
1730 case MemBarrierKind::kAnyStore: {
1731 type = BarrierAll;
1732 break;
1733 }
1734 case MemBarrierKind::kLoadAny: {
1735 type = BarrierReads;
1736 break;
1737 }
1738 case MemBarrierKind::kStoreStore: {
1739 type = BarrierWrites;
1740 break;
1741 }
1742 default:
1743 LOG(FATAL) << "Unexpected memory barrier " << kind;
1744 }
1745 __ Dmb(InnerShareable, type);
1746}
1747
Serban Constantinescu02164b32014-11-13 14:05:07 +00001748void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1749 HBasicBlock* successor) {
1750 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001751 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1752 if (slow_path == nullptr) {
1753 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1754 instruction->SetSlowPath(slow_path);
1755 codegen_->AddSlowPath(slow_path);
1756 if (successor != nullptr) {
1757 DCHECK(successor->IsLoopHeader());
1758 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1759 }
1760 } else {
1761 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1762 }
1763
Serban Constantinescu02164b32014-11-13 14:05:07 +00001764 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1765 Register temp = temps.AcquireW();
1766
Andreas Gampe542451c2016-07-26 09:02:02 -07001767 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001768 if (successor == nullptr) {
1769 __ Cbnz(temp, slow_path->GetEntryLabel());
1770 __ Bind(slow_path->GetReturnLabel());
1771 } else {
1772 __ Cbz(temp, codegen_->GetLabelOf(successor));
1773 __ B(slow_path->GetEntryLabel());
1774 // slow_path will return to GetLabelOf(successor).
1775 }
1776}
1777
Alexandre Rames5319def2014-10-23 10:03:10 +01001778InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1779 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001780 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001781 assembler_(codegen->GetAssembler()),
1782 codegen_(codegen) {}
1783
1784#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001785 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001786
1787#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1788
1789enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001790 // Using a base helps identify when we hit such breakpoints.
1791 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001792#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1793 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1794#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1795};
1796
1797#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001798 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001799 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1800 } \
1801 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1802 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1803 locations->SetOut(Location::Any()); \
1804 }
1805 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1806#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1807
1808#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001809#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001810
Alexandre Rames67555f72014-11-18 10:55:16 +00001811void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001812 DCHECK_EQ(instr->InputCount(), 2U);
1813 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1814 Primitive::Type type = instr->GetResultType();
1815 switch (type) {
1816 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001817 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001818 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001819 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001820 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001821 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001822
1823 case Primitive::kPrimFloat:
1824 case Primitive::kPrimDouble:
1825 locations->SetInAt(0, Location::RequiresFpuRegister());
1826 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001827 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001828 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001829
Alexandre Rames5319def2014-10-23 10:03:10 +01001830 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001831 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001832 }
1833}
1834
Alexandre Rames09a99962015-04-15 11:47:56 +01001835void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001836 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1837
1838 bool object_field_get_with_read_barrier =
1839 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001840 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001841 new (GetGraph()->GetArena()) LocationSummary(instruction,
1842 object_field_get_with_read_barrier ?
1843 LocationSummary::kCallOnSlowPath :
1844 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001845 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001846 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01001847 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001848 locations->SetInAt(0, Location::RequiresRegister());
1849 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1850 locations->SetOut(Location::RequiresFpuRegister());
1851 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001852 // The output overlaps for an object field get when read barriers
1853 // are enabled: we do not want the load to overwrite the object's
1854 // location, as we need it to emit the read barrier.
1855 locations->SetOut(
1856 Location::RequiresRegister(),
1857 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001858 }
1859}
1860
1861void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1862 const FieldInfo& field_info) {
1863 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001864 LocationSummary* locations = instruction->GetLocations();
1865 Location base_loc = locations->InAt(0);
1866 Location out = locations->Out();
1867 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001868 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001869 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001870 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001871
Roland Levillain44015862016-01-22 11:47:17 +00001872 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1873 // Object FieldGet with Baker's read barrier case.
1874 MacroAssembler* masm = GetVIXLAssembler();
1875 UseScratchRegisterScope temps(masm);
1876 // /* HeapReference<Object> */ out = *(base + offset)
1877 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1878 Register temp = temps.AcquireW();
1879 // Note that potential implicit null checks are handled in this
1880 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1881 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1882 instruction,
1883 out,
1884 base,
1885 offset,
1886 temp,
1887 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001888 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001889 } else {
1890 // General case.
1891 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001892 // Note that a potential implicit null check is handled in this
1893 // CodeGeneratorARM64::LoadAcquire call.
1894 // NB: LoadAcquire will record the pc info if needed.
1895 codegen_->LoadAcquire(
1896 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001897 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001898 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001899 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001900 }
Roland Levillain44015862016-01-22 11:47:17 +00001901 if (field_type == Primitive::kPrimNot) {
1902 // If read barriers are enabled, emit read barriers other than
1903 // Baker's using a slow path (and also unpoison the loaded
1904 // reference, if heap poisoning is enabled).
1905 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1906 }
Roland Levillain4d027112015-07-01 15:41:14 +01001907 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001908}
1909
1910void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1911 LocationSummary* locations =
1912 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1913 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001914 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1915 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
1916 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001917 locations->SetInAt(1, Location::RequiresFpuRegister());
1918 } else {
1919 locations->SetInAt(1, Location::RequiresRegister());
1920 }
1921}
1922
1923void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001924 const FieldInfo& field_info,
1925 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001926 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001927 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001928
1929 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001930 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001931 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001932 Offset offset = field_info.GetFieldOffset();
1933 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001934
Roland Levillain4d027112015-07-01 15:41:14 +01001935 {
1936 // We use a block to end the scratch scope before the write barrier, thus
1937 // freeing the temporary registers so they can be used in `MarkGCCard`.
1938 UseScratchRegisterScope temps(GetVIXLAssembler());
1939
1940 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1941 DCHECK(value.IsW());
1942 Register temp = temps.AcquireW();
1943 __ Mov(temp, value.W());
1944 GetAssembler()->PoisonHeapReference(temp.W());
1945 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001946 }
Roland Levillain4d027112015-07-01 15:41:14 +01001947
1948 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001949 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1950 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001951 } else {
1952 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1953 codegen_->MaybeRecordImplicitNullCheck(instruction);
1954 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001955 }
1956
1957 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001958 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001959 }
1960}
1961
Alexandre Rames67555f72014-11-18 10:55:16 +00001962void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001963 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001964
1965 switch (type) {
1966 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001967 case Primitive::kPrimLong: {
1968 Register dst = OutputRegister(instr);
1969 Register lhs = InputRegisterAt(instr, 0);
1970 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001971 if (instr->IsAdd()) {
1972 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001973 } else if (instr->IsAnd()) {
1974 __ And(dst, lhs, rhs);
1975 } else if (instr->IsOr()) {
1976 __ Orr(dst, lhs, rhs);
1977 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001978 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001979 } else if (instr->IsRor()) {
1980 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001981 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001982 __ Ror(dst, lhs, shift);
1983 } else {
1984 // Ensure shift distance is in the same size register as the result. If
1985 // we are rotating a long and the shift comes in a w register originally,
1986 // we don't need to sxtw for use as an x since the shift distances are
1987 // all & reg_bits - 1.
1988 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1989 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001990 } else {
1991 DCHECK(instr->IsXor());
1992 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001993 }
1994 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001995 }
1996 case Primitive::kPrimFloat:
1997 case Primitive::kPrimDouble: {
1998 FPRegister dst = OutputFPRegister(instr);
1999 FPRegister lhs = InputFPRegisterAt(instr, 0);
2000 FPRegister rhs = InputFPRegisterAt(instr, 1);
2001 if (instr->IsAdd()) {
2002 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002003 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002004 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002005 } else {
2006 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002007 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002008 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002009 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002010 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002011 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002012 }
2013}
2014
Serban Constantinescu02164b32014-11-13 14:05:07 +00002015void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2016 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2017
2018 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2019 Primitive::Type type = instr->GetResultType();
2020 switch (type) {
2021 case Primitive::kPrimInt:
2022 case Primitive::kPrimLong: {
2023 locations->SetInAt(0, Location::RequiresRegister());
2024 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2025 locations->SetOut(Location::RequiresRegister());
2026 break;
2027 }
2028 default:
2029 LOG(FATAL) << "Unexpected shift type " << type;
2030 }
2031}
2032
2033void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2034 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2035
2036 Primitive::Type type = instr->GetType();
2037 switch (type) {
2038 case Primitive::kPrimInt:
2039 case Primitive::kPrimLong: {
2040 Register dst = OutputRegister(instr);
2041 Register lhs = InputRegisterAt(instr, 0);
2042 Operand rhs = InputOperandAt(instr, 1);
2043 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002044 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002045 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002046 if (instr->IsShl()) {
2047 __ Lsl(dst, lhs, shift_value);
2048 } else if (instr->IsShr()) {
2049 __ Asr(dst, lhs, shift_value);
2050 } else {
2051 __ Lsr(dst, lhs, shift_value);
2052 }
2053 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002054 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002055
2056 if (instr->IsShl()) {
2057 __ Lsl(dst, lhs, rhs_reg);
2058 } else if (instr->IsShr()) {
2059 __ Asr(dst, lhs, rhs_reg);
2060 } else {
2061 __ Lsr(dst, lhs, rhs_reg);
2062 }
2063 }
2064 break;
2065 }
2066 default:
2067 LOG(FATAL) << "Unexpected shift operation type " << type;
2068 }
2069}
2070
Alexandre Rames5319def2014-10-23 10:03:10 +01002071void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002072 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002073}
2074
2075void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002076 HandleBinaryOp(instruction);
2077}
2078
2079void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2080 HandleBinaryOp(instruction);
2081}
2082
2083void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2084 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002085}
2086
Artem Serov7fc63502016-02-09 17:15:29 +00002087void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002088 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2089 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2090 locations->SetInAt(0, Location::RequiresRegister());
2091 // There is no immediate variant of negated bitwise instructions in AArch64.
2092 locations->SetInAt(1, Location::RequiresRegister());
2093 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2094}
2095
Artem Serov7fc63502016-02-09 17:15:29 +00002096void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002097 Register dst = OutputRegister(instr);
2098 Register lhs = InputRegisterAt(instr, 0);
2099 Register rhs = InputRegisterAt(instr, 1);
2100
2101 switch (instr->GetOpKind()) {
2102 case HInstruction::kAnd:
2103 __ Bic(dst, lhs, rhs);
2104 break;
2105 case HInstruction::kOr:
2106 __ Orn(dst, lhs, rhs);
2107 break;
2108 case HInstruction::kXor:
2109 __ Eon(dst, lhs, rhs);
2110 break;
2111 default:
2112 LOG(FATAL) << "Unreachable";
2113 }
2114}
2115
Alexandre Rames8626b742015-11-25 16:28:08 +00002116void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
2117 HArm64DataProcWithShifterOp* instruction) {
2118 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2119 instruction->GetType() == Primitive::kPrimLong);
2120 LocationSummary* locations =
2121 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2122 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2123 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2124 } else {
2125 locations->SetInAt(0, Location::RequiresRegister());
2126 }
2127 locations->SetInAt(1, Location::RequiresRegister());
2128 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2129}
2130
2131void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
2132 HArm64DataProcWithShifterOp* instruction) {
2133 Primitive::Type type = instruction->GetType();
2134 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2135 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2136 Register out = OutputRegister(instruction);
2137 Register left;
2138 if (kind != HInstruction::kNeg) {
2139 left = InputRegisterAt(instruction, 0);
2140 }
2141 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
2142 // shifter operand operation, the IR generating `right_reg` (input to the type
2143 // conversion) can have a different type from the current instruction's type,
2144 // so we manually indicate the type.
2145 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00002146 int64_t shift_amount = instruction->GetShiftAmount() &
2147 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00002148
2149 Operand right_operand(0);
2150
2151 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2152 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
2153 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2154 } else {
2155 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
2156 }
2157
2158 // Logical binary operations do not support extension operations in the
2159 // operand. Note that VIXL would still manage if it was passed by generating
2160 // the extension as a separate instruction.
2161 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2162 DCHECK(!right_operand.IsExtendedRegister() ||
2163 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2164 kind != HInstruction::kNeg));
2165 switch (kind) {
2166 case HInstruction::kAdd:
2167 __ Add(out, left, right_operand);
2168 break;
2169 case HInstruction::kAnd:
2170 __ And(out, left, right_operand);
2171 break;
2172 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002173 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002174 __ Neg(out, right_operand);
2175 break;
2176 case HInstruction::kOr:
2177 __ Orr(out, left, right_operand);
2178 break;
2179 case HInstruction::kSub:
2180 __ Sub(out, left, right_operand);
2181 break;
2182 case HInstruction::kXor:
2183 __ Eor(out, left, right_operand);
2184 break;
2185 default:
2186 LOG(FATAL) << "Unexpected operation kind: " << kind;
2187 UNREACHABLE();
2188 }
2189}
2190
Artem Serov328429f2016-07-06 16:23:04 +01002191void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002192 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2193 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002194 LocationSummary* locations =
2195 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2196 locations->SetInAt(0, Location::RequiresRegister());
2197 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
2198 locations->SetOut(Location::RequiresRegister());
2199}
2200
Roland Levillain4a3aa572016-08-15 13:17:06 +00002201void InstructionCodeGeneratorARM64::VisitIntermediateAddress(
2202 HIntermediateAddress* instruction) {
2203 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2204 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002205 __ Add(OutputRegister(instruction),
2206 InputRegisterAt(instruction, 0),
2207 Operand(InputOperandAt(instruction, 1)));
2208}
2209
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002210void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002211 LocationSummary* locations =
2212 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002213 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2214 if (instr->GetOpKind() == HInstruction::kSub &&
2215 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002216 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002217 // Don't allocate register for Mneg instruction.
2218 } else {
2219 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2220 Location::RequiresRegister());
2221 }
2222 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2223 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002224 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2225}
2226
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002227void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002228 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002229 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2230 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002231
2232 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2233 // This fixup should be carried out for all multiply-accumulate instructions:
2234 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2235 if (instr->GetType() == Primitive::kPrimLong &&
2236 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2237 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002238 vixl::aarch64::Instruction* prev =
2239 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002240 if (prev->IsLoadOrStore()) {
2241 // Make sure we emit only exactly one nop.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002242 vixl::aarch64::CodeBufferCheckScope scope(masm,
2243 kInstructionSize,
2244 vixl::aarch64::CodeBufferCheckScope::kCheck,
2245 vixl::aarch64::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002246 __ nop();
2247 }
2248 }
2249
2250 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002251 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002252 __ Madd(res, mul_left, mul_right, accumulator);
2253 } else {
2254 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002255 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002256 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002257 __ Mneg(res, mul_left, mul_right);
2258 } else {
2259 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2260 __ Msub(res, mul_left, mul_right, accumulator);
2261 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002262 }
2263}
2264
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002265void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002266 bool object_array_get_with_read_barrier =
2267 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002268 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002269 new (GetGraph()->GetArena()) LocationSummary(instruction,
2270 object_array_get_with_read_barrier ?
2271 LocationSummary::kCallOnSlowPath :
2272 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002273 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002274 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002275 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002276 locations->SetInAt(0, Location::RequiresRegister());
2277 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002278 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2279 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2280 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002281 // The output overlaps in the case of an object array get with
2282 // read barriers enabled: we do not want the move to overwrite the
2283 // array's location, as we need it to emit the read barrier.
2284 locations->SetOut(
2285 Location::RequiresRegister(),
2286 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002287 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002288}
2289
2290void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002291 Primitive::Type type = instruction->GetType();
2292 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002293 LocationSummary* locations = instruction->GetLocations();
2294 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002295 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002296 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002297 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2298 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002299 MacroAssembler* masm = GetVIXLAssembler();
2300 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002301 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002302 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002303
Roland Levillain44015862016-01-22 11:47:17 +00002304 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2305 // Object ArrayGet with Baker's read barrier case.
2306 Register temp = temps.AcquireW();
Roland Levillain4a3aa572016-08-15 13:17:06 +00002307 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2308 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Roland Levillain44015862016-01-22 11:47:17 +00002309 // Note that a potential implicit null check is handled in the
2310 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2311 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2312 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002313 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002314 // General case.
2315 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002316 Register length;
2317 if (maybe_compressed_char_at) {
2318 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2319 length = temps.AcquireW();
2320 __ Ldr(length, HeapOperand(obj, count_offset));
2321 codegen_->MaybeRecordImplicitNullCheck(instruction);
2322 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002323 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002324 if (maybe_compressed_char_at) {
2325 vixl::aarch64::Label uncompressed_load, done;
2326 __ Tbz(length.W(), kWRegSize - 1, &uncompressed_load);
2327 __ Ldrb(Register(OutputCPURegister(instruction)),
2328 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2329 __ B(&done);
2330 __ Bind(&uncompressed_load);
2331 __ Ldrh(Register(OutputCPURegister(instruction)),
2332 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2333 __ Bind(&done);
2334 } else {
2335 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2336 source = HeapOperand(obj, offset);
2337 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002338 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002339 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002340 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002341 // The read barrier instrumentation does not support the
2342 // HIntermediateAddress instruction yet.
2343 DCHECK(!kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00002344 // We do not need to compute the intermediate address from the array: the
2345 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002346 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002347 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002348 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002349 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2350 }
2351 temp = obj;
2352 } else {
2353 __ Add(temp, obj, offset);
2354 }
jessicahandojo05765752016-09-09 19:01:32 -07002355 if (maybe_compressed_char_at) {
2356 vixl::aarch64::Label uncompressed_load, done;
2357 __ Tbz(length.W(), kWRegSize - 1, &uncompressed_load);
2358 __ Ldrb(Register(OutputCPURegister(instruction)),
2359 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2360 __ B(&done);
2361 __ Bind(&uncompressed_load);
2362 __ Ldrh(Register(OutputCPURegister(instruction)),
2363 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2364 __ Bind(&done);
2365 } else {
2366 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2367 }
Roland Levillain44015862016-01-22 11:47:17 +00002368 }
jessicahandojo05765752016-09-09 19:01:32 -07002369 if (!maybe_compressed_char_at) {
2370 codegen_->Load(type, OutputCPURegister(instruction), source);
2371 codegen_->MaybeRecordImplicitNullCheck(instruction);
2372 }
Roland Levillain44015862016-01-22 11:47:17 +00002373
2374 if (type == Primitive::kPrimNot) {
2375 static_assert(
2376 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2377 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2378 Location obj_loc = locations->InAt(0);
2379 if (index.IsConstant()) {
2380 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2381 } else {
2382 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2383 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002384 }
Roland Levillain4d027112015-07-01 15:41:14 +01002385 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002386}
2387
Alexandre Rames5319def2014-10-23 10:03:10 +01002388void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2389 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2390 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002391 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002392}
2393
2394void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002395 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002396 vixl::aarch64::Register out = OutputRegister(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002397 BlockPoolsScope block_pools(GetVIXLAssembler());
jessicahandojo05765752016-09-09 19:01:32 -07002398 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002399 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002400 // Mask out compression flag from String's array length.
2401 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2402 __ And(out.W(), out.W(), Operand(static_cast<int32_t>(INT32_MAX)));
2403 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002404}
2405
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002406void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002407 Primitive::Type value_type = instruction->GetComponentType();
2408
2409 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002410 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2411 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002412 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002413 LocationSummary::kCallOnSlowPath :
2414 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002415 locations->SetInAt(0, Location::RequiresRegister());
2416 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002417 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2418 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2419 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002420 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002421 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002422 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002423 }
2424}
2425
2426void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2427 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002428 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002429 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002430 bool needs_write_barrier =
2431 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002432
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002433 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002434 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002435 CPURegister source = value;
2436 Location index = locations->InAt(1);
2437 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2438 MemOperand destination = HeapOperand(array);
2439 MacroAssembler* masm = GetVIXLAssembler();
2440 BlockPoolsScope block_pools(masm);
2441
2442 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002443 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002444 if (index.IsConstant()) {
2445 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2446 destination = HeapOperand(array, offset);
2447 } else {
2448 UseScratchRegisterScope temps(masm);
2449 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002450 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002451 // The read barrier instrumentation does not support the
2452 // HIntermediateAddress instruction yet.
2453 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002454 // We do not need to compute the intermediate address from the array: the
2455 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002456 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002457 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002458 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002459 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2460 }
2461 temp = array;
2462 } else {
2463 __ Add(temp, array, offset);
2464 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002465 destination = HeapOperand(temp,
2466 XRegisterFrom(index),
2467 LSL,
2468 Primitive::ComponentSizeShift(value_type));
2469 }
2470 codegen_->Store(value_type, value, destination);
2471 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002472 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002473 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002474 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002475 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002476 {
2477 // We use a block to end the scratch scope before the write barrier, thus
2478 // freeing the temporary registers so they can be used in `MarkGCCard`.
2479 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002480 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002481 if (index.IsConstant()) {
2482 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002483 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002484 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002485 destination = HeapOperand(temp,
2486 XRegisterFrom(index),
2487 LSL,
2488 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002489 }
2490
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002491 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2492 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2493 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2494
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002495 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002496 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2497 codegen_->AddSlowPath(slow_path);
2498 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002499 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002500 __ Cbnz(Register(value), &non_zero);
2501 if (!index.IsConstant()) {
2502 __ Add(temp, array, offset);
2503 }
2504 __ Str(wzr, destination);
2505 codegen_->MaybeRecordImplicitNullCheck(instruction);
2506 __ B(&done);
2507 __ Bind(&non_zero);
2508 }
2509
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002510 // Note that when Baker read barriers are enabled, the type
2511 // checks are performed without read barriers. This is fine,
2512 // even in the case where a class object is in the from-space
2513 // after the flip, as a comparison involving such a type would
2514 // not produce a false positive; it may of course produce a
2515 // false negative, in which case we would take the ArraySet
2516 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002517
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002518 Register temp2 = temps.AcquireSameSizeAs(array);
2519 // /* HeapReference<Class> */ temp = array->klass_
2520 __ Ldr(temp, HeapOperand(array, class_offset));
2521 codegen_->MaybeRecordImplicitNullCheck(instruction);
2522 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002523
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002524 // /* HeapReference<Class> */ temp = temp->component_type_
2525 __ Ldr(temp, HeapOperand(temp, component_offset));
2526 // /* HeapReference<Class> */ temp2 = value->klass_
2527 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2528 // If heap poisoning is enabled, no need to unpoison `temp`
2529 // nor `temp2`, as we are comparing two poisoned references.
2530 __ Cmp(temp, temp2);
2531 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002532
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002533 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2534 vixl::aarch64::Label do_put;
2535 __ B(eq, &do_put);
2536 // If heap poisoning is enabled, the `temp` reference has
2537 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002538 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2539
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002540 // /* HeapReference<Class> */ temp = temp->super_class_
2541 __ Ldr(temp, HeapOperand(temp, super_offset));
2542 // If heap poisoning is enabled, no need to unpoison
2543 // `temp`, as we are comparing against null below.
2544 __ Cbnz(temp, slow_path->GetEntryLabel());
2545 __ Bind(&do_put);
2546 } else {
2547 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002548 }
2549 }
2550
2551 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002552 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002553 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002554 __ Mov(temp2, value.W());
2555 GetAssembler()->PoisonHeapReference(temp2);
2556 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002557 }
2558
2559 if (!index.IsConstant()) {
2560 __ Add(temp, array, offset);
2561 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002562 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002563
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002564 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002565 codegen_->MaybeRecordImplicitNullCheck(instruction);
2566 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002567 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002568
2569 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2570
2571 if (done.IsLinked()) {
2572 __ Bind(&done);
2573 }
2574
2575 if (slow_path != nullptr) {
2576 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002577 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002578 }
2579}
2580
Alexandre Rames67555f72014-11-18 10:55:16 +00002581void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002582 RegisterSet caller_saves = RegisterSet::Empty();
2583 InvokeRuntimeCallingConvention calling_convention;
2584 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2585 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2586 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002587 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002588 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002589}
2590
2591void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002592 BoundsCheckSlowPathARM64* slow_path =
2593 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002594 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002595 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2596 __ B(slow_path->GetEntryLabel(), hs);
2597}
2598
Alexandre Rames67555f72014-11-18 10:55:16 +00002599void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2600 LocationSummary* locations =
2601 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2602 locations->SetInAt(0, Location::RequiresRegister());
2603 if (check->HasUses()) {
2604 locations->SetOut(Location::SameAsFirstInput());
2605 }
2606}
2607
2608void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2609 // We assume the class is not null.
2610 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2611 check->GetLoadClass(), check, check->GetDexPc(), true);
2612 codegen_->AddSlowPath(slow_path);
2613 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2614}
2615
Roland Levillain1a653882016-03-18 18:05:57 +00002616static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2617 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2618 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2619}
2620
2621void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2622 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2623 Location rhs_loc = instruction->GetLocations()->InAt(1);
2624 if (rhs_loc.IsConstant()) {
2625 // 0.0 is the only immediate that can be encoded directly in
2626 // an FCMP instruction.
2627 //
2628 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2629 // specify that in a floating-point comparison, positive zero
2630 // and negative zero are considered equal, so we can use the
2631 // literal 0.0 for both cases here.
2632 //
2633 // Note however that some methods (Float.equal, Float.compare,
2634 // Float.compareTo, Double.equal, Double.compare,
2635 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2636 // StrictMath.min) consider 0.0 to be (strictly) greater than
2637 // -0.0. So if we ever translate calls to these methods into a
2638 // HCompare instruction, we must handle the -0.0 case with
2639 // care here.
2640 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2641 __ Fcmp(lhs_reg, 0.0);
2642 } else {
2643 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2644 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002645}
2646
Serban Constantinescu02164b32014-11-13 14:05:07 +00002647void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002648 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002649 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2650 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002651 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002652 case Primitive::kPrimBoolean:
2653 case Primitive::kPrimByte:
2654 case Primitive::kPrimShort:
2655 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002656 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002657 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002658 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002659 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002660 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2661 break;
2662 }
2663 case Primitive::kPrimFloat:
2664 case Primitive::kPrimDouble: {
2665 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002666 locations->SetInAt(1,
2667 IsFloatingPointZeroConstant(compare->InputAt(1))
2668 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2669 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002670 locations->SetOut(Location::RequiresRegister());
2671 break;
2672 }
2673 default:
2674 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2675 }
2676}
2677
2678void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2679 Primitive::Type in_type = compare->InputAt(0)->GetType();
2680
2681 // 0 if: left == right
2682 // 1 if: left > right
2683 // -1 if: left < right
2684 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002685 case Primitive::kPrimBoolean:
2686 case Primitive::kPrimByte:
2687 case Primitive::kPrimShort:
2688 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002689 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002690 case Primitive::kPrimLong: {
2691 Register result = OutputRegister(compare);
2692 Register left = InputRegisterAt(compare, 0);
2693 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002694 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002695 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2696 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002697 break;
2698 }
2699 case Primitive::kPrimFloat:
2700 case Primitive::kPrimDouble: {
2701 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002702 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002703 __ Cset(result, ne);
2704 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002705 break;
2706 }
2707 default:
2708 LOG(FATAL) << "Unimplemented compare type " << in_type;
2709 }
2710}
2711
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002712void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002713 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002714
2715 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2716 locations->SetInAt(0, Location::RequiresFpuRegister());
2717 locations->SetInAt(1,
2718 IsFloatingPointZeroConstant(instruction->InputAt(1))
2719 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2720 : Location::RequiresFpuRegister());
2721 } else {
2722 // Integer cases.
2723 locations->SetInAt(0, Location::RequiresRegister());
2724 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2725 }
2726
David Brazdilb3e773e2016-01-26 11:28:37 +00002727 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002728 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002729 }
2730}
2731
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002732void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002733 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002734 return;
2735 }
2736
2737 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002738 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002739 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002740
Roland Levillain7f63c522015-07-13 15:54:55 +00002741 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002742 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002743 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002744 } else {
2745 // Integer cases.
2746 Register lhs = InputRegisterAt(instruction, 0);
2747 Operand rhs = InputOperandAt(instruction, 1);
2748 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002749 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002750 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002751}
2752
2753#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2754 M(Equal) \
2755 M(NotEqual) \
2756 M(LessThan) \
2757 M(LessThanOrEqual) \
2758 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002759 M(GreaterThanOrEqual) \
2760 M(Below) \
2761 M(BelowOrEqual) \
2762 M(Above) \
2763 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002764#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002765void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2766void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002767FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002768#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002769#undef FOR_EACH_CONDITION_INSTRUCTION
2770
Zheng Xuc6667102015-05-15 16:08:45 +08002771void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2772 DCHECK(instruction->IsDiv() || instruction->IsRem());
2773
2774 LocationSummary* locations = instruction->GetLocations();
2775 Location second = locations->InAt(1);
2776 DCHECK(second.IsConstant());
2777
2778 Register out = OutputRegister(instruction);
2779 Register dividend = InputRegisterAt(instruction, 0);
2780 int64_t imm = Int64FromConstant(second.GetConstant());
2781 DCHECK(imm == 1 || imm == -1);
2782
2783 if (instruction->IsRem()) {
2784 __ Mov(out, 0);
2785 } else {
2786 if (imm == 1) {
2787 __ Mov(out, dividend);
2788 } else {
2789 __ Neg(out, dividend);
2790 }
2791 }
2792}
2793
2794void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2795 DCHECK(instruction->IsDiv() || instruction->IsRem());
2796
2797 LocationSummary* locations = instruction->GetLocations();
2798 Location second = locations->InAt(1);
2799 DCHECK(second.IsConstant());
2800
2801 Register out = OutputRegister(instruction);
2802 Register dividend = InputRegisterAt(instruction, 0);
2803 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002804 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002805 int ctz_imm = CTZ(abs_imm);
2806
2807 UseScratchRegisterScope temps(GetVIXLAssembler());
2808 Register temp = temps.AcquireSameSizeAs(out);
2809
2810 if (instruction->IsDiv()) {
2811 __ Add(temp, dividend, abs_imm - 1);
2812 __ Cmp(dividend, 0);
2813 __ Csel(out, temp, dividend, lt);
2814 if (imm > 0) {
2815 __ Asr(out, out, ctz_imm);
2816 } else {
2817 __ Neg(out, Operand(out, ASR, ctz_imm));
2818 }
2819 } else {
2820 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2821 __ Asr(temp, dividend, bits - 1);
2822 __ Lsr(temp, temp, bits - ctz_imm);
2823 __ Add(out, dividend, temp);
2824 __ And(out, out, abs_imm - 1);
2825 __ Sub(out, out, temp);
2826 }
2827}
2828
2829void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2830 DCHECK(instruction->IsDiv() || instruction->IsRem());
2831
2832 LocationSummary* locations = instruction->GetLocations();
2833 Location second = locations->InAt(1);
2834 DCHECK(second.IsConstant());
2835
2836 Register out = OutputRegister(instruction);
2837 Register dividend = InputRegisterAt(instruction, 0);
2838 int64_t imm = Int64FromConstant(second.GetConstant());
2839
2840 Primitive::Type type = instruction->GetResultType();
2841 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2842
2843 int64_t magic;
2844 int shift;
2845 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2846
2847 UseScratchRegisterScope temps(GetVIXLAssembler());
2848 Register temp = temps.AcquireSameSizeAs(out);
2849
2850 // temp = get_high(dividend * magic)
2851 __ Mov(temp, magic);
2852 if (type == Primitive::kPrimLong) {
2853 __ Smulh(temp, dividend, temp);
2854 } else {
2855 __ Smull(temp.X(), dividend, temp);
2856 __ Lsr(temp.X(), temp.X(), 32);
2857 }
2858
2859 if (imm > 0 && magic < 0) {
2860 __ Add(temp, temp, dividend);
2861 } else if (imm < 0 && magic > 0) {
2862 __ Sub(temp, temp, dividend);
2863 }
2864
2865 if (shift != 0) {
2866 __ Asr(temp, temp, shift);
2867 }
2868
2869 if (instruction->IsDiv()) {
2870 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2871 } else {
2872 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2873 // TODO: Strength reduction for msub.
2874 Register temp_imm = temps.AcquireSameSizeAs(out);
2875 __ Mov(temp_imm, imm);
2876 __ Msub(out, temp, temp_imm, dividend);
2877 }
2878}
2879
2880void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2881 DCHECK(instruction->IsDiv() || instruction->IsRem());
2882 Primitive::Type type = instruction->GetResultType();
2883 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2884
2885 LocationSummary* locations = instruction->GetLocations();
2886 Register out = OutputRegister(instruction);
2887 Location second = locations->InAt(1);
2888
2889 if (second.IsConstant()) {
2890 int64_t imm = Int64FromConstant(second.GetConstant());
2891
2892 if (imm == 0) {
2893 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2894 } else if (imm == 1 || imm == -1) {
2895 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002896 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002897 DivRemByPowerOfTwo(instruction);
2898 } else {
2899 DCHECK(imm <= -2 || imm >= 2);
2900 GenerateDivRemWithAnyConstant(instruction);
2901 }
2902 } else {
2903 Register dividend = InputRegisterAt(instruction, 0);
2904 Register divisor = InputRegisterAt(instruction, 1);
2905 if (instruction->IsDiv()) {
2906 __ Sdiv(out, dividend, divisor);
2907 } else {
2908 UseScratchRegisterScope temps(GetVIXLAssembler());
2909 Register temp = temps.AcquireSameSizeAs(out);
2910 __ Sdiv(temp, dividend, divisor);
2911 __ Msub(out, temp, divisor, dividend);
2912 }
2913 }
2914}
2915
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002916void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2917 LocationSummary* locations =
2918 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2919 switch (div->GetResultType()) {
2920 case Primitive::kPrimInt:
2921 case Primitive::kPrimLong:
2922 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002923 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002924 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2925 break;
2926
2927 case Primitive::kPrimFloat:
2928 case Primitive::kPrimDouble:
2929 locations->SetInAt(0, Location::RequiresFpuRegister());
2930 locations->SetInAt(1, Location::RequiresFpuRegister());
2931 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2932 break;
2933
2934 default:
2935 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2936 }
2937}
2938
2939void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2940 Primitive::Type type = div->GetResultType();
2941 switch (type) {
2942 case Primitive::kPrimInt:
2943 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002944 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002945 break;
2946
2947 case Primitive::kPrimFloat:
2948 case Primitive::kPrimDouble:
2949 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2950 break;
2951
2952 default:
2953 LOG(FATAL) << "Unexpected div type " << type;
2954 }
2955}
2956
Alexandre Rames67555f72014-11-18 10:55:16 +00002957void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002958 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002959 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00002960}
2961
2962void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2963 SlowPathCodeARM64* slow_path =
2964 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2965 codegen_->AddSlowPath(slow_path);
2966 Location value = instruction->GetLocations()->InAt(0);
2967
Alexandre Rames3e69f162014-12-10 10:36:50 +00002968 Primitive::Type type = instruction->GetType();
2969
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002970 if (!Primitive::IsIntegralType(type)) {
2971 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002972 return;
2973 }
2974
Alexandre Rames67555f72014-11-18 10:55:16 +00002975 if (value.IsConstant()) {
2976 int64_t divisor = Int64ConstantFrom(value);
2977 if (divisor == 0) {
2978 __ B(slow_path->GetEntryLabel());
2979 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002980 // A division by a non-null constant is valid. We don't need to perform
2981 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002982 }
2983 } else {
2984 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2985 }
2986}
2987
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002988void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2989 LocationSummary* locations =
2990 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2991 locations->SetOut(Location::ConstantLocation(constant));
2992}
2993
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002994void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2995 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002996 // Will be generated at use site.
2997}
2998
Alexandre Rames5319def2014-10-23 10:03:10 +01002999void LocationsBuilderARM64::VisitExit(HExit* exit) {
3000 exit->SetLocations(nullptr);
3001}
3002
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003003void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003004}
3005
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003006void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3007 LocationSummary* locations =
3008 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3009 locations->SetOut(Location::ConstantLocation(constant));
3010}
3011
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003012void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003013 // Will be generated at use site.
3014}
3015
David Brazdilfc6a86a2015-06-26 10:33:45 +00003016void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003017 DCHECK(!successor->IsExitBlock());
3018 HBasicBlock* block = got->GetBlock();
3019 HInstruction* previous = got->GetPrevious();
3020 HLoopInformation* info = block->GetLoopInformation();
3021
David Brazdil46e2a392015-03-16 17:31:52 +00003022 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003023 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3024 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3025 return;
3026 }
3027 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3028 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3029 }
3030 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003031 __ B(codegen_->GetLabelOf(successor));
3032 }
3033}
3034
David Brazdilfc6a86a2015-06-26 10:33:45 +00003035void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3036 got->SetLocations(nullptr);
3037}
3038
3039void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3040 HandleGoto(got, got->GetSuccessor());
3041}
3042
3043void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3044 try_boundary->SetLocations(nullptr);
3045}
3046
3047void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3048 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3049 if (!successor->IsExitBlock()) {
3050 HandleGoto(try_boundary, successor);
3051 }
3052}
3053
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003054void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003055 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003056 vixl::aarch64::Label* true_target,
3057 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003058 // FP branching requires both targets to be explicit. If either of the targets
3059 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003060 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003061 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003062
David Brazdil0debae72015-11-12 18:37:00 +00003063 if (true_target == nullptr && false_target == nullptr) {
3064 // Nothing to do. The code always falls through.
3065 return;
3066 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003067 // Constant condition, statically compared against "true" (integer value 1).
3068 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003069 if (true_target != nullptr) {
3070 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003071 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003072 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003073 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003074 if (false_target != nullptr) {
3075 __ B(false_target);
3076 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003077 }
David Brazdil0debae72015-11-12 18:37:00 +00003078 return;
3079 }
3080
3081 // The following code generates these patterns:
3082 // (1) true_target == nullptr && false_target != nullptr
3083 // - opposite condition true => branch to false_target
3084 // (2) true_target != nullptr && false_target == nullptr
3085 // - condition true => branch to true_target
3086 // (3) true_target != nullptr && false_target != nullptr
3087 // - condition true => branch to true_target
3088 // - branch to false_target
3089 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003090 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003091 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003092 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003093 if (true_target == nullptr) {
3094 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3095 } else {
3096 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3097 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003098 } else {
3099 // The condition instruction has not been materialized, use its inputs as
3100 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003101 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003102
David Brazdil0debae72015-11-12 18:37:00 +00003103 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003104 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003105 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003106 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003107 IfCondition opposite_condition = condition->GetOppositeCondition();
3108 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003109 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003110 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003111 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003112 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003113 // Integer cases.
3114 Register lhs = InputRegisterAt(condition, 0);
3115 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003116
3117 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003118 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003119 if (true_target == nullptr) {
3120 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3121 non_fallthrough_target = false_target;
3122 } else {
3123 arm64_cond = ARM64Condition(condition->GetCondition());
3124 non_fallthrough_target = true_target;
3125 }
3126
Aart Bik086d27e2016-01-20 17:02:00 -08003127 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003128 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003129 switch (arm64_cond) {
3130 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003131 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003132 break;
3133 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003134 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003135 break;
3136 case lt:
3137 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003138 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003139 break;
3140 case ge:
3141 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003142 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003143 break;
3144 default:
3145 // Without the `static_cast` the compiler throws an error for
3146 // `-Werror=sign-promo`.
3147 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3148 }
3149 } else {
3150 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003151 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003152 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003153 }
3154 }
David Brazdil0debae72015-11-12 18:37:00 +00003155
3156 // If neither branch falls through (case 3), the conditional branch to `true_target`
3157 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3158 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003159 __ B(false_target);
3160 }
David Brazdil0debae72015-11-12 18:37:00 +00003161
3162 if (fallthrough_target.IsLinked()) {
3163 __ Bind(&fallthrough_target);
3164 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003165}
3166
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003167void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3168 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003169 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003170 locations->SetInAt(0, Location::RequiresRegister());
3171 }
3172}
3173
3174void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003175 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3176 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003177 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3178 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3179 true_target = nullptr;
3180 }
3181 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3182 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3183 false_target = nullptr;
3184 }
David Brazdil0debae72015-11-12 18:37:00 +00003185 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003186}
3187
3188void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3189 LocationSummary* locations = new (GetGraph()->GetArena())
3190 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003191 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003192 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003193 locations->SetInAt(0, Location::RequiresRegister());
3194 }
3195}
3196
3197void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003198 SlowPathCodeARM64* slow_path =
3199 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003200 GenerateTestAndBranch(deoptimize,
3201 /* condition_input_index */ 0,
3202 slow_path->GetEntryLabel(),
3203 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003204}
3205
David Brazdilc0b601b2016-02-08 14:20:45 +00003206static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3207 return condition->IsCondition() &&
3208 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3209}
3210
Alexandre Rames880f1192016-06-13 16:04:50 +01003211static inline Condition GetConditionForSelect(HCondition* condition) {
3212 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003213 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3214 : ARM64Condition(cond);
3215}
3216
David Brazdil74eb1b22015-12-14 11:44:01 +00003217void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3218 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003219 if (Primitive::IsFloatingPointType(select->GetType())) {
3220 locations->SetInAt(0, Location::RequiresFpuRegister());
3221 locations->SetInAt(1, Location::RequiresFpuRegister());
3222 locations->SetOut(Location::RequiresFpuRegister());
3223 } else {
3224 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3225 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3226 bool is_true_value_constant = cst_true_value != nullptr;
3227 bool is_false_value_constant = cst_false_value != nullptr;
3228 // Ask VIXL whether we should synthesize constants in registers.
3229 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3230 Operand true_op = is_true_value_constant ?
3231 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3232 Operand false_op = is_false_value_constant ?
3233 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3234 bool true_value_in_register = false;
3235 bool false_value_in_register = false;
3236 MacroAssembler::GetCselSynthesisInformation(
3237 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3238 true_value_in_register |= !is_true_value_constant;
3239 false_value_in_register |= !is_false_value_constant;
3240
3241 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3242 : Location::ConstantLocation(cst_true_value));
3243 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3244 : Location::ConstantLocation(cst_false_value));
3245 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003246 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003247
David Brazdil74eb1b22015-12-14 11:44:01 +00003248 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3249 locations->SetInAt(2, Location::RequiresRegister());
3250 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003251}
3252
3253void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003254 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003255 Condition csel_cond;
3256
3257 if (IsBooleanValueOrMaterializedCondition(cond)) {
3258 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003259 // Use the condition flags set by the previous instruction.
3260 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003261 } else {
3262 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003263 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003264 }
3265 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003266 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003267 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003268 } else {
3269 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003270 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003271 }
3272
Alexandre Rames880f1192016-06-13 16:04:50 +01003273 if (Primitive::IsFloatingPointType(select->GetType())) {
3274 __ Fcsel(OutputFPRegister(select),
3275 InputFPRegisterAt(select, 1),
3276 InputFPRegisterAt(select, 0),
3277 csel_cond);
3278 } else {
3279 __ Csel(OutputRegister(select),
3280 InputOperandAt(select, 1),
3281 InputOperandAt(select, 0),
3282 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003283 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003284}
3285
David Srbecky0cf44932015-12-09 14:09:59 +00003286void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3287 new (GetGraph()->GetArena()) LocationSummary(info);
3288}
3289
David Srbeckyd28f4a02016-03-14 17:14:24 +00003290void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3291 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003292}
3293
3294void CodeGeneratorARM64::GenerateNop() {
3295 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003296}
3297
Alexandre Rames5319def2014-10-23 10:03:10 +01003298void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003299 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003300}
3301
3302void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003303 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003304}
3305
3306void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003307 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003308}
3309
3310void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003311 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003312}
3313
Roland Levillain44015862016-01-22 11:47:17 +00003314static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
3315 return kEmitCompilerReadBarrier &&
3316 (kUseBakerReadBarrier ||
3317 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3318 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3319 type_check_kind == TypeCheckKind::kArrayObjectCheck);
3320}
3321
Alexandre Rames67555f72014-11-18 10:55:16 +00003322void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003323 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003324 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003325 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003326 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003327 case TypeCheckKind::kExactCheck:
3328 case TypeCheckKind::kAbstractClassCheck:
3329 case TypeCheckKind::kClassHierarchyCheck:
3330 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003331 call_kind =
3332 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003333 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003334 break;
3335 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003336 case TypeCheckKind::kUnresolvedCheck:
3337 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003338 call_kind = LocationSummary::kCallOnSlowPath;
3339 break;
3340 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003341
Alexandre Rames67555f72014-11-18 10:55:16 +00003342 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003343 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003344 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003345 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003346 locations->SetInAt(0, Location::RequiresRegister());
3347 locations->SetInAt(1, Location::RequiresRegister());
3348 // The "out" register is used as a temporary, so it overlaps with the inputs.
3349 // Note that TypeCheckSlowPathARM64 uses this register too.
3350 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3351 // When read barriers are enabled, we need a temporary register for
3352 // some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003353 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003354 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003355 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003356}
3357
3358void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003359 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003360 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003361 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003362 Register obj = InputRegisterAt(instruction, 0);
3363 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003364 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003365 Register out = OutputRegister(instruction);
Roland Levillain44015862016-01-22 11:47:17 +00003366 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3367 locations->GetTemp(0) :
3368 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003369 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3370 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3371 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3372 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003373
Scott Wakeling97c72b72016-06-24 16:19:36 +01003374 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003375 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003376
3377 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003378 // Avoid null check if we know `obj` is not null.
3379 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003380 __ Cbz(obj, &zero);
3381 }
3382
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003383 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003384 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003385
Roland Levillain44015862016-01-22 11:47:17 +00003386 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003387 case TypeCheckKind::kExactCheck: {
3388 __ Cmp(out, cls);
3389 __ Cset(out, eq);
3390 if (zero.IsLinked()) {
3391 __ B(&done);
3392 }
3393 break;
3394 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003395
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003396 case TypeCheckKind::kAbstractClassCheck: {
3397 // If the class is abstract, we eagerly fetch the super class of the
3398 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003399 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003400 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003401 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003402 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003403 // If `out` is null, we use it for the result, and jump to `done`.
3404 __ Cbz(out, &done);
3405 __ Cmp(out, cls);
3406 __ B(ne, &loop);
3407 __ Mov(out, 1);
3408 if (zero.IsLinked()) {
3409 __ B(&done);
3410 }
3411 break;
3412 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003413
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003414 case TypeCheckKind::kClassHierarchyCheck: {
3415 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003416 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003417 __ Bind(&loop);
3418 __ Cmp(out, cls);
3419 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003420 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003421 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003422 __ Cbnz(out, &loop);
3423 // If `out` is null, we use it for the result, and jump to `done`.
3424 __ B(&done);
3425 __ Bind(&success);
3426 __ Mov(out, 1);
3427 if (zero.IsLinked()) {
3428 __ B(&done);
3429 }
3430 break;
3431 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003432
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003433 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003434 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003435 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003436 __ Cmp(out, cls);
3437 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003438 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003439 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003440 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003441 // If `out` is null, we use it for the result, and jump to `done`.
3442 __ Cbz(out, &done);
3443 __ Ldrh(out, HeapOperand(out, primitive_offset));
3444 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3445 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003446 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003447 __ Mov(out, 1);
3448 __ B(&done);
3449 break;
3450 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003451
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003452 case TypeCheckKind::kArrayCheck: {
3453 __ Cmp(out, cls);
3454 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003455 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3456 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003457 codegen_->AddSlowPath(slow_path);
3458 __ B(ne, slow_path->GetEntryLabel());
3459 __ Mov(out, 1);
3460 if (zero.IsLinked()) {
3461 __ B(&done);
3462 }
3463 break;
3464 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003465
Calin Juravle98893e12015-10-02 21:05:03 +01003466 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003467 case TypeCheckKind::kInterfaceCheck: {
3468 // Note that we indeed only call on slow path, but we always go
3469 // into the slow path for the unresolved and interface check
3470 // cases.
3471 //
3472 // We cannot directly call the InstanceofNonTrivial runtime
3473 // entry point without resorting to a type checking slow path
3474 // here (i.e. by calling InvokeRuntime directly), as it would
3475 // require to assign fixed registers for the inputs of this
3476 // HInstanceOf instruction (following the runtime calling
3477 // convention), which might be cluttered by the potential first
3478 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003479 //
3480 // TODO: Introduce a new runtime entry point taking the object
3481 // to test (instead of its class) as argument, and let it deal
3482 // with the read barrier issues. This will let us refactor this
3483 // case of the `switch` code as it was previously (with a direct
3484 // call to the runtime not using a type checking slow path).
3485 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003486 DCHECK(locations->OnlyCallsOnSlowPath());
3487 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3488 /* is_fatal */ false);
3489 codegen_->AddSlowPath(slow_path);
3490 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003491 if (zero.IsLinked()) {
3492 __ B(&done);
3493 }
3494 break;
3495 }
3496 }
3497
3498 if (zero.IsLinked()) {
3499 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003500 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003501 }
3502
3503 if (done.IsLinked()) {
3504 __ Bind(&done);
3505 }
3506
3507 if (slow_path != nullptr) {
3508 __ Bind(slow_path->GetExitLabel());
3509 }
3510}
3511
3512void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3513 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3514 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3515
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003516 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3517 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003518 case TypeCheckKind::kExactCheck:
3519 case TypeCheckKind::kAbstractClassCheck:
3520 case TypeCheckKind::kClassHierarchyCheck:
3521 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003522 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3523 LocationSummary::kCallOnSlowPath :
3524 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003525 break;
3526 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003527 case TypeCheckKind::kUnresolvedCheck:
3528 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003529 call_kind = LocationSummary::kCallOnSlowPath;
3530 break;
3531 }
3532
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003533 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3534 locations->SetInAt(0, Location::RequiresRegister());
3535 locations->SetInAt(1, Location::RequiresRegister());
3536 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3537 locations->AddTemp(Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003538 // When read barriers are enabled, we need an additional temporary
3539 // register for some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003540 if (TypeCheckNeedsATemporary(type_check_kind)) {
3541 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003542 }
3543}
3544
3545void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003546 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003547 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003548 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003549 Register obj = InputRegisterAt(instruction, 0);
3550 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003551 Location temp_loc = locations->GetTemp(0);
Roland Levillain44015862016-01-22 11:47:17 +00003552 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3553 locations->GetTemp(1) :
3554 Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003555 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003556 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3557 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3558 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3559 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003560
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003561 bool is_type_check_slow_path_fatal =
3562 (type_check_kind == TypeCheckKind::kExactCheck ||
3563 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3564 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3565 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3566 !instruction->CanThrowIntoCatchBlock();
3567 SlowPathCodeARM64* type_check_slow_path =
3568 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3569 is_type_check_slow_path_fatal);
3570 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003571
Scott Wakeling97c72b72016-06-24 16:19:36 +01003572 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003573 // Avoid null check if we know obj is not null.
3574 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003575 __ Cbz(obj, &done);
3576 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003577
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003578 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003579 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003580
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003581 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003582 case TypeCheckKind::kExactCheck:
3583 case TypeCheckKind::kArrayCheck: {
3584 __ Cmp(temp, cls);
3585 // Jump to slow path for throwing the exception or doing a
3586 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003587 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003588 break;
3589 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003590
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003591 case TypeCheckKind::kAbstractClassCheck: {
3592 // If the class is abstract, we eagerly fetch the super class of the
3593 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003594 vixl::aarch64::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003595 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003596 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003597 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003598
3599 // If the class reference currently in `temp` is not null, jump
3600 // to the `compare_classes` label to compare it with the checked
3601 // class.
3602 __ Cbnz(temp, &compare_classes);
3603 // Otherwise, jump to the slow path to throw the exception.
3604 //
3605 // But before, move back the object's class into `temp` before
3606 // going into the slow path, as it has been overwritten in the
3607 // meantime.
3608 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003609 GenerateReferenceLoadTwoRegisters(
3610 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003611 __ B(type_check_slow_path->GetEntryLabel());
3612
3613 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003614 __ Cmp(temp, cls);
3615 __ B(ne, &loop);
3616 break;
3617 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003618
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003619 case TypeCheckKind::kClassHierarchyCheck: {
3620 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003621 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003622 __ Bind(&loop);
3623 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003624 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003625
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003626 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003627 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003628
3629 // If the class reference currently in `temp` is not null, jump
3630 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003631 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003632 // Otherwise, jump to the slow path to throw the exception.
3633 //
3634 // But before, move back the object's class into `temp` before
3635 // going into the slow path, as it has been overwritten in the
3636 // meantime.
3637 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003638 GenerateReferenceLoadTwoRegisters(
3639 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003640 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003641 break;
3642 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003643
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003644 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003645 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003646 vixl::aarch64::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003647 __ Cmp(temp, cls);
3648 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003649
3650 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003651 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003652 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003653
3654 // If the component type is not null (i.e. the object is indeed
3655 // an array), jump to label `check_non_primitive_component_type`
3656 // to further check that this component type is not a primitive
3657 // type.
3658 __ Cbnz(temp, &check_non_primitive_component_type);
3659 // Otherwise, jump to the slow path to throw the exception.
3660 //
3661 // But before, move back the object's class into `temp` before
3662 // going into the slow path, as it has been overwritten in the
3663 // meantime.
3664 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003665 GenerateReferenceLoadTwoRegisters(
3666 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003667 __ B(type_check_slow_path->GetEntryLabel());
3668
3669 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003670 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3671 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003672 __ Cbz(temp, &done);
3673 // Same comment as above regarding `temp` and the slow path.
3674 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003675 GenerateReferenceLoadTwoRegisters(
3676 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003677 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003678 break;
3679 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003680
Calin Juravle98893e12015-10-02 21:05:03 +01003681 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003682 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003683 // We always go into the type check slow path for the unresolved
3684 // and interface check cases.
3685 //
3686 // We cannot directly call the CheckCast runtime entry point
3687 // without resorting to a type checking slow path here (i.e. by
3688 // calling InvokeRuntime directly), as it would require to
3689 // assign fixed registers for the inputs of this HInstanceOf
3690 // instruction (following the runtime calling convention), which
3691 // might be cluttered by the potential first read barrier
3692 // emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003693 //
3694 // TODO: Introduce a new runtime entry point taking the object
3695 // to test (instead of its class) as argument, and let it deal
3696 // with the read barrier issues. This will let us refactor this
3697 // case of the `switch` code as it was previously (with a direct
3698 // call to the runtime not using a type checking slow path).
3699 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003700 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003701 break;
3702 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003703 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003704
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003705 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003706}
3707
Alexandre Rames5319def2014-10-23 10:03:10 +01003708void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3709 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3710 locations->SetOut(Location::ConstantLocation(constant));
3711}
3712
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003713void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003714 // Will be generated at use site.
3715}
3716
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003717void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3718 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3719 locations->SetOut(Location::ConstantLocation(constant));
3720}
3721
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003722void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003723 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003724}
3725
Calin Juravle175dc732015-08-25 15:42:32 +01003726void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3727 // The trampoline uses the same calling convention as dex calling conventions,
3728 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3729 // the method_idx.
3730 HandleInvoke(invoke);
3731}
3732
3733void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3734 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3735}
3736
Alexandre Rames5319def2014-10-23 10:03:10 +01003737void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003738 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003739 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003740}
3741
Alexandre Rames67555f72014-11-18 10:55:16 +00003742void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3743 HandleInvoke(invoke);
3744}
3745
3746void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3747 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003748 LocationSummary* locations = invoke->GetLocations();
3749 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003750 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003751 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003752 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003753
3754 // The register ip1 is required to be used for the hidden argument in
3755 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003756 MacroAssembler* masm = GetVIXLAssembler();
3757 UseScratchRegisterScope scratch_scope(masm);
3758 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003759 scratch_scope.Exclude(ip1);
3760 __ Mov(ip1, invoke->GetDexMethodIndex());
3761
Alexandre Rames67555f72014-11-18 10:55:16 +00003762 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003763 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003764 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003765 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003766 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003767 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003768 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003769 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003770 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003771 // Instead of simply (possibly) unpoisoning `temp` here, we should
3772 // emit a read barrier for the previous class reference load.
3773 // However this is not required in practice, as this is an
3774 // intermediate/temporary reference and because the current
3775 // concurrent copying collector keeps the from-space memory
3776 // intact/accessible until the end of the marking phase (the
3777 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003778 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003779 __ Ldr(temp,
3780 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3781 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003782 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003783 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003784 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003785 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003786 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003787 // lr();
3788 __ Blr(lr);
3789 DCHECK(!codegen_->IsLeafMethod());
3790 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3791}
3792
3793void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003794 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3795 if (intrinsic.TryDispatch(invoke)) {
3796 return;
3797 }
3798
Alexandre Rames67555f72014-11-18 10:55:16 +00003799 HandleInvoke(invoke);
3800}
3801
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003802void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003803 // Explicit clinit checks triggered by static invokes must have been pruned by
3804 // art::PrepareForRegisterAllocation.
3805 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003806
Andreas Gampe878d58c2015-01-15 23:24:00 -08003807 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3808 if (intrinsic.TryDispatch(invoke)) {
3809 return;
3810 }
3811
Alexandre Rames67555f72014-11-18 10:55:16 +00003812 HandleInvoke(invoke);
3813}
3814
Andreas Gampe878d58c2015-01-15 23:24:00 -08003815static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3816 if (invoke->GetLocations()->Intrinsified()) {
3817 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3818 intrinsic.Dispatch(invoke);
3819 return true;
3820 }
3821 return false;
3822}
3823
Vladimir Markodc151b22015-10-15 18:02:30 +01003824HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3825 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003826 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003827 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003828 return desired_dispatch_info;
3829}
3830
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003831void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003832 // For better instruction scheduling we load the direct code pointer before the method pointer.
3833 bool direct_code_loaded = false;
3834 switch (invoke->GetCodePtrLocation()) {
3835 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3836 // LR = code address from literal pool with link-time patch.
3837 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3838 direct_code_loaded = true;
3839 break;
3840 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3841 // LR = invoke->GetDirectCodePtr();
3842 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3843 direct_code_loaded = true;
3844 break;
3845 default:
3846 break;
3847 }
3848
Andreas Gampe878d58c2015-01-15 23:24:00 -08003849 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003850 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3851 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003852 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
3853 uint32_t offset =
3854 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003855 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003856 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00003857 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003858 }
Vladimir Marko58155012015-08-19 12:49:41 +00003859 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003860 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003861 break;
3862 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3863 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003864 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003865 break;
3866 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3867 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003868 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003869 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3870 break;
3871 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3872 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003873 const DexFile& dex_file = invoke->GetDexFile();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003874 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003875 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00003876 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00003877 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003878 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003879 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00003880 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00003881 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003882 }
Vladimir Marko58155012015-08-19 12:49:41 +00003883 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003884 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003885 Register reg = XRegisterFrom(temp);
3886 Register method_reg;
3887 if (current_method.IsRegister()) {
3888 method_reg = XRegisterFrom(current_method);
3889 } else {
3890 DCHECK(invoke->GetLocations()->Intrinsified());
3891 DCHECK(!current_method.IsValid());
3892 method_reg = reg;
3893 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3894 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003895
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003896 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003897 __ Ldr(reg.X(),
3898 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07003899 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003900 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01003901 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3902 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003903 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3904 break;
3905 }
3906 }
3907
3908 switch (invoke->GetCodePtrLocation()) {
3909 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3910 __ Bl(&frame_entry_label_);
3911 break;
3912 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00003913 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3914 invoke->GetTargetMethod().dex_method_index);
Scott Wakeling97c72b72016-06-24 16:19:36 +01003915 vixl::aarch64::Label* label = &relative_call_patches_.back().label;
3916 SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003917 __ Bind(label);
3918 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003919 break;
3920 }
3921 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3922 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3923 // LR prepared above for better instruction scheduling.
3924 DCHECK(direct_code_loaded);
3925 // lr()
3926 __ Blr(lr);
3927 break;
3928 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3929 // LR = callee_method->entry_point_from_quick_compiled_code_;
3930 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003931 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07003932 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003933 // lr()
3934 __ Blr(lr);
3935 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003936 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003937
Andreas Gampe878d58c2015-01-15 23:24:00 -08003938 DCHECK(!IsLeafMethod());
3939}
3940
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003941void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003942 // Use the calling convention instead of the location of the receiver, as
3943 // intrinsics may have put the receiver in a different register. In the intrinsics
3944 // slow path, the arguments have been moved to the right place, so here we are
3945 // guaranteed that the receiver is the first register of the calling convention.
3946 InvokeDexCallingConvention calling_convention;
3947 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003948 Register temp = XRegisterFrom(temp_in);
3949 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3950 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3951 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003952 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003953
3954 BlockPoolsScope block_pools(GetVIXLAssembler());
3955
3956 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003957 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003958 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003959 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003960 // Instead of simply (possibly) unpoisoning `temp` here, we should
3961 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003962 // intermediate/temporary reference and because the current
3963 // concurrent copying collector keeps the from-space memory
3964 // intact/accessible until the end of the marking phase (the
3965 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003966 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3967 // temp = temp->GetMethodAt(method_offset);
3968 __ Ldr(temp, MemOperand(temp, method_offset));
3969 // lr = temp->GetEntryPoint();
3970 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3971 // lr();
3972 __ Blr(lr);
3973}
3974
Scott Wakeling97c72b72016-06-24 16:19:36 +01003975vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
3976 const DexFile& dex_file,
3977 uint32_t string_index,
3978 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003979 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
3980}
3981
Scott Wakeling97c72b72016-06-24 16:19:36 +01003982vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
3983 const DexFile& dex_file,
3984 uint32_t type_index,
3985 vixl::aarch64::Label* adrp_label) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003986 return NewPcRelativePatch(dex_file, type_index, adrp_label, &pc_relative_type_patches_);
3987}
3988
Scott Wakeling97c72b72016-06-24 16:19:36 +01003989vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
3990 const DexFile& dex_file,
3991 uint32_t element_offset,
3992 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003993 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
3994}
3995
Scott Wakeling97c72b72016-06-24 16:19:36 +01003996vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
3997 const DexFile& dex_file,
3998 uint32_t offset_or_index,
3999 vixl::aarch64::Label* adrp_label,
4000 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004001 // Add a patch entry and return the label.
4002 patches->emplace_back(dex_file, offset_or_index);
4003 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004004 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004005 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4006 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4007 return label;
4008}
4009
Scott Wakeling97c72b72016-06-24 16:19:36 +01004010vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004011 const DexFile& dex_file, uint32_t string_index) {
4012 return boot_image_string_patches_.GetOrCreate(
4013 StringReference(&dex_file, string_index),
4014 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4015}
4016
Scott Wakeling97c72b72016-06-24 16:19:36 +01004017vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004018 const DexFile& dex_file, uint32_t type_index) {
4019 return boot_image_type_patches_.GetOrCreate(
4020 TypeReference(&dex_file, type_index),
4021 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4022}
4023
Scott Wakeling97c72b72016-06-24 16:19:36 +01004024vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4025 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004026 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
4027 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
4028 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
4029}
4030
Scott Wakeling97c72b72016-06-24 16:19:36 +01004031vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(
4032 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004033 return DeduplicateUint64Literal(address);
4034}
4035
Vladimir Markoaad75c62016-10-03 08:46:48 +00004036void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4037 vixl::aarch64::Register reg) {
4038 DCHECK(reg.IsX());
4039 SingleEmissionCheckScope guard(GetVIXLAssembler());
4040 __ Bind(fixup_label);
4041 __ adrp(reg, /* offset placeholder */ 0);
4042}
4043
4044void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4045 vixl::aarch64::Register out,
4046 vixl::aarch64::Register base) {
4047 DCHECK(out.IsX());
4048 DCHECK(base.IsX());
4049 SingleEmissionCheckScope guard(GetVIXLAssembler());
4050 __ Bind(fixup_label);
4051 __ add(out, base, Operand(/* offset placeholder */ 0));
4052}
4053
4054void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4055 vixl::aarch64::Register out,
4056 vixl::aarch64::Register base) {
4057 DCHECK(base.IsX());
4058 SingleEmissionCheckScope guard(GetVIXLAssembler());
4059 __ Bind(fixup_label);
4060 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4061}
4062
4063template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4064inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4065 const ArenaDeque<PcRelativePatchInfo>& infos,
4066 ArenaVector<LinkerPatch>* linker_patches) {
4067 for (const PcRelativePatchInfo& info : infos) {
4068 linker_patches->push_back(Factory(info.label.GetLocation(),
4069 &info.target_dex_file,
4070 info.pc_insn_label->GetLocation(),
4071 info.offset_or_index));
4072 }
4073}
4074
Vladimir Marko58155012015-08-19 12:49:41 +00004075void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4076 DCHECK(linker_patches->empty());
4077 size_t size =
4078 method_patches_.size() +
4079 call_patches_.size() +
4080 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004081 pc_relative_dex_cache_patches_.size() +
4082 boot_image_string_patches_.size() +
4083 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004084 boot_image_type_patches_.size() +
4085 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004086 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004087 linker_patches->reserve(size);
4088 for (const auto& entry : method_patches_) {
4089 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004090 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
4091 linker_patches->push_back(LinkerPatch::MethodPatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00004092 target_method.dex_file,
4093 target_method.dex_method_index));
4094 }
4095 for (const auto& entry : call_patches_) {
4096 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004097 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
4098 linker_patches->push_back(LinkerPatch::CodePatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00004099 target_method.dex_file,
4100 target_method.dex_method_index));
4101 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004102 for (const PatchInfo<vixl::aarch64::Label>& info : relative_call_patches_) {
4103 linker_patches->push_back(
4104 LinkerPatch::RelativeCodePatch(info.label.GetLocation(), &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004105 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004106 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004107 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004108 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004109 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004110 info.offset_or_index));
4111 }
4112 for (const auto& entry : boot_image_string_patches_) {
4113 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004114 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4115 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004116 target_string.dex_file,
4117 target_string.string_index));
4118 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004119 if (!GetCompilerOptions().IsBootImage()) {
4120 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4121 linker_patches);
4122 } else {
4123 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4124 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004125 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004126 for (const auto& entry : boot_image_type_patches_) {
4127 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004128 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4129 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004130 target_type.dex_file,
4131 target_type.type_index));
4132 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004133 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4134 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004135 for (const auto& entry : boot_image_address_patches_) {
4136 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004137 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4138 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00004139 }
4140}
4141
Scott Wakeling97c72b72016-06-24 16:19:36 +01004142vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004143 Uint32ToLiteralMap* map) {
4144 return map->GetOrCreate(
4145 value,
4146 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4147}
4148
Scott Wakeling97c72b72016-06-24 16:19:36 +01004149vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004150 return uint64_literals_.GetOrCreate(
4151 value,
4152 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004153}
4154
Scott Wakeling97c72b72016-06-24 16:19:36 +01004155vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004156 MethodReference target_method,
4157 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004158 return map->GetOrCreate(
4159 target_method,
4160 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004161}
4162
Scott Wakeling97c72b72016-06-24 16:19:36 +01004163vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004164 MethodReference target_method) {
4165 return DeduplicateMethodLiteral(target_method, &method_patches_);
4166}
4167
Scott Wakeling97c72b72016-06-24 16:19:36 +01004168vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004169 MethodReference target_method) {
4170 return DeduplicateMethodLiteral(target_method, &call_patches_);
4171}
4172
4173
Andreas Gampe878d58c2015-01-15 23:24:00 -08004174void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004175 // Explicit clinit checks triggered by static invokes must have been pruned by
4176 // art::PrepareForRegisterAllocation.
4177 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004178
Andreas Gampe878d58c2015-01-15 23:24:00 -08004179 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4180 return;
4181 }
4182
Alexandre Ramesd921d642015-04-16 15:07:16 +01004183 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004184 LocationSummary* locations = invoke->GetLocations();
4185 codegen_->GenerateStaticOrDirectCall(
4186 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004187 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004188}
4189
4190void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004191 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4192 return;
4193 }
4194
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004195 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004196 DCHECK(!codegen_->IsLeafMethod());
4197 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4198}
4199
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004200HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4201 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004202 switch (desired_class_load_kind) {
4203 case HLoadClass::LoadKind::kReferrersClass:
4204 break;
4205 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4206 DCHECK(!GetCompilerOptions().GetCompilePic());
4207 break;
4208 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4209 DCHECK(GetCompilerOptions().GetCompilePic());
4210 break;
4211 case HLoadClass::LoadKind::kBootImageAddress:
4212 break;
4213 case HLoadClass::LoadKind::kDexCacheAddress:
4214 DCHECK(Runtime::Current()->UseJitCompilation());
4215 break;
4216 case HLoadClass::LoadKind::kDexCachePcRelative:
4217 DCHECK(!Runtime::Current()->UseJitCompilation());
4218 break;
4219 case HLoadClass::LoadKind::kDexCacheViaMethod:
4220 break;
4221 }
4222 return desired_class_load_kind;
4223}
4224
Alexandre Rames67555f72014-11-18 10:55:16 +00004225void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004226 if (cls->NeedsAccessCheck()) {
4227 InvokeRuntimeCallingConvention calling_convention;
4228 CodeGenerator::CreateLoadClassLocationSummary(
4229 cls,
4230 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01004231 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004232 /* code_generator_supports_read_barrier */ true);
4233 return;
4234 }
4235
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004236 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4237 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004238 ? LocationSummary::kCallOnSlowPath
4239 : LocationSummary::kNoCall;
4240 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004241 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004242 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004243 }
4244
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004245 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4246 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
4247 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4248 locations->SetInAt(0, Location::RequiresRegister());
4249 }
4250 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004251}
4252
4253void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004254 if (cls->NeedsAccessCheck()) {
4255 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004256 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004257 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01004258 return;
4259 }
4260
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004261 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004262 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004263
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004264 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004265 bool generate_null_check = false;
4266 switch (cls->GetLoadKind()) {
4267 case HLoadClass::LoadKind::kReferrersClass: {
4268 DCHECK(!cls->CanCallRuntime());
4269 DCHECK(!cls->MustGenerateClinitCheck());
4270 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4271 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004272 GenerateGcRootFieldLoad(cls,
4273 out_loc,
4274 current_method,
4275 ArtMethod::DeclaringClassOffset().Int32Value(),
4276 /*fixup_label*/ nullptr,
4277 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004278 break;
4279 }
4280 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004281 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004282 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4283 cls->GetTypeIndex()));
4284 break;
4285 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004286 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004287 // Add ADRP with its PC-relative type patch.
4288 const DexFile& dex_file = cls->GetDexFile();
4289 uint32_t type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004290 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004291 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004292 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004293 vixl::aarch64::Label* add_label =
4294 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004295 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004296 break;
4297 }
4298 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004299 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004300 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4301 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4302 break;
4303 }
4304 case HLoadClass::LoadKind::kDexCacheAddress: {
4305 DCHECK_NE(cls->GetAddress(), 0u);
4306 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4307 // that gives a 16KiB range. To try and reduce the number of literals if we load
4308 // multiple types, simply split the dex cache address to a 16KiB aligned base
4309 // loaded from a literal and the remaining offset embedded in the load.
4310 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4311 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4312 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4313 uint64_t base_address = cls->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4314 uint32_t offset = cls->GetAddress() & MaxInt<uint64_t>(offset_bits);
4315 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4316 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004317 GenerateGcRootFieldLoad(cls,
4318 out_loc,
4319 out.X(),
4320 offset,
4321 /*fixup_label*/ nullptr,
4322 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004323 generate_null_check = !cls->IsInDexCache();
4324 break;
4325 }
4326 case HLoadClass::LoadKind::kDexCachePcRelative: {
4327 // Add ADRP with its PC-relative DexCache access patch.
4328 const DexFile& dex_file = cls->GetDexFile();
4329 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004330 vixl::aarch64::Label* adrp_label =
4331 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004332 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004333 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004334 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004335 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4336 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004337 GenerateGcRootFieldLoad(cls,
4338 out_loc,
4339 out.X(),
4340 /* offset placeholder */ 0,
4341 ldr_label,
4342 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004343 generate_null_check = !cls->IsInDexCache();
4344 break;
4345 }
4346 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4347 MemberOffset resolved_types_offset =
4348 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4349 // /* GcRoot<mirror::Class>[] */ out =
4350 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4351 Register current_method = InputRegisterAt(cls, 0);
4352 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4353 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004354 GenerateGcRootFieldLoad(cls,
4355 out_loc,
4356 out.X(),
4357 CodeGenerator::GetCacheOffset(cls->GetTypeIndex()),
4358 /*fixup_label*/ nullptr,
4359 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004360 generate_null_check = !cls->IsInDexCache();
4361 break;
4362 }
4363 }
4364
4365 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4366 DCHECK(cls->CanCallRuntime());
4367 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4368 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4369 codegen_->AddSlowPath(slow_path);
4370 if (generate_null_check) {
4371 __ Cbz(out, slow_path->GetEntryLabel());
4372 }
4373 if (cls->MustGenerateClinitCheck()) {
4374 GenerateClassInitializationCheck(slow_path, out);
4375 } else {
4376 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004377 }
4378 }
4379}
4380
David Brazdilcb1c0552015-08-04 16:22:25 +01004381static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004382 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004383}
4384
Alexandre Rames67555f72014-11-18 10:55:16 +00004385void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4386 LocationSummary* locations =
4387 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4388 locations->SetOut(Location::RequiresRegister());
4389}
4390
4391void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004392 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4393}
4394
4395void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4396 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4397}
4398
4399void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4400 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004401}
4402
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004403HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4404 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004405 switch (desired_string_load_kind) {
4406 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4407 DCHECK(!GetCompilerOptions().GetCompilePic());
4408 break;
4409 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4410 DCHECK(GetCompilerOptions().GetCompilePic());
4411 break;
4412 case HLoadString::LoadKind::kBootImageAddress:
4413 break;
4414 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01004415 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004416 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004417 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004418 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004419 break;
4420 case HLoadString::LoadKind::kDexCacheViaMethod:
4421 break;
4422 }
4423 return desired_string_load_kind;
4424}
4425
Alexandre Rames67555f72014-11-18 10:55:16 +00004426void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004427 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
Vladimir Markoaad75c62016-10-03 08:46:48 +00004428 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
4429 ? LocationSummary::kCallOnMainOnly
4430 : LocationSummary::kCallOnSlowPath)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004431 : LocationSummary::kNoCall;
4432 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004433 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004434 InvokeRuntimeCallingConvention calling_convention;
4435 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4436 } else {
4437 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004438 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4439 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4440 // Rely on the pResolveString and/or marking to save everything, including temps.
4441 RegisterSet caller_saves = RegisterSet::Empty();
4442 InvokeRuntimeCallingConvention calling_convention;
4443 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4444 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4445 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4446 Primitive::kPrimNot).GetCode());
4447 locations->SetCustomSlowPathCallerSaves(caller_saves);
4448 } else {
4449 // For non-Baker read barrier we have a temp-clobbering call.
4450 }
4451 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004452 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004453}
4454
4455void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004456 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004457
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004458 switch (load->GetLoadKind()) {
4459 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004460 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4461 load->GetStringIndex()));
4462 return; // No dex cache slow path.
4463 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004464 // Add ADRP with its PC-relative String patch.
4465 const DexFile& dex_file = load->GetDexFile();
4466 uint32_t string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004467 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004468 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004469 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004470 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004471 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004472 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004473 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004474 return; // No dex cache slow path.
4475 }
4476 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004477 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4478 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4479 return; // No dex cache slow path.
4480 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004481 case HLoadString::LoadKind::kBssEntry: {
4482 // Add ADRP with its PC-relative String .bss entry patch.
4483 const DexFile& dex_file = load->GetDexFile();
4484 uint32_t string_index = load->GetStringIndex();
4485 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004486 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4487 Register temp = temps.AcquireX();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004488 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004489 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004490 // Add LDR with its PC-relative String patch.
4491 vixl::aarch64::Label* ldr_label =
4492 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
4493 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4494 GenerateGcRootFieldLoad(load,
4495 load->GetLocations()->Out(),
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004496 temp,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004497 /* placeholder */ 0u,
4498 ldr_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004499 SlowPathCodeARM64* slow_path =
4500 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004501 codegen_->AddSlowPath(slow_path);
4502 __ Cbz(out.X(), slow_path->GetEntryLabel());
4503 __ Bind(slow_path->GetExitLabel());
4504 return;
4505 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004506 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004507 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004508 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004509
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004510 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004511 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004512 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004513 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex());
4514 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4515 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004516}
4517
Alexandre Rames5319def2014-10-23 10:03:10 +01004518void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4519 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4520 locations->SetOut(Location::ConstantLocation(constant));
4521}
4522
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004523void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004524 // Will be generated at use site.
4525}
4526
Alexandre Rames67555f72014-11-18 10:55:16 +00004527void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4528 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004529 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004530 InvokeRuntimeCallingConvention calling_convention;
4531 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4532}
4533
4534void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004535 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject: kQuickUnlockObject,
4536 instruction,
4537 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004538 if (instruction->IsEnter()) {
4539 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4540 } else {
4541 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4542 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004543}
4544
Alexandre Rames42d641b2014-10-27 14:00:51 +00004545void LocationsBuilderARM64::VisitMul(HMul* mul) {
4546 LocationSummary* locations =
4547 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4548 switch (mul->GetResultType()) {
4549 case Primitive::kPrimInt:
4550 case Primitive::kPrimLong:
4551 locations->SetInAt(0, Location::RequiresRegister());
4552 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004553 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004554 break;
4555
4556 case Primitive::kPrimFloat:
4557 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004558 locations->SetInAt(0, Location::RequiresFpuRegister());
4559 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004560 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004561 break;
4562
4563 default:
4564 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4565 }
4566}
4567
4568void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4569 switch (mul->GetResultType()) {
4570 case Primitive::kPrimInt:
4571 case Primitive::kPrimLong:
4572 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4573 break;
4574
4575 case Primitive::kPrimFloat:
4576 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004577 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004578 break;
4579
4580 default:
4581 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4582 }
4583}
4584
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004585void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4586 LocationSummary* locations =
4587 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4588 switch (neg->GetResultType()) {
4589 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004590 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004591 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004592 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004593 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004594
4595 case Primitive::kPrimFloat:
4596 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004597 locations->SetInAt(0, Location::RequiresFpuRegister());
4598 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004599 break;
4600
4601 default:
4602 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4603 }
4604}
4605
4606void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4607 switch (neg->GetResultType()) {
4608 case Primitive::kPrimInt:
4609 case Primitive::kPrimLong:
4610 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4611 break;
4612
4613 case Primitive::kPrimFloat:
4614 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004615 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004616 break;
4617
4618 default:
4619 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4620 }
4621}
4622
4623void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4624 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004625 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004626 InvokeRuntimeCallingConvention calling_convention;
4627 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004628 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004629 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004630 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004631}
4632
4633void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4634 LocationSummary* locations = instruction->GetLocations();
4635 InvokeRuntimeCallingConvention calling_convention;
4636 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4637 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004638 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004639 // Note: if heap poisoning is enabled, the entry point takes cares
4640 // of poisoning the reference.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004641 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004642 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004643}
4644
Alexandre Rames5319def2014-10-23 10:03:10 +01004645void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4646 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004647 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004648 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004649 if (instruction->IsStringAlloc()) {
4650 locations->AddTemp(LocationFrom(kArtMethodRegister));
4651 } else {
4652 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4653 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4654 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004655 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4656}
4657
4658void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004659 // Note: if heap poisoning is enabled, the entry point takes cares
4660 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004661 if (instruction->IsStringAlloc()) {
4662 // String is allocated through StringFactory. Call NewEmptyString entry point.
4663 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004664 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004665 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4666 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4667 __ Blr(lr);
4668 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4669 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004670 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004671 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4672 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004673}
4674
4675void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4676 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004677 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004678 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004679}
4680
4681void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004682 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004683 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004684 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004685 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004686 break;
4687
4688 default:
4689 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4690 }
4691}
4692
David Brazdil66d126e2015-04-03 16:02:44 +01004693void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4694 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4695 locations->SetInAt(0, Location::RequiresRegister());
4696 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4697}
4698
4699void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004700 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004701}
4702
Alexandre Rames5319def2014-10-23 10:03:10 +01004703void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004704 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4705 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01004706}
4707
Calin Juravle2ae48182016-03-16 14:05:09 +00004708void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4709 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004710 return;
4711 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004712
Alexandre Ramesd921d642015-04-16 15:07:16 +01004713 BlockPoolsScope block_pools(GetVIXLAssembler());
4714 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004715 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004716 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004717}
4718
Calin Juravle2ae48182016-03-16 14:05:09 +00004719void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004720 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004721 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004722
4723 LocationSummary* locations = instruction->GetLocations();
4724 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004725
4726 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004727}
4728
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004729void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004730 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004731}
4732
Alexandre Rames67555f72014-11-18 10:55:16 +00004733void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4734 HandleBinaryOp(instruction);
4735}
4736
4737void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4738 HandleBinaryOp(instruction);
4739}
4740
Alexandre Rames3e69f162014-12-10 10:36:50 +00004741void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4742 LOG(FATAL) << "Unreachable";
4743}
4744
4745void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4746 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4747}
4748
Alexandre Rames5319def2014-10-23 10:03:10 +01004749void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4750 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4751 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4752 if (location.IsStackSlot()) {
4753 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4754 } else if (location.IsDoubleStackSlot()) {
4755 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4756 }
4757 locations->SetOut(location);
4758}
4759
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004760void InstructionCodeGeneratorARM64::VisitParameterValue(
4761 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004762 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004763}
4764
4765void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4766 LocationSummary* locations =
4767 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004768 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004769}
4770
4771void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4772 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4773 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004774}
4775
4776void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4777 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004778 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004779 locations->SetInAt(i, Location::Any());
4780 }
4781 locations->SetOut(Location::Any());
4782}
4783
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004784void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004785 LOG(FATAL) << "Unreachable";
4786}
4787
Serban Constantinescu02164b32014-11-13 14:05:07 +00004788void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004789 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004790 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004791 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4792 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004793 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4794
4795 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004796 case Primitive::kPrimInt:
4797 case Primitive::kPrimLong:
4798 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004799 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004800 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4801 break;
4802
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004803 case Primitive::kPrimFloat:
4804 case Primitive::kPrimDouble: {
4805 InvokeRuntimeCallingConvention calling_convention;
4806 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4807 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4808 locations->SetOut(calling_convention.GetReturnLocation(type));
4809
4810 break;
4811 }
4812
Serban Constantinescu02164b32014-11-13 14:05:07 +00004813 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004814 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004815 }
4816}
4817
4818void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4819 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004820
Serban Constantinescu02164b32014-11-13 14:05:07 +00004821 switch (type) {
4822 case Primitive::kPrimInt:
4823 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004824 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004825 break;
4826 }
4827
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004828 case Primitive::kPrimFloat:
4829 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004830 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4831 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004832 if (type == Primitive::kPrimFloat) {
4833 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4834 } else {
4835 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4836 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004837 break;
4838 }
4839
Serban Constantinescu02164b32014-11-13 14:05:07 +00004840 default:
4841 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004842 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004843 }
4844}
4845
Calin Juravle27df7582015-04-17 19:12:31 +01004846void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4847 memory_barrier->SetLocations(nullptr);
4848}
4849
4850void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004851 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004852}
4853
Alexandre Rames5319def2014-10-23 10:03:10 +01004854void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4855 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4856 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004857 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004858}
4859
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004860void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004861 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004862}
4863
4864void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4865 instruction->SetLocations(nullptr);
4866}
4867
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004868void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004869 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004870}
4871
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004872void LocationsBuilderARM64::VisitRor(HRor* ror) {
4873 HandleBinaryOp(ror);
4874}
4875
4876void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4877 HandleBinaryOp(ror);
4878}
4879
Serban Constantinescu02164b32014-11-13 14:05:07 +00004880void LocationsBuilderARM64::VisitShl(HShl* shl) {
4881 HandleShift(shl);
4882}
4883
4884void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4885 HandleShift(shl);
4886}
4887
4888void LocationsBuilderARM64::VisitShr(HShr* shr) {
4889 HandleShift(shr);
4890}
4891
4892void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4893 HandleShift(shr);
4894}
4895
Alexandre Rames5319def2014-10-23 10:03:10 +01004896void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004897 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004898}
4899
4900void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004901 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004902}
4903
Alexandre Rames67555f72014-11-18 10:55:16 +00004904void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004905 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004906}
4907
4908void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004909 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004910}
4911
4912void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004913 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004914}
4915
Alexandre Rames67555f72014-11-18 10:55:16 +00004916void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004917 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004918}
4919
Calin Juravlee460d1d2015-09-29 04:52:17 +01004920void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4921 HUnresolvedInstanceFieldGet* instruction) {
4922 FieldAccessCallingConventionARM64 calling_convention;
4923 codegen_->CreateUnresolvedFieldLocationSummary(
4924 instruction, instruction->GetFieldType(), calling_convention);
4925}
4926
4927void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4928 HUnresolvedInstanceFieldGet* instruction) {
4929 FieldAccessCallingConventionARM64 calling_convention;
4930 codegen_->GenerateUnresolvedFieldAccess(instruction,
4931 instruction->GetFieldType(),
4932 instruction->GetFieldIndex(),
4933 instruction->GetDexPc(),
4934 calling_convention);
4935}
4936
4937void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4938 HUnresolvedInstanceFieldSet* instruction) {
4939 FieldAccessCallingConventionARM64 calling_convention;
4940 codegen_->CreateUnresolvedFieldLocationSummary(
4941 instruction, instruction->GetFieldType(), calling_convention);
4942}
4943
4944void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4945 HUnresolvedInstanceFieldSet* instruction) {
4946 FieldAccessCallingConventionARM64 calling_convention;
4947 codegen_->GenerateUnresolvedFieldAccess(instruction,
4948 instruction->GetFieldType(),
4949 instruction->GetFieldIndex(),
4950 instruction->GetDexPc(),
4951 calling_convention);
4952}
4953
4954void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4955 HUnresolvedStaticFieldGet* instruction) {
4956 FieldAccessCallingConventionARM64 calling_convention;
4957 codegen_->CreateUnresolvedFieldLocationSummary(
4958 instruction, instruction->GetFieldType(), calling_convention);
4959}
4960
4961void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4962 HUnresolvedStaticFieldGet* instruction) {
4963 FieldAccessCallingConventionARM64 calling_convention;
4964 codegen_->GenerateUnresolvedFieldAccess(instruction,
4965 instruction->GetFieldType(),
4966 instruction->GetFieldIndex(),
4967 instruction->GetDexPc(),
4968 calling_convention);
4969}
4970
4971void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4972 HUnresolvedStaticFieldSet* instruction) {
4973 FieldAccessCallingConventionARM64 calling_convention;
4974 codegen_->CreateUnresolvedFieldLocationSummary(
4975 instruction, instruction->GetFieldType(), calling_convention);
4976}
4977
4978void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4979 HUnresolvedStaticFieldSet* instruction) {
4980 FieldAccessCallingConventionARM64 calling_convention;
4981 codegen_->GenerateUnresolvedFieldAccess(instruction,
4982 instruction->GetFieldType(),
4983 instruction->GetFieldIndex(),
4984 instruction->GetDexPc(),
4985 calling_convention);
4986}
4987
Alexandre Rames5319def2014-10-23 10:03:10 +01004988void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01004989 LocationSummary* locations =
4990 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004991 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01004992}
4993
4994void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004995 HBasicBlock* block = instruction->GetBlock();
4996 if (block->GetLoopInformation() != nullptr) {
4997 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4998 // The back edge will generate the suspend check.
4999 return;
5000 }
5001 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5002 // The goto will generate the suspend check.
5003 return;
5004 }
5005 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005006}
5007
Alexandre Rames67555f72014-11-18 10:55:16 +00005008void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5009 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005010 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005011 InvokeRuntimeCallingConvention calling_convention;
5012 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5013}
5014
5015void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005016 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005017 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005018}
5019
5020void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5021 LocationSummary* locations =
5022 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5023 Primitive::Type input_type = conversion->GetInputType();
5024 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005025 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005026 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5027 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5028 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5029 }
5030
Alexandre Rames542361f2015-01-29 16:57:31 +00005031 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005032 locations->SetInAt(0, Location::RequiresFpuRegister());
5033 } else {
5034 locations->SetInAt(0, Location::RequiresRegister());
5035 }
5036
Alexandre Rames542361f2015-01-29 16:57:31 +00005037 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005038 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5039 } else {
5040 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5041 }
5042}
5043
5044void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5045 Primitive::Type result_type = conversion->GetResultType();
5046 Primitive::Type input_type = conversion->GetInputType();
5047
5048 DCHECK_NE(input_type, result_type);
5049
Alexandre Rames542361f2015-01-29 16:57:31 +00005050 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005051 int result_size = Primitive::ComponentSize(result_type);
5052 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005053 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005054 Register output = OutputRegister(conversion);
5055 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005056 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005057 // 'int' values are used directly as W registers, discarding the top
5058 // bits, so we don't need to sign-extend and can just perform a move.
5059 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5060 // top 32 bits of the target register. We theoretically could leave those
5061 // bits unchanged, but we would have to make sure that no code uses a
5062 // 32bit input value as a 64bit value assuming that the top 32 bits are
5063 // zero.
5064 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005065 } else if (result_type == Primitive::kPrimChar ||
5066 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5067 __ Ubfx(output,
5068 output.IsX() ? source.X() : source.W(),
5069 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005070 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005071 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005072 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005073 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005074 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005075 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005076 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5077 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005078 } else if (Primitive::IsFloatingPointType(result_type) &&
5079 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005080 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5081 } else {
5082 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5083 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005084 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005085}
Alexandre Rames67555f72014-11-18 10:55:16 +00005086
Serban Constantinescu02164b32014-11-13 14:05:07 +00005087void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5088 HandleShift(ushr);
5089}
5090
5091void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5092 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005093}
5094
5095void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5096 HandleBinaryOp(instruction);
5097}
5098
5099void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5100 HandleBinaryOp(instruction);
5101}
5102
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005103void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005104 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005105 LOG(FATAL) << "Unreachable";
5106}
5107
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005108void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005109 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005110 LOG(FATAL) << "Unreachable";
5111}
5112
Mark Mendellfe57faa2015-09-18 09:26:15 -04005113// Simple implementation of packed switch - generate cascaded compare/jumps.
5114void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5115 LocationSummary* locations =
5116 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5117 locations->SetInAt(0, Location::RequiresRegister());
5118}
5119
5120void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5121 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005122 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005123 Register value_reg = InputRegisterAt(switch_instr, 0);
5124 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5125
Zheng Xu3927c8b2015-11-18 17:46:25 +08005126 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005127 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005128 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5129 // make sure we don't emit it if the target may run out of range.
5130 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5131 // ranges and emit the tables only as required.
5132 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005133
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005134 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005135 // Current instruction id is an upper bound of the number of HIRs in the graph.
5136 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5137 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005138 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5139 Register temp = temps.AcquireW();
5140 __ Subs(temp, value_reg, Operand(lower_bound));
5141
Zheng Xu3927c8b2015-11-18 17:46:25 +08005142 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005143 // Jump to successors[0] if value == lower_bound.
5144 __ B(eq, codegen_->GetLabelOf(successors[0]));
5145 int32_t last_index = 0;
5146 for (; num_entries - last_index > 2; last_index += 2) {
5147 __ Subs(temp, temp, Operand(2));
5148 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5149 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5150 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5151 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5152 }
5153 if (num_entries - last_index == 2) {
5154 // The last missing case_value.
5155 __ Cmp(temp, Operand(1));
5156 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005157 }
5158
5159 // And the default for any other value.
5160 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5161 __ B(codegen_->GetLabelOf(default_block));
5162 }
5163 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005164 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005165
5166 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5167
5168 // Below instructions should use at most one blocked register. Since there are two blocked
5169 // registers, we are free to block one.
5170 Register temp_w = temps.AcquireW();
5171 Register index;
5172 // Remove the bias.
5173 if (lower_bound != 0) {
5174 index = temp_w;
5175 __ Sub(index, value_reg, Operand(lower_bound));
5176 } else {
5177 index = value_reg;
5178 }
5179
5180 // Jump to default block if index is out of the range.
5181 __ Cmp(index, Operand(num_entries));
5182 __ B(hs, codegen_->GetLabelOf(default_block));
5183
5184 // In current VIXL implementation, it won't require any blocked registers to encode the
5185 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5186 // register pressure.
5187 Register table_base = temps.AcquireX();
5188 // Load jump offset from the table.
5189 __ Adr(table_base, jump_table->GetTableStartLabel());
5190 Register jump_offset = temp_w;
5191 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5192
5193 // Jump to target block by branching to table_base(pc related) + offset.
5194 Register target_address = table_base;
5195 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5196 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005197 }
5198}
5199
Roland Levillain44015862016-01-22 11:47:17 +00005200void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
5201 Location out,
5202 uint32_t offset,
5203 Location maybe_temp) {
5204 Primitive::Type type = Primitive::kPrimNot;
5205 Register out_reg = RegisterFrom(out, type);
5206 if (kEmitCompilerReadBarrier) {
5207 Register temp_reg = RegisterFrom(maybe_temp, type);
5208 if (kUseBakerReadBarrier) {
5209 // Load with fast path based Baker's read barrier.
5210 // /* HeapReference<Object> */ out = *(out + offset)
5211 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5212 out,
5213 out_reg,
5214 offset,
5215 temp_reg,
5216 /* needs_null_check */ false,
5217 /* use_load_acquire */ false);
5218 } else {
5219 // Load with slow path based read barrier.
5220 // Save the value of `out` into `maybe_temp` before overwriting it
5221 // in the following move operation, as we will need it for the
5222 // read barrier below.
5223 __ Mov(temp_reg, out_reg);
5224 // /* HeapReference<Object> */ out = *(out + offset)
5225 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5226 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5227 }
5228 } else {
5229 // Plain load with no read barrier.
5230 // /* HeapReference<Object> */ out = *(out + offset)
5231 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5232 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5233 }
5234}
5235
5236void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
5237 Location out,
5238 Location obj,
5239 uint32_t offset,
5240 Location maybe_temp) {
5241 Primitive::Type type = Primitive::kPrimNot;
5242 Register out_reg = RegisterFrom(out, type);
5243 Register obj_reg = RegisterFrom(obj, type);
5244 if (kEmitCompilerReadBarrier) {
5245 if (kUseBakerReadBarrier) {
5246 // Load with fast path based Baker's read barrier.
5247 Register temp_reg = RegisterFrom(maybe_temp, type);
5248 // /* HeapReference<Object> */ out = *(obj + offset)
5249 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5250 out,
5251 obj_reg,
5252 offset,
5253 temp_reg,
5254 /* needs_null_check */ false,
5255 /* use_load_acquire */ false);
5256 } else {
5257 // Load with slow path based read barrier.
5258 // /* HeapReference<Object> */ out = *(obj + offset)
5259 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5260 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5261 }
5262 } else {
5263 // Plain load with no read barrier.
5264 // /* HeapReference<Object> */ out = *(obj + offset)
5265 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5266 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5267 }
5268}
5269
5270void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(HInstruction* instruction,
5271 Location root,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005272 Register obj,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005273 uint32_t offset,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005274 vixl::aarch64::Label* fixup_label,
5275 bool requires_read_barrier) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005276 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005277 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005278 if (requires_read_barrier) {
5279 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005280 if (kUseBakerReadBarrier) {
5281 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5282 // Baker's read barrier are used:
5283 //
5284 // root = obj.field;
5285 // if (Thread::Current()->GetIsGcMarking()) {
5286 // root = ReadBarrier::Mark(root)
5287 // }
5288
5289 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005290 if (fixup_label == nullptr) {
5291 __ Ldr(root_reg, MemOperand(obj, offset));
5292 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005293 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005294 }
Roland Levillain44015862016-01-22 11:47:17 +00005295 static_assert(
5296 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5297 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5298 "have different sizes.");
5299 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5300 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5301 "have different sizes.");
5302
Vladimir Marko953437b2016-08-24 08:30:46 +00005303 // Slow path marking the GC root `root`.
Roland Levillain44015862016-01-22 11:47:17 +00005304 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005305 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root);
Roland Levillain44015862016-01-22 11:47:17 +00005306 codegen_->AddSlowPath(slow_path);
5307
5308 MacroAssembler* masm = GetVIXLAssembler();
5309 UseScratchRegisterScope temps(masm);
5310 Register temp = temps.AcquireW();
5311 // temp = Thread::Current()->GetIsGcMarking()
Andreas Gampe542451c2016-07-26 09:02:02 -07005312 __ Ldr(temp, MemOperand(tr, Thread::IsGcMarkingOffset<kArm64PointerSize>().Int32Value()));
Roland Levillain44015862016-01-22 11:47:17 +00005313 __ Cbnz(temp, slow_path->GetEntryLabel());
5314 __ Bind(slow_path->GetExitLabel());
5315 } else {
5316 // GC root loaded through a slow path for read barriers other
5317 // than Baker's.
5318 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005319 if (fixup_label == nullptr) {
5320 __ Add(root_reg.X(), obj.X(), offset);
5321 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005322 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005323 }
Roland Levillain44015862016-01-22 11:47:17 +00005324 // /* mirror::Object* */ root = root->Read()
5325 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5326 }
5327 } else {
5328 // Plain GC root load with no read barrier.
5329 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005330 if (fixup_label == nullptr) {
5331 __ Ldr(root_reg, MemOperand(obj, offset));
5332 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005333 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005334 }
Roland Levillain44015862016-01-22 11:47:17 +00005335 // Note that GC roots are not affected by heap poisoning, thus we
5336 // do not have to unpoison `root_reg` here.
5337 }
5338}
5339
5340void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5341 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005342 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005343 uint32_t offset,
5344 Register temp,
5345 bool needs_null_check,
5346 bool use_load_acquire) {
5347 DCHECK(kEmitCompilerReadBarrier);
5348 DCHECK(kUseBakerReadBarrier);
5349
5350 // /* HeapReference<Object> */ ref = *(obj + offset)
5351 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005352 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01005353 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5354 ref,
5355 obj,
5356 offset,
5357 no_index,
5358 no_scale_factor,
5359 temp,
5360 needs_null_check,
5361 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005362}
5363
5364void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5365 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005366 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005367 uint32_t data_offset,
5368 Location index,
5369 Register temp,
5370 bool needs_null_check) {
5371 DCHECK(kEmitCompilerReadBarrier);
5372 DCHECK(kUseBakerReadBarrier);
5373
5374 // Array cells are never volatile variables, therefore array loads
5375 // never use Load-Acquire instructions on ARM64.
5376 const bool use_load_acquire = false;
5377
Roland Levillainbfea3352016-06-23 13:48:47 +01005378 static_assert(
5379 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5380 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005381 // /* HeapReference<Object> */ ref =
5382 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005383 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5384 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5385 ref,
5386 obj,
5387 data_offset,
5388 index,
5389 scale_factor,
5390 temp,
5391 needs_null_check,
5392 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005393}
5394
5395void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5396 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005397 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005398 uint32_t offset,
5399 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005400 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005401 Register temp,
5402 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005403 bool use_load_acquire,
5404 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00005405 DCHECK(kEmitCompilerReadBarrier);
5406 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005407 // If we are emitting an array load, we should not be using a
5408 // Load Acquire instruction. In other words:
5409 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5410 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005411
5412 MacroAssembler* masm = GetVIXLAssembler();
5413 UseScratchRegisterScope temps(masm);
5414
5415 // In slow path based read barriers, the read barrier call is
5416 // inserted after the original load. However, in fast path based
5417 // Baker's read barriers, we need to perform the load of
5418 // mirror::Object::monitor_ *before* the original reference load.
5419 // This load-load ordering is required by the read barrier.
5420 // The fast path/slow path (for Baker's algorithm) should look like:
5421 //
5422 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5423 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5424 // HeapReference<Object> ref = *src; // Original reference load.
5425 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
5426 // if (is_gray) {
5427 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5428 // }
5429 //
5430 // Note: the original implementation in ReadBarrier::Barrier is
5431 // slightly more complex as it performs additional checks that we do
5432 // not do here for performance reasons.
5433
5434 Primitive::Type type = Primitive::kPrimNot;
5435 Register ref_reg = RegisterFrom(ref, type);
5436 DCHECK(obj.IsW());
5437 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5438
5439 // /* int32_t */ monitor = obj->monitor_
5440 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5441 if (needs_null_check) {
5442 MaybeRecordImplicitNullCheck(instruction);
5443 }
5444 // /* LockWord */ lock_word = LockWord(monitor)
5445 static_assert(sizeof(LockWord) == sizeof(int32_t),
5446 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005447
Vladimir Marko877a0332016-07-11 19:30:56 +01005448 // Introduce a dependency on the lock_word including rb_state,
5449 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005450 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005451 // `obj` is unchanged by this operation, but its value now depends
5452 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005453 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005454
5455 // The actual reference load.
5456 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005457 // Load types involving an "index": ArrayGet,
5458 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5459 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01005460 if (use_load_acquire) {
5461 // UnsafeGetObjectVolatile intrinsic case.
5462 // Register `index` is not an index in an object array, but an
5463 // offset to an object reference field within object `obj`.
5464 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5465 DCHECK(instruction->GetLocations()->Intrinsified());
5466 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5467 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005468 DCHECK_EQ(offset, 0u);
5469 DCHECK_EQ(scale_factor, 0u);
5470 DCHECK_EQ(needs_null_check, 0u);
Roland Levillainbfea3352016-06-23 13:48:47 +01005471 // /* HeapReference<Object> */ ref = *(obj + index)
5472 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5473 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005474 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005475 // ArrayGet and UnsafeGetObject intrinsics cases.
5476 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5477 if (index.IsConstant()) {
5478 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5479 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5480 } else {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005481 Register temp3 = temps.AcquireW();
5482 __ Add(temp3, obj, offset);
5483 Load(type, ref_reg, HeapOperand(temp3, XRegisterFrom(index), LSL, scale_factor));
5484 temps.Release(temp3);
Roland Levillainbfea3352016-06-23 13:48:47 +01005485 }
Roland Levillain44015862016-01-22 11:47:17 +00005486 }
Roland Levillain44015862016-01-22 11:47:17 +00005487 } else {
5488 // /* HeapReference<Object> */ ref = *(obj + offset)
5489 MemOperand field = HeapOperand(obj, offset);
5490 if (use_load_acquire) {
5491 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5492 } else {
5493 Load(type, ref_reg, field);
5494 }
5495 }
5496
5497 // Object* ref = ref_addr->AsMirrorPtr()
5498 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5499
Vladimir Marko953437b2016-08-24 08:30:46 +00005500 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005501 SlowPathCodeARM64* slow_path;
5502 if (always_update_field) {
5503 // ReadBarrierMarkAndUpdateFieldSlowPathARM64 only supports
5504 // address of the form `obj + field_offset`, where `obj` is a
5505 // register and `field_offset` is a register. Thus `offset` and
5506 // `scale_factor` above are expected to be null in this code path.
5507 DCHECK_EQ(offset, 0u);
5508 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
5509 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM64(
5510 instruction, ref, obj, /* field_offset */ index, temp);
5511 } else {
5512 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
5513 }
Roland Levillain44015862016-01-22 11:47:17 +00005514 AddSlowPath(slow_path);
5515
5516 // if (rb_state == ReadBarrier::gray_ptr_)
5517 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005518 // Given the numeric representation, it's enough to check the low bit of the rb_state.
5519 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
5520 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
5521 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
5522 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005523 __ Bind(slow_path->GetExitLabel());
5524}
5525
5526void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5527 Location out,
5528 Location ref,
5529 Location obj,
5530 uint32_t offset,
5531 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005532 DCHECK(kEmitCompilerReadBarrier);
5533
Roland Levillain44015862016-01-22 11:47:17 +00005534 // Insert a slow path based read barrier *after* the reference load.
5535 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005536 // If heap poisoning is enabled, the unpoisoning of the loaded
5537 // reference will be carried out by the runtime within the slow
5538 // path.
5539 //
5540 // Note that `ref` currently does not get unpoisoned (when heap
5541 // poisoning is enabled), which is alright as the `ref` argument is
5542 // not used by the artReadBarrierSlow entry point.
5543 //
5544 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5545 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5546 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5547 AddSlowPath(slow_path);
5548
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005549 __ B(slow_path->GetEntryLabel());
5550 __ Bind(slow_path->GetExitLabel());
5551}
5552
Roland Levillain44015862016-01-22 11:47:17 +00005553void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5554 Location out,
5555 Location ref,
5556 Location obj,
5557 uint32_t offset,
5558 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005559 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005560 // Baker's read barriers shall be handled by the fast path
5561 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5562 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005563 // If heap poisoning is enabled, unpoisoning will be taken care of
5564 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005565 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005566 } else if (kPoisonHeapReferences) {
5567 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5568 }
5569}
5570
Roland Levillain44015862016-01-22 11:47:17 +00005571void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5572 Location out,
5573 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005574 DCHECK(kEmitCompilerReadBarrier);
5575
Roland Levillain44015862016-01-22 11:47:17 +00005576 // Insert a slow path based read barrier *after* the GC root load.
5577 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005578 // Note that GC roots are not affected by heap poisoning, so we do
5579 // not need to do anything special for this here.
5580 SlowPathCodeARM64* slow_path =
5581 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5582 AddSlowPath(slow_path);
5583
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005584 __ B(slow_path->GetEntryLabel());
5585 __ Bind(slow_path->GetExitLabel());
5586}
5587
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005588void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5589 LocationSummary* locations =
5590 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5591 locations->SetInAt(0, Location::RequiresRegister());
5592 locations->SetOut(Location::RequiresRegister());
5593}
5594
5595void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5596 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005597 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005598 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005599 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005600 __ Ldr(XRegisterFrom(locations->Out()),
5601 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005602 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005603 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005604 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005605 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5606 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005607 __ Ldr(XRegisterFrom(locations->Out()),
5608 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005609 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005610}
5611
5612
5613
Alexandre Rames67555f72014-11-18 10:55:16 +00005614#undef __
5615#undef QUICK_ENTRY_POINT
5616
Alexandre Rames5319def2014-10-23 10:03:10 +01005617} // namespace arm64
5618} // namespace art