blob: cf824a14e31a234e84ca0c0c9f7ee7fa6c17df40 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Serban Constantinescu579885a2015-02-22 20:51:33 +000019#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080024#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010025#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080026#include "intrinsics.h"
27#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000030#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010031#include "thread.h"
32#include "utils/arm64/assembler_arm64.h"
33#include "utils/assembler.h"
34#include "utils/stack_checks.h"
35
Scott Wakeling97c72b72016-06-24 16:19:36 +010036using namespace vixl::aarch64; // NOLINT(build/namespaces)
Alexandre Rames5319def2014-10-23 10:03:10 +010037
38#ifdef __
39#error "ARM64 Codegen VIXL macro-assembler macro already defined."
40#endif
41
Alexandre Rames5319def2014-10-23 10:03:10 +010042namespace art {
43
Roland Levillain22ccc3a2015-11-24 13:10:05 +000044template<class MirrorType>
45class GcRoot;
46
Alexandre Rames5319def2014-10-23 10:03:10 +010047namespace arm64 {
48
Alexandre Ramesbe919d92016-08-23 18:33:36 +010049using helpers::ARM64EncodableConstantOrRegister;
50using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080051using helpers::CPURegisterFrom;
52using helpers::DRegisterFrom;
53using helpers::FPRegisterFrom;
54using helpers::HeapOperand;
55using helpers::HeapOperandFrom;
56using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010057using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080058using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080059using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010060using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080061using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010062using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080063using helpers::LocationFrom;
64using helpers::OperandFromMemOperand;
65using helpers::OutputCPURegister;
66using helpers::OutputFPRegister;
67using helpers::OutputRegister;
68using helpers::RegisterFrom;
69using helpers::StackOperandFrom;
70using helpers::VIXLRegCodeFromART;
71using helpers::WRegisterFrom;
72using helpers::XRegisterFrom;
73
Alexandre Rames5319def2014-10-23 10:03:10 +010074static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000075// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080076// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
77// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000078static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010079
Alexandre Rames5319def2014-10-23 10:03:10 +010080inline Condition ARM64Condition(IfCondition cond) {
81 switch (cond) {
82 case kCondEQ: return eq;
83 case kCondNE: return ne;
84 case kCondLT: return lt;
85 case kCondLE: return le;
86 case kCondGT: return gt;
87 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070088 case kCondB: return lo;
89 case kCondBE: return ls;
90 case kCondA: return hi;
91 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010092 }
Roland Levillain7f63c522015-07-13 15:54:55 +000093 LOG(FATAL) << "Unreachable";
94 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010095}
96
Vladimir Markod6e069b2016-01-18 11:11:01 +000097inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
98 // The ARM64 condition codes can express all the necessary branches, see the
99 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
100 // There is no dex instruction or HIR that would need the missing conditions
101 // "equal or unordered" or "not equal".
102 switch (cond) {
103 case kCondEQ: return eq;
104 case kCondNE: return ne /* unordered */;
105 case kCondLT: return gt_bias ? cc : lt /* unordered */;
106 case kCondLE: return gt_bias ? ls : le /* unordered */;
107 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
108 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
109 default:
110 LOG(FATAL) << "UNREACHABLE";
111 UNREACHABLE();
112 }
113}
114
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000115Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000116 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
117 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
118 // but we use the exact registers for clarity.
119 if (return_type == Primitive::kPrimFloat) {
120 return LocationFrom(s0);
121 } else if (return_type == Primitive::kPrimDouble) {
122 return LocationFrom(d0);
123 } else if (return_type == Primitive::kPrimLong) {
124 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100125 } else if (return_type == Primitive::kPrimVoid) {
126 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000127 } else {
128 return LocationFrom(w0);
129 }
130}
131
Alexandre Rames5319def2014-10-23 10:03:10 +0100132Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000133 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100134}
135
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100136// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
137#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700138#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100139
Zheng Xuda403092015-04-24 17:35:39 +0800140// Calculate memory accessing operand for save/restore live registers.
141static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100142 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800143 int64_t spill_offset,
144 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100145 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
146 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
147 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800148 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100149 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800150 codegen->GetNumberOfFloatingPointRegisters()));
151
Vladimir Marko804b03f2016-09-14 16:26:36 +0100152 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
153 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800154
155 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
156 UseScratchRegisterScope temps(masm);
157
158 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100159 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
160 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800161 int64_t reg_size = kXRegSizeInBytes;
162 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
163 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100164 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800165 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
166 // If the offset does not fit in the instruction's immediate field, use an alternate register
167 // to compute the base address(float point registers spill base address).
168 Register new_base = temps.AcquireSameSizeAs(base);
169 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
170 base = new_base;
171 spill_offset = -core_spill_size;
172 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
173 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
174 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
175 }
176
177 if (is_save) {
178 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
179 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
180 } else {
181 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
182 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
183 }
184}
185
186void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800187 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100188 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
189 for (uint32_t i : LowToHighBits(core_spills)) {
190 // If the register holds an object, update the stack mask.
191 if (locations->RegisterContainsObject(i)) {
192 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800193 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100194 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
195 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
196 saved_core_stack_offsets_[i] = stack_offset;
197 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800198 }
199
Vladimir Marko804b03f2016-09-14 16:26:36 +0100200 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
201 for (uint32_t i : LowToHighBits(fp_spills)) {
202 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
203 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
204 saved_fpu_stack_offsets_[i] = stack_offset;
205 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800206 }
207
Vladimir Marko804b03f2016-09-14 16:26:36 +0100208 SaveRestoreLiveRegistersHelper(codegen,
209 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800210 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
211}
212
213void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100214 SaveRestoreLiveRegistersHelper(codegen,
215 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800216 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
217}
218
Alexandre Rames5319def2014-10-23 10:03:10 +0100219class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
220 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000221 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100222
Alexandre Rames67555f72014-11-18 10:55:16 +0000223 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100224 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000225 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100226
Alexandre Rames5319def2014-10-23 10:03:10 +0100227 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000228 if (instruction_->CanThrowIntoCatchBlock()) {
229 // Live registers will be restored in the catch block if caught.
230 SaveLiveRegisters(codegen, instruction_->GetLocations());
231 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000232 // We're moving two locations to locations that could overlap, so we need a parallel
233 // move resolver.
234 InvokeRuntimeCallingConvention calling_convention;
235 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100236 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
237 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000238 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
239 ? kQuickThrowStringBounds
240 : kQuickThrowArrayBounds;
241 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100242 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800243 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100244 }
245
Alexandre Rames8158f282015-08-07 10:26:17 +0100246 bool IsFatal() const OVERRIDE { return true; }
247
Alexandre Rames9931f312015-06-19 14:47:01 +0100248 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
249
Alexandre Rames5319def2014-10-23 10:03:10 +0100250 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100251 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
252};
253
Alexandre Rames67555f72014-11-18 10:55:16 +0000254class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
255 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000256 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000257
258 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
259 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
260 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000261 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800262 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000263 }
264
Alexandre Rames8158f282015-08-07 10:26:17 +0100265 bool IsFatal() const OVERRIDE { return true; }
266
Alexandre Rames9931f312015-06-19 14:47:01 +0100267 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
268
Alexandre Rames67555f72014-11-18 10:55:16 +0000269 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000270 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
271};
272
273class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
274 public:
275 LoadClassSlowPathARM64(HLoadClass* cls,
276 HInstruction* at,
277 uint32_t dex_pc,
278 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000279 : SlowPathCodeARM64(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000280 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
281 }
282
283 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000284 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000285 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
286
287 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000288 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000289
290 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000291 dex::TypeIndex type_index = cls_->GetTypeIndex();
292 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000293 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
294 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000295 arm64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800296 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100297 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800298 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100299 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800300 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000301
302 // Move the class to the desired location.
303 Location out = locations->Out();
304 if (out.IsValid()) {
305 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000306 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000307 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000308 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000309 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000310 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
311 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
312 if (cls_ == instruction_ && cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
313 DCHECK(out.IsValid());
314 UseScratchRegisterScope temps(arm64_codegen->GetVIXLAssembler());
315 Register temp = temps.AcquireX();
316 const DexFile& dex_file = cls_->GetDexFile();
317 // TODO: Change art_quick_initialize_type/art_quick_initialize_static_storage to
318 // kSaveEverything and use a temporary for the ADRP in the fast path, so that we
319 // can avoid the ADRP here.
320 vixl::aarch64::Label* adrp_label =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000321 arm64_codegen->NewBssEntryTypePatch(dex_file, type_index);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000322 arm64_codegen->EmitAdrpPlaceholder(adrp_label, temp);
323 vixl::aarch64::Label* strp_label =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000324 arm64_codegen->NewBssEntryTypePatch(dex_file, type_index, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000325 {
326 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
327 __ Bind(strp_label);
328 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
329 MemOperand(temp, /* offset placeholder */ 0));
330 }
331 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000332 __ B(GetExitLabel());
333 }
334
Alexandre Rames9931f312015-06-19 14:47:01 +0100335 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
336
Alexandre Rames67555f72014-11-18 10:55:16 +0000337 private:
338 // The class this slow path will load.
339 HLoadClass* const cls_;
340
Alexandre Rames67555f72014-11-18 10:55:16 +0000341 // The dex PC of `at_`.
342 const uint32_t dex_pc_;
343
344 // Whether to initialize the class.
345 const bool do_clinit_;
346
347 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
348};
349
Vladimir Markoaad75c62016-10-03 08:46:48 +0000350class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
351 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100352 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
353 : SlowPathCodeARM64(instruction),
354 temp_(temp),
355 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000356
357 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
358 LocationSummary* locations = instruction_->GetLocations();
359 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
360 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
361
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100362 // temp_ is a scratch register. Make sure it's not used for saving/restoring registers.
363 UseScratchRegisterScope temps(arm64_codegen->GetVIXLAssembler());
364 temps.Exclude(temp_);
365
Vladimir Markoaad75c62016-10-03 08:46:48 +0000366 __ Bind(GetEntryLabel());
367 SaveLiveRegisters(codegen, locations);
368
369 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000370 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
371 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000372 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
373 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
374 Primitive::Type type = instruction_->GetType();
375 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
376
377 RestoreLiveRegisters(codegen, locations);
378
379 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000380 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100381 if (!kUseReadBarrier || kUseBakerReadBarrier) {
382 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
383 } else {
384 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
385 adrp_label_ = arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
386 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
387 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000388 vixl::aarch64::Label* strp_label =
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100389 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000390 {
391 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
392 __ Bind(strp_label);
393 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100394 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000395 }
396
397 __ B(GetExitLabel());
398 }
399
400 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
401
402 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100403 const Register temp_;
404 vixl::aarch64::Label* adrp_label_;
405
Vladimir Markoaad75c62016-10-03 08:46:48 +0000406 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
407};
408
Alexandre Rames5319def2014-10-23 10:03:10 +0100409class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
410 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000411 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100412
Alexandre Rames67555f72014-11-18 10:55:16 +0000413 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
414 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100415 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000416 if (instruction_->CanThrowIntoCatchBlock()) {
417 // Live registers will be restored in the catch block if caught.
418 SaveLiveRegisters(codegen, instruction_->GetLocations());
419 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000420 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
421 instruction_,
422 instruction_->GetDexPc(),
423 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800424 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100425 }
426
Alexandre Rames8158f282015-08-07 10:26:17 +0100427 bool IsFatal() const OVERRIDE { return true; }
428
Alexandre Rames9931f312015-06-19 14:47:01 +0100429 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
430
Alexandre Rames5319def2014-10-23 10:03:10 +0100431 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100432 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
433};
434
435class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
436 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100437 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000438 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100439
Alexandre Rames67555f72014-11-18 10:55:16 +0000440 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
441 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100442 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000443 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800444 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000445 if (successor_ == nullptr) {
446 __ B(GetReturnLabel());
447 } else {
448 __ B(arm64_codegen->GetLabelOf(successor_));
449 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100450 }
451
Scott Wakeling97c72b72016-06-24 16:19:36 +0100452 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100453 DCHECK(successor_ == nullptr);
454 return &return_label_;
455 }
456
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100457 HBasicBlock* GetSuccessor() const {
458 return successor_;
459 }
460
Alexandre Rames9931f312015-06-19 14:47:01 +0100461 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
462
Alexandre Rames5319def2014-10-23 10:03:10 +0100463 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100464 // If not null, the block to branch to after the suspend check.
465 HBasicBlock* const successor_;
466
467 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100468 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100469
470 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
471};
472
Alexandre Rames67555f72014-11-18 10:55:16 +0000473class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
474 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000475 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000476 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000477
478 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000479 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800480
Alexandre Rames3e69f162014-12-10 10:36:50 +0000481 DCHECK(instruction_->IsCheckCast()
482 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
483 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100484 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000485
Alexandre Rames67555f72014-11-18 10:55:16 +0000486 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000487
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000488 if (!is_fatal_) {
489 SaveLiveRegisters(codegen, locations);
490 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000491
492 // We're moving two locations to locations that could overlap, so we need a parallel
493 // move resolver.
494 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800495 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800496 LocationFrom(calling_convention.GetRegisterAt(0)),
497 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800498 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800499 LocationFrom(calling_convention.GetRegisterAt(1)),
500 Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000501 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000502 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800503 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000504 Primitive::Type ret_type = instruction_->GetType();
505 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
506 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
507 } else {
508 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800509 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
510 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000511 }
512
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000513 if (!is_fatal_) {
514 RestoreLiveRegisters(codegen, locations);
515 __ B(GetExitLabel());
516 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000517 }
518
Alexandre Rames9931f312015-06-19 14:47:01 +0100519 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100520 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100521
Alexandre Rames67555f72014-11-18 10:55:16 +0000522 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000523 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000524
Alexandre Rames67555f72014-11-18 10:55:16 +0000525 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
526};
527
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700528class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
529 public:
Aart Bik42249c32016-01-07 15:33:50 -0800530 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000531 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700532
533 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800534 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700535 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000536 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000537 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700538 }
539
Alexandre Rames9931f312015-06-19 14:47:01 +0100540 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
541
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700542 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700543 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
544};
545
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100546class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
547 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000548 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100549
550 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
551 LocationSummary* locations = instruction_->GetLocations();
552 __ Bind(GetEntryLabel());
553 SaveLiveRegisters(codegen, locations);
554
555 InvokeRuntimeCallingConvention calling_convention;
556 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
557 parallel_move.AddMove(
558 locations->InAt(0),
559 LocationFrom(calling_convention.GetRegisterAt(0)),
560 Primitive::kPrimNot,
561 nullptr);
562 parallel_move.AddMove(
563 locations->InAt(1),
564 LocationFrom(calling_convention.GetRegisterAt(1)),
565 Primitive::kPrimInt,
566 nullptr);
567 parallel_move.AddMove(
568 locations->InAt(2),
569 LocationFrom(calling_convention.GetRegisterAt(2)),
570 Primitive::kPrimNot,
571 nullptr);
572 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
573
574 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000575 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100576 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
577 RestoreLiveRegisters(codegen, locations);
578 __ B(GetExitLabel());
579 }
580
581 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
582
583 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100584 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
585};
586
Zheng Xu3927c8b2015-11-18 17:46:25 +0800587void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
588 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000589 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800590
591 // We are about to use the assembler to place literals directly. Make sure we have enough
592 // underlying code buffer and we have generated the jump table with right size.
Scott Wakelingb77051e2016-11-21 19:46:00 +0000593 vixl::CodeBufferCheckScope scope(codegen->GetVIXLAssembler(),
594 num_entries * sizeof(int32_t),
595 vixl::CodeBufferCheckScope::kReserveBufferSpace,
596 vixl::CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800597
598 __ Bind(&table_start_);
599 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
600 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100601 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800602 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100603 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800604 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
605 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
606 Literal<int32_t> literal(jump_offset);
607 __ place(&literal);
608 }
609}
610
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100611// Slow path marking an object reference `ref` during a read
612// barrier. The field `obj.field` in the object `obj` holding this
613// reference does not get updated by this slow path after marking (see
614// ReadBarrierMarkAndUpdateFieldSlowPathARM64 below for that).
615//
616// This means that after the execution of this slow path, `ref` will
617// always be up-to-date, but `obj.field` may not; i.e., after the
618// flip, `ref` will be a to-space reference, but `obj.field` will
619// probably still be a from-space reference (unless it gets updated by
620// another thread, or if another thread installed another object
621// reference (different from `ref`) in `obj.field`).
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800622// If entrypoint is a valid location it is assumed to already be holding the entrypoint. The case
623// where the entrypoint is passed in is for the GcRoot read barrier.
Roland Levillain44015862016-01-22 11:47:17 +0000624class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
625 public:
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800626 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
627 Location ref,
628 Location entrypoint = Location::NoLocation())
629 : SlowPathCodeARM64(instruction),
630 ref_(ref),
631 entrypoint_(entrypoint) {
Roland Levillain44015862016-01-22 11:47:17 +0000632 DCHECK(kEmitCompilerReadBarrier);
633 }
634
635 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
636
637 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
638 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000639 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100640 DCHECK(ref_.IsRegister()) << ref_;
641 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain44015862016-01-22 11:47:17 +0000642 DCHECK(instruction_->IsInstanceFieldGet() ||
643 instruction_->IsStaticFieldGet() ||
644 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100645 instruction_->IsArraySet() ||
Roland Levillain44015862016-01-22 11:47:17 +0000646 instruction_->IsLoadClass() ||
647 instruction_->IsLoadString() ||
648 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100649 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100650 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
651 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000652 << "Unexpected instruction in read barrier marking slow path: "
653 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000654 // The read barrier instrumentation of object ArrayGet
655 // instructions does not support the HIntermediateAddress
656 // instruction.
657 DCHECK(!(instruction_->IsArrayGet() &&
658 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain44015862016-01-22 11:47:17 +0000659
660 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100661 // No need to save live registers; it's taken care of by the
662 // entrypoint. Also, there is no need to update the stack mask,
663 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000664 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100665 DCHECK_NE(ref_.reg(), LR);
666 DCHECK_NE(ref_.reg(), WSP);
667 DCHECK_NE(ref_.reg(), WZR);
Roland Levillain0b671c02016-08-19 12:02:34 +0100668 // IP0 is used internally by the ReadBarrierMarkRegX entry point
669 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100670 DCHECK_NE(ref_.reg(), IP0);
671 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
Roland Levillain02b75802016-07-13 11:54:35 +0100672 // "Compact" slow path, saving two moves.
673 //
674 // Instead of using the standard runtime calling convention (input
675 // and output in W0):
676 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100677 // W0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100678 // W0 <- ReadBarrierMark(W0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100679 // ref <- W0
Roland Levillain02b75802016-07-13 11:54:35 +0100680 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100681 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100682 // of a dedicated entrypoint:
683 //
684 // rX <- ReadBarrierMarkRegX(rX)
685 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800686 if (entrypoint_.IsValid()) {
687 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
688 __ Blr(XRegisterFrom(entrypoint_));
689 } else {
690 // Entrypoint is not already loaded, load from the thread.
691 int32_t entry_point_offset =
692 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
693 // This runtime call does not require a stack map.
694 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
695 }
Roland Levillain44015862016-01-22 11:47:17 +0000696 __ B(GetExitLabel());
697 }
698
699 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100700 // The location (register) of the marked object reference.
701 const Location ref_;
Roland Levillain44015862016-01-22 11:47:17 +0000702
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800703 // The location of the entrypoint if it is already loaded.
704 const Location entrypoint_;
705
Roland Levillain44015862016-01-22 11:47:17 +0000706 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
707};
708
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100709// Slow path marking an object reference `ref` during a read barrier,
710// and if needed, atomically updating the field `obj.field` in the
711// object `obj` holding this reference after marking (contrary to
712// ReadBarrierMarkSlowPathARM64 above, which never tries to update
713// `obj.field`).
714//
715// This means that after the execution of this slow path, both `ref`
716// and `obj.field` will be up-to-date; i.e., after the flip, both will
717// hold the same to-space reference (unless another thread installed
718// another object reference (different from `ref`) in `obj.field`).
719class ReadBarrierMarkAndUpdateFieldSlowPathARM64 : public SlowPathCodeARM64 {
720 public:
721 ReadBarrierMarkAndUpdateFieldSlowPathARM64(HInstruction* instruction,
722 Location ref,
723 Register obj,
724 Location field_offset,
725 Register temp)
726 : SlowPathCodeARM64(instruction),
727 ref_(ref),
728 obj_(obj),
729 field_offset_(field_offset),
730 temp_(temp) {
731 DCHECK(kEmitCompilerReadBarrier);
732 }
733
734 const char* GetDescription() const OVERRIDE {
735 return "ReadBarrierMarkAndUpdateFieldSlowPathARM64";
736 }
737
738 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
739 LocationSummary* locations = instruction_->GetLocations();
740 Register ref_reg = WRegisterFrom(ref_);
741 DCHECK(locations->CanCall());
742 DCHECK(ref_.IsRegister()) << ref_;
743 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
744 // This slow path is only used by the UnsafeCASObject intrinsic.
745 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
746 << "Unexpected instruction in read barrier marking and field updating slow path: "
747 << instruction_->DebugName();
748 DCHECK(instruction_->GetLocations()->Intrinsified());
749 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
750 DCHECK(field_offset_.IsRegister()) << field_offset_;
751
752 __ Bind(GetEntryLabel());
753
754 // Save the old reference.
755 // Note that we cannot use IP to save the old reference, as IP is
756 // used internally by the ReadBarrierMarkRegX entry point, and we
757 // need the old reference after the call to that entry point.
758 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
759 __ Mov(temp_.W(), ref_reg);
760
761 // No need to save live registers; it's taken care of by the
762 // entrypoint. Also, there is no need to update the stack mask,
763 // as this runtime call will not trigger a garbage collection.
764 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
765 DCHECK_NE(ref_.reg(), LR);
766 DCHECK_NE(ref_.reg(), WSP);
767 DCHECK_NE(ref_.reg(), WZR);
768 // IP0 is used internally by the ReadBarrierMarkRegX entry point
769 // as a temporary, it cannot be the entry point's input/output.
770 DCHECK_NE(ref_.reg(), IP0);
771 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
772 // "Compact" slow path, saving two moves.
773 //
774 // Instead of using the standard runtime calling convention (input
775 // and output in W0):
776 //
777 // W0 <- ref
778 // W0 <- ReadBarrierMark(W0)
779 // ref <- W0
780 //
781 // we just use rX (the register containing `ref`) as input and output
782 // of a dedicated entrypoint:
783 //
784 // rX <- ReadBarrierMarkRegX(rX)
785 //
786 int32_t entry_point_offset =
787 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
788 // This runtime call does not require a stack map.
789 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
790
791 // If the new reference is different from the old reference,
792 // update the field in the holder (`*(obj_ + field_offset_)`).
793 //
794 // Note that this field could also hold a different object, if
795 // another thread had concurrently changed it. In that case, the
796 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
797 // (CAS) operation below would abort the CAS, leaving the field
798 // as-is.
799 vixl::aarch64::Label done;
800 __ Cmp(temp_.W(), ref_reg);
801 __ B(eq, &done);
802
803 // Update the the holder's field atomically. This may fail if
804 // mutator updates before us, but it's OK. This is achieved
805 // using a strong compare-and-set (CAS) operation with relaxed
806 // memory synchronization ordering, where the expected value is
807 // the old reference and the desired value is the new reference.
808
809 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
810 UseScratchRegisterScope temps(masm);
811
812 // Convenience aliases.
813 Register base = obj_.W();
814 Register offset = XRegisterFrom(field_offset_);
815 Register expected = temp_.W();
816 Register value = ref_reg;
817 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
818 Register tmp_value = temps.AcquireW(); // Value in memory.
819
820 __ Add(tmp_ptr, base.X(), Operand(offset));
821
822 if (kPoisonHeapReferences) {
823 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
824 if (value.Is(expected)) {
825 // Do not poison `value`, as it is the same register as
826 // `expected`, which has just been poisoned.
827 } else {
828 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
829 }
830 }
831
832 // do {
833 // tmp_value = [tmp_ptr] - expected;
834 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
835
Roland Levillain24a4d112016-10-26 13:10:46 +0100836 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100837 __ Bind(&loop_head);
838 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
839 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +0100840 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100841 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
842 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +0100843 __ B(&exit_loop);
844 __ Bind(&comparison_failed);
845 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100846 __ Bind(&exit_loop);
847
848 if (kPoisonHeapReferences) {
849 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
850 if (value.Is(expected)) {
851 // Do not unpoison `value`, as it is the same register as
852 // `expected`, which has just been unpoisoned.
853 } else {
854 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
855 }
856 }
857
858 __ Bind(&done);
859 __ B(GetExitLabel());
860 }
861
862 private:
863 // The location (register) of the marked object reference.
864 const Location ref_;
865 // The register containing the object holding the marked object reference field.
866 const Register obj_;
867 // The location of the offset of the marked reference field within `obj_`.
868 Location field_offset_;
869
870 const Register temp_;
871
872 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM64);
873};
874
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000875// Slow path generating a read barrier for a heap reference.
876class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
877 public:
878 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
879 Location out,
880 Location ref,
881 Location obj,
882 uint32_t offset,
883 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000884 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000885 out_(out),
886 ref_(ref),
887 obj_(obj),
888 offset_(offset),
889 index_(index) {
890 DCHECK(kEmitCompilerReadBarrier);
891 // If `obj` is equal to `out` or `ref`, it means the initial object
892 // has been overwritten by (or after) the heap object reference load
893 // to be instrumented, e.g.:
894 //
895 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000896 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000897 //
898 // In that case, we have lost the information about the original
899 // object, and the emitted read barrier cannot work properly.
900 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
901 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
902 }
903
904 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
905 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
906 LocationSummary* locations = instruction_->GetLocations();
907 Primitive::Type type = Primitive::kPrimNot;
908 DCHECK(locations->CanCall());
909 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100910 DCHECK(instruction_->IsInstanceFieldGet() ||
911 instruction_->IsStaticFieldGet() ||
912 instruction_->IsArrayGet() ||
913 instruction_->IsInstanceOf() ||
914 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100915 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000916 << "Unexpected instruction in read barrier for heap reference slow path: "
917 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000918 // The read barrier instrumentation of object ArrayGet
919 // instructions does not support the HIntermediateAddress
920 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000921 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100922 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000923
924 __ Bind(GetEntryLabel());
925
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000926 SaveLiveRegisters(codegen, locations);
927
928 // We may have to change the index's value, but as `index_` is a
929 // constant member (like other "inputs" of this slow path),
930 // introduce a copy of it, `index`.
931 Location index = index_;
932 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100933 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000934 if (instruction_->IsArrayGet()) {
935 // Compute the actual memory offset and store it in `index`.
936 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
937 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
938 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
939 // We are about to change the value of `index_reg` (see the
940 // calls to vixl::MacroAssembler::Lsl and
941 // vixl::MacroAssembler::Mov below), but it has
942 // not been saved by the previous call to
943 // art::SlowPathCode::SaveLiveRegisters, as it is a
944 // callee-save register --
945 // art::SlowPathCode::SaveLiveRegisters does not consider
946 // callee-save registers, as it has been designed with the
947 // assumption that callee-save registers are supposed to be
948 // handled by the called function. So, as a callee-save
949 // register, `index_reg` _would_ eventually be saved onto
950 // the stack, but it would be too late: we would have
951 // changed its value earlier. Therefore, we manually save
952 // it here into another freely available register,
953 // `free_reg`, chosen of course among the caller-save
954 // registers (as a callee-save `free_reg` register would
955 // exhibit the same problem).
956 //
957 // Note we could have requested a temporary register from
958 // the register allocator instead; but we prefer not to, as
959 // this is a slow path, and we know we can find a
960 // caller-save register that is available.
961 Register free_reg = FindAvailableCallerSaveRegister(codegen);
962 __ Mov(free_reg.W(), index_reg);
963 index_reg = free_reg;
964 index = LocationFrom(index_reg);
965 } else {
966 // The initial register stored in `index_` has already been
967 // saved in the call to art::SlowPathCode::SaveLiveRegisters
968 // (as it is not a callee-save register), so we can freely
969 // use it.
970 }
971 // Shifting the index value contained in `index_reg` by the scale
972 // factor (2) cannot overflow in practice, as the runtime is
973 // unable to allocate object arrays with a size larger than
974 // 2^26 - 1 (that is, 2^28 - 4 bytes).
975 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
976 static_assert(
977 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
978 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
979 __ Add(index_reg, index_reg, Operand(offset_));
980 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100981 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
982 // intrinsics, `index_` is not shifted by a scale factor of 2
983 // (as in the case of ArrayGet), as it is actually an offset
984 // to an object field within an object.
985 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000986 DCHECK(instruction_->GetLocations()->Intrinsified());
987 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
988 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
989 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100990 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100991 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000992 }
993 }
994
995 // We're moving two or three locations to locations that could
996 // overlap, so we need a parallel move resolver.
997 InvokeRuntimeCallingConvention calling_convention;
998 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
999 parallel_move.AddMove(ref_,
1000 LocationFrom(calling_convention.GetRegisterAt(0)),
1001 type,
1002 nullptr);
1003 parallel_move.AddMove(obj_,
1004 LocationFrom(calling_convention.GetRegisterAt(1)),
1005 type,
1006 nullptr);
1007 if (index.IsValid()) {
1008 parallel_move.AddMove(index,
1009 LocationFrom(calling_convention.GetRegisterAt(2)),
1010 Primitive::kPrimInt,
1011 nullptr);
1012 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1013 } else {
1014 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1015 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1016 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001017 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001018 instruction_,
1019 instruction_->GetDexPc(),
1020 this);
1021 CheckEntrypointTypes<
1022 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1023 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1024
1025 RestoreLiveRegisters(codegen, locations);
1026
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001027 __ B(GetExitLabel());
1028 }
1029
1030 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1031
1032 private:
1033 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001034 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1035 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001036 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1037 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1038 return Register(VIXLRegCodeFromART(i), kXRegSize);
1039 }
1040 }
1041 // We shall never fail to find a free caller-save register, as
1042 // there are more than two core caller-save registers on ARM64
1043 // (meaning it is possible to find one which is different from
1044 // `ref` and `obj`).
1045 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1046 LOG(FATAL) << "Could not find a free register";
1047 UNREACHABLE();
1048 }
1049
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001050 const Location out_;
1051 const Location ref_;
1052 const Location obj_;
1053 const uint32_t offset_;
1054 // An additional location containing an index to an array.
1055 // Only used for HArrayGet and the UnsafeGetObject &
1056 // UnsafeGetObjectVolatile intrinsics.
1057 const Location index_;
1058
1059 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1060};
1061
1062// Slow path generating a read barrier for a GC root.
1063class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1064 public:
1065 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001066 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001067 DCHECK(kEmitCompilerReadBarrier);
1068 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001069
1070 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1071 LocationSummary* locations = instruction_->GetLocations();
1072 Primitive::Type type = Primitive::kPrimNot;
1073 DCHECK(locations->CanCall());
1074 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001075 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1076 << "Unexpected instruction in read barrier for GC root slow path: "
1077 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001078
1079 __ Bind(GetEntryLabel());
1080 SaveLiveRegisters(codegen, locations);
1081
1082 InvokeRuntimeCallingConvention calling_convention;
1083 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1084 // The argument of the ReadBarrierForRootSlow is not a managed
1085 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1086 // thus we need a 64-bit move here, and we cannot use
1087 //
1088 // arm64_codegen->MoveLocation(
1089 // LocationFrom(calling_convention.GetRegisterAt(0)),
1090 // root_,
1091 // type);
1092 //
1093 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1094 // reference type (`Primitive::kPrimNot`).
1095 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001096 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001097 instruction_,
1098 instruction_->GetDexPc(),
1099 this);
1100 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1101 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1102
1103 RestoreLiveRegisters(codegen, locations);
1104 __ B(GetExitLabel());
1105 }
1106
1107 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1108
1109 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001110 const Location out_;
1111 const Location root_;
1112
1113 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1114};
1115
Alexandre Rames5319def2014-10-23 10:03:10 +01001116#undef __
1117
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001118Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001119 Location next_location;
1120 if (type == Primitive::kPrimVoid) {
1121 LOG(FATAL) << "Unreachable type " << type;
1122 }
1123
Alexandre Rames542361f2015-01-29 16:57:31 +00001124 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001125 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
1126 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +00001127 } else if (!Primitive::IsFloatingPointType(type) &&
1128 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001129 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1130 } else {
1131 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001132 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1133 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001134 }
1135
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001136 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001137 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001138 return next_location;
1139}
1140
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001141Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001142 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001143}
1144
Serban Constantinescu579885a2015-02-22 20:51:33 +00001145CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1146 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001147 const CompilerOptions& compiler_options,
1148 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001149 : CodeGenerator(graph,
1150 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001151 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001152 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001153 callee_saved_core_registers.GetList(),
1154 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001155 compiler_options,
1156 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001157 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001158 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001159 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001160 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001161 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001162 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001163 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001164 uint32_literals_(std::less<uint32_t>(),
1165 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001166 uint64_literals_(std::less<uint64_t>(),
1167 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001168 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1169 boot_image_string_patches_(StringReferenceValueComparator(),
1170 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1171 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001172 boot_image_type_patches_(TypeReferenceValueComparator(),
1173 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1174 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001175 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001176 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001177 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1178 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001179 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1180 jit_class_patches_(TypeReferenceValueComparator(),
1181 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001182 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001183 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001184}
Alexandre Rames5319def2014-10-23 10:03:10 +01001185
Alexandre Rames67555f72014-11-18 10:55:16 +00001186#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001187
Zheng Xu3927c8b2015-11-18 17:46:25 +08001188void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001189 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001190 jump_table->EmitTable(this);
1191 }
1192}
1193
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001194void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001195 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001196 // Ensure we emit the literal pool.
1197 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001198
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001199 CodeGenerator::Finalize(allocator);
1200}
1201
Zheng Xuad4450e2015-04-17 18:48:56 +08001202void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1203 // Note: There are 6 kinds of moves:
1204 // 1. constant -> GPR/FPR (non-cycle)
1205 // 2. constant -> stack (non-cycle)
1206 // 3. GPR/FPR -> GPR/FPR
1207 // 4. GPR/FPR -> stack
1208 // 5. stack -> GPR/FPR
1209 // 6. stack -> stack (non-cycle)
1210 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1211 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1212 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1213 // dependency.
1214 vixl_temps_.Open(GetVIXLAssembler());
1215}
1216
1217void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1218 vixl_temps_.Close();
1219}
1220
1221Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
1222 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
1223 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
1224 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
1225 Location scratch = GetScratchLocation(kind);
1226 if (!scratch.Equals(Location::NoLocation())) {
1227 return scratch;
1228 }
1229 // Allocate from VIXL temp registers.
1230 if (kind == Location::kRegister) {
1231 scratch = LocationFrom(vixl_temps_.AcquireX());
1232 } else {
1233 DCHECK(kind == Location::kFpuRegister);
1234 scratch = LocationFrom(vixl_temps_.AcquireD());
1235 }
1236 AddScratchLocation(scratch);
1237 return scratch;
1238}
1239
1240void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1241 if (loc.IsRegister()) {
1242 vixl_temps_.Release(XRegisterFrom(loc));
1243 } else {
1244 DCHECK(loc.IsFpuRegister());
1245 vixl_temps_.Release(DRegisterFrom(loc));
1246 }
1247 RemoveScratchLocation(loc);
1248}
1249
Alexandre Rames3e69f162014-12-10 10:36:50 +00001250void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001251 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001252 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001253}
1254
Alexandre Rames5319def2014-10-23 10:03:10 +01001255void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001256 MacroAssembler* masm = GetVIXLAssembler();
1257 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001258 __ Bind(&frame_entry_label_);
1259
Serban Constantinescu02164b32014-11-13 14:05:07 +00001260 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1261 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001262 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001263 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001264 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001265 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001266 __ Ldr(wzr, MemOperand(temp, 0));
1267 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001268 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001269
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001270 if (!HasEmptyFrame()) {
1271 int frame_size = GetFrameSize();
1272 // Stack layout:
1273 // sp[frame_size - 8] : lr.
1274 // ... : other preserved core registers.
1275 // ... : other preserved fp registers.
1276 // ... : reserved frame space.
1277 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001278
1279 // Save the current method if we need it. Note that we do not
1280 // do this in HCurrentMethod, as the instruction might have been removed
1281 // in the SSA graph.
1282 if (RequiresCurrentMethod()) {
1283 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001284 } else {
1285 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001286 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001287 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001288 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1289 frame_size - GetCoreSpillSize());
1290 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1291 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001292
1293 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1294 // Initialize should_deoptimize flag to 0.
1295 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1296 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1297 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001298 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001299}
1300
1301void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001302 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001303 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001304 if (!HasEmptyFrame()) {
1305 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001306 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1307 frame_size - FrameEntrySpillSize());
1308 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1309 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001310 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001311 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001312 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001313 __ Ret();
1314 GetAssembler()->cfi().RestoreState();
1315 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001316}
1317
Scott Wakeling97c72b72016-06-24 16:19:36 +01001318CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001319 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001320 return CPURegList(CPURegister::kRegister, kXRegSize,
1321 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001322}
1323
Scott Wakeling97c72b72016-06-24 16:19:36 +01001324CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001325 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1326 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001327 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1328 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001329}
1330
Alexandre Rames5319def2014-10-23 10:03:10 +01001331void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1332 __ Bind(GetLabelOf(block));
1333}
1334
Calin Juravle175dc732015-08-25 15:42:32 +01001335void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1336 DCHECK(location.IsRegister());
1337 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1338}
1339
Calin Juravlee460d1d2015-09-29 04:52:17 +01001340void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1341 if (location.IsRegister()) {
1342 locations->AddTemp(location);
1343 } else {
1344 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1345 }
1346}
1347
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001348void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001349 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001350 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001351 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001352 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001353 if (value_can_be_null) {
1354 __ Cbz(value, &done);
1355 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001356 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001357 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001358 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001359 if (value_can_be_null) {
1360 __ Bind(&done);
1361 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001362}
1363
David Brazdil58282f42016-01-14 12:45:10 +00001364void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001365 // Blocked core registers:
1366 // lr : Runtime reserved.
1367 // tr : Runtime reserved.
1368 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1369 // ip1 : VIXL core temp.
1370 // ip0 : VIXL core temp.
1371 //
1372 // Blocked fp registers:
1373 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001374 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1375 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001376 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001377 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001378 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001379
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001380 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001381 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001382 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001383 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001384
David Brazdil58282f42016-01-14 12:45:10 +00001385 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001386 // Stubs do not save callee-save floating point registers. If the graph
1387 // is debuggable, we need to deal with these registers differently. For
1388 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001389 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1390 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001391 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001392 }
1393 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001394}
1395
Alexandre Rames3e69f162014-12-10 10:36:50 +00001396size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1397 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1398 __ Str(reg, MemOperand(sp, stack_index));
1399 return kArm64WordSize;
1400}
1401
1402size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1403 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1404 __ Ldr(reg, MemOperand(sp, stack_index));
1405 return kArm64WordSize;
1406}
1407
1408size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1409 FPRegister reg = FPRegister(reg_id, kDRegSize);
1410 __ Str(reg, MemOperand(sp, stack_index));
1411 return kArm64WordSize;
1412}
1413
1414size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1415 FPRegister reg = FPRegister(reg_id, kDRegSize);
1416 __ Ldr(reg, MemOperand(sp, stack_index));
1417 return kArm64WordSize;
1418}
1419
Alexandre Rames5319def2014-10-23 10:03:10 +01001420void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001421 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001422}
1423
1424void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001425 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001426}
1427
Alexandre Rames67555f72014-11-18 10:55:16 +00001428void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001429 if (constant->IsIntConstant()) {
1430 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1431 } else if (constant->IsLongConstant()) {
1432 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1433 } else if (constant->IsNullConstant()) {
1434 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001435 } else if (constant->IsFloatConstant()) {
1436 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1437 } else {
1438 DCHECK(constant->IsDoubleConstant());
1439 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1440 }
1441}
1442
Alexandre Rames3e69f162014-12-10 10:36:50 +00001443
1444static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1445 DCHECK(constant.IsConstant());
1446 HConstant* cst = constant.GetConstant();
1447 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001448 // Null is mapped to a core W register, which we associate with kPrimInt.
1449 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001450 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1451 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1452 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1453}
1454
Calin Juravlee460d1d2015-09-29 04:52:17 +01001455void CodeGeneratorARM64::MoveLocation(Location destination,
1456 Location source,
1457 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001458 if (source.Equals(destination)) {
1459 return;
1460 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001461
1462 // A valid move can always be inferred from the destination and source
1463 // locations. When moving from and to a register, the argument type can be
1464 // used to generate 32bit instead of 64bit moves. In debug mode we also
1465 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001466 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001467
1468 if (destination.IsRegister() || destination.IsFpuRegister()) {
1469 if (unspecified_type) {
1470 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1471 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001472 (src_cst != nullptr && (src_cst->IsIntConstant()
1473 || src_cst->IsFloatConstant()
1474 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001475 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001476 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001477 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001478 // If the source is a double stack slot or a 64bit constant, a 64bit
1479 // type is appropriate. Else the source is a register, and since the
1480 // type has not been specified, we chose a 64bit type to force a 64bit
1481 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001482 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001483 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001484 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001485 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1486 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1487 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001488 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1489 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1490 __ Ldr(dst, StackOperandFrom(source));
1491 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001492 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001493 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001494 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001495 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001496 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001497 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001498 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001499 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1500 ? Primitive::kPrimLong
1501 : Primitive::kPrimInt;
1502 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1503 }
1504 } else {
1505 DCHECK(source.IsFpuRegister());
1506 if (destination.IsRegister()) {
1507 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1508 ? Primitive::kPrimDouble
1509 : Primitive::kPrimFloat;
1510 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1511 } else {
1512 DCHECK(destination.IsFpuRegister());
1513 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001514 }
1515 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001516 } else { // The destination is not a register. It must be a stack slot.
1517 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1518 if (source.IsRegister() || source.IsFpuRegister()) {
1519 if (unspecified_type) {
1520 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001521 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001522 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001523 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001524 }
1525 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001526 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1527 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1528 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001529 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001530 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1531 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001532 UseScratchRegisterScope temps(GetVIXLAssembler());
1533 HConstant* src_cst = source.GetConstant();
1534 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001535 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001536 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1537 ? Register(xzr)
1538 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001539 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001540 if (src_cst->IsIntConstant()) {
1541 temp = temps.AcquireW();
1542 } else if (src_cst->IsLongConstant()) {
1543 temp = temps.AcquireX();
1544 } else if (src_cst->IsFloatConstant()) {
1545 temp = temps.AcquireS();
1546 } else {
1547 DCHECK(src_cst->IsDoubleConstant());
1548 temp = temps.AcquireD();
1549 }
1550 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001551 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001552 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001553 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001554 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001555 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001556 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001557 // Use any scratch register (a core or a floating-point one)
1558 // from VIXL scratch register pools as a temporary.
1559 //
1560 // We used to only use the FP scratch register pool, but in some
1561 // rare cases the only register from this pool (D31) would
1562 // already be used (e.g. within a ParallelMove instruction, when
1563 // a move is blocked by a another move requiring a scratch FP
1564 // register, which would reserve D31). To prevent this issue, we
1565 // ask for a scratch register of any type (core or FP).
1566 CPURegister temp =
1567 temps.AcquireCPURegisterOfSize(destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001568 __ Ldr(temp, StackOperandFrom(source));
1569 __ Str(temp, StackOperandFrom(destination));
1570 }
1571 }
1572}
1573
1574void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001575 CPURegister dst,
1576 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001577 switch (type) {
1578 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001579 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001580 break;
1581 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001582 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001583 break;
1584 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001585 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001586 break;
1587 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001588 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001589 break;
1590 case Primitive::kPrimInt:
1591 case Primitive::kPrimNot:
1592 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001593 case Primitive::kPrimFloat:
1594 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001595 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001596 __ Ldr(dst, src);
1597 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001598 case Primitive::kPrimVoid:
1599 LOG(FATAL) << "Unreachable type " << type;
1600 }
1601}
1602
Calin Juravle77520bc2015-01-12 18:45:46 +00001603void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001604 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001605 const MemOperand& src,
1606 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001607 MacroAssembler* masm = GetVIXLAssembler();
1608 BlockPoolsScope block_pools(masm);
1609 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001610 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001611 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001612
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001613 DCHECK(!src.IsPreIndex());
1614 DCHECK(!src.IsPostIndex());
1615
1616 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001617 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001618 MemOperand base = MemOperand(temp_base);
1619 switch (type) {
1620 case Primitive::kPrimBoolean:
1621 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001622 if (needs_null_check) {
1623 MaybeRecordImplicitNullCheck(instruction);
1624 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001625 break;
1626 case Primitive::kPrimByte:
1627 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001628 if (needs_null_check) {
1629 MaybeRecordImplicitNullCheck(instruction);
1630 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001631 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1632 break;
1633 case Primitive::kPrimChar:
1634 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001635 if (needs_null_check) {
1636 MaybeRecordImplicitNullCheck(instruction);
1637 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001638 break;
1639 case Primitive::kPrimShort:
1640 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001641 if (needs_null_check) {
1642 MaybeRecordImplicitNullCheck(instruction);
1643 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001644 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1645 break;
1646 case Primitive::kPrimInt:
1647 case Primitive::kPrimNot:
1648 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001649 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001650 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001651 if (needs_null_check) {
1652 MaybeRecordImplicitNullCheck(instruction);
1653 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001654 break;
1655 case Primitive::kPrimFloat:
1656 case Primitive::kPrimDouble: {
1657 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001658 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001659
1660 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1661 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001662 if (needs_null_check) {
1663 MaybeRecordImplicitNullCheck(instruction);
1664 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001665 __ Fmov(FPRegister(dst), temp);
1666 break;
1667 }
1668 case Primitive::kPrimVoid:
1669 LOG(FATAL) << "Unreachable type " << type;
1670 }
1671}
1672
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001673void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001674 CPURegister src,
1675 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001676 switch (type) {
1677 case Primitive::kPrimBoolean:
1678 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001679 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001680 break;
1681 case Primitive::kPrimChar:
1682 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001683 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001684 break;
1685 case Primitive::kPrimInt:
1686 case Primitive::kPrimNot:
1687 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001688 case Primitive::kPrimFloat:
1689 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001690 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001691 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001692 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001693 case Primitive::kPrimVoid:
1694 LOG(FATAL) << "Unreachable type " << type;
1695 }
1696}
1697
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001698void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1699 CPURegister src,
1700 const MemOperand& dst) {
1701 UseScratchRegisterScope temps(GetVIXLAssembler());
1702 Register temp_base = temps.AcquireX();
1703
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001704 DCHECK(!dst.IsPreIndex());
1705 DCHECK(!dst.IsPostIndex());
1706
1707 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001708 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001709 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001710 MemOperand base = MemOperand(temp_base);
1711 switch (type) {
1712 case Primitive::kPrimBoolean:
1713 case Primitive::kPrimByte:
1714 __ Stlrb(Register(src), base);
1715 break;
1716 case Primitive::kPrimChar:
1717 case Primitive::kPrimShort:
1718 __ Stlrh(Register(src), base);
1719 break;
1720 case Primitive::kPrimInt:
1721 case Primitive::kPrimNot:
1722 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001723 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001724 __ Stlr(Register(src), base);
1725 break;
1726 case Primitive::kPrimFloat:
1727 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001728 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001729 Register temp_src;
1730 if (src.IsZero()) {
1731 // The zero register is used to avoid synthesizing zero constants.
1732 temp_src = Register(src);
1733 } else {
1734 DCHECK(src.IsFPRegister());
1735 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1736 __ Fmov(temp_src, FPRegister(src));
1737 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001738
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001739 __ Stlr(temp_src, base);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001740 break;
1741 }
1742 case Primitive::kPrimVoid:
1743 LOG(FATAL) << "Unreachable type " << type;
1744 }
1745}
1746
Calin Juravle175dc732015-08-25 15:42:32 +01001747void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1748 HInstruction* instruction,
1749 uint32_t dex_pc,
1750 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001751 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001752 GenerateInvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001753 if (EntrypointRequiresStackMap(entrypoint)) {
1754 RecordPcInfo(instruction, dex_pc, slow_path);
1755 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001756}
1757
Roland Levillaindec8f632016-07-22 17:10:06 +01001758void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1759 HInstruction* instruction,
1760 SlowPathCode* slow_path) {
1761 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001762 GenerateInvokeRuntime(entry_point_offset);
1763}
1764
1765void CodeGeneratorARM64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001766 BlockPoolsScope block_pools(GetVIXLAssembler());
1767 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1768 __ Blr(lr);
1769}
1770
Alexandre Rames67555f72014-11-18 10:55:16 +00001771void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001772 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001773 UseScratchRegisterScope temps(GetVIXLAssembler());
1774 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001775 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1776
Serban Constantinescu02164b32014-11-13 14:05:07 +00001777 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001778 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1779 __ Add(temp, class_reg, status_offset);
1780 __ Ldar(temp, HeapOperand(temp));
1781 __ Cmp(temp, mirror::Class::kStatusInitialized);
1782 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001783 __ Bind(slow_path->GetExitLabel());
1784}
Alexandre Rames5319def2014-10-23 10:03:10 +01001785
Roland Levillain44015862016-01-22 11:47:17 +00001786void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001787 BarrierType type = BarrierAll;
1788
1789 switch (kind) {
1790 case MemBarrierKind::kAnyAny:
1791 case MemBarrierKind::kAnyStore: {
1792 type = BarrierAll;
1793 break;
1794 }
1795 case MemBarrierKind::kLoadAny: {
1796 type = BarrierReads;
1797 break;
1798 }
1799 case MemBarrierKind::kStoreStore: {
1800 type = BarrierWrites;
1801 break;
1802 }
1803 default:
1804 LOG(FATAL) << "Unexpected memory barrier " << kind;
1805 }
1806 __ Dmb(InnerShareable, type);
1807}
1808
Serban Constantinescu02164b32014-11-13 14:05:07 +00001809void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1810 HBasicBlock* successor) {
1811 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001812 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1813 if (slow_path == nullptr) {
1814 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1815 instruction->SetSlowPath(slow_path);
1816 codegen_->AddSlowPath(slow_path);
1817 if (successor != nullptr) {
1818 DCHECK(successor->IsLoopHeader());
1819 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1820 }
1821 } else {
1822 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1823 }
1824
Serban Constantinescu02164b32014-11-13 14:05:07 +00001825 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1826 Register temp = temps.AcquireW();
1827
Andreas Gampe542451c2016-07-26 09:02:02 -07001828 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001829 if (successor == nullptr) {
1830 __ Cbnz(temp, slow_path->GetEntryLabel());
1831 __ Bind(slow_path->GetReturnLabel());
1832 } else {
1833 __ Cbz(temp, codegen_->GetLabelOf(successor));
1834 __ B(slow_path->GetEntryLabel());
1835 // slow_path will return to GetLabelOf(successor).
1836 }
1837}
1838
Alexandre Rames5319def2014-10-23 10:03:10 +01001839InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1840 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001841 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001842 assembler_(codegen->GetAssembler()),
1843 codegen_(codegen) {}
1844
1845#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001846 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001847
1848#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1849
1850enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001851 // Using a base helps identify when we hit such breakpoints.
1852 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001853#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1854 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1855#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1856};
1857
1858#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001859 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001860 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1861 } \
1862 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1863 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1864 locations->SetOut(Location::Any()); \
1865 }
1866 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1867#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1868
1869#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001870#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001871
Alexandre Rames67555f72014-11-18 10:55:16 +00001872void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001873 DCHECK_EQ(instr->InputCount(), 2U);
1874 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1875 Primitive::Type type = instr->GetResultType();
1876 switch (type) {
1877 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001878 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001879 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001880 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001881 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001882 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001883
1884 case Primitive::kPrimFloat:
1885 case Primitive::kPrimDouble:
1886 locations->SetInAt(0, Location::RequiresFpuRegister());
1887 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001888 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001889 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001890
Alexandre Rames5319def2014-10-23 10:03:10 +01001891 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001892 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001893 }
1894}
1895
Alexandre Rames09a99962015-04-15 11:47:56 +01001896void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001897 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1898
1899 bool object_field_get_with_read_barrier =
1900 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001901 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001902 new (GetGraph()->GetArena()) LocationSummary(instruction,
1903 object_field_get_with_read_barrier ?
1904 LocationSummary::kCallOnSlowPath :
1905 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001906 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001907 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00001908 // We need a temporary register for the read barrier marking slow
1909 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
1910 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko70e97462016-08-09 11:04:26 +01001911 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001912 locations->SetInAt(0, Location::RequiresRegister());
1913 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1914 locations->SetOut(Location::RequiresFpuRegister());
1915 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001916 // The output overlaps for an object field get when read barriers
1917 // are enabled: we do not want the load to overwrite the object's
1918 // location, as we need it to emit the read barrier.
1919 locations->SetOut(
1920 Location::RequiresRegister(),
1921 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001922 }
1923}
1924
1925void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1926 const FieldInfo& field_info) {
1927 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001928 LocationSummary* locations = instruction->GetLocations();
1929 Location base_loc = locations->InAt(0);
1930 Location out = locations->Out();
1931 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001932 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001933 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001934 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001935
Roland Levillain44015862016-01-22 11:47:17 +00001936 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1937 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00001938 // /* HeapReference<Object> */ out = *(base + offset)
1939 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
Roland Levillaind0b51832017-01-26 19:04:23 +00001940 Register temp = WRegisterFrom(locations->GetTemp(0));
Roland Levillain44015862016-01-22 11:47:17 +00001941 // Note that potential implicit null checks are handled in this
1942 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1943 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1944 instruction,
1945 out,
1946 base,
1947 offset,
1948 temp,
1949 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001950 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001951 } else {
1952 // General case.
1953 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001954 // Note that a potential implicit null check is handled in this
1955 // CodeGeneratorARM64::LoadAcquire call.
1956 // NB: LoadAcquire will record the pc info if needed.
1957 codegen_->LoadAcquire(
1958 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001959 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001960 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001961 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001962 }
Roland Levillain44015862016-01-22 11:47:17 +00001963 if (field_type == Primitive::kPrimNot) {
1964 // If read barriers are enabled, emit read barriers other than
1965 // Baker's using a slow path (and also unpoison the loaded
1966 // reference, if heap poisoning is enabled).
1967 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1968 }
Roland Levillain4d027112015-07-01 15:41:14 +01001969 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001970}
1971
1972void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1973 LocationSummary* locations =
1974 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1975 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001976 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1977 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
1978 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001979 locations->SetInAt(1, Location::RequiresFpuRegister());
1980 } else {
1981 locations->SetInAt(1, Location::RequiresRegister());
1982 }
1983}
1984
1985void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001986 const FieldInfo& field_info,
1987 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001988 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001989 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001990
1991 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001992 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001993 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001994 Offset offset = field_info.GetFieldOffset();
1995 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001996
Roland Levillain4d027112015-07-01 15:41:14 +01001997 {
1998 // We use a block to end the scratch scope before the write barrier, thus
1999 // freeing the temporary registers so they can be used in `MarkGCCard`.
2000 UseScratchRegisterScope temps(GetVIXLAssembler());
2001
2002 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2003 DCHECK(value.IsW());
2004 Register temp = temps.AcquireW();
2005 __ Mov(temp, value.W());
2006 GetAssembler()->PoisonHeapReference(temp.W());
2007 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002008 }
Roland Levillain4d027112015-07-01 15:41:14 +01002009
2010 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002011 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
2012 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01002013 } else {
2014 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2015 codegen_->MaybeRecordImplicitNullCheck(instruction);
2016 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002017 }
2018
2019 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002020 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002021 }
2022}
2023
Alexandre Rames67555f72014-11-18 10:55:16 +00002024void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002025 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002026
2027 switch (type) {
2028 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002029 case Primitive::kPrimLong: {
2030 Register dst = OutputRegister(instr);
2031 Register lhs = InputRegisterAt(instr, 0);
2032 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002033 if (instr->IsAdd()) {
2034 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002035 } else if (instr->IsAnd()) {
2036 __ And(dst, lhs, rhs);
2037 } else if (instr->IsOr()) {
2038 __ Orr(dst, lhs, rhs);
2039 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002040 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002041 } else if (instr->IsRor()) {
2042 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002043 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002044 __ Ror(dst, lhs, shift);
2045 } else {
2046 // Ensure shift distance is in the same size register as the result. If
2047 // we are rotating a long and the shift comes in a w register originally,
2048 // we don't need to sxtw for use as an x since the shift distances are
2049 // all & reg_bits - 1.
2050 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2051 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002052 } else {
2053 DCHECK(instr->IsXor());
2054 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002055 }
2056 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002057 }
2058 case Primitive::kPrimFloat:
2059 case Primitive::kPrimDouble: {
2060 FPRegister dst = OutputFPRegister(instr);
2061 FPRegister lhs = InputFPRegisterAt(instr, 0);
2062 FPRegister rhs = InputFPRegisterAt(instr, 1);
2063 if (instr->IsAdd()) {
2064 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002065 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002066 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002067 } else {
2068 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002069 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002070 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002071 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002072 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002073 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002074 }
2075}
2076
Serban Constantinescu02164b32014-11-13 14:05:07 +00002077void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2078 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2079
2080 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2081 Primitive::Type type = instr->GetResultType();
2082 switch (type) {
2083 case Primitive::kPrimInt:
2084 case Primitive::kPrimLong: {
2085 locations->SetInAt(0, Location::RequiresRegister());
2086 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2087 locations->SetOut(Location::RequiresRegister());
2088 break;
2089 }
2090 default:
2091 LOG(FATAL) << "Unexpected shift type " << type;
2092 }
2093}
2094
2095void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2096 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2097
2098 Primitive::Type type = instr->GetType();
2099 switch (type) {
2100 case Primitive::kPrimInt:
2101 case Primitive::kPrimLong: {
2102 Register dst = OutputRegister(instr);
2103 Register lhs = InputRegisterAt(instr, 0);
2104 Operand rhs = InputOperandAt(instr, 1);
2105 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002106 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002107 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002108 if (instr->IsShl()) {
2109 __ Lsl(dst, lhs, shift_value);
2110 } else if (instr->IsShr()) {
2111 __ Asr(dst, lhs, shift_value);
2112 } else {
2113 __ Lsr(dst, lhs, shift_value);
2114 }
2115 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002116 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002117
2118 if (instr->IsShl()) {
2119 __ Lsl(dst, lhs, rhs_reg);
2120 } else if (instr->IsShr()) {
2121 __ Asr(dst, lhs, rhs_reg);
2122 } else {
2123 __ Lsr(dst, lhs, rhs_reg);
2124 }
2125 }
2126 break;
2127 }
2128 default:
2129 LOG(FATAL) << "Unexpected shift operation type " << type;
2130 }
2131}
2132
Alexandre Rames5319def2014-10-23 10:03:10 +01002133void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002134 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002135}
2136
2137void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002138 HandleBinaryOp(instruction);
2139}
2140
2141void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2142 HandleBinaryOp(instruction);
2143}
2144
2145void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2146 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002147}
2148
Artem Serov7fc63502016-02-09 17:15:29 +00002149void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002150 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2151 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2152 locations->SetInAt(0, Location::RequiresRegister());
2153 // There is no immediate variant of negated bitwise instructions in AArch64.
2154 locations->SetInAt(1, Location::RequiresRegister());
2155 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2156}
2157
Artem Serov7fc63502016-02-09 17:15:29 +00002158void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002159 Register dst = OutputRegister(instr);
2160 Register lhs = InputRegisterAt(instr, 0);
2161 Register rhs = InputRegisterAt(instr, 1);
2162
2163 switch (instr->GetOpKind()) {
2164 case HInstruction::kAnd:
2165 __ Bic(dst, lhs, rhs);
2166 break;
2167 case HInstruction::kOr:
2168 __ Orn(dst, lhs, rhs);
2169 break;
2170 case HInstruction::kXor:
2171 __ Eon(dst, lhs, rhs);
2172 break;
2173 default:
2174 LOG(FATAL) << "Unreachable";
2175 }
2176}
2177
Alexandre Rames8626b742015-11-25 16:28:08 +00002178void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
2179 HArm64DataProcWithShifterOp* instruction) {
2180 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2181 instruction->GetType() == Primitive::kPrimLong);
2182 LocationSummary* locations =
2183 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2184 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2185 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2186 } else {
2187 locations->SetInAt(0, Location::RequiresRegister());
2188 }
2189 locations->SetInAt(1, Location::RequiresRegister());
2190 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2191}
2192
2193void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
2194 HArm64DataProcWithShifterOp* instruction) {
2195 Primitive::Type type = instruction->GetType();
2196 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2197 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2198 Register out = OutputRegister(instruction);
2199 Register left;
2200 if (kind != HInstruction::kNeg) {
2201 left = InputRegisterAt(instruction, 0);
2202 }
2203 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
2204 // shifter operand operation, the IR generating `right_reg` (input to the type
2205 // conversion) can have a different type from the current instruction's type,
2206 // so we manually indicate the type.
2207 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00002208 int64_t shift_amount = instruction->GetShiftAmount() &
2209 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00002210
2211 Operand right_operand(0);
2212
2213 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2214 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
2215 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2216 } else {
2217 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
2218 }
2219
2220 // Logical binary operations do not support extension operations in the
2221 // operand. Note that VIXL would still manage if it was passed by generating
2222 // the extension as a separate instruction.
2223 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2224 DCHECK(!right_operand.IsExtendedRegister() ||
2225 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2226 kind != HInstruction::kNeg));
2227 switch (kind) {
2228 case HInstruction::kAdd:
2229 __ Add(out, left, right_operand);
2230 break;
2231 case HInstruction::kAnd:
2232 __ And(out, left, right_operand);
2233 break;
2234 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002235 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002236 __ Neg(out, right_operand);
2237 break;
2238 case HInstruction::kOr:
2239 __ Orr(out, left, right_operand);
2240 break;
2241 case HInstruction::kSub:
2242 __ Sub(out, left, right_operand);
2243 break;
2244 case HInstruction::kXor:
2245 __ Eor(out, left, right_operand);
2246 break;
2247 default:
2248 LOG(FATAL) << "Unexpected operation kind: " << kind;
2249 UNREACHABLE();
2250 }
2251}
2252
Artem Serov328429f2016-07-06 16:23:04 +01002253void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002254 LocationSummary* locations =
2255 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2256 locations->SetInAt(0, Location::RequiresRegister());
2257 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
2258 locations->SetOut(Location::RequiresRegister());
2259}
2260
Roland Levillain19c54192016-11-04 13:44:09 +00002261void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002262 __ Add(OutputRegister(instruction),
2263 InputRegisterAt(instruction, 0),
2264 Operand(InputOperandAt(instruction, 1)));
2265}
2266
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002267void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002268 LocationSummary* locations =
2269 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002270 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2271 if (instr->GetOpKind() == HInstruction::kSub &&
2272 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002273 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002274 // Don't allocate register for Mneg instruction.
2275 } else {
2276 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2277 Location::RequiresRegister());
2278 }
2279 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2280 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002281 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2282}
2283
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002284void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002285 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002286 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2287 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002288
2289 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2290 // This fixup should be carried out for all multiply-accumulate instructions:
2291 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2292 if (instr->GetType() == Primitive::kPrimLong &&
2293 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2294 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002295 vixl::aarch64::Instruction* prev =
2296 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002297 if (prev->IsLoadOrStore()) {
2298 // Make sure we emit only exactly one nop.
Scott Wakelingb77051e2016-11-21 19:46:00 +00002299 vixl::CodeBufferCheckScope scope(masm,
2300 kInstructionSize,
2301 vixl::CodeBufferCheckScope::kReserveBufferSpace,
2302 vixl::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002303 __ nop();
2304 }
2305 }
2306
2307 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002308 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002309 __ Madd(res, mul_left, mul_right, accumulator);
2310 } else {
2311 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002312 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002313 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002314 __ Mneg(res, mul_left, mul_right);
2315 } else {
2316 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2317 __ Msub(res, mul_left, mul_right, accumulator);
2318 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002319 }
2320}
2321
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002322void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002323 bool object_array_get_with_read_barrier =
2324 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002325 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002326 new (GetGraph()->GetArena()) LocationSummary(instruction,
2327 object_array_get_with_read_barrier ?
2328 LocationSummary::kCallOnSlowPath :
2329 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002330 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002331 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002332 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002333 locations->SetInAt(0, Location::RequiresRegister());
2334 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002335 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2336 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2337 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002338 // The output overlaps in the case of an object array get with
2339 // read barriers enabled: we do not want the move to overwrite the
2340 // array's location, as we need it to emit the read barrier.
2341 locations->SetOut(
2342 Location::RequiresRegister(),
2343 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002344 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002345}
2346
2347void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002348 Primitive::Type type = instruction->GetType();
2349 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002350 LocationSummary* locations = instruction->GetLocations();
2351 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002352 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002353 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002354 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2355 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002356 MacroAssembler* masm = GetVIXLAssembler();
2357 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002358 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002359 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002360
Roland Levillain19c54192016-11-04 13:44:09 +00002361 // The read barrier instrumentation of object ArrayGet instructions
2362 // does not support the HIntermediateAddress instruction.
2363 DCHECK(!((type == Primitive::kPrimNot) &&
2364 instruction->GetArray()->IsIntermediateAddress() &&
2365 kEmitCompilerReadBarrier));
2366
Roland Levillain44015862016-01-22 11:47:17 +00002367 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2368 // Object ArrayGet with Baker's read barrier case.
2369 Register temp = temps.AcquireW();
Roland Levillain44015862016-01-22 11:47:17 +00002370 // Note that a potential implicit null check is handled in the
2371 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2372 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2373 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002374 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002375 // General case.
2376 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002377 Register length;
2378 if (maybe_compressed_char_at) {
2379 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2380 length = temps.AcquireW();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002381 if (instruction->GetArray()->IsIntermediateAddress()) {
2382 DCHECK_LT(count_offset, offset);
2383 int64_t adjusted_offset = static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2384 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2385 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2386 } else {
2387 __ Ldr(length, HeapOperand(obj, count_offset));
2388 }
jessicahandojo05765752016-09-09 19:01:32 -07002389 codegen_->MaybeRecordImplicitNullCheck(instruction);
2390 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002391 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002392 if (maybe_compressed_char_at) {
2393 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002394 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2395 "Expecting 0=compressed, 1=uncompressed");
2396 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002397 __ Ldrb(Register(OutputCPURegister(instruction)),
2398 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2399 __ B(&done);
2400 __ Bind(&uncompressed_load);
2401 __ Ldrh(Register(OutputCPURegister(instruction)),
2402 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2403 __ Bind(&done);
2404 } else {
2405 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2406 source = HeapOperand(obj, offset);
2407 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002408 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002409 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002410 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002411 // We do not need to compute the intermediate address from the array: the
2412 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002413 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002414 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002415 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002416 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2417 }
2418 temp = obj;
2419 } else {
2420 __ Add(temp, obj, offset);
2421 }
jessicahandojo05765752016-09-09 19:01:32 -07002422 if (maybe_compressed_char_at) {
2423 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002424 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2425 "Expecting 0=compressed, 1=uncompressed");
2426 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002427 __ Ldrb(Register(OutputCPURegister(instruction)),
2428 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2429 __ B(&done);
2430 __ Bind(&uncompressed_load);
2431 __ Ldrh(Register(OutputCPURegister(instruction)),
2432 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2433 __ Bind(&done);
2434 } else {
2435 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2436 }
Roland Levillain44015862016-01-22 11:47:17 +00002437 }
jessicahandojo05765752016-09-09 19:01:32 -07002438 if (!maybe_compressed_char_at) {
2439 codegen_->Load(type, OutputCPURegister(instruction), source);
2440 codegen_->MaybeRecordImplicitNullCheck(instruction);
2441 }
Roland Levillain44015862016-01-22 11:47:17 +00002442
2443 if (type == Primitive::kPrimNot) {
2444 static_assert(
2445 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2446 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2447 Location obj_loc = locations->InAt(0);
2448 if (index.IsConstant()) {
2449 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2450 } else {
2451 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2452 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002453 }
Roland Levillain4d027112015-07-01 15:41:14 +01002454 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002455}
2456
Alexandre Rames5319def2014-10-23 10:03:10 +01002457void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2458 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2459 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002460 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002461}
2462
2463void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002464 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002465 vixl::aarch64::Register out = OutputRegister(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002466 BlockPoolsScope block_pools(GetVIXLAssembler());
jessicahandojo05765752016-09-09 19:01:32 -07002467 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002468 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002469 // Mask out compression flag from String's array length.
2470 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002471 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002472 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002473}
2474
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002475void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002476 Primitive::Type value_type = instruction->GetComponentType();
2477
2478 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002479 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2480 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002481 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002482 LocationSummary::kCallOnSlowPath :
2483 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002484 locations->SetInAt(0, Location::RequiresRegister());
2485 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002486 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2487 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2488 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002489 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002490 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002491 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002492 }
2493}
2494
2495void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2496 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002497 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002498 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002499 bool needs_write_barrier =
2500 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002501
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002502 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002503 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002504 CPURegister source = value;
2505 Location index = locations->InAt(1);
2506 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2507 MemOperand destination = HeapOperand(array);
2508 MacroAssembler* masm = GetVIXLAssembler();
2509 BlockPoolsScope block_pools(masm);
2510
2511 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002512 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002513 if (index.IsConstant()) {
2514 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2515 destination = HeapOperand(array, offset);
2516 } else {
2517 UseScratchRegisterScope temps(masm);
2518 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002519 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002520 // We do not need to compute the intermediate address from the array: the
2521 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002522 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002523 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002524 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002525 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2526 }
2527 temp = array;
2528 } else {
2529 __ Add(temp, array, offset);
2530 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002531 destination = HeapOperand(temp,
2532 XRegisterFrom(index),
2533 LSL,
2534 Primitive::ComponentSizeShift(value_type));
2535 }
2536 codegen_->Store(value_type, value, destination);
2537 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002538 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002539 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002540 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002541 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002542 {
2543 // We use a block to end the scratch scope before the write barrier, thus
2544 // freeing the temporary registers so they can be used in `MarkGCCard`.
2545 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002546 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002547 if (index.IsConstant()) {
2548 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002549 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002550 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002551 destination = HeapOperand(temp,
2552 XRegisterFrom(index),
2553 LSL,
2554 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002555 }
2556
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002557 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2558 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2559 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2560
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002561 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002562 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2563 codegen_->AddSlowPath(slow_path);
2564 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002565 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002566 __ Cbnz(Register(value), &non_zero);
2567 if (!index.IsConstant()) {
2568 __ Add(temp, array, offset);
2569 }
2570 __ Str(wzr, destination);
2571 codegen_->MaybeRecordImplicitNullCheck(instruction);
2572 __ B(&done);
2573 __ Bind(&non_zero);
2574 }
2575
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002576 // Note that when Baker read barriers are enabled, the type
2577 // checks are performed without read barriers. This is fine,
2578 // even in the case where a class object is in the from-space
2579 // after the flip, as a comparison involving such a type would
2580 // not produce a false positive; it may of course produce a
2581 // false negative, in which case we would take the ArraySet
2582 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002583
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002584 Register temp2 = temps.AcquireSameSizeAs(array);
2585 // /* HeapReference<Class> */ temp = array->klass_
2586 __ Ldr(temp, HeapOperand(array, class_offset));
2587 codegen_->MaybeRecordImplicitNullCheck(instruction);
2588 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002589
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002590 // /* HeapReference<Class> */ temp = temp->component_type_
2591 __ Ldr(temp, HeapOperand(temp, component_offset));
2592 // /* HeapReference<Class> */ temp2 = value->klass_
2593 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2594 // If heap poisoning is enabled, no need to unpoison `temp`
2595 // nor `temp2`, as we are comparing two poisoned references.
2596 __ Cmp(temp, temp2);
2597 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002598
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002599 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2600 vixl::aarch64::Label do_put;
2601 __ B(eq, &do_put);
2602 // If heap poisoning is enabled, the `temp` reference has
2603 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002604 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2605
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002606 // /* HeapReference<Class> */ temp = temp->super_class_
2607 __ Ldr(temp, HeapOperand(temp, super_offset));
2608 // If heap poisoning is enabled, no need to unpoison
2609 // `temp`, as we are comparing against null below.
2610 __ Cbnz(temp, slow_path->GetEntryLabel());
2611 __ Bind(&do_put);
2612 } else {
2613 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002614 }
2615 }
2616
2617 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002618 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002619 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002620 __ Mov(temp2, value.W());
2621 GetAssembler()->PoisonHeapReference(temp2);
2622 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002623 }
2624
2625 if (!index.IsConstant()) {
2626 __ Add(temp, array, offset);
2627 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002628 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002629
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002630 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002631 codegen_->MaybeRecordImplicitNullCheck(instruction);
2632 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002633 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002634
2635 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2636
2637 if (done.IsLinked()) {
2638 __ Bind(&done);
2639 }
2640
2641 if (slow_path != nullptr) {
2642 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002643 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002644 }
2645}
2646
Alexandre Rames67555f72014-11-18 10:55:16 +00002647void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002648 RegisterSet caller_saves = RegisterSet::Empty();
2649 InvokeRuntimeCallingConvention calling_convention;
2650 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2651 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2652 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002653 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002654 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002655}
2656
2657void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002658 BoundsCheckSlowPathARM64* slow_path =
2659 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002660 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002661 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2662 __ B(slow_path->GetEntryLabel(), hs);
2663}
2664
Alexandre Rames67555f72014-11-18 10:55:16 +00002665void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2666 LocationSummary* locations =
2667 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2668 locations->SetInAt(0, Location::RequiresRegister());
2669 if (check->HasUses()) {
2670 locations->SetOut(Location::SameAsFirstInput());
2671 }
2672}
2673
2674void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2675 // We assume the class is not null.
2676 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2677 check->GetLoadClass(), check, check->GetDexPc(), true);
2678 codegen_->AddSlowPath(slow_path);
2679 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2680}
2681
Roland Levillain1a653882016-03-18 18:05:57 +00002682static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2683 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2684 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2685}
2686
2687void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2688 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2689 Location rhs_loc = instruction->GetLocations()->InAt(1);
2690 if (rhs_loc.IsConstant()) {
2691 // 0.0 is the only immediate that can be encoded directly in
2692 // an FCMP instruction.
2693 //
2694 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2695 // specify that in a floating-point comparison, positive zero
2696 // and negative zero are considered equal, so we can use the
2697 // literal 0.0 for both cases here.
2698 //
2699 // Note however that some methods (Float.equal, Float.compare,
2700 // Float.compareTo, Double.equal, Double.compare,
2701 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2702 // StrictMath.min) consider 0.0 to be (strictly) greater than
2703 // -0.0. So if we ever translate calls to these methods into a
2704 // HCompare instruction, we must handle the -0.0 case with
2705 // care here.
2706 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2707 __ Fcmp(lhs_reg, 0.0);
2708 } else {
2709 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2710 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002711}
2712
Serban Constantinescu02164b32014-11-13 14:05:07 +00002713void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002714 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002715 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2716 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002717 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002718 case Primitive::kPrimBoolean:
2719 case Primitive::kPrimByte:
2720 case Primitive::kPrimShort:
2721 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002722 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002723 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002724 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002725 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002726 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2727 break;
2728 }
2729 case Primitive::kPrimFloat:
2730 case Primitive::kPrimDouble: {
2731 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002732 locations->SetInAt(1,
2733 IsFloatingPointZeroConstant(compare->InputAt(1))
2734 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2735 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002736 locations->SetOut(Location::RequiresRegister());
2737 break;
2738 }
2739 default:
2740 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2741 }
2742}
2743
2744void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2745 Primitive::Type in_type = compare->InputAt(0)->GetType();
2746
2747 // 0 if: left == right
2748 // 1 if: left > right
2749 // -1 if: left < right
2750 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002751 case Primitive::kPrimBoolean:
2752 case Primitive::kPrimByte:
2753 case Primitive::kPrimShort:
2754 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002755 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002756 case Primitive::kPrimLong: {
2757 Register result = OutputRegister(compare);
2758 Register left = InputRegisterAt(compare, 0);
2759 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002760 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002761 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2762 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002763 break;
2764 }
2765 case Primitive::kPrimFloat:
2766 case Primitive::kPrimDouble: {
2767 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002768 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002769 __ Cset(result, ne);
2770 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002771 break;
2772 }
2773 default:
2774 LOG(FATAL) << "Unimplemented compare type " << in_type;
2775 }
2776}
2777
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002778void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002779 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002780
2781 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2782 locations->SetInAt(0, Location::RequiresFpuRegister());
2783 locations->SetInAt(1,
2784 IsFloatingPointZeroConstant(instruction->InputAt(1))
2785 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2786 : Location::RequiresFpuRegister());
2787 } else {
2788 // Integer cases.
2789 locations->SetInAt(0, Location::RequiresRegister());
2790 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2791 }
2792
David Brazdilb3e773e2016-01-26 11:28:37 +00002793 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002794 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002795 }
2796}
2797
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002798void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002799 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002800 return;
2801 }
2802
2803 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002804 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002805 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002806
Roland Levillain7f63c522015-07-13 15:54:55 +00002807 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002808 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002809 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002810 } else {
2811 // Integer cases.
2812 Register lhs = InputRegisterAt(instruction, 0);
2813 Operand rhs = InputOperandAt(instruction, 1);
2814 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002815 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002816 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002817}
2818
2819#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2820 M(Equal) \
2821 M(NotEqual) \
2822 M(LessThan) \
2823 M(LessThanOrEqual) \
2824 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002825 M(GreaterThanOrEqual) \
2826 M(Below) \
2827 M(BelowOrEqual) \
2828 M(Above) \
2829 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002830#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002831void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2832void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002833FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002834#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002835#undef FOR_EACH_CONDITION_INSTRUCTION
2836
Zheng Xuc6667102015-05-15 16:08:45 +08002837void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2838 DCHECK(instruction->IsDiv() || instruction->IsRem());
2839
2840 LocationSummary* locations = instruction->GetLocations();
2841 Location second = locations->InAt(1);
2842 DCHECK(second.IsConstant());
2843
2844 Register out = OutputRegister(instruction);
2845 Register dividend = InputRegisterAt(instruction, 0);
2846 int64_t imm = Int64FromConstant(second.GetConstant());
2847 DCHECK(imm == 1 || imm == -1);
2848
2849 if (instruction->IsRem()) {
2850 __ Mov(out, 0);
2851 } else {
2852 if (imm == 1) {
2853 __ Mov(out, dividend);
2854 } else {
2855 __ Neg(out, dividend);
2856 }
2857 }
2858}
2859
2860void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2861 DCHECK(instruction->IsDiv() || instruction->IsRem());
2862
2863 LocationSummary* locations = instruction->GetLocations();
2864 Location second = locations->InAt(1);
2865 DCHECK(second.IsConstant());
2866
2867 Register out = OutputRegister(instruction);
2868 Register dividend = InputRegisterAt(instruction, 0);
2869 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002870 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002871 int ctz_imm = CTZ(abs_imm);
2872
2873 UseScratchRegisterScope temps(GetVIXLAssembler());
2874 Register temp = temps.AcquireSameSizeAs(out);
2875
2876 if (instruction->IsDiv()) {
2877 __ Add(temp, dividend, abs_imm - 1);
2878 __ Cmp(dividend, 0);
2879 __ Csel(out, temp, dividend, lt);
2880 if (imm > 0) {
2881 __ Asr(out, out, ctz_imm);
2882 } else {
2883 __ Neg(out, Operand(out, ASR, ctz_imm));
2884 }
2885 } else {
2886 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2887 __ Asr(temp, dividend, bits - 1);
2888 __ Lsr(temp, temp, bits - ctz_imm);
2889 __ Add(out, dividend, temp);
2890 __ And(out, out, abs_imm - 1);
2891 __ Sub(out, out, temp);
2892 }
2893}
2894
2895void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2896 DCHECK(instruction->IsDiv() || instruction->IsRem());
2897
2898 LocationSummary* locations = instruction->GetLocations();
2899 Location second = locations->InAt(1);
2900 DCHECK(second.IsConstant());
2901
2902 Register out = OutputRegister(instruction);
2903 Register dividend = InputRegisterAt(instruction, 0);
2904 int64_t imm = Int64FromConstant(second.GetConstant());
2905
2906 Primitive::Type type = instruction->GetResultType();
2907 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2908
2909 int64_t magic;
2910 int shift;
2911 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2912
2913 UseScratchRegisterScope temps(GetVIXLAssembler());
2914 Register temp = temps.AcquireSameSizeAs(out);
2915
2916 // temp = get_high(dividend * magic)
2917 __ Mov(temp, magic);
2918 if (type == Primitive::kPrimLong) {
2919 __ Smulh(temp, dividend, temp);
2920 } else {
2921 __ Smull(temp.X(), dividend, temp);
2922 __ Lsr(temp.X(), temp.X(), 32);
2923 }
2924
2925 if (imm > 0 && magic < 0) {
2926 __ Add(temp, temp, dividend);
2927 } else if (imm < 0 && magic > 0) {
2928 __ Sub(temp, temp, dividend);
2929 }
2930
2931 if (shift != 0) {
2932 __ Asr(temp, temp, shift);
2933 }
2934
2935 if (instruction->IsDiv()) {
2936 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2937 } else {
2938 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2939 // TODO: Strength reduction for msub.
2940 Register temp_imm = temps.AcquireSameSizeAs(out);
2941 __ Mov(temp_imm, imm);
2942 __ Msub(out, temp, temp_imm, dividend);
2943 }
2944}
2945
2946void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2947 DCHECK(instruction->IsDiv() || instruction->IsRem());
2948 Primitive::Type type = instruction->GetResultType();
2949 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2950
2951 LocationSummary* locations = instruction->GetLocations();
2952 Register out = OutputRegister(instruction);
2953 Location second = locations->InAt(1);
2954
2955 if (second.IsConstant()) {
2956 int64_t imm = Int64FromConstant(second.GetConstant());
2957
2958 if (imm == 0) {
2959 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2960 } else if (imm == 1 || imm == -1) {
2961 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002962 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002963 DivRemByPowerOfTwo(instruction);
2964 } else {
2965 DCHECK(imm <= -2 || imm >= 2);
2966 GenerateDivRemWithAnyConstant(instruction);
2967 }
2968 } else {
2969 Register dividend = InputRegisterAt(instruction, 0);
2970 Register divisor = InputRegisterAt(instruction, 1);
2971 if (instruction->IsDiv()) {
2972 __ Sdiv(out, dividend, divisor);
2973 } else {
2974 UseScratchRegisterScope temps(GetVIXLAssembler());
2975 Register temp = temps.AcquireSameSizeAs(out);
2976 __ Sdiv(temp, dividend, divisor);
2977 __ Msub(out, temp, divisor, dividend);
2978 }
2979 }
2980}
2981
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002982void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2983 LocationSummary* locations =
2984 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2985 switch (div->GetResultType()) {
2986 case Primitive::kPrimInt:
2987 case Primitive::kPrimLong:
2988 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002989 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002990 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2991 break;
2992
2993 case Primitive::kPrimFloat:
2994 case Primitive::kPrimDouble:
2995 locations->SetInAt(0, Location::RequiresFpuRegister());
2996 locations->SetInAt(1, Location::RequiresFpuRegister());
2997 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2998 break;
2999
3000 default:
3001 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3002 }
3003}
3004
3005void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3006 Primitive::Type type = div->GetResultType();
3007 switch (type) {
3008 case Primitive::kPrimInt:
3009 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003010 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003011 break;
3012
3013 case Primitive::kPrimFloat:
3014 case Primitive::kPrimDouble:
3015 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3016 break;
3017
3018 default:
3019 LOG(FATAL) << "Unexpected div type " << type;
3020 }
3021}
3022
Alexandre Rames67555f72014-11-18 10:55:16 +00003023void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003024 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003025 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003026}
3027
3028void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3029 SlowPathCodeARM64* slow_path =
3030 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3031 codegen_->AddSlowPath(slow_path);
3032 Location value = instruction->GetLocations()->InAt(0);
3033
Alexandre Rames3e69f162014-12-10 10:36:50 +00003034 Primitive::Type type = instruction->GetType();
3035
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003036 if (!Primitive::IsIntegralType(type)) {
3037 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003038 return;
3039 }
3040
Alexandre Rames67555f72014-11-18 10:55:16 +00003041 if (value.IsConstant()) {
3042 int64_t divisor = Int64ConstantFrom(value);
3043 if (divisor == 0) {
3044 __ B(slow_path->GetEntryLabel());
3045 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003046 // A division by a non-null constant is valid. We don't need to perform
3047 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003048 }
3049 } else {
3050 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3051 }
3052}
3053
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003054void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3055 LocationSummary* locations =
3056 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3057 locations->SetOut(Location::ConstantLocation(constant));
3058}
3059
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003060void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3061 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003062 // Will be generated at use site.
3063}
3064
Alexandre Rames5319def2014-10-23 10:03:10 +01003065void LocationsBuilderARM64::VisitExit(HExit* exit) {
3066 exit->SetLocations(nullptr);
3067}
3068
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003069void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003070}
3071
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003072void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3073 LocationSummary* locations =
3074 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3075 locations->SetOut(Location::ConstantLocation(constant));
3076}
3077
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003078void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003079 // Will be generated at use site.
3080}
3081
David Brazdilfc6a86a2015-06-26 10:33:45 +00003082void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003083 DCHECK(!successor->IsExitBlock());
3084 HBasicBlock* block = got->GetBlock();
3085 HInstruction* previous = got->GetPrevious();
3086 HLoopInformation* info = block->GetLoopInformation();
3087
David Brazdil46e2a392015-03-16 17:31:52 +00003088 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003089 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3090 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3091 return;
3092 }
3093 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3094 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3095 }
3096 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003097 __ B(codegen_->GetLabelOf(successor));
3098 }
3099}
3100
David Brazdilfc6a86a2015-06-26 10:33:45 +00003101void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3102 got->SetLocations(nullptr);
3103}
3104
3105void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3106 HandleGoto(got, got->GetSuccessor());
3107}
3108
3109void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3110 try_boundary->SetLocations(nullptr);
3111}
3112
3113void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3114 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3115 if (!successor->IsExitBlock()) {
3116 HandleGoto(try_boundary, successor);
3117 }
3118}
3119
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003120void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003121 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003122 vixl::aarch64::Label* true_target,
3123 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003124 // FP branching requires both targets to be explicit. If either of the targets
3125 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003126 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003127 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003128
David Brazdil0debae72015-11-12 18:37:00 +00003129 if (true_target == nullptr && false_target == nullptr) {
3130 // Nothing to do. The code always falls through.
3131 return;
3132 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003133 // Constant condition, statically compared against "true" (integer value 1).
3134 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003135 if (true_target != nullptr) {
3136 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003137 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003138 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003139 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003140 if (false_target != nullptr) {
3141 __ B(false_target);
3142 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003143 }
David Brazdil0debae72015-11-12 18:37:00 +00003144 return;
3145 }
3146
3147 // The following code generates these patterns:
3148 // (1) true_target == nullptr && false_target != nullptr
3149 // - opposite condition true => branch to false_target
3150 // (2) true_target != nullptr && false_target == nullptr
3151 // - condition true => branch to true_target
3152 // (3) true_target != nullptr && false_target != nullptr
3153 // - condition true => branch to true_target
3154 // - branch to false_target
3155 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003156 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003157 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003158 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003159 if (true_target == nullptr) {
3160 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3161 } else {
3162 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3163 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003164 } else {
3165 // The condition instruction has not been materialized, use its inputs as
3166 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003167 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003168
David Brazdil0debae72015-11-12 18:37:00 +00003169 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003170 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003171 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003172 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003173 IfCondition opposite_condition = condition->GetOppositeCondition();
3174 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003175 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003176 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003177 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003178 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003179 // Integer cases.
3180 Register lhs = InputRegisterAt(condition, 0);
3181 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003182
3183 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003184 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003185 if (true_target == nullptr) {
3186 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3187 non_fallthrough_target = false_target;
3188 } else {
3189 arm64_cond = ARM64Condition(condition->GetCondition());
3190 non_fallthrough_target = true_target;
3191 }
3192
Aart Bik086d27e2016-01-20 17:02:00 -08003193 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003194 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003195 switch (arm64_cond) {
3196 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003197 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003198 break;
3199 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003200 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003201 break;
3202 case lt:
3203 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003204 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003205 break;
3206 case ge:
3207 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003208 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003209 break;
3210 default:
3211 // Without the `static_cast` the compiler throws an error for
3212 // `-Werror=sign-promo`.
3213 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3214 }
3215 } else {
3216 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003217 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003218 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003219 }
3220 }
David Brazdil0debae72015-11-12 18:37:00 +00003221
3222 // If neither branch falls through (case 3), the conditional branch to `true_target`
3223 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3224 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003225 __ B(false_target);
3226 }
David Brazdil0debae72015-11-12 18:37:00 +00003227
3228 if (fallthrough_target.IsLinked()) {
3229 __ Bind(&fallthrough_target);
3230 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003231}
3232
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003233void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3234 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003235 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003236 locations->SetInAt(0, Location::RequiresRegister());
3237 }
3238}
3239
3240void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003241 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3242 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003243 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3244 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3245 true_target = nullptr;
3246 }
3247 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3248 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3249 false_target = nullptr;
3250 }
David Brazdil0debae72015-11-12 18:37:00 +00003251 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003252}
3253
3254void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3255 LocationSummary* locations = new (GetGraph()->GetArena())
3256 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003257 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003258 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003259 locations->SetInAt(0, Location::RequiresRegister());
3260 }
3261}
3262
3263void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003264 SlowPathCodeARM64* slow_path =
3265 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003266 GenerateTestAndBranch(deoptimize,
3267 /* condition_input_index */ 0,
3268 slow_path->GetEntryLabel(),
3269 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003270}
3271
Mingyao Yang063fc772016-08-02 11:02:54 -07003272void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3273 LocationSummary* locations = new (GetGraph()->GetArena())
3274 LocationSummary(flag, LocationSummary::kNoCall);
3275 locations->SetOut(Location::RequiresRegister());
3276}
3277
3278void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3279 __ Ldr(OutputRegister(flag),
3280 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3281}
3282
David Brazdilc0b601b2016-02-08 14:20:45 +00003283static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3284 return condition->IsCondition() &&
3285 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3286}
3287
Alexandre Rames880f1192016-06-13 16:04:50 +01003288static inline Condition GetConditionForSelect(HCondition* condition) {
3289 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003290 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3291 : ARM64Condition(cond);
3292}
3293
David Brazdil74eb1b22015-12-14 11:44:01 +00003294void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3295 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003296 if (Primitive::IsFloatingPointType(select->GetType())) {
3297 locations->SetInAt(0, Location::RequiresFpuRegister());
3298 locations->SetInAt(1, Location::RequiresFpuRegister());
3299 locations->SetOut(Location::RequiresFpuRegister());
3300 } else {
3301 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3302 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3303 bool is_true_value_constant = cst_true_value != nullptr;
3304 bool is_false_value_constant = cst_false_value != nullptr;
3305 // Ask VIXL whether we should synthesize constants in registers.
3306 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3307 Operand true_op = is_true_value_constant ?
3308 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3309 Operand false_op = is_false_value_constant ?
3310 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3311 bool true_value_in_register = false;
3312 bool false_value_in_register = false;
3313 MacroAssembler::GetCselSynthesisInformation(
3314 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3315 true_value_in_register |= !is_true_value_constant;
3316 false_value_in_register |= !is_false_value_constant;
3317
3318 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3319 : Location::ConstantLocation(cst_true_value));
3320 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3321 : Location::ConstantLocation(cst_false_value));
3322 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003323 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003324
David Brazdil74eb1b22015-12-14 11:44:01 +00003325 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3326 locations->SetInAt(2, Location::RequiresRegister());
3327 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003328}
3329
3330void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003331 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003332 Condition csel_cond;
3333
3334 if (IsBooleanValueOrMaterializedCondition(cond)) {
3335 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003336 // Use the condition flags set by the previous instruction.
3337 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003338 } else {
3339 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003340 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003341 }
3342 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003343 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003344 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003345 } else {
3346 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003347 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003348 }
3349
Alexandre Rames880f1192016-06-13 16:04:50 +01003350 if (Primitive::IsFloatingPointType(select->GetType())) {
3351 __ Fcsel(OutputFPRegister(select),
3352 InputFPRegisterAt(select, 1),
3353 InputFPRegisterAt(select, 0),
3354 csel_cond);
3355 } else {
3356 __ Csel(OutputRegister(select),
3357 InputOperandAt(select, 1),
3358 InputOperandAt(select, 0),
3359 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003360 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003361}
3362
David Srbecky0cf44932015-12-09 14:09:59 +00003363void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3364 new (GetGraph()->GetArena()) LocationSummary(info);
3365}
3366
David Srbeckyd28f4a02016-03-14 17:14:24 +00003367void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3368 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003369}
3370
3371void CodeGeneratorARM64::GenerateNop() {
3372 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003373}
3374
Alexandre Rames5319def2014-10-23 10:03:10 +01003375void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003376 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003377}
3378
3379void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003380 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003381}
3382
3383void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003384 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003385}
3386
3387void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003388 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003389}
3390
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003391// Temp is used for read barrier.
3392static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3393 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003394 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003395 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3396 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3397 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3398 return 1;
3399 }
3400 return 0;
3401}
3402
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003403// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003404// interface pointer, one for loading the current interface.
3405// The other checks have one temp for loading the object's class.
3406static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3407 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3408 return 3;
3409 }
3410 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003411}
3412
Alexandre Rames67555f72014-11-18 10:55:16 +00003413void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003414 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003415 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003416 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003417 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003418 case TypeCheckKind::kExactCheck:
3419 case TypeCheckKind::kAbstractClassCheck:
3420 case TypeCheckKind::kClassHierarchyCheck:
3421 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003422 call_kind =
3423 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003424 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003425 break;
3426 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003427 case TypeCheckKind::kUnresolvedCheck:
3428 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003429 call_kind = LocationSummary::kCallOnSlowPath;
3430 break;
3431 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003432
Alexandre Rames67555f72014-11-18 10:55:16 +00003433 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003434 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003435 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003436 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003437 locations->SetInAt(0, Location::RequiresRegister());
3438 locations->SetInAt(1, Location::RequiresRegister());
3439 // The "out" register is used as a temporary, so it overlaps with the inputs.
3440 // Note that TypeCheckSlowPathARM64 uses this register too.
3441 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003442 // Add temps if necessary for read barriers.
3443 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003444}
3445
3446void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003447 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003448 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003449 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003450 Register obj = InputRegisterAt(instruction, 0);
3451 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003452 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003453 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003454 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3455 DCHECK_LE(num_temps, 1u);
3456 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003457 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3458 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3459 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3460 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003461
Scott Wakeling97c72b72016-06-24 16:19:36 +01003462 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003463 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003464
3465 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003466 // Avoid null check if we know `obj` is not null.
3467 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003468 __ Cbz(obj, &zero);
3469 }
3470
Roland Levillain44015862016-01-22 11:47:17 +00003471 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003472 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003473 // /* HeapReference<Class> */ out = obj->klass_
3474 GenerateReferenceLoadTwoRegisters(instruction,
3475 out_loc,
3476 obj_loc,
3477 class_offset,
3478 maybe_temp_loc,
3479 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003480 __ Cmp(out, cls);
3481 __ Cset(out, eq);
3482 if (zero.IsLinked()) {
3483 __ B(&done);
3484 }
3485 break;
3486 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003487
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003488 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003489 // /* HeapReference<Class> */ out = obj->klass_
3490 GenerateReferenceLoadTwoRegisters(instruction,
3491 out_loc,
3492 obj_loc,
3493 class_offset,
3494 maybe_temp_loc,
3495 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003496 // If the class is abstract, we eagerly fetch the super class of the
3497 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003498 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003499 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003500 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003501 GenerateReferenceLoadOneRegister(instruction,
3502 out_loc,
3503 super_offset,
3504 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003505 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003506 // If `out` is null, we use it for the result, and jump to `done`.
3507 __ Cbz(out, &done);
3508 __ Cmp(out, cls);
3509 __ B(ne, &loop);
3510 __ Mov(out, 1);
3511 if (zero.IsLinked()) {
3512 __ B(&done);
3513 }
3514 break;
3515 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003516
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003517 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003518 // /* HeapReference<Class> */ out = obj->klass_
3519 GenerateReferenceLoadTwoRegisters(instruction,
3520 out_loc,
3521 obj_loc,
3522 class_offset,
3523 maybe_temp_loc,
3524 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003525 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003526 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003527 __ Bind(&loop);
3528 __ Cmp(out, cls);
3529 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003530 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003531 GenerateReferenceLoadOneRegister(instruction,
3532 out_loc,
3533 super_offset,
3534 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003535 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003536 __ Cbnz(out, &loop);
3537 // If `out` is null, we use it for the result, and jump to `done`.
3538 __ B(&done);
3539 __ Bind(&success);
3540 __ Mov(out, 1);
3541 if (zero.IsLinked()) {
3542 __ B(&done);
3543 }
3544 break;
3545 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003546
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003547 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003548 // /* HeapReference<Class> */ out = obj->klass_
3549 GenerateReferenceLoadTwoRegisters(instruction,
3550 out_loc,
3551 obj_loc,
3552 class_offset,
3553 maybe_temp_loc,
3554 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003555 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003556 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003557 __ Cmp(out, cls);
3558 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003559 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003560 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003561 GenerateReferenceLoadOneRegister(instruction,
3562 out_loc,
3563 component_offset,
3564 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003565 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003566 // If `out` is null, we use it for the result, and jump to `done`.
3567 __ Cbz(out, &done);
3568 __ Ldrh(out, HeapOperand(out, primitive_offset));
3569 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3570 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003571 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003572 __ Mov(out, 1);
3573 __ B(&done);
3574 break;
3575 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003576
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003577 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003578 // No read barrier since the slow path will retry upon failure.
3579 // /* HeapReference<Class> */ out = obj->klass_
3580 GenerateReferenceLoadTwoRegisters(instruction,
3581 out_loc,
3582 obj_loc,
3583 class_offset,
3584 maybe_temp_loc,
3585 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003586 __ Cmp(out, cls);
3587 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003588 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3589 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003590 codegen_->AddSlowPath(slow_path);
3591 __ B(ne, slow_path->GetEntryLabel());
3592 __ Mov(out, 1);
3593 if (zero.IsLinked()) {
3594 __ B(&done);
3595 }
3596 break;
3597 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003598
Calin Juravle98893e12015-10-02 21:05:03 +01003599 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003600 case TypeCheckKind::kInterfaceCheck: {
3601 // Note that we indeed only call on slow path, but we always go
3602 // into the slow path for the unresolved and interface check
3603 // cases.
3604 //
3605 // We cannot directly call the InstanceofNonTrivial runtime
3606 // entry point without resorting to a type checking slow path
3607 // here (i.e. by calling InvokeRuntime directly), as it would
3608 // require to assign fixed registers for the inputs of this
3609 // HInstanceOf instruction (following the runtime calling
3610 // convention), which might be cluttered by the potential first
3611 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003612 //
3613 // TODO: Introduce a new runtime entry point taking the object
3614 // to test (instead of its class) as argument, and let it deal
3615 // with the read barrier issues. This will let us refactor this
3616 // case of the `switch` code as it was previously (with a direct
3617 // call to the runtime not using a type checking slow path).
3618 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003619 DCHECK(locations->OnlyCallsOnSlowPath());
3620 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3621 /* is_fatal */ false);
3622 codegen_->AddSlowPath(slow_path);
3623 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003624 if (zero.IsLinked()) {
3625 __ B(&done);
3626 }
3627 break;
3628 }
3629 }
3630
3631 if (zero.IsLinked()) {
3632 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003633 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003634 }
3635
3636 if (done.IsLinked()) {
3637 __ Bind(&done);
3638 }
3639
3640 if (slow_path != nullptr) {
3641 __ Bind(slow_path->GetExitLabel());
3642 }
3643}
3644
3645void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3646 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3647 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3648
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003649 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3650 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003651 case TypeCheckKind::kExactCheck:
3652 case TypeCheckKind::kAbstractClassCheck:
3653 case TypeCheckKind::kClassHierarchyCheck:
3654 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003655 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3656 LocationSummary::kCallOnSlowPath :
3657 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003658 break;
3659 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003660 case TypeCheckKind::kUnresolvedCheck:
3661 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003662 call_kind = LocationSummary::kCallOnSlowPath;
3663 break;
3664 }
3665
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003666 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3667 locations->SetInAt(0, Location::RequiresRegister());
3668 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003669 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
3670 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003671}
3672
3673void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003674 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003675 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003676 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003677 Register obj = InputRegisterAt(instruction, 0);
3678 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003679 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3680 DCHECK_GE(num_temps, 1u);
3681 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003682 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003683 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
3684 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003685 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003686 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3687 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3688 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3689 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3690 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3691 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3692 const uint32_t object_array_data_offset =
3693 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003694
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003695 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003696 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3697 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3698 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003699 if (!kEmitCompilerReadBarrier) {
3700 is_type_check_slow_path_fatal =
3701 (type_check_kind == TypeCheckKind::kExactCheck ||
3702 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3703 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3704 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3705 !instruction->CanThrowIntoCatchBlock();
3706 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003707 SlowPathCodeARM64* type_check_slow_path =
3708 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3709 is_type_check_slow_path_fatal);
3710 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003711
Scott Wakeling97c72b72016-06-24 16:19:36 +01003712 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003713 // Avoid null check if we know obj is not null.
3714 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003715 __ Cbz(obj, &done);
3716 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003717
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003718 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003719 case TypeCheckKind::kExactCheck:
3720 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003721 // /* HeapReference<Class> */ temp = obj->klass_
3722 GenerateReferenceLoadTwoRegisters(instruction,
3723 temp_loc,
3724 obj_loc,
3725 class_offset,
3726 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003727 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003728
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003729 __ Cmp(temp, cls);
3730 // Jump to slow path for throwing the exception or doing a
3731 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003732 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003733 break;
3734 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003735
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003736 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003737 // /* HeapReference<Class> */ temp = obj->klass_
3738 GenerateReferenceLoadTwoRegisters(instruction,
3739 temp_loc,
3740 obj_loc,
3741 class_offset,
3742 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003743 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003744
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003745 // If the class is abstract, we eagerly fetch the super class of the
3746 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003747 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003748 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003749 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003750 GenerateReferenceLoadOneRegister(instruction,
3751 temp_loc,
3752 super_offset,
3753 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003754 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003755
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003756 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3757 // exception.
3758 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3759 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003760 __ Cmp(temp, cls);
3761 __ B(ne, &loop);
3762 break;
3763 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003764
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003765 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003766 // /* HeapReference<Class> */ temp = obj->klass_
3767 GenerateReferenceLoadTwoRegisters(instruction,
3768 temp_loc,
3769 obj_loc,
3770 class_offset,
3771 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003772 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003773
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003774 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003775 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003776 __ Bind(&loop);
3777 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003778 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003779
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003780 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003781 GenerateReferenceLoadOneRegister(instruction,
3782 temp_loc,
3783 super_offset,
3784 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003785 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003786
3787 // If the class reference currently in `temp` is not null, jump
3788 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003789 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003790 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003791 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003792 break;
3793 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003794
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003795 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003796 // /* HeapReference<Class> */ temp = obj->klass_
3797 GenerateReferenceLoadTwoRegisters(instruction,
3798 temp_loc,
3799 obj_loc,
3800 class_offset,
3801 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003802 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003803
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003804 // Do an exact check.
3805 __ Cmp(temp, cls);
3806 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003807
3808 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003809 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003810 GenerateReferenceLoadOneRegister(instruction,
3811 temp_loc,
3812 component_offset,
3813 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003814 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003815
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003816 // If the component type is null, jump to the slow path to throw the exception.
3817 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3818 // Otherwise, the object is indeed an array. Further check that this component type is not a
3819 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003820 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3821 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003822 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003823 break;
3824 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003825
Calin Juravle98893e12015-10-02 21:05:03 +01003826 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003827 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003828 //
3829 // We cannot directly call the CheckCast runtime entry point
3830 // without resorting to a type checking slow path here (i.e. by
3831 // calling InvokeRuntime directly), as it would require to
3832 // assign fixed registers for the inputs of this HInstanceOf
3833 // instruction (following the runtime calling convention), which
3834 // might be cluttered by the potential first read barrier
3835 // emission at the beginning of this method.
3836 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003837 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003838 case TypeCheckKind::kInterfaceCheck: {
3839 // /* HeapReference<Class> */ temp = obj->klass_
3840 GenerateReferenceLoadTwoRegisters(instruction,
3841 temp_loc,
3842 obj_loc,
3843 class_offset,
3844 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003845 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003846
3847 // /* HeapReference<Class> */ temp = temp->iftable_
3848 GenerateReferenceLoadTwoRegisters(instruction,
3849 temp_loc,
3850 temp_loc,
3851 iftable_offset,
3852 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003853 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003854 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003855 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08003856 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003857 vixl::aarch64::Label start_loop;
3858 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003859 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003860 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
3861 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003862 // Go to next interface.
3863 __ Add(temp, temp, 2 * kHeapReferenceSize);
3864 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003865 // Compare the classes and continue the loop if they do not match.
3866 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
3867 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003868 break;
3869 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003870 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003871 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003872
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003873 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003874}
3875
Alexandre Rames5319def2014-10-23 10:03:10 +01003876void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3877 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3878 locations->SetOut(Location::ConstantLocation(constant));
3879}
3880
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003881void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003882 // Will be generated at use site.
3883}
3884
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003885void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3886 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3887 locations->SetOut(Location::ConstantLocation(constant));
3888}
3889
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003890void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003891 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003892}
3893
Calin Juravle175dc732015-08-25 15:42:32 +01003894void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3895 // The trampoline uses the same calling convention as dex calling conventions,
3896 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3897 // the method_idx.
3898 HandleInvoke(invoke);
3899}
3900
3901void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3902 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3903}
3904
Alexandre Rames5319def2014-10-23 10:03:10 +01003905void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003906 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003907 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003908}
3909
Alexandre Rames67555f72014-11-18 10:55:16 +00003910void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3911 HandleInvoke(invoke);
3912}
3913
3914void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3915 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003916 LocationSummary* locations = invoke->GetLocations();
3917 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003918 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003919 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003920 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003921
3922 // The register ip1 is required to be used for the hidden argument in
3923 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003924 MacroAssembler* masm = GetVIXLAssembler();
3925 UseScratchRegisterScope scratch_scope(masm);
3926 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003927 scratch_scope.Exclude(ip1);
3928 __ Mov(ip1, invoke->GetDexMethodIndex());
3929
Alexandre Rames67555f72014-11-18 10:55:16 +00003930 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003931 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003932 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003933 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003934 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003935 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003936 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003937 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003938 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003939 // Instead of simply (possibly) unpoisoning `temp` here, we should
3940 // emit a read barrier for the previous class reference load.
3941 // However this is not required in practice, as this is an
3942 // intermediate/temporary reference and because the current
3943 // concurrent copying collector keeps the from-space memory
3944 // intact/accessible until the end of the marking phase (the
3945 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003946 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003947 __ Ldr(temp,
3948 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3949 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003950 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003951 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003952 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003953 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003954 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003955 // lr();
3956 __ Blr(lr);
3957 DCHECK(!codegen_->IsLeafMethod());
3958 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3959}
3960
3961void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003962 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3963 if (intrinsic.TryDispatch(invoke)) {
3964 return;
3965 }
3966
Alexandre Rames67555f72014-11-18 10:55:16 +00003967 HandleInvoke(invoke);
3968}
3969
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003970void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003971 // Explicit clinit checks triggered by static invokes must have been pruned by
3972 // art::PrepareForRegisterAllocation.
3973 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003974
Andreas Gampe878d58c2015-01-15 23:24:00 -08003975 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3976 if (intrinsic.TryDispatch(invoke)) {
3977 return;
3978 }
3979
Alexandre Rames67555f72014-11-18 10:55:16 +00003980 HandleInvoke(invoke);
3981}
3982
Andreas Gampe878d58c2015-01-15 23:24:00 -08003983static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3984 if (invoke->GetLocations()->Intrinsified()) {
3985 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3986 intrinsic.Dispatch(invoke);
3987 return true;
3988 }
3989 return false;
3990}
3991
Vladimir Markodc151b22015-10-15 18:02:30 +01003992HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3993 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003994 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003995 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003996 return desired_dispatch_info;
3997}
3998
TatWai Chongd8c052a2016-11-02 16:12:48 +08003999Location CodeGeneratorARM64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4000 Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004001 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004002 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4003 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004004 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4005 uint32_t offset =
4006 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004007 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004008 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004009 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004010 }
Vladimir Marko58155012015-08-19 12:49:41 +00004011 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004012 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004013 break;
4014 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4015 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004016 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004017 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004018 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4019 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004020 const DexFile& dex_file = invoke->GetDexFileForPcRelativeDexCache();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004021 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004022 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004023 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004024 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004025 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004026 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004027 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004028 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004029 }
Vladimir Marko58155012015-08-19 12:49:41 +00004030 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004031 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004032 Register reg = XRegisterFrom(temp);
4033 Register method_reg;
4034 if (current_method.IsRegister()) {
4035 method_reg = XRegisterFrom(current_method);
4036 } else {
4037 DCHECK(invoke->GetLocations()->Intrinsified());
4038 DCHECK(!current_method.IsValid());
4039 method_reg = reg;
4040 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4041 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004042
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004043 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004044 __ Ldr(reg.X(),
4045 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004046 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004047 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004048 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4049 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004050 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4051 break;
4052 }
4053 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08004054 return callee_method;
4055}
4056
4057void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4058 // All registers are assumed to be correctly set up.
4059 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004060
4061 switch (invoke->GetCodePtrLocation()) {
4062 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4063 __ Bl(&frame_entry_label_);
4064 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004065 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4066 // LR = callee_method->entry_point_from_quick_compiled_code_;
4067 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004068 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004069 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004070 // lr()
4071 __ Blr(lr);
4072 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004073 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004074
Andreas Gampe878d58c2015-01-15 23:24:00 -08004075 DCHECK(!IsLeafMethod());
4076}
4077
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004078void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004079 // Use the calling convention instead of the location of the receiver, as
4080 // intrinsics may have put the receiver in a different register. In the intrinsics
4081 // slow path, the arguments have been moved to the right place, so here we are
4082 // guaranteed that the receiver is the first register of the calling convention.
4083 InvokeDexCallingConvention calling_convention;
4084 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004085 Register temp = XRegisterFrom(temp_in);
4086 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4087 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4088 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004089 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004090
4091 BlockPoolsScope block_pools(GetVIXLAssembler());
4092
4093 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004094 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004095 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004096 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004097 // Instead of simply (possibly) unpoisoning `temp` here, we should
4098 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004099 // intermediate/temporary reference and because the current
4100 // concurrent copying collector keeps the from-space memory
4101 // intact/accessible until the end of the marking phase (the
4102 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004103 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4104 // temp = temp->GetMethodAt(method_offset);
4105 __ Ldr(temp, MemOperand(temp, method_offset));
4106 // lr = temp->GetEntryPoint();
4107 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
4108 // lr();
4109 __ Blr(lr);
4110}
4111
Orion Hodsonac141392017-01-13 11:53:47 +00004112void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4113 HandleInvoke(invoke);
4114}
4115
4116void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4117 codegen_->GenerateInvokePolymorphicCall(invoke);
4118}
4119
Scott Wakeling97c72b72016-06-24 16:19:36 +01004120vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4121 const DexFile& dex_file,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004122 dex::StringIndex string_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004123 vixl::aarch64::Label* adrp_label) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004124 return
4125 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004126}
4127
Scott Wakeling97c72b72016-06-24 16:19:36 +01004128vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4129 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004130 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004131 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004132 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004133}
4134
Vladimir Marko1998cd02017-01-13 13:02:58 +00004135vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4136 const DexFile& dex_file,
4137 dex::TypeIndex type_index,
4138 vixl::aarch64::Label* adrp_label) {
4139 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4140}
4141
Scott Wakeling97c72b72016-06-24 16:19:36 +01004142vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4143 const DexFile& dex_file,
4144 uint32_t element_offset,
4145 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004146 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4147}
4148
Scott Wakeling97c72b72016-06-24 16:19:36 +01004149vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4150 const DexFile& dex_file,
4151 uint32_t offset_or_index,
4152 vixl::aarch64::Label* adrp_label,
4153 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004154 // Add a patch entry and return the label.
4155 patches->emplace_back(dex_file, offset_or_index);
4156 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004157 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004158 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4159 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4160 return label;
4161}
4162
Scott Wakeling97c72b72016-06-24 16:19:36 +01004163vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004164 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004165 return boot_image_string_patches_.GetOrCreate(
4166 StringReference(&dex_file, string_index),
4167 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4168}
4169
Scott Wakeling97c72b72016-06-24 16:19:36 +01004170vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004171 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004172 return boot_image_type_patches_.GetOrCreate(
4173 TypeReference(&dex_file, type_index),
4174 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4175}
4176
Scott Wakeling97c72b72016-06-24 16:19:36 +01004177vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4178 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004179 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
4180 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
4181 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
4182}
4183
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004184vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004185 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4186 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4187 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004188 return jit_string_patches_.GetOrCreate(
4189 StringReference(&dex_file, string_index),
4190 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4191}
4192
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004193vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004194 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4195 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4196 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004197 return jit_class_patches_.GetOrCreate(
4198 TypeReference(&dex_file, type_index),
4199 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4200}
4201
Vladimir Markoaad75c62016-10-03 08:46:48 +00004202void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4203 vixl::aarch64::Register reg) {
4204 DCHECK(reg.IsX());
4205 SingleEmissionCheckScope guard(GetVIXLAssembler());
4206 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004207 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004208}
4209
4210void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4211 vixl::aarch64::Register out,
4212 vixl::aarch64::Register base) {
4213 DCHECK(out.IsX());
4214 DCHECK(base.IsX());
4215 SingleEmissionCheckScope guard(GetVIXLAssembler());
4216 __ Bind(fixup_label);
4217 __ add(out, base, Operand(/* offset placeholder */ 0));
4218}
4219
4220void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4221 vixl::aarch64::Register out,
4222 vixl::aarch64::Register base) {
4223 DCHECK(base.IsX());
4224 SingleEmissionCheckScope guard(GetVIXLAssembler());
4225 __ Bind(fixup_label);
4226 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4227}
4228
4229template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4230inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4231 const ArenaDeque<PcRelativePatchInfo>& infos,
4232 ArenaVector<LinkerPatch>* linker_patches) {
4233 for (const PcRelativePatchInfo& info : infos) {
4234 linker_patches->push_back(Factory(info.label.GetLocation(),
4235 &info.target_dex_file,
4236 info.pc_insn_label->GetLocation(),
4237 info.offset_or_index));
4238 }
4239}
4240
Vladimir Marko58155012015-08-19 12:49:41 +00004241void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4242 DCHECK(linker_patches->empty());
4243 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004244 pc_relative_dex_cache_patches_.size() +
4245 boot_image_string_patches_.size() +
4246 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004247 boot_image_type_patches_.size() +
4248 pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004249 type_bss_entry_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004250 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004251 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004252 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004253 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004254 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004255 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004256 info.offset_or_index));
4257 }
4258 for (const auto& entry : boot_image_string_patches_) {
4259 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004260 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4261 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004262 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004263 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004264 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004265 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004266 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004267 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4268 linker_patches);
4269 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004270 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4271 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004272 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4273 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004274 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004275 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4276 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004277 for (const auto& entry : boot_image_type_patches_) {
4278 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004279 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4280 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004281 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004282 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004283 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004284 for (const auto& entry : boot_image_address_patches_) {
4285 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004286 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4287 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00004288 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004289 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004290}
4291
Scott Wakeling97c72b72016-06-24 16:19:36 +01004292vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004293 Uint32ToLiteralMap* map) {
4294 return map->GetOrCreate(
4295 value,
4296 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4297}
4298
Scott Wakeling97c72b72016-06-24 16:19:36 +01004299vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004300 return uint64_literals_.GetOrCreate(
4301 value,
4302 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004303}
4304
Scott Wakeling97c72b72016-06-24 16:19:36 +01004305vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004306 MethodReference target_method,
4307 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004308 return map->GetOrCreate(
4309 target_method,
4310 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004311}
4312
Andreas Gampe878d58c2015-01-15 23:24:00 -08004313void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004314 // Explicit clinit checks triggered by static invokes must have been pruned by
4315 // art::PrepareForRegisterAllocation.
4316 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004317
Andreas Gampe878d58c2015-01-15 23:24:00 -08004318 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4319 return;
4320 }
4321
Alexandre Ramesd921d642015-04-16 15:07:16 +01004322 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004323 LocationSummary* locations = invoke->GetLocations();
4324 codegen_->GenerateStaticOrDirectCall(
4325 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004326 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004327}
4328
4329void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004330 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4331 return;
4332 }
4333
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004334 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004335 DCHECK(!codegen_->IsLeafMethod());
4336 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4337}
4338
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004339HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4340 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004341 switch (desired_class_load_kind) {
4342 case HLoadClass::LoadKind::kReferrersClass:
4343 break;
4344 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4345 DCHECK(!GetCompilerOptions().GetCompilePic());
4346 break;
4347 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4348 DCHECK(GetCompilerOptions().GetCompilePic());
4349 break;
4350 case HLoadClass::LoadKind::kBootImageAddress:
4351 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004352 case HLoadClass::LoadKind::kBssEntry:
4353 DCHECK(!Runtime::Current()->UseJitCompilation());
4354 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004355 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004356 DCHECK(Runtime::Current()->UseJitCompilation());
4357 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004358 case HLoadClass::LoadKind::kDexCacheViaMethod:
4359 break;
4360 }
4361 return desired_class_load_kind;
4362}
4363
Alexandre Rames67555f72014-11-18 10:55:16 +00004364void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004365 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4366 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004367 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004368 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004369 cls,
4370 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004371 LocationFrom(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004372 return;
4373 }
Vladimir Marko41559982017-01-06 14:04:23 +00004374 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004375
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004376 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4377 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004378 ? LocationSummary::kCallOnSlowPath
4379 : LocationSummary::kNoCall;
4380 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004381 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004382 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004383 }
4384
Vladimir Marko41559982017-01-06 14:04:23 +00004385 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004386 locations->SetInAt(0, Location::RequiresRegister());
4387 }
4388 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004389}
4390
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004391// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4392// move.
4393void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004394 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4395 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4396 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004397 return;
4398 }
Vladimir Marko41559982017-01-06 14:04:23 +00004399 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004400
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004401 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004402 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004403
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004404 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4405 ? kWithoutReadBarrier
4406 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004407 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004408 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004409 case HLoadClass::LoadKind::kReferrersClass: {
4410 DCHECK(!cls->CanCallRuntime());
4411 DCHECK(!cls->MustGenerateClinitCheck());
4412 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4413 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004414 GenerateGcRootFieldLoad(cls,
4415 out_loc,
4416 current_method,
4417 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004418 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004419 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004420 break;
4421 }
4422 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004423 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004424 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4425 cls->GetTypeIndex()));
4426 break;
4427 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004428 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004429 // Add ADRP with its PC-relative type patch.
4430 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004431 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004432 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004433 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004434 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004435 vixl::aarch64::Label* add_label =
4436 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004437 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004438 break;
4439 }
4440 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004441 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004442 uint32_t address = dchecked_integral_cast<uint32_t>(
4443 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4444 DCHECK_NE(address, 0u);
4445 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004446 break;
4447 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004448 case HLoadClass::LoadKind::kBssEntry: {
4449 // Add ADRP with its PC-relative Class .bss entry patch.
4450 const DexFile& dex_file = cls->GetDexFile();
4451 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko1998cd02017-01-13 13:02:58 +00004452 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004453 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
4454 // Add LDR with its PC-relative Class patch.
4455 vixl::aarch64::Label* ldr_label =
Vladimir Marko1998cd02017-01-13 13:02:58 +00004456 codegen_->NewBssEntryTypePatch(dex_file, type_index, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004457 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4458 GenerateGcRootFieldLoad(cls,
4459 cls->GetLocations()->Out(),
4460 out.X(),
4461 /* placeholder */ 0u,
4462 ldr_label,
4463 kCompilerReadBarrierOption);
4464 generate_null_check = true;
4465 break;
4466 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004467 case HLoadClass::LoadKind::kJitTableAddress: {
4468 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4469 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004470 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004471 GenerateGcRootFieldLoad(cls,
4472 out_loc,
4473 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004474 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004475 /* fixup_label */ nullptr,
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004476 kCompilerReadBarrierOption);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004477 break;
4478 }
Vladimir Marko41559982017-01-06 14:04:23 +00004479 case HLoadClass::LoadKind::kDexCacheViaMethod:
4480 LOG(FATAL) << "UNREACHABLE";
4481 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004482 }
4483
4484 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4485 DCHECK(cls->CanCallRuntime());
4486 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4487 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4488 codegen_->AddSlowPath(slow_path);
4489 if (generate_null_check) {
4490 __ Cbz(out, slow_path->GetEntryLabel());
4491 }
4492 if (cls->MustGenerateClinitCheck()) {
4493 GenerateClassInitializationCheck(slow_path, out);
4494 } else {
4495 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004496 }
4497 }
4498}
4499
David Brazdilcb1c0552015-08-04 16:22:25 +01004500static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004501 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004502}
4503
Alexandre Rames67555f72014-11-18 10:55:16 +00004504void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4505 LocationSummary* locations =
4506 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4507 locations->SetOut(Location::RequiresRegister());
4508}
4509
4510void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004511 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4512}
4513
4514void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4515 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4516}
4517
4518void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4519 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004520}
4521
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004522HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4523 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004524 switch (desired_string_load_kind) {
4525 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4526 DCHECK(!GetCompilerOptions().GetCompilePic());
4527 break;
4528 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4529 DCHECK(GetCompilerOptions().GetCompilePic());
4530 break;
4531 case HLoadString::LoadKind::kBootImageAddress:
4532 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004533 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004534 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004535 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004536 case HLoadString::LoadKind::kJitTableAddress:
4537 DCHECK(Runtime::Current()->UseJitCompilation());
4538 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004539 case HLoadString::LoadKind::kDexCacheViaMethod:
4540 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004541 }
4542 return desired_string_load_kind;
4543}
4544
Alexandre Rames67555f72014-11-18 10:55:16 +00004545void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004546 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004547 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004548 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004549 InvokeRuntimeCallingConvention calling_convention;
4550 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4551 } else {
4552 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004553 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4554 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4555 // Rely on the pResolveString and/or marking to save everything, including temps.
4556 RegisterSet caller_saves = RegisterSet::Empty();
4557 InvokeRuntimeCallingConvention calling_convention;
4558 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4559 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4560 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4561 Primitive::kPrimNot).GetCode());
4562 locations->SetCustomSlowPathCallerSaves(caller_saves);
4563 } else {
4564 // For non-Baker read barrier we have a temp-clobbering call.
4565 }
4566 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004567 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004568}
4569
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004570// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4571// move.
4572void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00004573 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004574 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004575
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004576 switch (load->GetLoadKind()) {
4577 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004578 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4579 load->GetStringIndex()));
4580 return; // No dex cache slow path.
4581 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004582 // Add ADRP with its PC-relative String patch.
4583 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004584 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004585 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004586 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004587 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004588 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004589 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004590 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004591 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004592 return; // No dex cache slow path.
4593 }
4594 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004595 uint32_t address = dchecked_integral_cast<uint32_t>(
4596 reinterpret_cast<uintptr_t>(load->GetString().Get()));
4597 DCHECK_NE(address, 0u);
4598 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004599 return; // No dex cache slow path.
4600 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004601 case HLoadString::LoadKind::kBssEntry: {
4602 // Add ADRP with its PC-relative String .bss entry patch.
4603 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004604 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004605 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004606 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4607 Register temp = temps.AcquireX();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004608 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004609 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004610 // Add LDR with its PC-relative String patch.
4611 vixl::aarch64::Label* ldr_label =
4612 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004613 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00004614 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004615 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004616 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01004617 /* offset placeholder */ 0u,
4618 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004619 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004620 SlowPathCodeARM64* slow_path =
4621 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004622 codegen_->AddSlowPath(slow_path);
4623 __ Cbz(out.X(), slow_path->GetEntryLabel());
4624 __ Bind(slow_path->GetExitLabel());
4625 return;
4626 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004627 case HLoadString::LoadKind::kJitTableAddress: {
4628 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004629 load->GetStringIndex(),
4630 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004631 GenerateGcRootFieldLoad(load,
4632 out_loc,
4633 out.X(),
4634 /* offset */ 0,
4635 /* fixup_label */ nullptr,
4636 kCompilerReadBarrierOption);
4637 return;
4638 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004639 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004640 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004641 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004642
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004643 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004644 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004645 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004646 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004647 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4648 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004649}
4650
Alexandre Rames5319def2014-10-23 10:03:10 +01004651void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4652 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4653 locations->SetOut(Location::ConstantLocation(constant));
4654}
4655
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004656void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004657 // Will be generated at use site.
4658}
4659
Alexandre Rames67555f72014-11-18 10:55:16 +00004660void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4661 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004662 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004663 InvokeRuntimeCallingConvention calling_convention;
4664 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4665}
4666
4667void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004668 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004669 instruction,
4670 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004671 if (instruction->IsEnter()) {
4672 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4673 } else {
4674 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4675 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004676}
4677
Alexandre Rames42d641b2014-10-27 14:00:51 +00004678void LocationsBuilderARM64::VisitMul(HMul* mul) {
4679 LocationSummary* locations =
4680 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4681 switch (mul->GetResultType()) {
4682 case Primitive::kPrimInt:
4683 case Primitive::kPrimLong:
4684 locations->SetInAt(0, Location::RequiresRegister());
4685 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004686 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004687 break;
4688
4689 case Primitive::kPrimFloat:
4690 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004691 locations->SetInAt(0, Location::RequiresFpuRegister());
4692 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004693 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004694 break;
4695
4696 default:
4697 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4698 }
4699}
4700
4701void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4702 switch (mul->GetResultType()) {
4703 case Primitive::kPrimInt:
4704 case Primitive::kPrimLong:
4705 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4706 break;
4707
4708 case Primitive::kPrimFloat:
4709 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004710 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004711 break;
4712
4713 default:
4714 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4715 }
4716}
4717
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004718void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4719 LocationSummary* locations =
4720 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4721 switch (neg->GetResultType()) {
4722 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004723 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004724 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004725 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004726 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004727
4728 case Primitive::kPrimFloat:
4729 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004730 locations->SetInAt(0, Location::RequiresFpuRegister());
4731 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004732 break;
4733
4734 default:
4735 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4736 }
4737}
4738
4739void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4740 switch (neg->GetResultType()) {
4741 case Primitive::kPrimInt:
4742 case Primitive::kPrimLong:
4743 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4744 break;
4745
4746 case Primitive::kPrimFloat:
4747 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004748 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004749 break;
4750
4751 default:
4752 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4753 }
4754}
4755
4756void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4757 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004758 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004759 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004760 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004761 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4762 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004763}
4764
4765void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004766 // Note: if heap poisoning is enabled, the entry point takes cares
4767 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00004768 QuickEntrypointEnum entrypoint =
4769 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4770 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004771 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004772}
4773
Alexandre Rames5319def2014-10-23 10:03:10 +01004774void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4775 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004776 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004777 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004778 if (instruction->IsStringAlloc()) {
4779 locations->AddTemp(LocationFrom(kArtMethodRegister));
4780 } else {
4781 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004782 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004783 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4784}
4785
4786void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004787 // Note: if heap poisoning is enabled, the entry point takes cares
4788 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004789 if (instruction->IsStringAlloc()) {
4790 // String is allocated through StringFactory. Call NewEmptyString entry point.
4791 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004792 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004793 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4794 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4795 __ Blr(lr);
4796 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4797 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004798 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004799 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004800 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004801}
4802
4803void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4804 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004805 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004806 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004807}
4808
4809void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004810 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004811 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004812 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004813 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004814 break;
4815
4816 default:
4817 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4818 }
4819}
4820
David Brazdil66d126e2015-04-03 16:02:44 +01004821void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4822 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4823 locations->SetInAt(0, Location::RequiresRegister());
4824 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4825}
4826
4827void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004828 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004829}
4830
Alexandre Rames5319def2014-10-23 10:03:10 +01004831void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004832 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4833 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01004834}
4835
Calin Juravle2ae48182016-03-16 14:05:09 +00004836void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4837 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004838 return;
4839 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004840
Alexandre Ramesd921d642015-04-16 15:07:16 +01004841 BlockPoolsScope block_pools(GetVIXLAssembler());
4842 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004843 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004844 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004845}
4846
Calin Juravle2ae48182016-03-16 14:05:09 +00004847void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004848 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004849 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004850
4851 LocationSummary* locations = instruction->GetLocations();
4852 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004853
4854 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004855}
4856
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004857void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004858 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004859}
4860
Alexandre Rames67555f72014-11-18 10:55:16 +00004861void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4862 HandleBinaryOp(instruction);
4863}
4864
4865void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4866 HandleBinaryOp(instruction);
4867}
4868
Alexandre Rames3e69f162014-12-10 10:36:50 +00004869void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4870 LOG(FATAL) << "Unreachable";
4871}
4872
4873void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4874 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4875}
4876
Alexandre Rames5319def2014-10-23 10:03:10 +01004877void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4878 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4879 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4880 if (location.IsStackSlot()) {
4881 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4882 } else if (location.IsDoubleStackSlot()) {
4883 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4884 }
4885 locations->SetOut(location);
4886}
4887
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004888void InstructionCodeGeneratorARM64::VisitParameterValue(
4889 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004890 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004891}
4892
4893void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4894 LocationSummary* locations =
4895 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004896 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004897}
4898
4899void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4900 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4901 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004902}
4903
4904void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4905 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004906 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004907 locations->SetInAt(i, Location::Any());
4908 }
4909 locations->SetOut(Location::Any());
4910}
4911
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004912void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004913 LOG(FATAL) << "Unreachable";
4914}
4915
Serban Constantinescu02164b32014-11-13 14:05:07 +00004916void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004917 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004918 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004919 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4920 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004921 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4922
4923 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004924 case Primitive::kPrimInt:
4925 case Primitive::kPrimLong:
4926 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004927 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004928 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4929 break;
4930
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004931 case Primitive::kPrimFloat:
4932 case Primitive::kPrimDouble: {
4933 InvokeRuntimeCallingConvention calling_convention;
4934 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4935 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4936 locations->SetOut(calling_convention.GetReturnLocation(type));
4937
4938 break;
4939 }
4940
Serban Constantinescu02164b32014-11-13 14:05:07 +00004941 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004942 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004943 }
4944}
4945
4946void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4947 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004948
Serban Constantinescu02164b32014-11-13 14:05:07 +00004949 switch (type) {
4950 case Primitive::kPrimInt:
4951 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004952 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004953 break;
4954 }
4955
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004956 case Primitive::kPrimFloat:
4957 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004958 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4959 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004960 if (type == Primitive::kPrimFloat) {
4961 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4962 } else {
4963 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4964 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004965 break;
4966 }
4967
Serban Constantinescu02164b32014-11-13 14:05:07 +00004968 default:
4969 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004970 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004971 }
4972}
4973
Calin Juravle27df7582015-04-17 19:12:31 +01004974void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4975 memory_barrier->SetLocations(nullptr);
4976}
4977
4978void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004979 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004980}
4981
Alexandre Rames5319def2014-10-23 10:03:10 +01004982void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4983 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4984 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004985 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004986}
4987
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004988void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004989 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004990}
4991
4992void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4993 instruction->SetLocations(nullptr);
4994}
4995
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004996void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004997 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004998}
4999
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005000void LocationsBuilderARM64::VisitRor(HRor* ror) {
5001 HandleBinaryOp(ror);
5002}
5003
5004void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5005 HandleBinaryOp(ror);
5006}
5007
Serban Constantinescu02164b32014-11-13 14:05:07 +00005008void LocationsBuilderARM64::VisitShl(HShl* shl) {
5009 HandleShift(shl);
5010}
5011
5012void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5013 HandleShift(shl);
5014}
5015
5016void LocationsBuilderARM64::VisitShr(HShr* shr) {
5017 HandleShift(shr);
5018}
5019
5020void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5021 HandleShift(shr);
5022}
5023
Alexandre Rames5319def2014-10-23 10:03:10 +01005024void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005025 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005026}
5027
5028void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005029 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005030}
5031
Alexandre Rames67555f72014-11-18 10:55:16 +00005032void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005033 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00005034}
5035
5036void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005037 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005038}
5039
5040void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005041 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005042}
5043
Alexandre Rames67555f72014-11-18 10:55:16 +00005044void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005045 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005046}
5047
Calin Juravlee460d1d2015-09-29 04:52:17 +01005048void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5049 HUnresolvedInstanceFieldGet* instruction) {
5050 FieldAccessCallingConventionARM64 calling_convention;
5051 codegen_->CreateUnresolvedFieldLocationSummary(
5052 instruction, instruction->GetFieldType(), calling_convention);
5053}
5054
5055void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5056 HUnresolvedInstanceFieldGet* instruction) {
5057 FieldAccessCallingConventionARM64 calling_convention;
5058 codegen_->GenerateUnresolvedFieldAccess(instruction,
5059 instruction->GetFieldType(),
5060 instruction->GetFieldIndex(),
5061 instruction->GetDexPc(),
5062 calling_convention);
5063}
5064
5065void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5066 HUnresolvedInstanceFieldSet* instruction) {
5067 FieldAccessCallingConventionARM64 calling_convention;
5068 codegen_->CreateUnresolvedFieldLocationSummary(
5069 instruction, instruction->GetFieldType(), calling_convention);
5070}
5071
5072void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5073 HUnresolvedInstanceFieldSet* instruction) {
5074 FieldAccessCallingConventionARM64 calling_convention;
5075 codegen_->GenerateUnresolvedFieldAccess(instruction,
5076 instruction->GetFieldType(),
5077 instruction->GetFieldIndex(),
5078 instruction->GetDexPc(),
5079 calling_convention);
5080}
5081
5082void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5083 HUnresolvedStaticFieldGet* instruction) {
5084 FieldAccessCallingConventionARM64 calling_convention;
5085 codegen_->CreateUnresolvedFieldLocationSummary(
5086 instruction, instruction->GetFieldType(), calling_convention);
5087}
5088
5089void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5090 HUnresolvedStaticFieldGet* instruction) {
5091 FieldAccessCallingConventionARM64 calling_convention;
5092 codegen_->GenerateUnresolvedFieldAccess(instruction,
5093 instruction->GetFieldType(),
5094 instruction->GetFieldIndex(),
5095 instruction->GetDexPc(),
5096 calling_convention);
5097}
5098
5099void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5100 HUnresolvedStaticFieldSet* instruction) {
5101 FieldAccessCallingConventionARM64 calling_convention;
5102 codegen_->CreateUnresolvedFieldLocationSummary(
5103 instruction, instruction->GetFieldType(), calling_convention);
5104}
5105
5106void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5107 HUnresolvedStaticFieldSet* instruction) {
5108 FieldAccessCallingConventionARM64 calling_convention;
5109 codegen_->GenerateUnresolvedFieldAccess(instruction,
5110 instruction->GetFieldType(),
5111 instruction->GetFieldIndex(),
5112 instruction->GetDexPc(),
5113 calling_convention);
5114}
5115
Alexandre Rames5319def2014-10-23 10:03:10 +01005116void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005117 LocationSummary* locations =
5118 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005119 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01005120}
5121
5122void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005123 HBasicBlock* block = instruction->GetBlock();
5124 if (block->GetLoopInformation() != nullptr) {
5125 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5126 // The back edge will generate the suspend check.
5127 return;
5128 }
5129 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5130 // The goto will generate the suspend check.
5131 return;
5132 }
5133 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005134}
5135
Alexandre Rames67555f72014-11-18 10:55:16 +00005136void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5137 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005138 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005139 InvokeRuntimeCallingConvention calling_convention;
5140 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5141}
5142
5143void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005144 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005145 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005146}
5147
5148void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5149 LocationSummary* locations =
5150 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5151 Primitive::Type input_type = conversion->GetInputType();
5152 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005153 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005154 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5155 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5156 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5157 }
5158
Alexandre Rames542361f2015-01-29 16:57:31 +00005159 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005160 locations->SetInAt(0, Location::RequiresFpuRegister());
5161 } else {
5162 locations->SetInAt(0, Location::RequiresRegister());
5163 }
5164
Alexandre Rames542361f2015-01-29 16:57:31 +00005165 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005166 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5167 } else {
5168 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5169 }
5170}
5171
5172void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5173 Primitive::Type result_type = conversion->GetResultType();
5174 Primitive::Type input_type = conversion->GetInputType();
5175
5176 DCHECK_NE(input_type, result_type);
5177
Alexandre Rames542361f2015-01-29 16:57:31 +00005178 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005179 int result_size = Primitive::ComponentSize(result_type);
5180 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005181 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005182 Register output = OutputRegister(conversion);
5183 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005184 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005185 // 'int' values are used directly as W registers, discarding the top
5186 // bits, so we don't need to sign-extend and can just perform a move.
5187 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5188 // top 32 bits of the target register. We theoretically could leave those
5189 // bits unchanged, but we would have to make sure that no code uses a
5190 // 32bit input value as a 64bit value assuming that the top 32 bits are
5191 // zero.
5192 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005193 } else if (result_type == Primitive::kPrimChar ||
5194 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5195 __ Ubfx(output,
5196 output.IsX() ? source.X() : source.W(),
5197 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005198 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005199 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005200 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005201 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005202 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005203 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005204 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5205 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005206 } else if (Primitive::IsFloatingPointType(result_type) &&
5207 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005208 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5209 } else {
5210 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5211 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005212 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005213}
Alexandre Rames67555f72014-11-18 10:55:16 +00005214
Serban Constantinescu02164b32014-11-13 14:05:07 +00005215void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5216 HandleShift(ushr);
5217}
5218
5219void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5220 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005221}
5222
5223void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5224 HandleBinaryOp(instruction);
5225}
5226
5227void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5228 HandleBinaryOp(instruction);
5229}
5230
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005231void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005232 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005233 LOG(FATAL) << "Unreachable";
5234}
5235
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005236void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005237 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005238 LOG(FATAL) << "Unreachable";
5239}
5240
Mark Mendellfe57faa2015-09-18 09:26:15 -04005241// Simple implementation of packed switch - generate cascaded compare/jumps.
5242void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5243 LocationSummary* locations =
5244 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5245 locations->SetInAt(0, Location::RequiresRegister());
5246}
5247
5248void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5249 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005250 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005251 Register value_reg = InputRegisterAt(switch_instr, 0);
5252 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5253
Zheng Xu3927c8b2015-11-18 17:46:25 +08005254 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005255 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005256 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5257 // make sure we don't emit it if the target may run out of range.
5258 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5259 // ranges and emit the tables only as required.
5260 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005261
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005262 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005263 // Current instruction id is an upper bound of the number of HIRs in the graph.
5264 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5265 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005266 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5267 Register temp = temps.AcquireW();
5268 __ Subs(temp, value_reg, Operand(lower_bound));
5269
Zheng Xu3927c8b2015-11-18 17:46:25 +08005270 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005271 // Jump to successors[0] if value == lower_bound.
5272 __ B(eq, codegen_->GetLabelOf(successors[0]));
5273 int32_t last_index = 0;
5274 for (; num_entries - last_index > 2; last_index += 2) {
5275 __ Subs(temp, temp, Operand(2));
5276 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5277 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5278 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5279 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5280 }
5281 if (num_entries - last_index == 2) {
5282 // The last missing case_value.
5283 __ Cmp(temp, Operand(1));
5284 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005285 }
5286
5287 // And the default for any other value.
5288 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5289 __ B(codegen_->GetLabelOf(default_block));
5290 }
5291 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005292 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005293
5294 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5295
5296 // Below instructions should use at most one blocked register. Since there are two blocked
5297 // registers, we are free to block one.
5298 Register temp_w = temps.AcquireW();
5299 Register index;
5300 // Remove the bias.
5301 if (lower_bound != 0) {
5302 index = temp_w;
5303 __ Sub(index, value_reg, Operand(lower_bound));
5304 } else {
5305 index = value_reg;
5306 }
5307
5308 // Jump to default block if index is out of the range.
5309 __ Cmp(index, Operand(num_entries));
5310 __ B(hs, codegen_->GetLabelOf(default_block));
5311
5312 // In current VIXL implementation, it won't require any blocked registers to encode the
5313 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5314 // register pressure.
5315 Register table_base = temps.AcquireX();
5316 // Load jump offset from the table.
5317 __ Adr(table_base, jump_table->GetTableStartLabel());
5318 Register jump_offset = temp_w;
5319 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5320
5321 // Jump to target block by branching to table_base(pc related) + offset.
5322 Register target_address = table_base;
5323 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5324 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005325 }
5326}
5327
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005328void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5329 HInstruction* instruction,
5330 Location out,
5331 uint32_t offset,
5332 Location maybe_temp,
5333 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005334 Primitive::Type type = Primitive::kPrimNot;
5335 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005336 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005337 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005338 Register temp_reg = RegisterFrom(maybe_temp, type);
5339 if (kUseBakerReadBarrier) {
5340 // Load with fast path based Baker's read barrier.
5341 // /* HeapReference<Object> */ out = *(out + offset)
5342 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5343 out,
5344 out_reg,
5345 offset,
5346 temp_reg,
5347 /* needs_null_check */ false,
5348 /* use_load_acquire */ false);
5349 } else {
5350 // Load with slow path based read barrier.
5351 // Save the value of `out` into `maybe_temp` before overwriting it
5352 // in the following move operation, as we will need it for the
5353 // read barrier below.
5354 __ Mov(temp_reg, out_reg);
5355 // /* HeapReference<Object> */ out = *(out + offset)
5356 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5357 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5358 }
5359 } else {
5360 // Plain load with no read barrier.
5361 // /* HeapReference<Object> */ out = *(out + offset)
5362 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5363 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5364 }
5365}
5366
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005367void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5368 HInstruction* instruction,
5369 Location out,
5370 Location obj,
5371 uint32_t offset,
5372 Location maybe_temp,
5373 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005374 Primitive::Type type = Primitive::kPrimNot;
5375 Register out_reg = RegisterFrom(out, type);
5376 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005377 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005378 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005379 if (kUseBakerReadBarrier) {
5380 // Load with fast path based Baker's read barrier.
5381 Register temp_reg = RegisterFrom(maybe_temp, type);
5382 // /* HeapReference<Object> */ out = *(obj + offset)
5383 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5384 out,
5385 obj_reg,
5386 offset,
5387 temp_reg,
5388 /* needs_null_check */ false,
5389 /* use_load_acquire */ false);
5390 } else {
5391 // Load with slow path based read barrier.
5392 // /* HeapReference<Object> */ out = *(obj + offset)
5393 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5394 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5395 }
5396 } else {
5397 // Plain load with no read barrier.
5398 // /* HeapReference<Object> */ out = *(obj + offset)
5399 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5400 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5401 }
5402}
5403
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005404void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5405 HInstruction* instruction,
5406 Location root,
5407 Register obj,
5408 uint32_t offset,
5409 vixl::aarch64::Label* fixup_label,
5410 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005411 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005412 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005413 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005414 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005415 if (kUseBakerReadBarrier) {
5416 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5417 // Baker's read barrier are used:
5418 //
5419 // root = obj.field;
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005420 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5421 // if (temp != null) {
5422 // root = temp(root)
Roland Levillain44015862016-01-22 11:47:17 +00005423 // }
5424
5425 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005426 if (fixup_label == nullptr) {
5427 __ Ldr(root_reg, MemOperand(obj, offset));
5428 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005429 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005430 }
Roland Levillain44015862016-01-22 11:47:17 +00005431 static_assert(
5432 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5433 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5434 "have different sizes.");
5435 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5436 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5437 "have different sizes.");
5438
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005439 Register temp = lr;
Roland Levillain44015862016-01-22 11:47:17 +00005440
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005441 // Slow path marking the GC root `root`. The entrypoint will alrady be loaded in temp.
5442 SlowPathCodeARM64* slow_path =
5443 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction,
5444 root,
5445 LocationFrom(temp));
5446 codegen_->AddSlowPath(slow_path);
5447 const int32_t entry_point_offset =
5448 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5449 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5450 // Loading the entrypoint does not require a load acquire since it is only changed when
5451 // threads are suspended or running a checkpoint.
5452 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5453 // The entrypoint is null when the GC is not marking, this prevents one load compared to
5454 // checking GetIsGcMarking.
Roland Levillain44015862016-01-22 11:47:17 +00005455 __ Cbnz(temp, slow_path->GetEntryLabel());
5456 __ Bind(slow_path->GetExitLabel());
5457 } else {
5458 // GC root loaded through a slow path for read barriers other
5459 // than Baker's.
5460 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005461 if (fixup_label == nullptr) {
5462 __ Add(root_reg.X(), obj.X(), offset);
5463 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005464 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005465 }
Roland Levillain44015862016-01-22 11:47:17 +00005466 // /* mirror::Object* */ root = root->Read()
5467 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5468 }
5469 } else {
5470 // Plain GC root load with no read barrier.
5471 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005472 if (fixup_label == nullptr) {
5473 __ Ldr(root_reg, MemOperand(obj, offset));
5474 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005475 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005476 }
Roland Levillain44015862016-01-22 11:47:17 +00005477 // Note that GC roots are not affected by heap poisoning, thus we
5478 // do not have to unpoison `root_reg` here.
5479 }
5480}
5481
5482void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5483 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005484 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005485 uint32_t offset,
5486 Register temp,
5487 bool needs_null_check,
5488 bool use_load_acquire) {
5489 DCHECK(kEmitCompilerReadBarrier);
5490 DCHECK(kUseBakerReadBarrier);
5491
5492 // /* HeapReference<Object> */ ref = *(obj + offset)
5493 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005494 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01005495 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5496 ref,
5497 obj,
5498 offset,
5499 no_index,
5500 no_scale_factor,
5501 temp,
5502 needs_null_check,
5503 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005504}
5505
5506void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5507 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005508 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005509 uint32_t data_offset,
5510 Location index,
5511 Register temp,
5512 bool needs_null_check) {
5513 DCHECK(kEmitCompilerReadBarrier);
5514 DCHECK(kUseBakerReadBarrier);
5515
5516 // Array cells are never volatile variables, therefore array loads
5517 // never use Load-Acquire instructions on ARM64.
5518 const bool use_load_acquire = false;
5519
Roland Levillainbfea3352016-06-23 13:48:47 +01005520 static_assert(
5521 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5522 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005523 // /* HeapReference<Object> */ ref =
5524 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005525 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5526 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5527 ref,
5528 obj,
5529 data_offset,
5530 index,
5531 scale_factor,
5532 temp,
5533 needs_null_check,
5534 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005535}
5536
5537void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5538 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005539 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005540 uint32_t offset,
5541 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005542 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005543 Register temp,
5544 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005545 bool use_load_acquire,
5546 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00005547 DCHECK(kEmitCompilerReadBarrier);
5548 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005549 // If we are emitting an array load, we should not be using a
5550 // Load Acquire instruction. In other words:
5551 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5552 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005553
5554 MacroAssembler* masm = GetVIXLAssembler();
5555 UseScratchRegisterScope temps(masm);
5556
5557 // In slow path based read barriers, the read barrier call is
5558 // inserted after the original load. However, in fast path based
5559 // Baker's read barriers, we need to perform the load of
5560 // mirror::Object::monitor_ *before* the original reference load.
5561 // This load-load ordering is required by the read barrier.
5562 // The fast path/slow path (for Baker's algorithm) should look like:
5563 //
5564 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5565 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5566 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005567 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain44015862016-01-22 11:47:17 +00005568 // if (is_gray) {
5569 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5570 // }
5571 //
5572 // Note: the original implementation in ReadBarrier::Barrier is
5573 // slightly more complex as it performs additional checks that we do
5574 // not do here for performance reasons.
5575
5576 Primitive::Type type = Primitive::kPrimNot;
5577 Register ref_reg = RegisterFrom(ref, type);
5578 DCHECK(obj.IsW());
5579 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5580
5581 // /* int32_t */ monitor = obj->monitor_
5582 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5583 if (needs_null_check) {
5584 MaybeRecordImplicitNullCheck(instruction);
5585 }
5586 // /* LockWord */ lock_word = LockWord(monitor)
5587 static_assert(sizeof(LockWord) == sizeof(int32_t),
5588 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005589
Vladimir Marko877a0332016-07-11 19:30:56 +01005590 // Introduce a dependency on the lock_word including rb_state,
5591 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005592 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005593 // `obj` is unchanged by this operation, but its value now depends
5594 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005595 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005596
5597 // The actual reference load.
5598 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005599 // Load types involving an "index": ArrayGet,
5600 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5601 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01005602 if (use_load_acquire) {
5603 // UnsafeGetObjectVolatile intrinsic case.
5604 // Register `index` is not an index in an object array, but an
5605 // offset to an object reference field within object `obj`.
5606 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5607 DCHECK(instruction->GetLocations()->Intrinsified());
5608 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5609 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005610 DCHECK_EQ(offset, 0u);
5611 DCHECK_EQ(scale_factor, 0u);
5612 DCHECK_EQ(needs_null_check, 0u);
Roland Levillainbfea3352016-06-23 13:48:47 +01005613 // /* HeapReference<Object> */ ref = *(obj + index)
5614 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5615 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005616 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005617 // ArrayGet and UnsafeGetObject intrinsics cases.
5618 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5619 if (index.IsConstant()) {
5620 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5621 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5622 } else {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005623 Register temp3 = temps.AcquireW();
5624 __ Add(temp3, obj, offset);
5625 Load(type, ref_reg, HeapOperand(temp3, XRegisterFrom(index), LSL, scale_factor));
5626 temps.Release(temp3);
Roland Levillainbfea3352016-06-23 13:48:47 +01005627 }
Roland Levillain44015862016-01-22 11:47:17 +00005628 }
Roland Levillain44015862016-01-22 11:47:17 +00005629 } else {
5630 // /* HeapReference<Object> */ ref = *(obj + offset)
5631 MemOperand field = HeapOperand(obj, offset);
5632 if (use_load_acquire) {
5633 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5634 } else {
5635 Load(type, ref_reg, field);
5636 }
5637 }
5638
5639 // Object* ref = ref_addr->AsMirrorPtr()
5640 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5641
Vladimir Marko953437b2016-08-24 08:30:46 +00005642 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005643 SlowPathCodeARM64* slow_path;
5644 if (always_update_field) {
5645 // ReadBarrierMarkAndUpdateFieldSlowPathARM64 only supports
5646 // address of the form `obj + field_offset`, where `obj` is a
5647 // register and `field_offset` is a register. Thus `offset` and
5648 // `scale_factor` above are expected to be null in this code path.
5649 DCHECK_EQ(offset, 0u);
5650 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
5651 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM64(
5652 instruction, ref, obj, /* field_offset */ index, temp);
5653 } else {
5654 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
5655 }
Roland Levillain44015862016-01-22 11:47:17 +00005656 AddSlowPath(slow_path);
5657
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005658 // if (rb_state == ReadBarrier::GrayState())
Roland Levillain44015862016-01-22 11:47:17 +00005659 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005660 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005661 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5662 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko877a0332016-07-11 19:30:56 +01005663 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005664 __ Bind(slow_path->GetExitLabel());
5665}
5666
5667void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5668 Location out,
5669 Location ref,
5670 Location obj,
5671 uint32_t offset,
5672 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005673 DCHECK(kEmitCompilerReadBarrier);
5674
Roland Levillain44015862016-01-22 11:47:17 +00005675 // Insert a slow path based read barrier *after* the reference load.
5676 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005677 // If heap poisoning is enabled, the unpoisoning of the loaded
5678 // reference will be carried out by the runtime within the slow
5679 // path.
5680 //
5681 // Note that `ref` currently does not get unpoisoned (when heap
5682 // poisoning is enabled), which is alright as the `ref` argument is
5683 // not used by the artReadBarrierSlow entry point.
5684 //
5685 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5686 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5687 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5688 AddSlowPath(slow_path);
5689
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005690 __ B(slow_path->GetEntryLabel());
5691 __ Bind(slow_path->GetExitLabel());
5692}
5693
Roland Levillain44015862016-01-22 11:47:17 +00005694void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5695 Location out,
5696 Location ref,
5697 Location obj,
5698 uint32_t offset,
5699 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005700 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005701 // Baker's read barriers shall be handled by the fast path
5702 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5703 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005704 // If heap poisoning is enabled, unpoisoning will be taken care of
5705 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005706 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005707 } else if (kPoisonHeapReferences) {
5708 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5709 }
5710}
5711
Roland Levillain44015862016-01-22 11:47:17 +00005712void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5713 Location out,
5714 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005715 DCHECK(kEmitCompilerReadBarrier);
5716
Roland Levillain44015862016-01-22 11:47:17 +00005717 // Insert a slow path based read barrier *after* the GC root load.
5718 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005719 // Note that GC roots are not affected by heap poisoning, so we do
5720 // not need to do anything special for this here.
5721 SlowPathCodeARM64* slow_path =
5722 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5723 AddSlowPath(slow_path);
5724
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005725 __ B(slow_path->GetEntryLabel());
5726 __ Bind(slow_path->GetExitLabel());
5727}
5728
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005729void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5730 LocationSummary* locations =
5731 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5732 locations->SetInAt(0, Location::RequiresRegister());
5733 locations->SetOut(Location::RequiresRegister());
5734}
5735
5736void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5737 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005738 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005739 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005740 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005741 __ Ldr(XRegisterFrom(locations->Out()),
5742 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005743 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005744 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005745 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005746 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5747 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005748 __ Ldr(XRegisterFrom(locations->Out()),
5749 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005750 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005751}
5752
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005753static void PatchJitRootUse(uint8_t* code,
5754 const uint8_t* roots_data,
5755 vixl::aarch64::Literal<uint32_t>* literal,
5756 uint64_t index_in_table) {
5757 uint32_t literal_offset = literal->GetOffset();
5758 uintptr_t address =
5759 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
5760 uint8_t* data = code + literal_offset;
5761 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
5762}
5763
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005764void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
5765 for (const auto& entry : jit_string_patches_) {
5766 const auto& it = jit_string_roots_.find(entry.first);
5767 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005768 PatchJitRootUse(code, roots_data, entry.second, it->second);
5769 }
5770 for (const auto& entry : jit_class_patches_) {
5771 const auto& it = jit_class_roots_.find(entry.first);
5772 DCHECK(it != jit_class_roots_.end());
5773 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005774 }
5775}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005776
Alexandre Rames67555f72014-11-18 10:55:16 +00005777#undef __
5778#undef QUICK_ENTRY_POINT
5779
Alexandre Rames5319def2014-10-23 10:03:10 +01005780} // namespace arm64
5781} // namespace art