blob: e685d598d66e468e33917d4f03eca71682fb6e42 [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()) {
1536 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant()) ? xzr : wzr;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001537 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001538 if (src_cst->IsIntConstant()) {
1539 temp = temps.AcquireW();
1540 } else if (src_cst->IsLongConstant()) {
1541 temp = temps.AcquireX();
1542 } else if (src_cst->IsFloatConstant()) {
1543 temp = temps.AcquireS();
1544 } else {
1545 DCHECK(src_cst->IsDoubleConstant());
1546 temp = temps.AcquireD();
1547 }
1548 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001549 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001550 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001551 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001552 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001553 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001554 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001555 // Use any scratch register (a core or a floating-point one)
1556 // from VIXL scratch register pools as a temporary.
1557 //
1558 // We used to only use the FP scratch register pool, but in some
1559 // rare cases the only register from this pool (D31) would
1560 // already be used (e.g. within a ParallelMove instruction, when
1561 // a move is blocked by a another move requiring a scratch FP
1562 // register, which would reserve D31). To prevent this issue, we
1563 // ask for a scratch register of any type (core or FP).
1564 CPURegister temp =
1565 temps.AcquireCPURegisterOfSize(destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001566 __ Ldr(temp, StackOperandFrom(source));
1567 __ Str(temp, StackOperandFrom(destination));
1568 }
1569 }
1570}
1571
1572void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001573 CPURegister dst,
1574 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001575 switch (type) {
1576 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001577 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001578 break;
1579 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001580 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001581 break;
1582 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001583 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001584 break;
1585 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001586 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001587 break;
1588 case Primitive::kPrimInt:
1589 case Primitive::kPrimNot:
1590 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001591 case Primitive::kPrimFloat:
1592 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001593 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001594 __ Ldr(dst, src);
1595 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001596 case Primitive::kPrimVoid:
1597 LOG(FATAL) << "Unreachable type " << type;
1598 }
1599}
1600
Calin Juravle77520bc2015-01-12 18:45:46 +00001601void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001602 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001603 const MemOperand& src,
1604 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001605 MacroAssembler* masm = GetVIXLAssembler();
1606 BlockPoolsScope block_pools(masm);
1607 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001608 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001609 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001610
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001611 DCHECK(!src.IsPreIndex());
1612 DCHECK(!src.IsPostIndex());
1613
1614 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001615 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001616 MemOperand base = MemOperand(temp_base);
1617 switch (type) {
1618 case Primitive::kPrimBoolean:
1619 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001620 if (needs_null_check) {
1621 MaybeRecordImplicitNullCheck(instruction);
1622 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001623 break;
1624 case Primitive::kPrimByte:
1625 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001626 if (needs_null_check) {
1627 MaybeRecordImplicitNullCheck(instruction);
1628 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001629 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1630 break;
1631 case Primitive::kPrimChar:
1632 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001633 if (needs_null_check) {
1634 MaybeRecordImplicitNullCheck(instruction);
1635 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001636 break;
1637 case Primitive::kPrimShort:
1638 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001639 if (needs_null_check) {
1640 MaybeRecordImplicitNullCheck(instruction);
1641 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001642 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1643 break;
1644 case Primitive::kPrimInt:
1645 case Primitive::kPrimNot:
1646 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001647 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001648 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001649 if (needs_null_check) {
1650 MaybeRecordImplicitNullCheck(instruction);
1651 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001652 break;
1653 case Primitive::kPrimFloat:
1654 case Primitive::kPrimDouble: {
1655 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001656 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001657
1658 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1659 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001660 if (needs_null_check) {
1661 MaybeRecordImplicitNullCheck(instruction);
1662 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001663 __ Fmov(FPRegister(dst), temp);
1664 break;
1665 }
1666 case Primitive::kPrimVoid:
1667 LOG(FATAL) << "Unreachable type " << type;
1668 }
1669}
1670
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001671void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001672 CPURegister src,
1673 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001674 switch (type) {
1675 case Primitive::kPrimBoolean:
1676 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001677 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001678 break;
1679 case Primitive::kPrimChar:
1680 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001681 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001682 break;
1683 case Primitive::kPrimInt:
1684 case Primitive::kPrimNot:
1685 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001686 case Primitive::kPrimFloat:
1687 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001688 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001689 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001690 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001691 case Primitive::kPrimVoid:
1692 LOG(FATAL) << "Unreachable type " << type;
1693 }
1694}
1695
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001696void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1697 CPURegister src,
1698 const MemOperand& dst) {
1699 UseScratchRegisterScope temps(GetVIXLAssembler());
1700 Register temp_base = temps.AcquireX();
1701
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001702 DCHECK(!dst.IsPreIndex());
1703 DCHECK(!dst.IsPostIndex());
1704
1705 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001706 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001707 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001708 MemOperand base = MemOperand(temp_base);
1709 switch (type) {
1710 case Primitive::kPrimBoolean:
1711 case Primitive::kPrimByte:
1712 __ Stlrb(Register(src), base);
1713 break;
1714 case Primitive::kPrimChar:
1715 case Primitive::kPrimShort:
1716 __ Stlrh(Register(src), base);
1717 break;
1718 case Primitive::kPrimInt:
1719 case Primitive::kPrimNot:
1720 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001721 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001722 __ Stlr(Register(src), base);
1723 break;
1724 case Primitive::kPrimFloat:
1725 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001726 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001727 Register temp_src;
1728 if (src.IsZero()) {
1729 // The zero register is used to avoid synthesizing zero constants.
1730 temp_src = Register(src);
1731 } else {
1732 DCHECK(src.IsFPRegister());
1733 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1734 __ Fmov(temp_src, FPRegister(src));
1735 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001736
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001737 __ Stlr(temp_src, base);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001738 break;
1739 }
1740 case Primitive::kPrimVoid:
1741 LOG(FATAL) << "Unreachable type " << type;
1742 }
1743}
1744
Calin Juravle175dc732015-08-25 15:42:32 +01001745void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1746 HInstruction* instruction,
1747 uint32_t dex_pc,
1748 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001749 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001750 GenerateInvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001751 if (EntrypointRequiresStackMap(entrypoint)) {
1752 RecordPcInfo(instruction, dex_pc, slow_path);
1753 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001754}
1755
Roland Levillaindec8f632016-07-22 17:10:06 +01001756void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1757 HInstruction* instruction,
1758 SlowPathCode* slow_path) {
1759 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001760 GenerateInvokeRuntime(entry_point_offset);
1761}
1762
1763void CodeGeneratorARM64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001764 BlockPoolsScope block_pools(GetVIXLAssembler());
1765 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1766 __ Blr(lr);
1767}
1768
Alexandre Rames67555f72014-11-18 10:55:16 +00001769void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001770 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001771 UseScratchRegisterScope temps(GetVIXLAssembler());
1772 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001773 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1774
Serban Constantinescu02164b32014-11-13 14:05:07 +00001775 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001776 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1777 __ Add(temp, class_reg, status_offset);
1778 __ Ldar(temp, HeapOperand(temp));
1779 __ Cmp(temp, mirror::Class::kStatusInitialized);
1780 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001781 __ Bind(slow_path->GetExitLabel());
1782}
Alexandre Rames5319def2014-10-23 10:03:10 +01001783
Roland Levillain44015862016-01-22 11:47:17 +00001784void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001785 BarrierType type = BarrierAll;
1786
1787 switch (kind) {
1788 case MemBarrierKind::kAnyAny:
1789 case MemBarrierKind::kAnyStore: {
1790 type = BarrierAll;
1791 break;
1792 }
1793 case MemBarrierKind::kLoadAny: {
1794 type = BarrierReads;
1795 break;
1796 }
1797 case MemBarrierKind::kStoreStore: {
1798 type = BarrierWrites;
1799 break;
1800 }
1801 default:
1802 LOG(FATAL) << "Unexpected memory barrier " << kind;
1803 }
1804 __ Dmb(InnerShareable, type);
1805}
1806
Serban Constantinescu02164b32014-11-13 14:05:07 +00001807void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1808 HBasicBlock* successor) {
1809 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001810 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1811 if (slow_path == nullptr) {
1812 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1813 instruction->SetSlowPath(slow_path);
1814 codegen_->AddSlowPath(slow_path);
1815 if (successor != nullptr) {
1816 DCHECK(successor->IsLoopHeader());
1817 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1818 }
1819 } else {
1820 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1821 }
1822
Serban Constantinescu02164b32014-11-13 14:05:07 +00001823 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1824 Register temp = temps.AcquireW();
1825
Andreas Gampe542451c2016-07-26 09:02:02 -07001826 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001827 if (successor == nullptr) {
1828 __ Cbnz(temp, slow_path->GetEntryLabel());
1829 __ Bind(slow_path->GetReturnLabel());
1830 } else {
1831 __ Cbz(temp, codegen_->GetLabelOf(successor));
1832 __ B(slow_path->GetEntryLabel());
1833 // slow_path will return to GetLabelOf(successor).
1834 }
1835}
1836
Alexandre Rames5319def2014-10-23 10:03:10 +01001837InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1838 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001839 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001840 assembler_(codegen->GetAssembler()),
1841 codegen_(codegen) {}
1842
1843#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001844 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001845
1846#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1847
1848enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001849 // Using a base helps identify when we hit such breakpoints.
1850 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001851#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1852 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1853#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1854};
1855
1856#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001857 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001858 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1859 } \
1860 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1861 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1862 locations->SetOut(Location::Any()); \
1863 }
1864 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1865#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1866
1867#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001868#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001869
Alexandre Rames67555f72014-11-18 10:55:16 +00001870void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001871 DCHECK_EQ(instr->InputCount(), 2U);
1872 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1873 Primitive::Type type = instr->GetResultType();
1874 switch (type) {
1875 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001876 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001877 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001878 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001879 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001880 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001881
1882 case Primitive::kPrimFloat:
1883 case Primitive::kPrimDouble:
1884 locations->SetInAt(0, Location::RequiresFpuRegister());
1885 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001886 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001887 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001888
Alexandre Rames5319def2014-10-23 10:03:10 +01001889 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001890 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001891 }
1892}
1893
Alexandre Rames09a99962015-04-15 11:47:56 +01001894void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001895 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1896
1897 bool object_field_get_with_read_barrier =
1898 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001899 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001900 new (GetGraph()->GetArena()) LocationSummary(instruction,
1901 object_field_get_with_read_barrier ?
1902 LocationSummary::kCallOnSlowPath :
1903 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001904 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001905 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00001906 // We need a temporary register for the read barrier marking slow
1907 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
1908 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko70e97462016-08-09 11:04:26 +01001909 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001910 locations->SetInAt(0, Location::RequiresRegister());
1911 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1912 locations->SetOut(Location::RequiresFpuRegister());
1913 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001914 // The output overlaps for an object field get when read barriers
1915 // are enabled: we do not want the load to overwrite the object's
1916 // location, as we need it to emit the read barrier.
1917 locations->SetOut(
1918 Location::RequiresRegister(),
1919 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001920 }
1921}
1922
1923void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1924 const FieldInfo& field_info) {
1925 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001926 LocationSummary* locations = instruction->GetLocations();
1927 Location base_loc = locations->InAt(0);
1928 Location out = locations->Out();
1929 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001930 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001931 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001932 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001933
Roland Levillain44015862016-01-22 11:47:17 +00001934 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1935 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00001936 // /* HeapReference<Object> */ out = *(base + offset)
1937 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
Roland Levillaind0b51832017-01-26 19:04:23 +00001938 Register temp = WRegisterFrom(locations->GetTemp(0));
Roland Levillain44015862016-01-22 11:47:17 +00001939 // Note that potential implicit null checks are handled in this
1940 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1941 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1942 instruction,
1943 out,
1944 base,
1945 offset,
1946 temp,
1947 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001948 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001949 } else {
1950 // General case.
1951 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001952 // Note that a potential implicit null check is handled in this
1953 // CodeGeneratorARM64::LoadAcquire call.
1954 // NB: LoadAcquire will record the pc info if needed.
1955 codegen_->LoadAcquire(
1956 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001957 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001958 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001959 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001960 }
Roland Levillain44015862016-01-22 11:47:17 +00001961 if (field_type == Primitive::kPrimNot) {
1962 // If read barriers are enabled, emit read barriers other than
1963 // Baker's using a slow path (and also unpoison the loaded
1964 // reference, if heap poisoning is enabled).
1965 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1966 }
Roland Levillain4d027112015-07-01 15:41:14 +01001967 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001968}
1969
1970void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1971 LocationSummary* locations =
1972 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1973 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001974 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1975 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
1976 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001977 locations->SetInAt(1, Location::RequiresFpuRegister());
1978 } else {
1979 locations->SetInAt(1, Location::RequiresRegister());
1980 }
1981}
1982
1983void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001984 const FieldInfo& field_info,
1985 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001986 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001987 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001988
1989 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001990 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001991 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001992 Offset offset = field_info.GetFieldOffset();
1993 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001994
Roland Levillain4d027112015-07-01 15:41:14 +01001995 {
1996 // We use a block to end the scratch scope before the write barrier, thus
1997 // freeing the temporary registers so they can be used in `MarkGCCard`.
1998 UseScratchRegisterScope temps(GetVIXLAssembler());
1999
2000 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2001 DCHECK(value.IsW());
2002 Register temp = temps.AcquireW();
2003 __ Mov(temp, value.W());
2004 GetAssembler()->PoisonHeapReference(temp.W());
2005 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002006 }
Roland Levillain4d027112015-07-01 15:41:14 +01002007
2008 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002009 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
2010 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01002011 } else {
2012 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2013 codegen_->MaybeRecordImplicitNullCheck(instruction);
2014 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002015 }
2016
2017 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002018 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002019 }
2020}
2021
Alexandre Rames67555f72014-11-18 10:55:16 +00002022void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002023 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002024
2025 switch (type) {
2026 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002027 case Primitive::kPrimLong: {
2028 Register dst = OutputRegister(instr);
2029 Register lhs = InputRegisterAt(instr, 0);
2030 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002031 if (instr->IsAdd()) {
2032 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002033 } else if (instr->IsAnd()) {
2034 __ And(dst, lhs, rhs);
2035 } else if (instr->IsOr()) {
2036 __ Orr(dst, lhs, rhs);
2037 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002038 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002039 } else if (instr->IsRor()) {
2040 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002041 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002042 __ Ror(dst, lhs, shift);
2043 } else {
2044 // Ensure shift distance is in the same size register as the result. If
2045 // we are rotating a long and the shift comes in a w register originally,
2046 // we don't need to sxtw for use as an x since the shift distances are
2047 // all & reg_bits - 1.
2048 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2049 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002050 } else {
2051 DCHECK(instr->IsXor());
2052 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002053 }
2054 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002055 }
2056 case Primitive::kPrimFloat:
2057 case Primitive::kPrimDouble: {
2058 FPRegister dst = OutputFPRegister(instr);
2059 FPRegister lhs = InputFPRegisterAt(instr, 0);
2060 FPRegister rhs = InputFPRegisterAt(instr, 1);
2061 if (instr->IsAdd()) {
2062 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002063 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002064 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002065 } else {
2066 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002067 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002068 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002069 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002070 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002071 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002072 }
2073}
2074
Serban Constantinescu02164b32014-11-13 14:05:07 +00002075void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2076 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2077
2078 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2079 Primitive::Type type = instr->GetResultType();
2080 switch (type) {
2081 case Primitive::kPrimInt:
2082 case Primitive::kPrimLong: {
2083 locations->SetInAt(0, Location::RequiresRegister());
2084 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2085 locations->SetOut(Location::RequiresRegister());
2086 break;
2087 }
2088 default:
2089 LOG(FATAL) << "Unexpected shift type " << type;
2090 }
2091}
2092
2093void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2094 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2095
2096 Primitive::Type type = instr->GetType();
2097 switch (type) {
2098 case Primitive::kPrimInt:
2099 case Primitive::kPrimLong: {
2100 Register dst = OutputRegister(instr);
2101 Register lhs = InputRegisterAt(instr, 0);
2102 Operand rhs = InputOperandAt(instr, 1);
2103 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002104 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002105 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002106 if (instr->IsShl()) {
2107 __ Lsl(dst, lhs, shift_value);
2108 } else if (instr->IsShr()) {
2109 __ Asr(dst, lhs, shift_value);
2110 } else {
2111 __ Lsr(dst, lhs, shift_value);
2112 }
2113 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002114 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002115
2116 if (instr->IsShl()) {
2117 __ Lsl(dst, lhs, rhs_reg);
2118 } else if (instr->IsShr()) {
2119 __ Asr(dst, lhs, rhs_reg);
2120 } else {
2121 __ Lsr(dst, lhs, rhs_reg);
2122 }
2123 }
2124 break;
2125 }
2126 default:
2127 LOG(FATAL) << "Unexpected shift operation type " << type;
2128 }
2129}
2130
Alexandre Rames5319def2014-10-23 10:03:10 +01002131void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002132 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002133}
2134
2135void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002136 HandleBinaryOp(instruction);
2137}
2138
2139void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2140 HandleBinaryOp(instruction);
2141}
2142
2143void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2144 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002145}
2146
Artem Serov7fc63502016-02-09 17:15:29 +00002147void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002148 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2149 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2150 locations->SetInAt(0, Location::RequiresRegister());
2151 // There is no immediate variant of negated bitwise instructions in AArch64.
2152 locations->SetInAt(1, Location::RequiresRegister());
2153 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2154}
2155
Artem Serov7fc63502016-02-09 17:15:29 +00002156void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002157 Register dst = OutputRegister(instr);
2158 Register lhs = InputRegisterAt(instr, 0);
2159 Register rhs = InputRegisterAt(instr, 1);
2160
2161 switch (instr->GetOpKind()) {
2162 case HInstruction::kAnd:
2163 __ Bic(dst, lhs, rhs);
2164 break;
2165 case HInstruction::kOr:
2166 __ Orn(dst, lhs, rhs);
2167 break;
2168 case HInstruction::kXor:
2169 __ Eon(dst, lhs, rhs);
2170 break;
2171 default:
2172 LOG(FATAL) << "Unreachable";
2173 }
2174}
2175
Alexandre Rames8626b742015-11-25 16:28:08 +00002176void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
2177 HArm64DataProcWithShifterOp* instruction) {
2178 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2179 instruction->GetType() == Primitive::kPrimLong);
2180 LocationSummary* locations =
2181 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2182 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2183 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2184 } else {
2185 locations->SetInAt(0, Location::RequiresRegister());
2186 }
2187 locations->SetInAt(1, Location::RequiresRegister());
2188 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2189}
2190
2191void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
2192 HArm64DataProcWithShifterOp* instruction) {
2193 Primitive::Type type = instruction->GetType();
2194 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2195 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2196 Register out = OutputRegister(instruction);
2197 Register left;
2198 if (kind != HInstruction::kNeg) {
2199 left = InputRegisterAt(instruction, 0);
2200 }
2201 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
2202 // shifter operand operation, the IR generating `right_reg` (input to the type
2203 // conversion) can have a different type from the current instruction's type,
2204 // so we manually indicate the type.
2205 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00002206 int64_t shift_amount = instruction->GetShiftAmount() &
2207 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00002208
2209 Operand right_operand(0);
2210
2211 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2212 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
2213 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2214 } else {
2215 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
2216 }
2217
2218 // Logical binary operations do not support extension operations in the
2219 // operand. Note that VIXL would still manage if it was passed by generating
2220 // the extension as a separate instruction.
2221 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2222 DCHECK(!right_operand.IsExtendedRegister() ||
2223 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2224 kind != HInstruction::kNeg));
2225 switch (kind) {
2226 case HInstruction::kAdd:
2227 __ Add(out, left, right_operand);
2228 break;
2229 case HInstruction::kAnd:
2230 __ And(out, left, right_operand);
2231 break;
2232 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002233 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002234 __ Neg(out, right_operand);
2235 break;
2236 case HInstruction::kOr:
2237 __ Orr(out, left, right_operand);
2238 break;
2239 case HInstruction::kSub:
2240 __ Sub(out, left, right_operand);
2241 break;
2242 case HInstruction::kXor:
2243 __ Eor(out, left, right_operand);
2244 break;
2245 default:
2246 LOG(FATAL) << "Unexpected operation kind: " << kind;
2247 UNREACHABLE();
2248 }
2249}
2250
Artem Serov328429f2016-07-06 16:23:04 +01002251void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002252 LocationSummary* locations =
2253 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2254 locations->SetInAt(0, Location::RequiresRegister());
2255 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
2256 locations->SetOut(Location::RequiresRegister());
2257}
2258
Roland Levillain19c54192016-11-04 13:44:09 +00002259void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002260 __ Add(OutputRegister(instruction),
2261 InputRegisterAt(instruction, 0),
2262 Operand(InputOperandAt(instruction, 1)));
2263}
2264
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002265void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002266 LocationSummary* locations =
2267 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002268 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2269 if (instr->GetOpKind() == HInstruction::kSub &&
2270 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002271 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002272 // Don't allocate register for Mneg instruction.
2273 } else {
2274 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2275 Location::RequiresRegister());
2276 }
2277 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2278 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002279 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2280}
2281
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002282void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002283 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002284 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2285 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002286
2287 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2288 // This fixup should be carried out for all multiply-accumulate instructions:
2289 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2290 if (instr->GetType() == Primitive::kPrimLong &&
2291 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2292 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002293 vixl::aarch64::Instruction* prev =
2294 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002295 if (prev->IsLoadOrStore()) {
2296 // Make sure we emit only exactly one nop.
Scott Wakelingb77051e2016-11-21 19:46:00 +00002297 vixl::CodeBufferCheckScope scope(masm,
2298 kInstructionSize,
2299 vixl::CodeBufferCheckScope::kReserveBufferSpace,
2300 vixl::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002301 __ nop();
2302 }
2303 }
2304
2305 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002306 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002307 __ Madd(res, mul_left, mul_right, accumulator);
2308 } else {
2309 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002310 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002311 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002312 __ Mneg(res, mul_left, mul_right);
2313 } else {
2314 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2315 __ Msub(res, mul_left, mul_right, accumulator);
2316 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002317 }
2318}
2319
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002320void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002321 bool object_array_get_with_read_barrier =
2322 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002323 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002324 new (GetGraph()->GetArena()) LocationSummary(instruction,
2325 object_array_get_with_read_barrier ?
2326 LocationSummary::kCallOnSlowPath :
2327 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002328 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002329 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002330 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002331 locations->SetInAt(0, Location::RequiresRegister());
2332 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002333 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2334 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2335 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002336 // The output overlaps in the case of an object array get with
2337 // read barriers enabled: we do not want the move to overwrite the
2338 // array's location, as we need it to emit the read barrier.
2339 locations->SetOut(
2340 Location::RequiresRegister(),
2341 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002342 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002343}
2344
2345void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002346 Primitive::Type type = instruction->GetType();
2347 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002348 LocationSummary* locations = instruction->GetLocations();
2349 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002350 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002351 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002352 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2353 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002354 MacroAssembler* masm = GetVIXLAssembler();
2355 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002356 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002357 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002358
Roland Levillain19c54192016-11-04 13:44:09 +00002359 // The read barrier instrumentation of object ArrayGet instructions
2360 // does not support the HIntermediateAddress instruction.
2361 DCHECK(!((type == Primitive::kPrimNot) &&
2362 instruction->GetArray()->IsIntermediateAddress() &&
2363 kEmitCompilerReadBarrier));
2364
Roland Levillain44015862016-01-22 11:47:17 +00002365 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2366 // Object ArrayGet with Baker's read barrier case.
2367 Register temp = temps.AcquireW();
Roland Levillain44015862016-01-22 11:47:17 +00002368 // Note that a potential implicit null check is handled in the
2369 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2370 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2371 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002372 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002373 // General case.
2374 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002375 Register length;
2376 if (maybe_compressed_char_at) {
2377 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2378 length = temps.AcquireW();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002379 if (instruction->GetArray()->IsIntermediateAddress()) {
2380 DCHECK_LT(count_offset, offset);
2381 int64_t adjusted_offset = static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2382 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2383 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2384 } else {
2385 __ Ldr(length, HeapOperand(obj, count_offset));
2386 }
jessicahandojo05765752016-09-09 19:01:32 -07002387 codegen_->MaybeRecordImplicitNullCheck(instruction);
2388 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002389 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002390 if (maybe_compressed_char_at) {
2391 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002392 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2393 "Expecting 0=compressed, 1=uncompressed");
2394 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002395 __ Ldrb(Register(OutputCPURegister(instruction)),
2396 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2397 __ B(&done);
2398 __ Bind(&uncompressed_load);
2399 __ Ldrh(Register(OutputCPURegister(instruction)),
2400 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2401 __ Bind(&done);
2402 } else {
2403 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2404 source = HeapOperand(obj, offset);
2405 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002406 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002407 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002408 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002409 // We do not need to compute the intermediate address from the array: the
2410 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002411 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002412 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002413 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002414 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2415 }
2416 temp = obj;
2417 } else {
2418 __ Add(temp, obj, offset);
2419 }
jessicahandojo05765752016-09-09 19:01:32 -07002420 if (maybe_compressed_char_at) {
2421 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002422 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2423 "Expecting 0=compressed, 1=uncompressed");
2424 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002425 __ Ldrb(Register(OutputCPURegister(instruction)),
2426 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2427 __ B(&done);
2428 __ Bind(&uncompressed_load);
2429 __ Ldrh(Register(OutputCPURegister(instruction)),
2430 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2431 __ Bind(&done);
2432 } else {
2433 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2434 }
Roland Levillain44015862016-01-22 11:47:17 +00002435 }
jessicahandojo05765752016-09-09 19:01:32 -07002436 if (!maybe_compressed_char_at) {
2437 codegen_->Load(type, OutputCPURegister(instruction), source);
2438 codegen_->MaybeRecordImplicitNullCheck(instruction);
2439 }
Roland Levillain44015862016-01-22 11:47:17 +00002440
2441 if (type == Primitive::kPrimNot) {
2442 static_assert(
2443 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2444 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2445 Location obj_loc = locations->InAt(0);
2446 if (index.IsConstant()) {
2447 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2448 } else {
2449 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2450 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002451 }
Roland Levillain4d027112015-07-01 15:41:14 +01002452 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002453}
2454
Alexandre Rames5319def2014-10-23 10:03:10 +01002455void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2456 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2457 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002458 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002459}
2460
2461void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002462 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002463 vixl::aarch64::Register out = OutputRegister(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002464 BlockPoolsScope block_pools(GetVIXLAssembler());
jessicahandojo05765752016-09-09 19:01:32 -07002465 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002466 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002467 // Mask out compression flag from String's array length.
2468 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002469 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002470 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002471}
2472
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002473void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002474 Primitive::Type value_type = instruction->GetComponentType();
2475
2476 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002477 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2478 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002479 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002480 LocationSummary::kCallOnSlowPath :
2481 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002482 locations->SetInAt(0, Location::RequiresRegister());
2483 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002484 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2485 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2486 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002487 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002488 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002489 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002490 }
2491}
2492
2493void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2494 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002495 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002496 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002497 bool needs_write_barrier =
2498 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002499
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002500 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002501 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002502 CPURegister source = value;
2503 Location index = locations->InAt(1);
2504 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2505 MemOperand destination = HeapOperand(array);
2506 MacroAssembler* masm = GetVIXLAssembler();
2507 BlockPoolsScope block_pools(masm);
2508
2509 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002510 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002511 if (index.IsConstant()) {
2512 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2513 destination = HeapOperand(array, offset);
2514 } else {
2515 UseScratchRegisterScope temps(masm);
2516 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002517 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002518 // We do not need to compute the intermediate address from the array: the
2519 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002520 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002521 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002522 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002523 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2524 }
2525 temp = array;
2526 } else {
2527 __ Add(temp, array, offset);
2528 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002529 destination = HeapOperand(temp,
2530 XRegisterFrom(index),
2531 LSL,
2532 Primitive::ComponentSizeShift(value_type));
2533 }
2534 codegen_->Store(value_type, value, destination);
2535 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002536 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002537 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002538 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002539 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002540 {
2541 // We use a block to end the scratch scope before the write barrier, thus
2542 // freeing the temporary registers so they can be used in `MarkGCCard`.
2543 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002544 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002545 if (index.IsConstant()) {
2546 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002547 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002548 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002549 destination = HeapOperand(temp,
2550 XRegisterFrom(index),
2551 LSL,
2552 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002553 }
2554
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002555 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2556 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2557 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2558
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002559 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002560 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2561 codegen_->AddSlowPath(slow_path);
2562 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002563 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002564 __ Cbnz(Register(value), &non_zero);
2565 if (!index.IsConstant()) {
2566 __ Add(temp, array, offset);
2567 }
2568 __ Str(wzr, destination);
2569 codegen_->MaybeRecordImplicitNullCheck(instruction);
2570 __ B(&done);
2571 __ Bind(&non_zero);
2572 }
2573
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002574 // Note that when Baker read barriers are enabled, the type
2575 // checks are performed without read barriers. This is fine,
2576 // even in the case where a class object is in the from-space
2577 // after the flip, as a comparison involving such a type would
2578 // not produce a false positive; it may of course produce a
2579 // false negative, in which case we would take the ArraySet
2580 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002581
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002582 Register temp2 = temps.AcquireSameSizeAs(array);
2583 // /* HeapReference<Class> */ temp = array->klass_
2584 __ Ldr(temp, HeapOperand(array, class_offset));
2585 codegen_->MaybeRecordImplicitNullCheck(instruction);
2586 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002587
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002588 // /* HeapReference<Class> */ temp = temp->component_type_
2589 __ Ldr(temp, HeapOperand(temp, component_offset));
2590 // /* HeapReference<Class> */ temp2 = value->klass_
2591 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2592 // If heap poisoning is enabled, no need to unpoison `temp`
2593 // nor `temp2`, as we are comparing two poisoned references.
2594 __ Cmp(temp, temp2);
2595 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002596
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002597 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2598 vixl::aarch64::Label do_put;
2599 __ B(eq, &do_put);
2600 // If heap poisoning is enabled, the `temp` reference has
2601 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002602 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2603
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002604 // /* HeapReference<Class> */ temp = temp->super_class_
2605 __ Ldr(temp, HeapOperand(temp, super_offset));
2606 // If heap poisoning is enabled, no need to unpoison
2607 // `temp`, as we are comparing against null below.
2608 __ Cbnz(temp, slow_path->GetEntryLabel());
2609 __ Bind(&do_put);
2610 } else {
2611 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002612 }
2613 }
2614
2615 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002616 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002617 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002618 __ Mov(temp2, value.W());
2619 GetAssembler()->PoisonHeapReference(temp2);
2620 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002621 }
2622
2623 if (!index.IsConstant()) {
2624 __ Add(temp, array, offset);
2625 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002626 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002627
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002628 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002629 codegen_->MaybeRecordImplicitNullCheck(instruction);
2630 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002631 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002632
2633 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2634
2635 if (done.IsLinked()) {
2636 __ Bind(&done);
2637 }
2638
2639 if (slow_path != nullptr) {
2640 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002641 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002642 }
2643}
2644
Alexandre Rames67555f72014-11-18 10:55:16 +00002645void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002646 RegisterSet caller_saves = RegisterSet::Empty();
2647 InvokeRuntimeCallingConvention calling_convention;
2648 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2649 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2650 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002651 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002652 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002653}
2654
2655void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002656 BoundsCheckSlowPathARM64* slow_path =
2657 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002658 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002659 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2660 __ B(slow_path->GetEntryLabel(), hs);
2661}
2662
Alexandre Rames67555f72014-11-18 10:55:16 +00002663void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2664 LocationSummary* locations =
2665 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2666 locations->SetInAt(0, Location::RequiresRegister());
2667 if (check->HasUses()) {
2668 locations->SetOut(Location::SameAsFirstInput());
2669 }
2670}
2671
2672void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2673 // We assume the class is not null.
2674 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2675 check->GetLoadClass(), check, check->GetDexPc(), true);
2676 codegen_->AddSlowPath(slow_path);
2677 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2678}
2679
Roland Levillain1a653882016-03-18 18:05:57 +00002680static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2681 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2682 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2683}
2684
2685void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2686 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2687 Location rhs_loc = instruction->GetLocations()->InAt(1);
2688 if (rhs_loc.IsConstant()) {
2689 // 0.0 is the only immediate that can be encoded directly in
2690 // an FCMP instruction.
2691 //
2692 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2693 // specify that in a floating-point comparison, positive zero
2694 // and negative zero are considered equal, so we can use the
2695 // literal 0.0 for both cases here.
2696 //
2697 // Note however that some methods (Float.equal, Float.compare,
2698 // Float.compareTo, Double.equal, Double.compare,
2699 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2700 // StrictMath.min) consider 0.0 to be (strictly) greater than
2701 // -0.0. So if we ever translate calls to these methods into a
2702 // HCompare instruction, we must handle the -0.0 case with
2703 // care here.
2704 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2705 __ Fcmp(lhs_reg, 0.0);
2706 } else {
2707 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2708 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002709}
2710
Serban Constantinescu02164b32014-11-13 14:05:07 +00002711void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002712 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002713 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2714 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002715 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002716 case Primitive::kPrimBoolean:
2717 case Primitive::kPrimByte:
2718 case Primitive::kPrimShort:
2719 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002720 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002721 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002722 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002723 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002724 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2725 break;
2726 }
2727 case Primitive::kPrimFloat:
2728 case Primitive::kPrimDouble: {
2729 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002730 locations->SetInAt(1,
2731 IsFloatingPointZeroConstant(compare->InputAt(1))
2732 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2733 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002734 locations->SetOut(Location::RequiresRegister());
2735 break;
2736 }
2737 default:
2738 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2739 }
2740}
2741
2742void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2743 Primitive::Type in_type = compare->InputAt(0)->GetType();
2744
2745 // 0 if: left == right
2746 // 1 if: left > right
2747 // -1 if: left < right
2748 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002749 case Primitive::kPrimBoolean:
2750 case Primitive::kPrimByte:
2751 case Primitive::kPrimShort:
2752 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002753 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002754 case Primitive::kPrimLong: {
2755 Register result = OutputRegister(compare);
2756 Register left = InputRegisterAt(compare, 0);
2757 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002758 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002759 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2760 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002761 break;
2762 }
2763 case Primitive::kPrimFloat:
2764 case Primitive::kPrimDouble: {
2765 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002766 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002767 __ Cset(result, ne);
2768 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002769 break;
2770 }
2771 default:
2772 LOG(FATAL) << "Unimplemented compare type " << in_type;
2773 }
2774}
2775
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002776void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002777 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002778
2779 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2780 locations->SetInAt(0, Location::RequiresFpuRegister());
2781 locations->SetInAt(1,
2782 IsFloatingPointZeroConstant(instruction->InputAt(1))
2783 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2784 : Location::RequiresFpuRegister());
2785 } else {
2786 // Integer cases.
2787 locations->SetInAt(0, Location::RequiresRegister());
2788 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2789 }
2790
David Brazdilb3e773e2016-01-26 11:28:37 +00002791 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002792 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002793 }
2794}
2795
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002796void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002797 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002798 return;
2799 }
2800
2801 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002802 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002803 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002804
Roland Levillain7f63c522015-07-13 15:54:55 +00002805 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002806 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002807 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002808 } else {
2809 // Integer cases.
2810 Register lhs = InputRegisterAt(instruction, 0);
2811 Operand rhs = InputOperandAt(instruction, 1);
2812 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002813 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002814 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002815}
2816
2817#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2818 M(Equal) \
2819 M(NotEqual) \
2820 M(LessThan) \
2821 M(LessThanOrEqual) \
2822 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002823 M(GreaterThanOrEqual) \
2824 M(Below) \
2825 M(BelowOrEqual) \
2826 M(Above) \
2827 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002828#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002829void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2830void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002831FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002832#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002833#undef FOR_EACH_CONDITION_INSTRUCTION
2834
Zheng Xuc6667102015-05-15 16:08:45 +08002835void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2836 DCHECK(instruction->IsDiv() || instruction->IsRem());
2837
2838 LocationSummary* locations = instruction->GetLocations();
2839 Location second = locations->InAt(1);
2840 DCHECK(second.IsConstant());
2841
2842 Register out = OutputRegister(instruction);
2843 Register dividend = InputRegisterAt(instruction, 0);
2844 int64_t imm = Int64FromConstant(second.GetConstant());
2845 DCHECK(imm == 1 || imm == -1);
2846
2847 if (instruction->IsRem()) {
2848 __ Mov(out, 0);
2849 } else {
2850 if (imm == 1) {
2851 __ Mov(out, dividend);
2852 } else {
2853 __ Neg(out, dividend);
2854 }
2855 }
2856}
2857
2858void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2859 DCHECK(instruction->IsDiv() || instruction->IsRem());
2860
2861 LocationSummary* locations = instruction->GetLocations();
2862 Location second = locations->InAt(1);
2863 DCHECK(second.IsConstant());
2864
2865 Register out = OutputRegister(instruction);
2866 Register dividend = InputRegisterAt(instruction, 0);
2867 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002868 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002869 int ctz_imm = CTZ(abs_imm);
2870
2871 UseScratchRegisterScope temps(GetVIXLAssembler());
2872 Register temp = temps.AcquireSameSizeAs(out);
2873
2874 if (instruction->IsDiv()) {
2875 __ Add(temp, dividend, abs_imm - 1);
2876 __ Cmp(dividend, 0);
2877 __ Csel(out, temp, dividend, lt);
2878 if (imm > 0) {
2879 __ Asr(out, out, ctz_imm);
2880 } else {
2881 __ Neg(out, Operand(out, ASR, ctz_imm));
2882 }
2883 } else {
2884 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2885 __ Asr(temp, dividend, bits - 1);
2886 __ Lsr(temp, temp, bits - ctz_imm);
2887 __ Add(out, dividend, temp);
2888 __ And(out, out, abs_imm - 1);
2889 __ Sub(out, out, temp);
2890 }
2891}
2892
2893void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2894 DCHECK(instruction->IsDiv() || instruction->IsRem());
2895
2896 LocationSummary* locations = instruction->GetLocations();
2897 Location second = locations->InAt(1);
2898 DCHECK(second.IsConstant());
2899
2900 Register out = OutputRegister(instruction);
2901 Register dividend = InputRegisterAt(instruction, 0);
2902 int64_t imm = Int64FromConstant(second.GetConstant());
2903
2904 Primitive::Type type = instruction->GetResultType();
2905 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2906
2907 int64_t magic;
2908 int shift;
2909 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2910
2911 UseScratchRegisterScope temps(GetVIXLAssembler());
2912 Register temp = temps.AcquireSameSizeAs(out);
2913
2914 // temp = get_high(dividend * magic)
2915 __ Mov(temp, magic);
2916 if (type == Primitive::kPrimLong) {
2917 __ Smulh(temp, dividend, temp);
2918 } else {
2919 __ Smull(temp.X(), dividend, temp);
2920 __ Lsr(temp.X(), temp.X(), 32);
2921 }
2922
2923 if (imm > 0 && magic < 0) {
2924 __ Add(temp, temp, dividend);
2925 } else if (imm < 0 && magic > 0) {
2926 __ Sub(temp, temp, dividend);
2927 }
2928
2929 if (shift != 0) {
2930 __ Asr(temp, temp, shift);
2931 }
2932
2933 if (instruction->IsDiv()) {
2934 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2935 } else {
2936 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2937 // TODO: Strength reduction for msub.
2938 Register temp_imm = temps.AcquireSameSizeAs(out);
2939 __ Mov(temp_imm, imm);
2940 __ Msub(out, temp, temp_imm, dividend);
2941 }
2942}
2943
2944void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2945 DCHECK(instruction->IsDiv() || instruction->IsRem());
2946 Primitive::Type type = instruction->GetResultType();
2947 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2948
2949 LocationSummary* locations = instruction->GetLocations();
2950 Register out = OutputRegister(instruction);
2951 Location second = locations->InAt(1);
2952
2953 if (second.IsConstant()) {
2954 int64_t imm = Int64FromConstant(second.GetConstant());
2955
2956 if (imm == 0) {
2957 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2958 } else if (imm == 1 || imm == -1) {
2959 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002960 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002961 DivRemByPowerOfTwo(instruction);
2962 } else {
2963 DCHECK(imm <= -2 || imm >= 2);
2964 GenerateDivRemWithAnyConstant(instruction);
2965 }
2966 } else {
2967 Register dividend = InputRegisterAt(instruction, 0);
2968 Register divisor = InputRegisterAt(instruction, 1);
2969 if (instruction->IsDiv()) {
2970 __ Sdiv(out, dividend, divisor);
2971 } else {
2972 UseScratchRegisterScope temps(GetVIXLAssembler());
2973 Register temp = temps.AcquireSameSizeAs(out);
2974 __ Sdiv(temp, dividend, divisor);
2975 __ Msub(out, temp, divisor, dividend);
2976 }
2977 }
2978}
2979
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002980void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2981 LocationSummary* locations =
2982 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2983 switch (div->GetResultType()) {
2984 case Primitive::kPrimInt:
2985 case Primitive::kPrimLong:
2986 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002987 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002988 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2989 break;
2990
2991 case Primitive::kPrimFloat:
2992 case Primitive::kPrimDouble:
2993 locations->SetInAt(0, Location::RequiresFpuRegister());
2994 locations->SetInAt(1, Location::RequiresFpuRegister());
2995 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2996 break;
2997
2998 default:
2999 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3000 }
3001}
3002
3003void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3004 Primitive::Type type = div->GetResultType();
3005 switch (type) {
3006 case Primitive::kPrimInt:
3007 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003008 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003009 break;
3010
3011 case Primitive::kPrimFloat:
3012 case Primitive::kPrimDouble:
3013 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3014 break;
3015
3016 default:
3017 LOG(FATAL) << "Unexpected div type " << type;
3018 }
3019}
3020
Alexandre Rames67555f72014-11-18 10:55:16 +00003021void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003022 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003023 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003024}
3025
3026void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3027 SlowPathCodeARM64* slow_path =
3028 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3029 codegen_->AddSlowPath(slow_path);
3030 Location value = instruction->GetLocations()->InAt(0);
3031
Alexandre Rames3e69f162014-12-10 10:36:50 +00003032 Primitive::Type type = instruction->GetType();
3033
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003034 if (!Primitive::IsIntegralType(type)) {
3035 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003036 return;
3037 }
3038
Alexandre Rames67555f72014-11-18 10:55:16 +00003039 if (value.IsConstant()) {
3040 int64_t divisor = Int64ConstantFrom(value);
3041 if (divisor == 0) {
3042 __ B(slow_path->GetEntryLabel());
3043 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003044 // A division by a non-null constant is valid. We don't need to perform
3045 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003046 }
3047 } else {
3048 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3049 }
3050}
3051
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003052void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3053 LocationSummary* locations =
3054 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3055 locations->SetOut(Location::ConstantLocation(constant));
3056}
3057
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003058void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3059 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003060 // Will be generated at use site.
3061}
3062
Alexandre Rames5319def2014-10-23 10:03:10 +01003063void LocationsBuilderARM64::VisitExit(HExit* exit) {
3064 exit->SetLocations(nullptr);
3065}
3066
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003067void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003068}
3069
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003070void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3071 LocationSummary* locations =
3072 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3073 locations->SetOut(Location::ConstantLocation(constant));
3074}
3075
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003076void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003077 // Will be generated at use site.
3078}
3079
David Brazdilfc6a86a2015-06-26 10:33:45 +00003080void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003081 DCHECK(!successor->IsExitBlock());
3082 HBasicBlock* block = got->GetBlock();
3083 HInstruction* previous = got->GetPrevious();
3084 HLoopInformation* info = block->GetLoopInformation();
3085
David Brazdil46e2a392015-03-16 17:31:52 +00003086 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003087 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3088 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3089 return;
3090 }
3091 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3092 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3093 }
3094 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003095 __ B(codegen_->GetLabelOf(successor));
3096 }
3097}
3098
David Brazdilfc6a86a2015-06-26 10:33:45 +00003099void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3100 got->SetLocations(nullptr);
3101}
3102
3103void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3104 HandleGoto(got, got->GetSuccessor());
3105}
3106
3107void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3108 try_boundary->SetLocations(nullptr);
3109}
3110
3111void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3112 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3113 if (!successor->IsExitBlock()) {
3114 HandleGoto(try_boundary, successor);
3115 }
3116}
3117
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003118void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003119 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003120 vixl::aarch64::Label* true_target,
3121 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003122 // FP branching requires both targets to be explicit. If either of the targets
3123 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003124 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003125 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003126
David Brazdil0debae72015-11-12 18:37:00 +00003127 if (true_target == nullptr && false_target == nullptr) {
3128 // Nothing to do. The code always falls through.
3129 return;
3130 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003131 // Constant condition, statically compared against "true" (integer value 1).
3132 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003133 if (true_target != nullptr) {
3134 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003135 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003136 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003137 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003138 if (false_target != nullptr) {
3139 __ B(false_target);
3140 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003141 }
David Brazdil0debae72015-11-12 18:37:00 +00003142 return;
3143 }
3144
3145 // The following code generates these patterns:
3146 // (1) true_target == nullptr && false_target != nullptr
3147 // - opposite condition true => branch to false_target
3148 // (2) true_target != nullptr && false_target == nullptr
3149 // - condition true => branch to true_target
3150 // (3) true_target != nullptr && false_target != nullptr
3151 // - condition true => branch to true_target
3152 // - branch to false_target
3153 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003154 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003155 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003156 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003157 if (true_target == nullptr) {
3158 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3159 } else {
3160 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3161 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003162 } else {
3163 // The condition instruction has not been materialized, use its inputs as
3164 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003165 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003166
David Brazdil0debae72015-11-12 18:37:00 +00003167 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003168 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003169 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003170 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003171 IfCondition opposite_condition = condition->GetOppositeCondition();
3172 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003173 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003174 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003175 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003176 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003177 // Integer cases.
3178 Register lhs = InputRegisterAt(condition, 0);
3179 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003180
3181 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003182 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003183 if (true_target == nullptr) {
3184 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3185 non_fallthrough_target = false_target;
3186 } else {
3187 arm64_cond = ARM64Condition(condition->GetCondition());
3188 non_fallthrough_target = true_target;
3189 }
3190
Aart Bik086d27e2016-01-20 17:02:00 -08003191 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003192 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003193 switch (arm64_cond) {
3194 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003195 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003196 break;
3197 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003198 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003199 break;
3200 case lt:
3201 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003202 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003203 break;
3204 case ge:
3205 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003206 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003207 break;
3208 default:
3209 // Without the `static_cast` the compiler throws an error for
3210 // `-Werror=sign-promo`.
3211 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3212 }
3213 } else {
3214 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003215 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003216 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003217 }
3218 }
David Brazdil0debae72015-11-12 18:37:00 +00003219
3220 // If neither branch falls through (case 3), the conditional branch to `true_target`
3221 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3222 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003223 __ B(false_target);
3224 }
David Brazdil0debae72015-11-12 18:37:00 +00003225
3226 if (fallthrough_target.IsLinked()) {
3227 __ Bind(&fallthrough_target);
3228 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003229}
3230
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003231void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3232 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003233 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003234 locations->SetInAt(0, Location::RequiresRegister());
3235 }
3236}
3237
3238void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003239 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3240 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003241 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3242 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3243 true_target = nullptr;
3244 }
3245 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3246 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3247 false_target = nullptr;
3248 }
David Brazdil0debae72015-11-12 18:37:00 +00003249 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003250}
3251
3252void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3253 LocationSummary* locations = new (GetGraph()->GetArena())
3254 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003255 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003256 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003257 locations->SetInAt(0, Location::RequiresRegister());
3258 }
3259}
3260
3261void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003262 SlowPathCodeARM64* slow_path =
3263 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003264 GenerateTestAndBranch(deoptimize,
3265 /* condition_input_index */ 0,
3266 slow_path->GetEntryLabel(),
3267 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003268}
3269
Mingyao Yang063fc772016-08-02 11:02:54 -07003270void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3271 LocationSummary* locations = new (GetGraph()->GetArena())
3272 LocationSummary(flag, LocationSummary::kNoCall);
3273 locations->SetOut(Location::RequiresRegister());
3274}
3275
3276void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3277 __ Ldr(OutputRegister(flag),
3278 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3279}
3280
David Brazdilc0b601b2016-02-08 14:20:45 +00003281static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3282 return condition->IsCondition() &&
3283 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3284}
3285
Alexandre Rames880f1192016-06-13 16:04:50 +01003286static inline Condition GetConditionForSelect(HCondition* condition) {
3287 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003288 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3289 : ARM64Condition(cond);
3290}
3291
David Brazdil74eb1b22015-12-14 11:44:01 +00003292void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3293 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003294 if (Primitive::IsFloatingPointType(select->GetType())) {
3295 locations->SetInAt(0, Location::RequiresFpuRegister());
3296 locations->SetInAt(1, Location::RequiresFpuRegister());
3297 locations->SetOut(Location::RequiresFpuRegister());
3298 } else {
3299 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3300 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3301 bool is_true_value_constant = cst_true_value != nullptr;
3302 bool is_false_value_constant = cst_false_value != nullptr;
3303 // Ask VIXL whether we should synthesize constants in registers.
3304 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3305 Operand true_op = is_true_value_constant ?
3306 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3307 Operand false_op = is_false_value_constant ?
3308 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3309 bool true_value_in_register = false;
3310 bool false_value_in_register = false;
3311 MacroAssembler::GetCselSynthesisInformation(
3312 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3313 true_value_in_register |= !is_true_value_constant;
3314 false_value_in_register |= !is_false_value_constant;
3315
3316 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3317 : Location::ConstantLocation(cst_true_value));
3318 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3319 : Location::ConstantLocation(cst_false_value));
3320 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003321 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003322
David Brazdil74eb1b22015-12-14 11:44:01 +00003323 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3324 locations->SetInAt(2, Location::RequiresRegister());
3325 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003326}
3327
3328void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003329 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003330 Condition csel_cond;
3331
3332 if (IsBooleanValueOrMaterializedCondition(cond)) {
3333 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003334 // Use the condition flags set by the previous instruction.
3335 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003336 } else {
3337 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003338 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003339 }
3340 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003341 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003342 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003343 } else {
3344 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003345 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003346 }
3347
Alexandre Rames880f1192016-06-13 16:04:50 +01003348 if (Primitive::IsFloatingPointType(select->GetType())) {
3349 __ Fcsel(OutputFPRegister(select),
3350 InputFPRegisterAt(select, 1),
3351 InputFPRegisterAt(select, 0),
3352 csel_cond);
3353 } else {
3354 __ Csel(OutputRegister(select),
3355 InputOperandAt(select, 1),
3356 InputOperandAt(select, 0),
3357 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003358 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003359}
3360
David Srbecky0cf44932015-12-09 14:09:59 +00003361void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3362 new (GetGraph()->GetArena()) LocationSummary(info);
3363}
3364
David Srbeckyd28f4a02016-03-14 17:14:24 +00003365void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3366 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003367}
3368
3369void CodeGeneratorARM64::GenerateNop() {
3370 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003371}
3372
Alexandre Rames5319def2014-10-23 10:03:10 +01003373void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003374 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003375}
3376
3377void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003378 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003379}
3380
3381void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003382 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003383}
3384
3385void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003386 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003387}
3388
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003389// Temp is used for read barrier.
3390static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3391 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003392 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003393 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3394 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3395 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3396 return 1;
3397 }
3398 return 0;
3399}
3400
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003401// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003402// interface pointer, one for loading the current interface.
3403// The other checks have one temp for loading the object's class.
3404static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3405 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3406 return 3;
3407 }
3408 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003409}
3410
Alexandre Rames67555f72014-11-18 10:55:16 +00003411void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003412 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003413 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003414 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003415 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003416 case TypeCheckKind::kExactCheck:
3417 case TypeCheckKind::kAbstractClassCheck:
3418 case TypeCheckKind::kClassHierarchyCheck:
3419 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003420 call_kind =
3421 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003422 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003423 break;
3424 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003425 case TypeCheckKind::kUnresolvedCheck:
3426 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003427 call_kind = LocationSummary::kCallOnSlowPath;
3428 break;
3429 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003430
Alexandre Rames67555f72014-11-18 10:55:16 +00003431 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003432 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003433 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003434 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003435 locations->SetInAt(0, Location::RequiresRegister());
3436 locations->SetInAt(1, Location::RequiresRegister());
3437 // The "out" register is used as a temporary, so it overlaps with the inputs.
3438 // Note that TypeCheckSlowPathARM64 uses this register too.
3439 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003440 // Add temps if necessary for read barriers.
3441 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003442}
3443
3444void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003445 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003446 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003447 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003448 Register obj = InputRegisterAt(instruction, 0);
3449 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003450 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003451 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003452 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3453 DCHECK_LE(num_temps, 1u);
3454 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003455 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3456 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3457 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3458 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003459
Scott Wakeling97c72b72016-06-24 16:19:36 +01003460 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003461 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003462
3463 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003464 // Avoid null check if we know `obj` is not null.
3465 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003466 __ Cbz(obj, &zero);
3467 }
3468
Roland Levillain44015862016-01-22 11:47:17 +00003469 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003470 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003471 // /* HeapReference<Class> */ out = obj->klass_
3472 GenerateReferenceLoadTwoRegisters(instruction,
3473 out_loc,
3474 obj_loc,
3475 class_offset,
3476 maybe_temp_loc,
3477 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003478 __ Cmp(out, cls);
3479 __ Cset(out, eq);
3480 if (zero.IsLinked()) {
3481 __ B(&done);
3482 }
3483 break;
3484 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003485
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003486 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003487 // /* HeapReference<Class> */ out = obj->klass_
3488 GenerateReferenceLoadTwoRegisters(instruction,
3489 out_loc,
3490 obj_loc,
3491 class_offset,
3492 maybe_temp_loc,
3493 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003494 // If the class is abstract, we eagerly fetch the super class of the
3495 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003496 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003497 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003498 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003499 GenerateReferenceLoadOneRegister(instruction,
3500 out_loc,
3501 super_offset,
3502 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003503 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003504 // If `out` is null, we use it for the result, and jump to `done`.
3505 __ Cbz(out, &done);
3506 __ Cmp(out, cls);
3507 __ B(ne, &loop);
3508 __ Mov(out, 1);
3509 if (zero.IsLinked()) {
3510 __ B(&done);
3511 }
3512 break;
3513 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003514
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003515 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003516 // /* HeapReference<Class> */ out = obj->klass_
3517 GenerateReferenceLoadTwoRegisters(instruction,
3518 out_loc,
3519 obj_loc,
3520 class_offset,
3521 maybe_temp_loc,
3522 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003523 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003524 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003525 __ Bind(&loop);
3526 __ Cmp(out, cls);
3527 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003528 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003529 GenerateReferenceLoadOneRegister(instruction,
3530 out_loc,
3531 super_offset,
3532 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003533 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003534 __ Cbnz(out, &loop);
3535 // If `out` is null, we use it for the result, and jump to `done`.
3536 __ B(&done);
3537 __ Bind(&success);
3538 __ Mov(out, 1);
3539 if (zero.IsLinked()) {
3540 __ B(&done);
3541 }
3542 break;
3543 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003544
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003545 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003546 // /* HeapReference<Class> */ out = obj->klass_
3547 GenerateReferenceLoadTwoRegisters(instruction,
3548 out_loc,
3549 obj_loc,
3550 class_offset,
3551 maybe_temp_loc,
3552 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003553 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003554 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003555 __ Cmp(out, cls);
3556 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003557 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003558 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003559 GenerateReferenceLoadOneRegister(instruction,
3560 out_loc,
3561 component_offset,
3562 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003563 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003564 // If `out` is null, we use it for the result, and jump to `done`.
3565 __ Cbz(out, &done);
3566 __ Ldrh(out, HeapOperand(out, primitive_offset));
3567 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3568 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003569 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003570 __ Mov(out, 1);
3571 __ B(&done);
3572 break;
3573 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003574
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003575 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003576 // No read barrier since the slow path will retry upon failure.
3577 // /* HeapReference<Class> */ out = obj->klass_
3578 GenerateReferenceLoadTwoRegisters(instruction,
3579 out_loc,
3580 obj_loc,
3581 class_offset,
3582 maybe_temp_loc,
3583 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003584 __ Cmp(out, cls);
3585 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003586 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3587 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003588 codegen_->AddSlowPath(slow_path);
3589 __ B(ne, slow_path->GetEntryLabel());
3590 __ Mov(out, 1);
3591 if (zero.IsLinked()) {
3592 __ B(&done);
3593 }
3594 break;
3595 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003596
Calin Juravle98893e12015-10-02 21:05:03 +01003597 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003598 case TypeCheckKind::kInterfaceCheck: {
3599 // Note that we indeed only call on slow path, but we always go
3600 // into the slow path for the unresolved and interface check
3601 // cases.
3602 //
3603 // We cannot directly call the InstanceofNonTrivial runtime
3604 // entry point without resorting to a type checking slow path
3605 // here (i.e. by calling InvokeRuntime directly), as it would
3606 // require to assign fixed registers for the inputs of this
3607 // HInstanceOf instruction (following the runtime calling
3608 // convention), which might be cluttered by the potential first
3609 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003610 //
3611 // TODO: Introduce a new runtime entry point taking the object
3612 // to test (instead of its class) as argument, and let it deal
3613 // with the read barrier issues. This will let us refactor this
3614 // case of the `switch` code as it was previously (with a direct
3615 // call to the runtime not using a type checking slow path).
3616 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003617 DCHECK(locations->OnlyCallsOnSlowPath());
3618 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3619 /* is_fatal */ false);
3620 codegen_->AddSlowPath(slow_path);
3621 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003622 if (zero.IsLinked()) {
3623 __ B(&done);
3624 }
3625 break;
3626 }
3627 }
3628
3629 if (zero.IsLinked()) {
3630 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003631 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003632 }
3633
3634 if (done.IsLinked()) {
3635 __ Bind(&done);
3636 }
3637
3638 if (slow_path != nullptr) {
3639 __ Bind(slow_path->GetExitLabel());
3640 }
3641}
3642
3643void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3644 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3645 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3646
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003647 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3648 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003649 case TypeCheckKind::kExactCheck:
3650 case TypeCheckKind::kAbstractClassCheck:
3651 case TypeCheckKind::kClassHierarchyCheck:
3652 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003653 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3654 LocationSummary::kCallOnSlowPath :
3655 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003656 break;
3657 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003658 case TypeCheckKind::kUnresolvedCheck:
3659 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003660 call_kind = LocationSummary::kCallOnSlowPath;
3661 break;
3662 }
3663
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003664 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3665 locations->SetInAt(0, Location::RequiresRegister());
3666 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003667 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
3668 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003669}
3670
3671void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003672 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003673 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003674 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003675 Register obj = InputRegisterAt(instruction, 0);
3676 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003677 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3678 DCHECK_GE(num_temps, 1u);
3679 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003680 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003681 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
3682 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003683 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003684 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3685 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3686 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3687 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3688 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3689 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3690 const uint32_t object_array_data_offset =
3691 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003692
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003693 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003694 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3695 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3696 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003697 if (!kEmitCompilerReadBarrier) {
3698 is_type_check_slow_path_fatal =
3699 (type_check_kind == TypeCheckKind::kExactCheck ||
3700 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3701 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3702 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3703 !instruction->CanThrowIntoCatchBlock();
3704 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003705 SlowPathCodeARM64* type_check_slow_path =
3706 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3707 is_type_check_slow_path_fatal);
3708 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003709
Scott Wakeling97c72b72016-06-24 16:19:36 +01003710 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003711 // Avoid null check if we know obj is not null.
3712 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003713 __ Cbz(obj, &done);
3714 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003715
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003716 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003717 case TypeCheckKind::kExactCheck:
3718 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003719 // /* HeapReference<Class> */ temp = obj->klass_
3720 GenerateReferenceLoadTwoRegisters(instruction,
3721 temp_loc,
3722 obj_loc,
3723 class_offset,
3724 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003725 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003726
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003727 __ Cmp(temp, cls);
3728 // Jump to slow path for throwing the exception or doing a
3729 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003730 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003731 break;
3732 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003733
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003734 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003735 // /* HeapReference<Class> */ temp = obj->klass_
3736 GenerateReferenceLoadTwoRegisters(instruction,
3737 temp_loc,
3738 obj_loc,
3739 class_offset,
3740 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003741 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003742
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003743 // If the class is abstract, we eagerly fetch the super class of the
3744 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003745 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003746 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003747 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003748 GenerateReferenceLoadOneRegister(instruction,
3749 temp_loc,
3750 super_offset,
3751 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003752 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003753
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003754 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3755 // exception.
3756 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3757 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003758 __ Cmp(temp, cls);
3759 __ B(ne, &loop);
3760 break;
3761 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003762
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003763 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003764 // /* HeapReference<Class> */ temp = obj->klass_
3765 GenerateReferenceLoadTwoRegisters(instruction,
3766 temp_loc,
3767 obj_loc,
3768 class_offset,
3769 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003770 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003771
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003772 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003773 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003774 __ Bind(&loop);
3775 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003776 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003777
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003778 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003779 GenerateReferenceLoadOneRegister(instruction,
3780 temp_loc,
3781 super_offset,
3782 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003783 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003784
3785 // If the class reference currently in `temp` is not null, jump
3786 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003787 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003788 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003789 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003790 break;
3791 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003792
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003793 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003794 // /* HeapReference<Class> */ temp = obj->klass_
3795 GenerateReferenceLoadTwoRegisters(instruction,
3796 temp_loc,
3797 obj_loc,
3798 class_offset,
3799 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003800 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003801
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003802 // Do an exact check.
3803 __ Cmp(temp, cls);
3804 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003805
3806 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003807 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003808 GenerateReferenceLoadOneRegister(instruction,
3809 temp_loc,
3810 component_offset,
3811 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003812 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003813
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003814 // If the component type is null, jump to the slow path to throw the exception.
3815 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3816 // Otherwise, the object is indeed an array. Further check that this component type is not a
3817 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003818 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3819 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003820 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003821 break;
3822 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003823
Calin Juravle98893e12015-10-02 21:05:03 +01003824 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003825 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003826 //
3827 // We cannot directly call the CheckCast runtime entry point
3828 // without resorting to a type checking slow path here (i.e. by
3829 // calling InvokeRuntime directly), as it would require to
3830 // assign fixed registers for the inputs of this HInstanceOf
3831 // instruction (following the runtime calling convention), which
3832 // might be cluttered by the potential first read barrier
3833 // emission at the beginning of this method.
3834 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003835 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003836 case TypeCheckKind::kInterfaceCheck: {
3837 // /* HeapReference<Class> */ temp = obj->klass_
3838 GenerateReferenceLoadTwoRegisters(instruction,
3839 temp_loc,
3840 obj_loc,
3841 class_offset,
3842 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003843 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003844
3845 // /* HeapReference<Class> */ temp = temp->iftable_
3846 GenerateReferenceLoadTwoRegisters(instruction,
3847 temp_loc,
3848 temp_loc,
3849 iftable_offset,
3850 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003851 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003852 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003853 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08003854 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003855 vixl::aarch64::Label start_loop;
3856 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003857 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003858 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
3859 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003860 // Go to next interface.
3861 __ Add(temp, temp, 2 * kHeapReferenceSize);
3862 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003863 // Compare the classes and continue the loop if they do not match.
3864 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
3865 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003866 break;
3867 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003868 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003869 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003870
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003871 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003872}
3873
Alexandre Rames5319def2014-10-23 10:03:10 +01003874void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3875 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3876 locations->SetOut(Location::ConstantLocation(constant));
3877}
3878
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003879void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003880 // Will be generated at use site.
3881}
3882
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003883void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3884 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3885 locations->SetOut(Location::ConstantLocation(constant));
3886}
3887
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003888void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003889 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003890}
3891
Calin Juravle175dc732015-08-25 15:42:32 +01003892void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3893 // The trampoline uses the same calling convention as dex calling conventions,
3894 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3895 // the method_idx.
3896 HandleInvoke(invoke);
3897}
3898
3899void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3900 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3901}
3902
Alexandre Rames5319def2014-10-23 10:03:10 +01003903void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003904 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003905 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003906}
3907
Alexandre Rames67555f72014-11-18 10:55:16 +00003908void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3909 HandleInvoke(invoke);
3910}
3911
3912void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3913 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003914 LocationSummary* locations = invoke->GetLocations();
3915 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003916 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003917 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003918 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003919
3920 // The register ip1 is required to be used for the hidden argument in
3921 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003922 MacroAssembler* masm = GetVIXLAssembler();
3923 UseScratchRegisterScope scratch_scope(masm);
3924 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003925 scratch_scope.Exclude(ip1);
3926 __ Mov(ip1, invoke->GetDexMethodIndex());
3927
Alexandre Rames67555f72014-11-18 10:55:16 +00003928 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003929 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003930 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003931 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003932 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003933 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003934 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003935 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003936 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003937 // Instead of simply (possibly) unpoisoning `temp` here, we should
3938 // emit a read barrier for the previous class reference load.
3939 // However this is not required in practice, as this is an
3940 // intermediate/temporary reference and because the current
3941 // concurrent copying collector keeps the from-space memory
3942 // intact/accessible until the end of the marking phase (the
3943 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003944 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003945 __ Ldr(temp,
3946 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3947 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003948 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003949 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003950 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003951 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003952 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003953 // lr();
3954 __ Blr(lr);
3955 DCHECK(!codegen_->IsLeafMethod());
3956 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3957}
3958
3959void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003960 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3961 if (intrinsic.TryDispatch(invoke)) {
3962 return;
3963 }
3964
Alexandre Rames67555f72014-11-18 10:55:16 +00003965 HandleInvoke(invoke);
3966}
3967
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003968void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003969 // Explicit clinit checks triggered by static invokes must have been pruned by
3970 // art::PrepareForRegisterAllocation.
3971 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003972
Andreas Gampe878d58c2015-01-15 23:24:00 -08003973 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3974 if (intrinsic.TryDispatch(invoke)) {
3975 return;
3976 }
3977
Alexandre Rames67555f72014-11-18 10:55:16 +00003978 HandleInvoke(invoke);
3979}
3980
Andreas Gampe878d58c2015-01-15 23:24:00 -08003981static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3982 if (invoke->GetLocations()->Intrinsified()) {
3983 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3984 intrinsic.Dispatch(invoke);
3985 return true;
3986 }
3987 return false;
3988}
3989
Vladimir Markodc151b22015-10-15 18:02:30 +01003990HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3991 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003992 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003993 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003994 return desired_dispatch_info;
3995}
3996
TatWai Chongd8c052a2016-11-02 16:12:48 +08003997Location CodeGeneratorARM64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
3998 Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003999 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004000 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4001 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004002 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4003 uint32_t offset =
4004 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004005 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004006 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004007 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004008 }
Vladimir Marko58155012015-08-19 12:49:41 +00004009 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004010 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004011 break;
4012 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4013 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004014 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004015 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004016 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4017 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004018 const DexFile& dex_file = invoke->GetDexFileForPcRelativeDexCache();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004019 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004020 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004021 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004022 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004023 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004024 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004025 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004026 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004027 }
Vladimir Marko58155012015-08-19 12:49:41 +00004028 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004029 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004030 Register reg = XRegisterFrom(temp);
4031 Register method_reg;
4032 if (current_method.IsRegister()) {
4033 method_reg = XRegisterFrom(current_method);
4034 } else {
4035 DCHECK(invoke->GetLocations()->Intrinsified());
4036 DCHECK(!current_method.IsValid());
4037 method_reg = reg;
4038 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4039 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004040
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004041 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004042 __ Ldr(reg.X(),
4043 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004044 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004045 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004046 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4047 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004048 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4049 break;
4050 }
4051 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08004052 return callee_method;
4053}
4054
4055void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4056 // All registers are assumed to be correctly set up.
4057 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004058
4059 switch (invoke->GetCodePtrLocation()) {
4060 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4061 __ Bl(&frame_entry_label_);
4062 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004063 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4064 // LR = callee_method->entry_point_from_quick_compiled_code_;
4065 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004066 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004067 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004068 // lr()
4069 __ Blr(lr);
4070 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004071 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004072
Andreas Gampe878d58c2015-01-15 23:24:00 -08004073 DCHECK(!IsLeafMethod());
4074}
4075
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004076void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004077 // Use the calling convention instead of the location of the receiver, as
4078 // intrinsics may have put the receiver in a different register. In the intrinsics
4079 // slow path, the arguments have been moved to the right place, so here we are
4080 // guaranteed that the receiver is the first register of the calling convention.
4081 InvokeDexCallingConvention calling_convention;
4082 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004083 Register temp = XRegisterFrom(temp_in);
4084 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4085 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4086 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004087 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004088
4089 BlockPoolsScope block_pools(GetVIXLAssembler());
4090
4091 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004092 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004093 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004094 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004095 // Instead of simply (possibly) unpoisoning `temp` here, we should
4096 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004097 // intermediate/temporary reference and because the current
4098 // concurrent copying collector keeps the from-space memory
4099 // intact/accessible until the end of the marking phase (the
4100 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004101 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4102 // temp = temp->GetMethodAt(method_offset);
4103 __ Ldr(temp, MemOperand(temp, method_offset));
4104 // lr = temp->GetEntryPoint();
4105 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
4106 // lr();
4107 __ Blr(lr);
4108}
4109
Orion Hodsonac141392017-01-13 11:53:47 +00004110void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4111 HandleInvoke(invoke);
4112}
4113
4114void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4115 codegen_->GenerateInvokePolymorphicCall(invoke);
4116}
4117
Scott Wakeling97c72b72016-06-24 16:19:36 +01004118vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4119 const DexFile& dex_file,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004120 dex::StringIndex string_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004121 vixl::aarch64::Label* adrp_label) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004122 return
4123 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004124}
4125
Scott Wakeling97c72b72016-06-24 16:19:36 +01004126vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4127 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004128 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004129 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004130 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004131}
4132
Vladimir Marko1998cd02017-01-13 13:02:58 +00004133vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4134 const DexFile& dex_file,
4135 dex::TypeIndex type_index,
4136 vixl::aarch64::Label* adrp_label) {
4137 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4138}
4139
Scott Wakeling97c72b72016-06-24 16:19:36 +01004140vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4141 const DexFile& dex_file,
4142 uint32_t element_offset,
4143 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004144 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4145}
4146
Scott Wakeling97c72b72016-06-24 16:19:36 +01004147vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4148 const DexFile& dex_file,
4149 uint32_t offset_or_index,
4150 vixl::aarch64::Label* adrp_label,
4151 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004152 // Add a patch entry and return the label.
4153 patches->emplace_back(dex_file, offset_or_index);
4154 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004155 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004156 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4157 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4158 return label;
4159}
4160
Scott Wakeling97c72b72016-06-24 16:19:36 +01004161vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004162 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004163 return boot_image_string_patches_.GetOrCreate(
4164 StringReference(&dex_file, string_index),
4165 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4166}
4167
Scott Wakeling97c72b72016-06-24 16:19:36 +01004168vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004169 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004170 return boot_image_type_patches_.GetOrCreate(
4171 TypeReference(&dex_file, type_index),
4172 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4173}
4174
Scott Wakeling97c72b72016-06-24 16:19:36 +01004175vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4176 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004177 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
4178 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
4179 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
4180}
4181
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004182vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004183 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4184 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4185 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004186 return jit_string_patches_.GetOrCreate(
4187 StringReference(&dex_file, string_index),
4188 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4189}
4190
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004191vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004192 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4193 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4194 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004195 return jit_class_patches_.GetOrCreate(
4196 TypeReference(&dex_file, type_index),
4197 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4198}
4199
Vladimir Markoaad75c62016-10-03 08:46:48 +00004200void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4201 vixl::aarch64::Register reg) {
4202 DCHECK(reg.IsX());
4203 SingleEmissionCheckScope guard(GetVIXLAssembler());
4204 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004205 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004206}
4207
4208void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4209 vixl::aarch64::Register out,
4210 vixl::aarch64::Register base) {
4211 DCHECK(out.IsX());
4212 DCHECK(base.IsX());
4213 SingleEmissionCheckScope guard(GetVIXLAssembler());
4214 __ Bind(fixup_label);
4215 __ add(out, base, Operand(/* offset placeholder */ 0));
4216}
4217
4218void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4219 vixl::aarch64::Register out,
4220 vixl::aarch64::Register base) {
4221 DCHECK(base.IsX());
4222 SingleEmissionCheckScope guard(GetVIXLAssembler());
4223 __ Bind(fixup_label);
4224 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4225}
4226
4227template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4228inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4229 const ArenaDeque<PcRelativePatchInfo>& infos,
4230 ArenaVector<LinkerPatch>* linker_patches) {
4231 for (const PcRelativePatchInfo& info : infos) {
4232 linker_patches->push_back(Factory(info.label.GetLocation(),
4233 &info.target_dex_file,
4234 info.pc_insn_label->GetLocation(),
4235 info.offset_or_index));
4236 }
4237}
4238
Vladimir Marko58155012015-08-19 12:49:41 +00004239void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4240 DCHECK(linker_patches->empty());
4241 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004242 pc_relative_dex_cache_patches_.size() +
4243 boot_image_string_patches_.size() +
4244 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004245 boot_image_type_patches_.size() +
4246 pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004247 type_bss_entry_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004248 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004249 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004250 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004251 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004252 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004253 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004254 info.offset_or_index));
4255 }
4256 for (const auto& entry : boot_image_string_patches_) {
4257 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004258 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4259 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004260 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004261 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004262 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004263 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004264 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004265 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4266 linker_patches);
4267 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004268 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4269 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004270 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4271 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004272 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004273 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4274 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004275 for (const auto& entry : boot_image_type_patches_) {
4276 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004277 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4278 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004279 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004280 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004281 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004282 for (const auto& entry : boot_image_address_patches_) {
4283 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004284 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4285 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00004286 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004287 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004288}
4289
Scott Wakeling97c72b72016-06-24 16:19:36 +01004290vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004291 Uint32ToLiteralMap* map) {
4292 return map->GetOrCreate(
4293 value,
4294 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4295}
4296
Scott Wakeling97c72b72016-06-24 16:19:36 +01004297vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004298 return uint64_literals_.GetOrCreate(
4299 value,
4300 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004301}
4302
Scott Wakeling97c72b72016-06-24 16:19:36 +01004303vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004304 MethodReference target_method,
4305 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004306 return map->GetOrCreate(
4307 target_method,
4308 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004309}
4310
Andreas Gampe878d58c2015-01-15 23:24:00 -08004311void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004312 // Explicit clinit checks triggered by static invokes must have been pruned by
4313 // art::PrepareForRegisterAllocation.
4314 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004315
Andreas Gampe878d58c2015-01-15 23:24:00 -08004316 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4317 return;
4318 }
4319
Alexandre Ramesd921d642015-04-16 15:07:16 +01004320 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004321 LocationSummary* locations = invoke->GetLocations();
4322 codegen_->GenerateStaticOrDirectCall(
4323 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004324 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004325}
4326
4327void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004328 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4329 return;
4330 }
4331
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004332 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004333 DCHECK(!codegen_->IsLeafMethod());
4334 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4335}
4336
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004337HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4338 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004339 switch (desired_class_load_kind) {
4340 case HLoadClass::LoadKind::kReferrersClass:
4341 break;
4342 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4343 DCHECK(!GetCompilerOptions().GetCompilePic());
4344 break;
4345 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4346 DCHECK(GetCompilerOptions().GetCompilePic());
4347 break;
4348 case HLoadClass::LoadKind::kBootImageAddress:
4349 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004350 case HLoadClass::LoadKind::kBssEntry:
4351 DCHECK(!Runtime::Current()->UseJitCompilation());
4352 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004353 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004354 DCHECK(Runtime::Current()->UseJitCompilation());
4355 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004356 case HLoadClass::LoadKind::kDexCacheViaMethod:
4357 break;
4358 }
4359 return desired_class_load_kind;
4360}
4361
Alexandre Rames67555f72014-11-18 10:55:16 +00004362void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004363 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4364 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004365 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004366 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004367 cls,
4368 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004369 LocationFrom(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004370 return;
4371 }
Vladimir Marko41559982017-01-06 14:04:23 +00004372 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004373
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004374 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4375 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004376 ? LocationSummary::kCallOnSlowPath
4377 : LocationSummary::kNoCall;
4378 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004379 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004380 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004381 }
4382
Vladimir Marko41559982017-01-06 14:04:23 +00004383 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004384 locations->SetInAt(0, Location::RequiresRegister());
4385 }
4386 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004387}
4388
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004389// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4390// move.
4391void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004392 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4393 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4394 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004395 return;
4396 }
Vladimir Marko41559982017-01-06 14:04:23 +00004397 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004398
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004399 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004400 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004401
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004402 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4403 ? kWithoutReadBarrier
4404 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004405 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004406 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004407 case HLoadClass::LoadKind::kReferrersClass: {
4408 DCHECK(!cls->CanCallRuntime());
4409 DCHECK(!cls->MustGenerateClinitCheck());
4410 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4411 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004412 GenerateGcRootFieldLoad(cls,
4413 out_loc,
4414 current_method,
4415 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004416 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004417 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004418 break;
4419 }
4420 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004421 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004422 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4423 cls->GetTypeIndex()));
4424 break;
4425 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004426 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004427 // Add ADRP with its PC-relative type patch.
4428 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004429 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004430 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004431 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004432 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004433 vixl::aarch64::Label* add_label =
4434 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004435 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004436 break;
4437 }
4438 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004439 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004440 uint32_t address = dchecked_integral_cast<uint32_t>(
4441 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4442 DCHECK_NE(address, 0u);
4443 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004444 break;
4445 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004446 case HLoadClass::LoadKind::kBssEntry: {
4447 // Add ADRP with its PC-relative Class .bss entry patch.
4448 const DexFile& dex_file = cls->GetDexFile();
4449 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko1998cd02017-01-13 13:02:58 +00004450 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004451 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
4452 // Add LDR with its PC-relative Class patch.
4453 vixl::aarch64::Label* ldr_label =
Vladimir Marko1998cd02017-01-13 13:02:58 +00004454 codegen_->NewBssEntryTypePatch(dex_file, type_index, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004455 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4456 GenerateGcRootFieldLoad(cls,
4457 cls->GetLocations()->Out(),
4458 out.X(),
4459 /* placeholder */ 0u,
4460 ldr_label,
4461 kCompilerReadBarrierOption);
4462 generate_null_check = true;
4463 break;
4464 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004465 case HLoadClass::LoadKind::kJitTableAddress: {
4466 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4467 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004468 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004469 GenerateGcRootFieldLoad(cls,
4470 out_loc,
4471 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004472 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004473 /* fixup_label */ nullptr,
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004474 kCompilerReadBarrierOption);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004475 break;
4476 }
Vladimir Marko41559982017-01-06 14:04:23 +00004477 case HLoadClass::LoadKind::kDexCacheViaMethod:
4478 LOG(FATAL) << "UNREACHABLE";
4479 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004480 }
4481
4482 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4483 DCHECK(cls->CanCallRuntime());
4484 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4485 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4486 codegen_->AddSlowPath(slow_path);
4487 if (generate_null_check) {
4488 __ Cbz(out, slow_path->GetEntryLabel());
4489 }
4490 if (cls->MustGenerateClinitCheck()) {
4491 GenerateClassInitializationCheck(slow_path, out);
4492 } else {
4493 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004494 }
4495 }
4496}
4497
David Brazdilcb1c0552015-08-04 16:22:25 +01004498static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004499 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004500}
4501
Alexandre Rames67555f72014-11-18 10:55:16 +00004502void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4503 LocationSummary* locations =
4504 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4505 locations->SetOut(Location::RequiresRegister());
4506}
4507
4508void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004509 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4510}
4511
4512void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4513 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4514}
4515
4516void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4517 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004518}
4519
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004520HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4521 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004522 switch (desired_string_load_kind) {
4523 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4524 DCHECK(!GetCompilerOptions().GetCompilePic());
4525 break;
4526 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4527 DCHECK(GetCompilerOptions().GetCompilePic());
4528 break;
4529 case HLoadString::LoadKind::kBootImageAddress:
4530 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004531 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004532 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004533 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004534 case HLoadString::LoadKind::kJitTableAddress:
4535 DCHECK(Runtime::Current()->UseJitCompilation());
4536 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004537 case HLoadString::LoadKind::kDexCacheViaMethod:
4538 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004539 }
4540 return desired_string_load_kind;
4541}
4542
Alexandre Rames67555f72014-11-18 10:55:16 +00004543void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004544 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004545 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004546 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004547 InvokeRuntimeCallingConvention calling_convention;
4548 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4549 } else {
4550 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004551 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4552 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4553 // Rely on the pResolveString and/or marking to save everything, including temps.
4554 RegisterSet caller_saves = RegisterSet::Empty();
4555 InvokeRuntimeCallingConvention calling_convention;
4556 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4557 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4558 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4559 Primitive::kPrimNot).GetCode());
4560 locations->SetCustomSlowPathCallerSaves(caller_saves);
4561 } else {
4562 // For non-Baker read barrier we have a temp-clobbering call.
4563 }
4564 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004565 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004566}
4567
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004568// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4569// move.
4570void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00004571 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004572 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004573
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004574 switch (load->GetLoadKind()) {
4575 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004576 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4577 load->GetStringIndex()));
4578 return; // No dex cache slow path.
4579 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004580 // Add ADRP with its PC-relative String patch.
4581 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004582 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004583 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004584 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004585 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004586 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004587 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004588 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004589 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004590 return; // No dex cache slow path.
4591 }
4592 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004593 uint32_t address = dchecked_integral_cast<uint32_t>(
4594 reinterpret_cast<uintptr_t>(load->GetString().Get()));
4595 DCHECK_NE(address, 0u);
4596 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004597 return; // No dex cache slow path.
4598 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004599 case HLoadString::LoadKind::kBssEntry: {
4600 // Add ADRP with its PC-relative String .bss entry patch.
4601 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004602 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004603 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004604 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4605 Register temp = temps.AcquireX();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004606 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004607 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004608 // Add LDR with its PC-relative String patch.
4609 vixl::aarch64::Label* ldr_label =
4610 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004611 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00004612 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004613 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004614 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01004615 /* offset placeholder */ 0u,
4616 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004617 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004618 SlowPathCodeARM64* slow_path =
4619 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004620 codegen_->AddSlowPath(slow_path);
4621 __ Cbz(out.X(), slow_path->GetEntryLabel());
4622 __ Bind(slow_path->GetExitLabel());
4623 return;
4624 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004625 case HLoadString::LoadKind::kJitTableAddress: {
4626 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004627 load->GetStringIndex(),
4628 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004629 GenerateGcRootFieldLoad(load,
4630 out_loc,
4631 out.X(),
4632 /* offset */ 0,
4633 /* fixup_label */ nullptr,
4634 kCompilerReadBarrierOption);
4635 return;
4636 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004637 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004638 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004639 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004640
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004641 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004642 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004643 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004644 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004645 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4646 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004647}
4648
Alexandre Rames5319def2014-10-23 10:03:10 +01004649void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4650 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4651 locations->SetOut(Location::ConstantLocation(constant));
4652}
4653
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004654void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004655 // Will be generated at use site.
4656}
4657
Alexandre Rames67555f72014-11-18 10:55:16 +00004658void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4659 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004660 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004661 InvokeRuntimeCallingConvention calling_convention;
4662 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4663}
4664
4665void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004666 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004667 instruction,
4668 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004669 if (instruction->IsEnter()) {
4670 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4671 } else {
4672 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4673 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004674}
4675
Alexandre Rames42d641b2014-10-27 14:00:51 +00004676void LocationsBuilderARM64::VisitMul(HMul* mul) {
4677 LocationSummary* locations =
4678 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4679 switch (mul->GetResultType()) {
4680 case Primitive::kPrimInt:
4681 case Primitive::kPrimLong:
4682 locations->SetInAt(0, Location::RequiresRegister());
4683 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004684 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004685 break;
4686
4687 case Primitive::kPrimFloat:
4688 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004689 locations->SetInAt(0, Location::RequiresFpuRegister());
4690 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004691 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004692 break;
4693
4694 default:
4695 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4696 }
4697}
4698
4699void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4700 switch (mul->GetResultType()) {
4701 case Primitive::kPrimInt:
4702 case Primitive::kPrimLong:
4703 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4704 break;
4705
4706 case Primitive::kPrimFloat:
4707 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004708 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004709 break;
4710
4711 default:
4712 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4713 }
4714}
4715
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004716void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4717 LocationSummary* locations =
4718 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4719 switch (neg->GetResultType()) {
4720 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004721 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004722 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004723 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004724 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004725
4726 case Primitive::kPrimFloat:
4727 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004728 locations->SetInAt(0, Location::RequiresFpuRegister());
4729 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004730 break;
4731
4732 default:
4733 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4734 }
4735}
4736
4737void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4738 switch (neg->GetResultType()) {
4739 case Primitive::kPrimInt:
4740 case Primitive::kPrimLong:
4741 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4742 break;
4743
4744 case Primitive::kPrimFloat:
4745 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004746 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004747 break;
4748
4749 default:
4750 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4751 }
4752}
4753
4754void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4755 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004756 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004757 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004758 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004759 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4760 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004761}
4762
4763void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004764 // Note: if heap poisoning is enabled, the entry point takes cares
4765 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004766 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
4767 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004768}
4769
Alexandre Rames5319def2014-10-23 10:03:10 +01004770void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4771 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004772 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004773 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004774 if (instruction->IsStringAlloc()) {
4775 locations->AddTemp(LocationFrom(kArtMethodRegister));
4776 } else {
4777 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004778 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004779 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4780}
4781
4782void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004783 // Note: if heap poisoning is enabled, the entry point takes cares
4784 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004785 if (instruction->IsStringAlloc()) {
4786 // String is allocated through StringFactory. Call NewEmptyString entry point.
4787 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004788 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004789 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4790 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4791 __ Blr(lr);
4792 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4793 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004794 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004795 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004796 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004797}
4798
4799void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4800 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004801 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004802 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004803}
4804
4805void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004806 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004807 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004808 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004809 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004810 break;
4811
4812 default:
4813 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4814 }
4815}
4816
David Brazdil66d126e2015-04-03 16:02:44 +01004817void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4818 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4819 locations->SetInAt(0, Location::RequiresRegister());
4820 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4821}
4822
4823void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004824 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004825}
4826
Alexandre Rames5319def2014-10-23 10:03:10 +01004827void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004828 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4829 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01004830}
4831
Calin Juravle2ae48182016-03-16 14:05:09 +00004832void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4833 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004834 return;
4835 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004836
Alexandre Ramesd921d642015-04-16 15:07:16 +01004837 BlockPoolsScope block_pools(GetVIXLAssembler());
4838 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004839 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004840 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004841}
4842
Calin Juravle2ae48182016-03-16 14:05:09 +00004843void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004844 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004845 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004846
4847 LocationSummary* locations = instruction->GetLocations();
4848 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004849
4850 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004851}
4852
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004853void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004854 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004855}
4856
Alexandre Rames67555f72014-11-18 10:55:16 +00004857void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4858 HandleBinaryOp(instruction);
4859}
4860
4861void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4862 HandleBinaryOp(instruction);
4863}
4864
Alexandre Rames3e69f162014-12-10 10:36:50 +00004865void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4866 LOG(FATAL) << "Unreachable";
4867}
4868
4869void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4870 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4871}
4872
Alexandre Rames5319def2014-10-23 10:03:10 +01004873void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4874 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4875 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4876 if (location.IsStackSlot()) {
4877 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4878 } else if (location.IsDoubleStackSlot()) {
4879 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4880 }
4881 locations->SetOut(location);
4882}
4883
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004884void InstructionCodeGeneratorARM64::VisitParameterValue(
4885 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004886 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004887}
4888
4889void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4890 LocationSummary* locations =
4891 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004892 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004893}
4894
4895void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4896 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4897 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004898}
4899
4900void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4901 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004902 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004903 locations->SetInAt(i, Location::Any());
4904 }
4905 locations->SetOut(Location::Any());
4906}
4907
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004908void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004909 LOG(FATAL) << "Unreachable";
4910}
4911
Serban Constantinescu02164b32014-11-13 14:05:07 +00004912void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004913 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004914 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004915 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4916 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004917 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4918
4919 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004920 case Primitive::kPrimInt:
4921 case Primitive::kPrimLong:
4922 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004923 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004924 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4925 break;
4926
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004927 case Primitive::kPrimFloat:
4928 case Primitive::kPrimDouble: {
4929 InvokeRuntimeCallingConvention calling_convention;
4930 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4931 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4932 locations->SetOut(calling_convention.GetReturnLocation(type));
4933
4934 break;
4935 }
4936
Serban Constantinescu02164b32014-11-13 14:05:07 +00004937 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004938 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004939 }
4940}
4941
4942void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4943 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004944
Serban Constantinescu02164b32014-11-13 14:05:07 +00004945 switch (type) {
4946 case Primitive::kPrimInt:
4947 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004948 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004949 break;
4950 }
4951
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004952 case Primitive::kPrimFloat:
4953 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004954 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4955 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004956 if (type == Primitive::kPrimFloat) {
4957 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4958 } else {
4959 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4960 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004961 break;
4962 }
4963
Serban Constantinescu02164b32014-11-13 14:05:07 +00004964 default:
4965 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004966 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004967 }
4968}
4969
Calin Juravle27df7582015-04-17 19:12:31 +01004970void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4971 memory_barrier->SetLocations(nullptr);
4972}
4973
4974void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004975 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004976}
4977
Alexandre Rames5319def2014-10-23 10:03:10 +01004978void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4979 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4980 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004981 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004982}
4983
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004984void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004985 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004986}
4987
4988void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4989 instruction->SetLocations(nullptr);
4990}
4991
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004992void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004993 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004994}
4995
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004996void LocationsBuilderARM64::VisitRor(HRor* ror) {
4997 HandleBinaryOp(ror);
4998}
4999
5000void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5001 HandleBinaryOp(ror);
5002}
5003
Serban Constantinescu02164b32014-11-13 14:05:07 +00005004void LocationsBuilderARM64::VisitShl(HShl* shl) {
5005 HandleShift(shl);
5006}
5007
5008void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5009 HandleShift(shl);
5010}
5011
5012void LocationsBuilderARM64::VisitShr(HShr* shr) {
5013 HandleShift(shr);
5014}
5015
5016void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5017 HandleShift(shr);
5018}
5019
Alexandre Rames5319def2014-10-23 10:03:10 +01005020void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005021 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005022}
5023
5024void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005025 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005026}
5027
Alexandre Rames67555f72014-11-18 10:55:16 +00005028void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005029 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00005030}
5031
5032void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005033 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005034}
5035
5036void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005037 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005038}
5039
Alexandre Rames67555f72014-11-18 10:55:16 +00005040void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005041 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005042}
5043
Calin Juravlee460d1d2015-09-29 04:52:17 +01005044void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5045 HUnresolvedInstanceFieldGet* instruction) {
5046 FieldAccessCallingConventionARM64 calling_convention;
5047 codegen_->CreateUnresolvedFieldLocationSummary(
5048 instruction, instruction->GetFieldType(), calling_convention);
5049}
5050
5051void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5052 HUnresolvedInstanceFieldGet* instruction) {
5053 FieldAccessCallingConventionARM64 calling_convention;
5054 codegen_->GenerateUnresolvedFieldAccess(instruction,
5055 instruction->GetFieldType(),
5056 instruction->GetFieldIndex(),
5057 instruction->GetDexPc(),
5058 calling_convention);
5059}
5060
5061void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5062 HUnresolvedInstanceFieldSet* instruction) {
5063 FieldAccessCallingConventionARM64 calling_convention;
5064 codegen_->CreateUnresolvedFieldLocationSummary(
5065 instruction, instruction->GetFieldType(), calling_convention);
5066}
5067
5068void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5069 HUnresolvedInstanceFieldSet* instruction) {
5070 FieldAccessCallingConventionARM64 calling_convention;
5071 codegen_->GenerateUnresolvedFieldAccess(instruction,
5072 instruction->GetFieldType(),
5073 instruction->GetFieldIndex(),
5074 instruction->GetDexPc(),
5075 calling_convention);
5076}
5077
5078void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5079 HUnresolvedStaticFieldGet* instruction) {
5080 FieldAccessCallingConventionARM64 calling_convention;
5081 codegen_->CreateUnresolvedFieldLocationSummary(
5082 instruction, instruction->GetFieldType(), calling_convention);
5083}
5084
5085void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5086 HUnresolvedStaticFieldGet* instruction) {
5087 FieldAccessCallingConventionARM64 calling_convention;
5088 codegen_->GenerateUnresolvedFieldAccess(instruction,
5089 instruction->GetFieldType(),
5090 instruction->GetFieldIndex(),
5091 instruction->GetDexPc(),
5092 calling_convention);
5093}
5094
5095void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5096 HUnresolvedStaticFieldSet* instruction) {
5097 FieldAccessCallingConventionARM64 calling_convention;
5098 codegen_->CreateUnresolvedFieldLocationSummary(
5099 instruction, instruction->GetFieldType(), calling_convention);
5100}
5101
5102void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5103 HUnresolvedStaticFieldSet* instruction) {
5104 FieldAccessCallingConventionARM64 calling_convention;
5105 codegen_->GenerateUnresolvedFieldAccess(instruction,
5106 instruction->GetFieldType(),
5107 instruction->GetFieldIndex(),
5108 instruction->GetDexPc(),
5109 calling_convention);
5110}
5111
Alexandre Rames5319def2014-10-23 10:03:10 +01005112void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005113 LocationSummary* locations =
5114 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005115 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01005116}
5117
5118void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005119 HBasicBlock* block = instruction->GetBlock();
5120 if (block->GetLoopInformation() != nullptr) {
5121 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5122 // The back edge will generate the suspend check.
5123 return;
5124 }
5125 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5126 // The goto will generate the suspend check.
5127 return;
5128 }
5129 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005130}
5131
Alexandre Rames67555f72014-11-18 10:55:16 +00005132void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5133 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005134 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005135 InvokeRuntimeCallingConvention calling_convention;
5136 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5137}
5138
5139void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005140 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005141 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005142}
5143
5144void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5145 LocationSummary* locations =
5146 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5147 Primitive::Type input_type = conversion->GetInputType();
5148 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005149 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005150 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5151 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5152 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5153 }
5154
Alexandre Rames542361f2015-01-29 16:57:31 +00005155 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005156 locations->SetInAt(0, Location::RequiresFpuRegister());
5157 } else {
5158 locations->SetInAt(0, Location::RequiresRegister());
5159 }
5160
Alexandre Rames542361f2015-01-29 16:57:31 +00005161 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005162 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5163 } else {
5164 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5165 }
5166}
5167
5168void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5169 Primitive::Type result_type = conversion->GetResultType();
5170 Primitive::Type input_type = conversion->GetInputType();
5171
5172 DCHECK_NE(input_type, result_type);
5173
Alexandre Rames542361f2015-01-29 16:57:31 +00005174 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005175 int result_size = Primitive::ComponentSize(result_type);
5176 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005177 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005178 Register output = OutputRegister(conversion);
5179 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005180 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005181 // 'int' values are used directly as W registers, discarding the top
5182 // bits, so we don't need to sign-extend and can just perform a move.
5183 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5184 // top 32 bits of the target register. We theoretically could leave those
5185 // bits unchanged, but we would have to make sure that no code uses a
5186 // 32bit input value as a 64bit value assuming that the top 32 bits are
5187 // zero.
5188 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005189 } else if (result_type == Primitive::kPrimChar ||
5190 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5191 __ Ubfx(output,
5192 output.IsX() ? source.X() : source.W(),
5193 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005194 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005195 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005196 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005197 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005198 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005199 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005200 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5201 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005202 } else if (Primitive::IsFloatingPointType(result_type) &&
5203 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005204 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5205 } else {
5206 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5207 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005208 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005209}
Alexandre Rames67555f72014-11-18 10:55:16 +00005210
Serban Constantinescu02164b32014-11-13 14:05:07 +00005211void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5212 HandleShift(ushr);
5213}
5214
5215void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5216 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005217}
5218
5219void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5220 HandleBinaryOp(instruction);
5221}
5222
5223void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5224 HandleBinaryOp(instruction);
5225}
5226
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005227void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005228 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005229 LOG(FATAL) << "Unreachable";
5230}
5231
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005232void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005233 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005234 LOG(FATAL) << "Unreachable";
5235}
5236
Mark Mendellfe57faa2015-09-18 09:26:15 -04005237// Simple implementation of packed switch - generate cascaded compare/jumps.
5238void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5239 LocationSummary* locations =
5240 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5241 locations->SetInAt(0, Location::RequiresRegister());
5242}
5243
5244void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5245 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005246 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005247 Register value_reg = InputRegisterAt(switch_instr, 0);
5248 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5249
Zheng Xu3927c8b2015-11-18 17:46:25 +08005250 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005251 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005252 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5253 // make sure we don't emit it if the target may run out of range.
5254 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5255 // ranges and emit the tables only as required.
5256 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005257
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005258 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005259 // Current instruction id is an upper bound of the number of HIRs in the graph.
5260 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5261 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005262 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5263 Register temp = temps.AcquireW();
5264 __ Subs(temp, value_reg, Operand(lower_bound));
5265
Zheng Xu3927c8b2015-11-18 17:46:25 +08005266 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005267 // Jump to successors[0] if value == lower_bound.
5268 __ B(eq, codegen_->GetLabelOf(successors[0]));
5269 int32_t last_index = 0;
5270 for (; num_entries - last_index > 2; last_index += 2) {
5271 __ Subs(temp, temp, Operand(2));
5272 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5273 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5274 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5275 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5276 }
5277 if (num_entries - last_index == 2) {
5278 // The last missing case_value.
5279 __ Cmp(temp, Operand(1));
5280 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005281 }
5282
5283 // And the default for any other value.
5284 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5285 __ B(codegen_->GetLabelOf(default_block));
5286 }
5287 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005288 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005289
5290 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5291
5292 // Below instructions should use at most one blocked register. Since there are two blocked
5293 // registers, we are free to block one.
5294 Register temp_w = temps.AcquireW();
5295 Register index;
5296 // Remove the bias.
5297 if (lower_bound != 0) {
5298 index = temp_w;
5299 __ Sub(index, value_reg, Operand(lower_bound));
5300 } else {
5301 index = value_reg;
5302 }
5303
5304 // Jump to default block if index is out of the range.
5305 __ Cmp(index, Operand(num_entries));
5306 __ B(hs, codegen_->GetLabelOf(default_block));
5307
5308 // In current VIXL implementation, it won't require any blocked registers to encode the
5309 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5310 // register pressure.
5311 Register table_base = temps.AcquireX();
5312 // Load jump offset from the table.
5313 __ Adr(table_base, jump_table->GetTableStartLabel());
5314 Register jump_offset = temp_w;
5315 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5316
5317 // Jump to target block by branching to table_base(pc related) + offset.
5318 Register target_address = table_base;
5319 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5320 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005321 }
5322}
5323
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005324void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5325 HInstruction* instruction,
5326 Location out,
5327 uint32_t offset,
5328 Location maybe_temp,
5329 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005330 Primitive::Type type = Primitive::kPrimNot;
5331 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005332 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005333 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005334 Register temp_reg = RegisterFrom(maybe_temp, type);
5335 if (kUseBakerReadBarrier) {
5336 // Load with fast path based Baker's read barrier.
5337 // /* HeapReference<Object> */ out = *(out + offset)
5338 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5339 out,
5340 out_reg,
5341 offset,
5342 temp_reg,
5343 /* needs_null_check */ false,
5344 /* use_load_acquire */ false);
5345 } else {
5346 // Load with slow path based read barrier.
5347 // Save the value of `out` into `maybe_temp` before overwriting it
5348 // in the following move operation, as we will need it for the
5349 // read barrier below.
5350 __ Mov(temp_reg, out_reg);
5351 // /* HeapReference<Object> */ out = *(out + offset)
5352 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5353 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5354 }
5355 } else {
5356 // Plain load with no read barrier.
5357 // /* HeapReference<Object> */ out = *(out + offset)
5358 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5359 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5360 }
5361}
5362
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005363void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5364 HInstruction* instruction,
5365 Location out,
5366 Location obj,
5367 uint32_t offset,
5368 Location maybe_temp,
5369 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005370 Primitive::Type type = Primitive::kPrimNot;
5371 Register out_reg = RegisterFrom(out, type);
5372 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005373 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005374 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005375 if (kUseBakerReadBarrier) {
5376 // Load with fast path based Baker's read barrier.
5377 Register temp_reg = RegisterFrom(maybe_temp, type);
5378 // /* HeapReference<Object> */ out = *(obj + offset)
5379 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5380 out,
5381 obj_reg,
5382 offset,
5383 temp_reg,
5384 /* needs_null_check */ false,
5385 /* use_load_acquire */ false);
5386 } else {
5387 // Load with slow path based read barrier.
5388 // /* HeapReference<Object> */ out = *(obj + offset)
5389 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5390 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5391 }
5392 } else {
5393 // Plain load with no read barrier.
5394 // /* HeapReference<Object> */ out = *(obj + offset)
5395 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5396 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5397 }
5398}
5399
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005400void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5401 HInstruction* instruction,
5402 Location root,
5403 Register obj,
5404 uint32_t offset,
5405 vixl::aarch64::Label* fixup_label,
5406 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005407 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005408 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005409 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005410 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005411 if (kUseBakerReadBarrier) {
5412 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5413 // Baker's read barrier are used:
5414 //
5415 // root = obj.field;
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005416 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5417 // if (temp != null) {
5418 // root = temp(root)
Roland Levillain44015862016-01-22 11:47:17 +00005419 // }
5420
5421 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005422 if (fixup_label == nullptr) {
5423 __ Ldr(root_reg, MemOperand(obj, offset));
5424 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005425 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005426 }
Roland Levillain44015862016-01-22 11:47:17 +00005427 static_assert(
5428 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5429 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5430 "have different sizes.");
5431 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5432 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5433 "have different sizes.");
5434
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005435 Register temp = lr;
Roland Levillain44015862016-01-22 11:47:17 +00005436
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005437 // Slow path marking the GC root `root`. The entrypoint will alrady be loaded in temp.
5438 SlowPathCodeARM64* slow_path =
5439 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction,
5440 root,
5441 LocationFrom(temp));
5442 codegen_->AddSlowPath(slow_path);
5443 const int32_t entry_point_offset =
5444 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5445 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5446 // Loading the entrypoint does not require a load acquire since it is only changed when
5447 // threads are suspended or running a checkpoint.
5448 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5449 // The entrypoint is null when the GC is not marking, this prevents one load compared to
5450 // checking GetIsGcMarking.
Roland Levillain44015862016-01-22 11:47:17 +00005451 __ Cbnz(temp, slow_path->GetEntryLabel());
5452 __ Bind(slow_path->GetExitLabel());
5453 } else {
5454 // GC root loaded through a slow path for read barriers other
5455 // than Baker's.
5456 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005457 if (fixup_label == nullptr) {
5458 __ Add(root_reg.X(), obj.X(), offset);
5459 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005460 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005461 }
Roland Levillain44015862016-01-22 11:47:17 +00005462 // /* mirror::Object* */ root = root->Read()
5463 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5464 }
5465 } else {
5466 // Plain GC root load with no read barrier.
5467 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005468 if (fixup_label == nullptr) {
5469 __ Ldr(root_reg, MemOperand(obj, offset));
5470 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005471 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005472 }
Roland Levillain44015862016-01-22 11:47:17 +00005473 // Note that GC roots are not affected by heap poisoning, thus we
5474 // do not have to unpoison `root_reg` here.
5475 }
5476}
5477
5478void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5479 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005480 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005481 uint32_t offset,
5482 Register temp,
5483 bool needs_null_check,
5484 bool use_load_acquire) {
5485 DCHECK(kEmitCompilerReadBarrier);
5486 DCHECK(kUseBakerReadBarrier);
5487
5488 // /* HeapReference<Object> */ ref = *(obj + offset)
5489 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005490 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01005491 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5492 ref,
5493 obj,
5494 offset,
5495 no_index,
5496 no_scale_factor,
5497 temp,
5498 needs_null_check,
5499 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005500}
5501
5502void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5503 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005504 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005505 uint32_t data_offset,
5506 Location index,
5507 Register temp,
5508 bool needs_null_check) {
5509 DCHECK(kEmitCompilerReadBarrier);
5510 DCHECK(kUseBakerReadBarrier);
5511
5512 // Array cells are never volatile variables, therefore array loads
5513 // never use Load-Acquire instructions on ARM64.
5514 const bool use_load_acquire = false;
5515
Roland Levillainbfea3352016-06-23 13:48:47 +01005516 static_assert(
5517 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5518 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005519 // /* HeapReference<Object> */ ref =
5520 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005521 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5522 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5523 ref,
5524 obj,
5525 data_offset,
5526 index,
5527 scale_factor,
5528 temp,
5529 needs_null_check,
5530 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005531}
5532
5533void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5534 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005535 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005536 uint32_t offset,
5537 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005538 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005539 Register temp,
5540 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005541 bool use_load_acquire,
5542 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00005543 DCHECK(kEmitCompilerReadBarrier);
5544 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005545 // If we are emitting an array load, we should not be using a
5546 // Load Acquire instruction. In other words:
5547 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5548 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005549
5550 MacroAssembler* masm = GetVIXLAssembler();
5551 UseScratchRegisterScope temps(masm);
5552
5553 // In slow path based read barriers, the read barrier call is
5554 // inserted after the original load. However, in fast path based
5555 // Baker's read barriers, we need to perform the load of
5556 // mirror::Object::monitor_ *before* the original reference load.
5557 // This load-load ordering is required by the read barrier.
5558 // The fast path/slow path (for Baker's algorithm) should look like:
5559 //
5560 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5561 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5562 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005563 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain44015862016-01-22 11:47:17 +00005564 // if (is_gray) {
5565 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5566 // }
5567 //
5568 // Note: the original implementation in ReadBarrier::Barrier is
5569 // slightly more complex as it performs additional checks that we do
5570 // not do here for performance reasons.
5571
5572 Primitive::Type type = Primitive::kPrimNot;
5573 Register ref_reg = RegisterFrom(ref, type);
5574 DCHECK(obj.IsW());
5575 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5576
5577 // /* int32_t */ monitor = obj->monitor_
5578 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5579 if (needs_null_check) {
5580 MaybeRecordImplicitNullCheck(instruction);
5581 }
5582 // /* LockWord */ lock_word = LockWord(monitor)
5583 static_assert(sizeof(LockWord) == sizeof(int32_t),
5584 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005585
Vladimir Marko877a0332016-07-11 19:30:56 +01005586 // Introduce a dependency on the lock_word including rb_state,
5587 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005588 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005589 // `obj` is unchanged by this operation, but its value now depends
5590 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005591 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005592
5593 // The actual reference load.
5594 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005595 // Load types involving an "index": ArrayGet,
5596 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5597 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01005598 if (use_load_acquire) {
5599 // UnsafeGetObjectVolatile intrinsic case.
5600 // Register `index` is not an index in an object array, but an
5601 // offset to an object reference field within object `obj`.
5602 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5603 DCHECK(instruction->GetLocations()->Intrinsified());
5604 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5605 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005606 DCHECK_EQ(offset, 0u);
5607 DCHECK_EQ(scale_factor, 0u);
5608 DCHECK_EQ(needs_null_check, 0u);
Roland Levillainbfea3352016-06-23 13:48:47 +01005609 // /* HeapReference<Object> */ ref = *(obj + index)
5610 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5611 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005612 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005613 // ArrayGet and UnsafeGetObject intrinsics cases.
5614 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5615 if (index.IsConstant()) {
5616 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5617 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5618 } else {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005619 Register temp3 = temps.AcquireW();
5620 __ Add(temp3, obj, offset);
5621 Load(type, ref_reg, HeapOperand(temp3, XRegisterFrom(index), LSL, scale_factor));
5622 temps.Release(temp3);
Roland Levillainbfea3352016-06-23 13:48:47 +01005623 }
Roland Levillain44015862016-01-22 11:47:17 +00005624 }
Roland Levillain44015862016-01-22 11:47:17 +00005625 } else {
5626 // /* HeapReference<Object> */ ref = *(obj + offset)
5627 MemOperand field = HeapOperand(obj, offset);
5628 if (use_load_acquire) {
5629 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5630 } else {
5631 Load(type, ref_reg, field);
5632 }
5633 }
5634
5635 // Object* ref = ref_addr->AsMirrorPtr()
5636 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5637
Vladimir Marko953437b2016-08-24 08:30:46 +00005638 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005639 SlowPathCodeARM64* slow_path;
5640 if (always_update_field) {
5641 // ReadBarrierMarkAndUpdateFieldSlowPathARM64 only supports
5642 // address of the form `obj + field_offset`, where `obj` is a
5643 // register and `field_offset` is a register. Thus `offset` and
5644 // `scale_factor` above are expected to be null in this code path.
5645 DCHECK_EQ(offset, 0u);
5646 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
5647 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM64(
5648 instruction, ref, obj, /* field_offset */ index, temp);
5649 } else {
5650 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
5651 }
Roland Levillain44015862016-01-22 11:47:17 +00005652 AddSlowPath(slow_path);
5653
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005654 // if (rb_state == ReadBarrier::GrayState())
Roland Levillain44015862016-01-22 11:47:17 +00005655 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005656 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005657 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5658 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko877a0332016-07-11 19:30:56 +01005659 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005660 __ Bind(slow_path->GetExitLabel());
5661}
5662
5663void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5664 Location out,
5665 Location ref,
5666 Location obj,
5667 uint32_t offset,
5668 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005669 DCHECK(kEmitCompilerReadBarrier);
5670
Roland Levillain44015862016-01-22 11:47:17 +00005671 // Insert a slow path based read barrier *after* the reference load.
5672 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005673 // If heap poisoning is enabled, the unpoisoning of the loaded
5674 // reference will be carried out by the runtime within the slow
5675 // path.
5676 //
5677 // Note that `ref` currently does not get unpoisoned (when heap
5678 // poisoning is enabled), which is alright as the `ref` argument is
5679 // not used by the artReadBarrierSlow entry point.
5680 //
5681 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5682 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5683 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5684 AddSlowPath(slow_path);
5685
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005686 __ B(slow_path->GetEntryLabel());
5687 __ Bind(slow_path->GetExitLabel());
5688}
5689
Roland Levillain44015862016-01-22 11:47:17 +00005690void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5691 Location out,
5692 Location ref,
5693 Location obj,
5694 uint32_t offset,
5695 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005696 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005697 // Baker's read barriers shall be handled by the fast path
5698 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5699 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005700 // If heap poisoning is enabled, unpoisoning will be taken care of
5701 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005702 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005703 } else if (kPoisonHeapReferences) {
5704 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5705 }
5706}
5707
Roland Levillain44015862016-01-22 11:47:17 +00005708void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5709 Location out,
5710 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005711 DCHECK(kEmitCompilerReadBarrier);
5712
Roland Levillain44015862016-01-22 11:47:17 +00005713 // Insert a slow path based read barrier *after* the GC root load.
5714 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005715 // Note that GC roots are not affected by heap poisoning, so we do
5716 // not need to do anything special for this here.
5717 SlowPathCodeARM64* slow_path =
5718 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5719 AddSlowPath(slow_path);
5720
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005721 __ B(slow_path->GetEntryLabel());
5722 __ Bind(slow_path->GetExitLabel());
5723}
5724
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005725void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5726 LocationSummary* locations =
5727 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5728 locations->SetInAt(0, Location::RequiresRegister());
5729 locations->SetOut(Location::RequiresRegister());
5730}
5731
5732void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5733 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005734 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005735 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005736 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005737 __ Ldr(XRegisterFrom(locations->Out()),
5738 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005739 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005740 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005741 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005742 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5743 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005744 __ Ldr(XRegisterFrom(locations->Out()),
5745 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005746 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005747}
5748
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005749static void PatchJitRootUse(uint8_t* code,
5750 const uint8_t* roots_data,
5751 vixl::aarch64::Literal<uint32_t>* literal,
5752 uint64_t index_in_table) {
5753 uint32_t literal_offset = literal->GetOffset();
5754 uintptr_t address =
5755 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
5756 uint8_t* data = code + literal_offset;
5757 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
5758}
5759
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005760void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
5761 for (const auto& entry : jit_string_patches_) {
5762 const auto& it = jit_string_roots_.find(entry.first);
5763 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005764 PatchJitRootUse(code, roots_data, entry.second, it->second);
5765 }
5766 for (const auto& entry : jit_class_patches_) {
5767 const auto& it = jit_class_roots_.find(entry.first);
5768 DCHECK(it != jit_class_roots_.end());
5769 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005770 }
5771}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005772
Alexandre Rames67555f72014-11-18 10:55:16 +00005773#undef __
5774#undef QUICK_ENTRY_POINT
5775
Alexandre Rames5319def2014-10-23 10:03:10 +01005776} // namespace arm64
5777} // namespace art